Skip to main content

Matrix calculus for scalar-valued functions

Author
Affiliation

Department of Economics, University of Houston

Published

November 30, 2026

Part of Math Camp: An Introduction to Mathematics and Statistics with Applications in Economics.
Main Stam4Econ site | View this section in the book | Author: Fan Wang | DOI: 10.5281/zenodo.24696926
Date: 2026-11-30 | HTML | PDF | QMD

1 Matrix calculus for scalar-valued functions

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).

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}\).

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\)