Skip to main content

9  Matrix Scalar and Vector Calculus

This section develops matrix calculus ideas.

9.1 Matrix calculus for scalar-valued functions

9.1.1 Scalar-valued functions

A scalar-valued function is a function that takes a vector as input and produces a single scalar (real number) as output. In mathematical notation, a scalar-valued function can be represented as:

\[ f: \mathbb{R}^n \rightarrow \mathbb{R} \]

where \(f\) is the function, \(\mathbb{R}^n\) is the n-dimensional real vector space (the input space), and \(\mathbb{R}\) is the set of real numbers (the output space).

9.1.2 Differential, gradient, and derivative

For a scalar-valued function, we can take derivative of the function with respect to its input vector. The derivative of a scalar-valued function is represented by the gradient, which is a (column) vector that contains the partial derivatives of the function with respect to each component of the input vector.

The gradient of a scalar-valued function \(f\) with respect to the input vector \(\mathbf{x}\) is denoted as:

\[ \nabla f(\mathbf{x}) = \left[ \frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \ldots, \frac{\partial f}{\partial x_n} \right]^T \]

where \(\nabla f(\mathbf{x})\) is the gradient vector, and \(\frac{\partial f}{\partial x_i}\) represents the partial derivative of \(f\) with respect to the \(i\)-th component of the input vector \(\mathbf{x}\).

We can also express the differential of the function \(f\) at a point \(\mathbf{x}\) as:

\[ df = \nabla f(\mathbf{x})^T d\mathbf{x} \]

where \(df\) is the differential of the function, \(\nabla f(\mathbf{x})^T\) is the transpose of the gradient vector, and \(d\mathbf{x}\) is a small change in the input vector. This expression says that the change in the function value \(df\) can be approximated by the dot product of the gradient and the change in the input vector, writing out the sum explicitly:

\[ df = \sum_{i=1}^{n} \frac{\partial f}{\partial x_i} dx_i \]

where \(dx_i\) is the small change in the \(i\)-th component of the input vector \(\mathbf{x}\).

9.1.3 Using matrix rules to take derivative of scalar-valued functions

Since we are dealing with a scalar-valued function, we do not need to use matrix algebra rules for derivation, we can just write out the scalar-valued function as a single equation and take the derivative using standard calculus rules.

However, matrix algebra makes it easier to express the function and its derivative in a compact form, especially when dealing with high-dimensional input vectors.

For example, consider the scalar-valued function:

\[ f(\mathbf{x}) = \mathbf{x}^T \mathbf{A} \mathbf{x} \]

where \(\mathbf{A}\) is a sqaure matrix. To find the gradient of this function with respect to \(\mathbf{x}\), we can use the following matrix calculus rule:

\[ \nabla (\mathbf{x}^T \mathbf{A} \mathbf{x}) = (\mathbf{A} + \mathbf{A}^T) \mathbf{x} \]

If \(\mathbf{A}\) is symmetric, \(\mathbf{A} = \mathbf{A}^T\), so the gradient simplifies to:

\[ \nabla f(\mathbf{x}) = 2 \mathbf{A} \mathbf{x} \]

Proposition:\(\nabla (\mathbf{x}^T \mathbf{A} \mathbf{x}) = (\mathbf{A} + \mathbf{A}^T) \mathbf{x}\)

Proof: We can derive this result using the properties of matrix derivatives. First, by definition, an infinitestimal change in \(f\) is the difference between the function \(f\) evaluated at shifting \(\mathbf{x}\) by an infinitestimal amount \(d\mathbf{x}\) and the function evaluated at \(\mathbf{x}\) itself:

\[ df = f(\mathbf{x} + d\mathbf{x}) - f(\mathbf{x}) \]

Second, we know what the function \(f\) is, so we can substitute it in:

\[ df = (\mathbf{x} + d\mathbf{x})^T \mathbf{A} (\mathbf{x} + d\mathbf{x}) - \mathbf{x}^T \mathbf{A} \mathbf{x} \]

Third, remember our basic matrix associative, commutative, and distributive rules, remember that we can do the same operations as in scalar algebra, except we can not switch the order of matrixes in multiplication. We can expand the above expression using the distributive rule as follows, and have two terms cancel out:

\[\begin{align} df &= \mathbf{x}^T \mathbf{A} \mathbf{x} + d\mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{x}^T \mathbf{A} d\mathbf{x} + d\mathbf{x}^T \mathbf{A} d\mathbf{x} - \mathbf{x}^T \mathbf{A} \mathbf{x}\\ &= d\mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{x}^T \mathbf{A} d\mathbf{x} + d\mathbf{x}^T \mathbf{A} d\mathbf{x} \end{align}\]

Fourth, now remember also our transpose rules, that we can distribute transpose over addition, and also multiplication but have to switch the order of multiplication. So we take the transpose of the first term twice (second line), and realize also that the terms are all scalars and the transpose of a scalar is a scalar (third line), and we have:

\[\begin{align} df &= d\mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{x}^T \mathbf{A} d\mathbf{x} + d\mathbf{x}^T \mathbf{A} d\mathbf{x}\\ &= d\mathbf{x}^T \mathbf{A} \mathbf{x} + (d\mathbf{x}^T \mathbf{A}^T \mathbf{x})^T + d\mathbf{x}^T \mathbf{A} d\mathbf{x}\\ &= d\mathbf{x}^T \mathbf{A} \mathbf{x} + d\mathbf{x}^T \mathbf{A}^T \mathbf{x} + d\mathbf{x}^T \mathbf{A} d\mathbf{x}\\ \end{align}\]

Fifth, what do we do with the third term above? Remember that \(d\mathbf{x}\) is infinitesimal, so the term \(d\mathbf{x}^T \mathbf{A} d\mathbf{x}\) is infinitesimal squared, which is negligible. So we can drop that term, and have:

\[ df = d\mathbf{x}^T \mathbf{A} \mathbf{x} + d\mathbf{x}^T \mathbf{A}^T \mathbf{x} \]

Finally, we can factor out \(d\mathbf{x}^T\) from the right hand side using the distributive rule, and have (also transposing a scalar again):

\[\begin{align} df &= d\mathbf{x}^T (\mathbf{A} + \mathbf{A}^T) \mathbf{x}\\ &= \mathbf{x}^T (\mathbf{A} + \mathbf{A}^T) d\mathbf{x}\\ & = \nabla f(\mathbf{x})^T d\mathbf{x} \end{align}\]

Hence, \(\nabla f(\mathbf{x}) = (\mathbf{A} + \mathbf{A}^T) \mathbf{x}\), as we wanted to show.\(\blacksquare\)

9.2 Vector-valued functions, product rule and chain rule with matrices

9.2.1 Jacobian

Let \(f(\mathbf{x})\) be a vector-valued function that maps an n by 1 vector \(\mathbf{x}\) to an m by 1 vector \(\mathbf{y}\), i.e., \(f: \mathbb{R}^n \rightarrow \mathbb{R}^m\).

\[ df = J_f(\mathbf{x}) d\mathbf{x} = f'(\mathbf{x}) d\mathbf{x} \]

where \(J_f(\mathbf{x})\) is the Jacobian matrix of \(f\) at \(\mathbf{x}\), which is an m by n matrix where the element in the i-th row and j-th column is the partial derivative of the i-th component of \(f\) with respect to the j-th component of \(\mathbf{x}\).

In the case when \(n=2\) and \(m=2\), we have:

\[ \begin{bmatrix} df_1 \\ df_2 \end{bmatrix} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} \end{bmatrix} \begin{bmatrix} dx_1 \\ dx_2 \end{bmatrix} \]

In scalar calculus, we wrote \(\frac{df}{dx} = f'(x)\). In matrix calculus, we can no longer write \(\frac{df}{dx}\) because \(f\) and \(x\) are vectors. Instead, we write \(df = J_f(x) dx\) or \(df = f'(x) dx\), understanding their respective dimensions. But just like in scalar calculus, the \(df\) represents a small change in \(f\) when \(x\) changes by a small amount \(dx\).

9.2.2 The product rule

Suppose we have \(H(X) = U(X) V(X)\), where \(U(X)\) and \(V(X)\) are “product-conforming”. What does it mean for them to be “product-conforming”? It means that the number of columns in \(U(X)\) must equal the number of rows in \(V(X)\), so that the matrix product \(U(X)V(X)\) is defined. If \(U(X)\) is a row vector and \(V(X)\) is a column vector, then their product is a scalar.\(U(X)\) or \(V(X)\) might be constant matrixes that do not include \(X\). The product rule extends to matrix calculus:

\[ dH = U dV + dU V \]

where \(dH\), \(dU\), and \(dV\) are the differentials of \(H(X)\), \(U(X)\), and \(V(X)\), respectively.

Proposition: if \(H(X) = U(X) V(X)\), then \(dH = U dV + dU V\), where \(dH\), \(dU\), and \(dV\) are the differentials of \(H(X)\), \(U(X)\), and \(V(X)\), respectively, and \(U(X)\) and \(V(X)\) are “product-conforming” matrices.

Proof: First we begin with expressing the definition of the differential and plugging in the definition of \(H(X)\):

\[\begin{align} dH(X) &= H(X + dX) - H(X)\\ &= U(X + dX)V(X + dX) - U(X)V(X)\\ \end{align}\]

Second, we use the definition of differential again to replace \(U(X + dX)\) and \(V(X + dX)\) with \(U(X) + dU\) and \(V(X) + dV\), respectively:

\[\begin{align} &U(X + dX)V(X + dX) - U(X)V(X)\\ =& (U(X) + dU)(V(X) + dV) - U(X)V(X)\\ \end{align}\]

Third, we use the distributive property of matrix multiplication to expand the product \((U(X) + dU)(V(X) + dV)\), and have two terms cancel out.

\[\begin{align} & (U(X) + dU)(V(X) + dV) - U(X)V(X)\\ =& U(X)V(X) + U(X)dV + dU V(X) + dU dV - U(X)V(X)\\ =& U(X)dV + dU V(X) + dU dV\\ =& U(X)dV + dU V(X) \end{align}\]

We ignore the last term \(dU dV\) because it is the square of two infinitesimals, and we are left with the first two terms. This is the product rule.

9.2.3 The chain rule

Similar to the product rule, we can extend the chain rule to matrix calculus. Suppose we have \(H(X) = U(V(X))\), where \(U\) is a function that takes a matrix as input and outputs another matrix, and \(V(X)\) is a function that takes a matrix as input and outputs another matrix. The chain rule states that:

\[ dH(X) = U'(V(X)) V'(X) dX = H'(X) dX \]

In this case, \(U'(V(X))\) is jacobian of \(U\) with respect to its input V, and V’(X) is the jacobian of V with respect to X. If \(H(X) \in R^{m}\), \(V(X) \in R^{n}\), and \(X \in R^{p}\), then \(U'(V(X))\) is an m by n matrix, and \(V'(X)\) is an n by p matrix. Thus, \(H'(X) = U'(V(X)) V'(X)\) is an m by p matrix.

Similar to the product rule, the chain rule can be proved using the definition of differential and ignoring the square of infinitesimals.

9.2.4 Matrix differential example, scalar matrix

Now we have an example, where we need to use the chain rule and the product rule together. The example is, let \(A\) be a \(n\) by \(k\) matrix, and \(X \in R^{k}\), and \(g: R^{k} \rightarrow R^{m}\). We define \(f\) as:

\[ f(X) = g(A^{T}AX) \]

We apply the chain rule and the product rule to find the differential of \(f(X)\) with respect to \(X\).

\[\begin{align} df(X) &= g'(A^{T}AX) d(A^{T}AX)\\ &= g'(A^{T}AX) ((A^{T}A) (dX) + (d(A^{T}A)) X)\\ &= g'(A^{T}AX) (A^{T}A dX + 0)\\ &= g'(A^{T}AX) A^{T}A dX\\ \end{align}\]

Thus, the jacobian of \(f(X)\) with respect to \(X\) is:

\[ f'(X) = g'(A^{T}AX) (A^{T}A) \]

9.2.5 Matrix differential example, “cubic” triple product

We have another example, where we need to use the chain rule and the product rule together again. The example is, let \(A\) be a \(n\) by \(k\) matrix, and \(X \in R^{k}\), and \(g: R^{k} \rightarrow R^{m}\). We define \(f\) as:

As an example, we have a “triple product”:

\[ f(X) = g(XX^{T}X) \]

We apply the chain rule and the product rule to find the differential of \(f(X)\) with respect to \(X\).

\[\begin{align} df(X) &= g'(XX^{T}X) d(XX^{T}X)\\ &= g'(XX^{T}X) (X X^{T} dX + d(XX^T) X)\\ &= g'(XX^{T}X) (X X^{T} dX + (Xd(X^T) + dX X^T) X)\\ &= g'(XX^{T}X) (X X^{T} dX + Xd(X)^TX + dX X^T X)\\ \end{align}\]

In the last line we distribute, and also use the fact that \(d(X^T) = d(X)^T\) (the differential of a transpose is the transpose of the differential). Now note that \(X^T X\) is a scalar, and also \(d(X)^T X\) is a scalar. Hence, they are equal to their own transposes, and we can move scalar. Thus, we have:

\[\begin{align} df(X) &= g'(XX^{T}X) (X X^{T} dX + Xd(X)^TX + dX X^T X)\\ &= g'(XX^{T}X) (X X^{T} dX + XX^TdX + X^T X dX)\\ &= g'(XX^{T}X) (2 X X^{T} + X^TX I) dX\\ \end{align}\]

where \(I\) is the identity matrix. Thus, the jacobian of \(f(X)\) with respect to \(X\) is:

\[ f'(X) = g'(XX^{T}X) (2 X X^{T} + X^TX I) \]

What if we were in the scalar algebra world, and had:\(f(x) = g(x^3)\), where \(x\) is a scalar. Then we would have:

\[\begin{align} df(x) &= g'(x^3) d(x^3)\\ &= g'(x^3) 3x^2 dx\\ \end{align}\]

Suppose \(k=1\), then \(X\) is a scalar, is the matrix-calculus derived result consistent with the scalar-calculus derived result? Yes:

\[\begin{align} df(X) &= g'(XX^TX) (2 X X^{T} + X^TX I) dX\\ &= g'(X^3) (2 X^2 + X^2) dX\\ &= g'(X^3) 3X^2 dX\\ \end{align}\]