Tutorials › Multivariate Statistics (Graduate) › The Frobenius Inner Product and Matrix Gradients

Matrix Calculus and Optimization · Tutorial 25 of 975

The Frobenius Inner Product and Matrix Gradients

Use the Frobenius inner product to represent a scalar function’s differential as a matrix gradient, and check its dimensions and interpretation.

Beginner 10 min read

What You'll Learn

  • Define the Frobenius inner product, norm, and gradient convention for real matrices.
  • Relate the trace expression for the inner product to its entrywise sum.
  • Prove existence and uniqueness of the gradient representing a differential.
  • Translate coordinate-based differentials into matrix gradients.
  • Interpret a gradient as the direction of steepest first-order increase.
  • Recognize dimension and orientation mistakes in matrix-gradient calculations.

Introduction

For a scalar function of a matrix, the differential describes how the function changes under a small matrix perturbation. The differential is a linear function of that perturbation; a matrix gradient is a convenient way to represent this linear function using an inner product. The representation is useful only when its convention is clear: in this tutorial, the gradient has the same dimensions as the matrix argument.

The key idea is that ordinary Euclidean geometry extends to the space of matrices. Its inner product is the Frobenius inner product, and it gives a precise meaning to the convention \(df=\operatorname{tr}((\nabla f)^T dX)\). The earlier material on matrix differentials as first-order linear maps supplies the notion of differential used here.

The Frobenius Inner Product

Let \(A,B\in\mathbb{R}^{m\times n}\). Write \(a_{ij}\) and \(b_{ij}\) for their entries. The following definition gives a scalar measure of how the two matrices align.

Definition: The Frobenius inner product of \(A\) and \(B\) is \(\langle A,B\rangle_F=\sum_{i=1}^{m}\sum_{j=1}^{n}a_{ij}b_{ij}=\operatorname{tr}(A^TB)\). The associated Frobenius norm is \(\|A\|_F=\sqrt{\langle A,A\rangle_F}=\sqrt{\sum_{i=1}^{m}\sum_{j=1}^{n}a_{ij}^2}\).

The trace expression and the entrywise expression are equal because the diagonal entries of \(A^TB\) are \((A^TB)_{jj}=\sum_{i=1}^{m}a_{ij}b_{ij}\), for \(j=1,\ldots,n\). Summing those diagonal entries gives \(\sum_{j=1}^{n}\sum_{i=1}^{m}a_{ij}b_{ij}\), which is the stated double sum. Thus the trace is a compact notation for the same ordinary dot product of the matrices’ entries.

This inner product is symmetric and bilinear, and it is positive definite: \(\langle A,A\rangle_F=\sum_{i,j}a_{ij}^2\geq 0\), with equality only when every entry of \(A\) is zero. Consequently, the space \(\mathbb{R}^{m\times n}\), with this inner product, behaves like a Euclidean space of dimension \(mn\). The Frobenius norm is the corresponding Euclidean length.

Worked Example: Computing an Inner Product and Norm

Let \(A=\begin{bmatrix}1&-2\\3&0\end{bmatrix}\) and \(B=\begin{bmatrix}4&1\\-1&2\end{bmatrix}\). Compute their Frobenius inner product and the Frobenius norm of \(A\).

Multiply corresponding entries and add: \(\langle A,B\rangle_F=(1)(4)+(-2)(1)+(3)(-1)+(0)(2)=4-2-3+0=-1\).

For the norm, square the entries of \(A\), add them, and take the square root: \(\|A\|_F=\sqrt{1^2+(-2)^2+3^2+0^2}=\sqrt{14}\). The trace form gives the same inner product: \(A^TB=\begin{bmatrix}1&-1\\-8&-2\end{bmatrix}\), so \(\operatorname{tr}(A^TB)=1+(-2)=-1\).

Differentials and the Gradient Convention

Suppose \(U\) is an open subset of \(\mathbb{R}^{m\times n}\) and \(f:U\to\mathbb{R}\). At \(X\in U\), differentiability means that there is a linear map \(L_X:\mathbb{R}^{m\times n}\to\mathbb{R}\) such that, for perturbations \(H\) tending to zero, \(f(X+H)=f(X)+L_X(H)+o(\|H\|_F)\). The linear map \(L_X\) is the differential at \(X\), written \(df_X\); its value at a perturbation \(H\) is \(df_X[H]\). The remainder condition means that the remainder divided by \(\|H\|_F\) tends to zero.

Definition: The gradient \(\nabla f(X)\in\mathbb{R}^{m\times n}\) is the matrix representing the differential under the Frobenius inner product: \(df_X[H]=\langle\nabla f(X),H\rangle_F=\operatorname{tr}((\nabla f(X))^TH)\) for every \(H\in\mathbb{R}^{m\times n}\). In differential notation, this convention is \(df=\operatorname{tr}((\nabla f)^T dX)\).

The differential is a linear map that takes a perturbation matrix to a scalar. The gradient is a matrix whose inner product with any perturbation returns that scalar. In particular, \(dX\) represents an arbitrary perturbation of the same dimensions as \(X\); it is not an independent matrix-valued output. The transpose in the convention ensures that \((\nabla f)^T dX\) is square and its trace is scalar, including when \(X\) is rectangular.

Why a Unique Gradient Exists

The gradient convention does not place an extra restriction on a differentiable scalar function. Every linear differential on the finite-dimensional matrix space has exactly one matrix representation under the Frobenius inner product.

Theorem: Let \(L:\mathbb{R}^{m\times n}\to\mathbb{R}\) be linear. There is a unique \(G\in\mathbb{R}^{m\times n}\) such that \(L(H)=\langle G,H\rangle_F\) for every \(H\in\mathbb{R}^{m\times n}\). In particular, at every point where \(f:U\to\mathbb{R}\) is differentiable on an open set \(U\), its differential has a unique Frobenius gradient.

Proof. Let \(E_{ij}\) be the \(m\times n\) matrix with a 1 in position \((i,j)\) and zeros elsewhere. Define \(g_{ij}=L(E_{ij})\), and let \(G\) be the matrix with entries \(g_{ij}\). Every \(H\in\mathbb{R}^{m\times n}\) can be written as \(H=\sum_{i=1}^{m}\sum_{j=1}^{n}h_{ij}E_{ij}\). By linearity, \(L(H)=\sum_{i=1}^{m}\sum_{j=1}^{n}h_{ij}L(E_{ij})=\sum_{i=1}^{m}\sum_{j=1}^{n}h_{ij}g_{ij}=\langle G,H\rangle_F\). This proves existence. If \(G_1\) and \(G_2\) both represent \(L\), then \(\langle G_1-G_2,H\rangle_F=0\) for every \(H\). Set \(H=G_1-G_2\). It follows that \(\|G_1-G_2\|_F^2=0\), so \(G_1=G_2\). For differentiable \(f\), apply the result to its linear differential \(df_X\) at each \(X\). \(\square\)

Translating Coordinate Differentials into Gradients

The theorem gives a direct calculation method. First express the differential in terms of the entries of a general perturbation \(H\). Then read off the coefficient of each \(h_{ij}\): that coefficient is the \((i,j)\) entry of the gradient. Each example below follows this procedure.

Worked Example: A Function of Two Matrix Entries

For \(X\in\mathbb{R}^{2\times2}\), let \(f(X)=x_{12}-2x_{21}\). Find the gradient at a general \(X\).

For a perturbation \(H=(h_{ij})\), the change is exactly \(f(X+H)-f(X)=h_{12}-2h_{21}\). This is linear in \(H\), so it is the differential: \(df_X[H]=h_{12}-2h_{21}\).

The coefficient of \(h_{11}\) is 0, that of \(h_{12}\) is 1, that of \(h_{21}\) is \(-2\), and that of \(h_{22}\) is 0. Therefore the gradient has the same pattern of coefficients: \(\nabla f(X)=\begin{bmatrix}0&1\\-2&0\end{bmatrix}\). Indeed, the Frobenius inner product of this matrix with \(H\) is \(h_{12}-2h_{21}\), as required.

Worked Example: Half the Squared Frobenius Norm

For \(X\in\mathbb{R}^{2\times2}\), let \(f(X)=\tfrac12\|X\|_F^2\). Find its differential and gradient, then evaluate the gradient at \(X=\begin{bmatrix}1&-2\\0&3\end{bmatrix}\).

Write \(H=(h_{ij})\). Entry by entry, \(f(X+H)=\tfrac12\sum_{i=1}^{2}\sum_{j=1}^{2}(x_{ij}+h_{ij})^2\). After subtracting \(f(X)\), the terms linear in \(H\) are \(\sum_{i,j}x_{ij}h_{ij}\), and the remaining term is \(\tfrac12\sum_{i,j}h_{ij}^2=\tfrac12\|H\|_F^2\). Thus \(f(X+H)=f(X)+\sum_{i,j}x_{ij}h_{ij}+\tfrac12\|H\|_F^2\). Since \(\tfrac12\|H\|_F^2=o(\|H\|_F)\) as \(H\to0\), the differential is \(df_X[H]=\sum_{i,j}x_{ij}h_{ij}\).

This is \(\langle X,H\rangle_F\), so \(\nabla f(X)=X\). At the specified matrix, \(\nabla f(X)=\begin{bmatrix}1&-2\\0&3\end{bmatrix}\). The gradient has the same dimensions and entries as the matrix argument.

Worked Example: A Nonlinear Function of Two Entries

For \(X\in\mathbb{R}^{2\times2}\), let \(f(X)=\sin(x_{11})+\tfrac12 x_{22}^2\). Find the gradient at \(X=\begin{bmatrix}\pi/6&0\\0&-2\end{bmatrix}\).

Only the \((1,1)\) and \((2,2)\) entries affect this function. For a perturbation \(H=(h_{ij})\), the ordinary one-variable derivatives give \(df_X[H]=\cos(x_{11})h_{11}+x_{22}h_{22}\). The other two perturbation entries have coefficient zero.

At \(x_{11}=\pi/6\), \(\cos(x_{11})=\sqrt{3}/2\); at \(x_{22}=-2\), the second coefficient is \(-2\). Consequently, \(\nabla f(X)=\begin{bmatrix}\sqrt{3}/2&0\\0&-2\end{bmatrix}\). Checking against the convention, \(\langle\nabla f(X),H\rangle_F=(\sqrt{3}/2)h_{11}-2h_{22}\), which equals the differential at this \(X\).

Directional Change and Steepest Increase

For any direction \(H\), the directional derivative at \(X\) is the first-order change per unit step in that direction: \(\left.\frac{d}{dt}f(X+tH)\right|_{t=0}=df_X[H]=\langle\nabla f(X),H\rangle_F\). If \(H\) is a unit direction, so that \(\|H\|_F=1\), the differential measures the change per unit Frobenius length.

Theorem: If \(f\) is differentiable at \(X\) and \(\nabla f(X)\neq0\), then among all perturbation directions \(H\) with \(\|H\|_F=1\), the largest directional derivative is \(\|\nabla f(X)\|_F\). It is attained at \(H=\nabla f(X)/\|\nabla f(X)\|_F\). If \(\nabla f(X)=0\), every directional derivative is zero.

Proof. Write \(G=\nabla f(X)\). The Cauchy–Schwarz inequality for the Frobenius inner product gives \(\langle G,H\rangle_F\leq|\langle G,H\rangle_F|\leq\|G\|_F\|H\|_F=\|G\|_F\) for every unit \(H\). When \(G\neq0\), choosing \(H=G/\|G\|_F\) gives \(\langle G,H\rangle_F=\|G\|_F\), so the upper bound is attained. When \(G=0\), \(\langle G,H\rangle_F=0\) for every \(H\). \(\square\)

This result explains the geometric role of the gradient: it points in the direction of greatest first-order increase, with its norm giving the rate of that increase per unit Frobenius length. It is a local statement about first-order change, not a claim that taking a step in that direction will always increase the function by the same amount.

Common Mistake: Transposing the Gradient

A frequent error is to confuse the gradient with its transpose. Under the convention here, if \(X\) is \(m\times n\), then \(\nabla f(X)\) is also \(m\times n\). The transpose appears inside the inner product: \(\operatorname{tr}((\nabla f)^T dX)\). It does not change the dimensions assigned to the gradient itself.

For example, when \(X\) is \(2\times3\), the expression \((\nabla f)^T dX\) is a \(3\times3\) matrix and its trace is scalar. Writing a \(3\times2\) matrix as the gradient instead would make its Frobenius inner product with the \(2\times3\) perturbation undefined. A related pitfall is to read coefficients in the wrong positions: the coefficient of \(h_{ij}\) belongs in gradient position \((i,j)\), not \((j,i)\).

Key takeaway: Express the differential as a linear function of an arbitrary perturbation \(H\). The coefficient of \(h_{ij}\) is the \((i,j)\) gradient entry, and the result must have the same dimensions as \(X\).

Check Your Understanding

Use the Frobenius inner product and the differential convention to answer these questions.

  1. For two \(2\times2\) matrices, write the Frobenius inner product as an entrywise sum. Why is the result a scalar?
  2. If a differential is \(df_X[H]=4h_{11}-h_{23}\), what is the gradient’s shape and which entries are nonzero?
  3. Why does uniqueness of the gradient follow if its Frobenius inner product with every perturbation is specified?
  4. For a differentiable scalar function, what is the directional derivative in direction \(H\), expressed using its gradient?
  5. When the gradient is nonzero, which unit-Frobenius direction gives the largest first-order increase?
Clinical Biostats