How to Differentiate a Hadamard Product
At a glance
- 1. Define the product
The Hadamard product multiplies corresponding entries of two vectors or matrices.
- 2. Apply the product rule
Differentiate each factor while holding the other factor fixed, then add the results.
- 3. Use diagonal matrices
For vectors, multiplication by a fixed vector becomes multiplication by its diagonal matrix.
- 4. Check components
Verify the result entry by entry to avoid confusing elementwise and matrix multiplication.
The Hadamard product, also called the elementwise product, multiplies corresponding entries rather than performing ordinary matrix multiplication. If and have the same dimensions, their Hadamard product is written
with entries
Its derivative follows the ordinary product rule entry by entry. The only extra issue is notation: for vectors, the derivative is usually a Jacobian matrix, while for matrices it is often most naturally written as a differential.
Product rule for vectors
Let be differentiable vector-valued functions, and define
The th component is
Applying the scalar product rule to each component gives
Equivalently, using diagonal matrices,
This is the most useful general formula. It says that the change in the Hadamard product has two contributions: the change in the first factor, weighted by the second, and the change in the second factor, weighted by the first.
If and depend on , let and denote their Jacobian matrices. Then the Jacobian of is
This formula has dimensions , as it should.
One factor fixed
If is a constant vector and only depends on , then . Therefore, since ,
and hence
In particular, if for a fixed vector , then
The diagonal matrix appears because each output component depends only on the corresponding input component.
Worked vector example
Let
Define . Its components are
Differentiate directly:
The same result follows from the Hadamard product rule. First,
Thus
The first row, for example, is
Matrix-valued functions
Let and be matrices of the same size, depending on a scalar . Their Hadamard product satisfies
This is exactly the ordinary product rule applied to every matrix entry.
For example, let
Then
Therefore,
For a matrix-valued function depending on a vector , the same identity holds with differentials:
If individual partial derivatives are required, take the corresponding partial derivative of this expression.
Relation to ordinary matrix multiplication
The Hadamard product is not the same as ordinary matrix multiplication. For example,
whereas ordinary multiplication gives
Consequently, do not replace the Hadamard product rule with a rule for matrix multiplication. For ordinary matrix products, the derivative is ; for Hadamard products, it is .
Common mistakes
- Confusing with ordinary multiplication: The dimensions and derivative formulas are different.
- Dropping one product-rule term: If both factors depend on the variable, both derivatives must appear.
- Ignoring the Jacobian convention: Some authors use Jacobians with gradients arranged as rows rather than columns. The componentwise formula remains valid, but matrix orientations may be transposed.
- Using different-sized factors: A Hadamard product requires corresponding entries, so the factors must have the same shape unless a separate broadcasting convention has been specified.
- Treating a matrix derivative as one ordinary matrix: With several scalar variables, the derivative of a matrix-valued function is generally a third-order tensor, or is represented more conveniently by differentials or vectorization.
References
- Matrix Differential Calculus with Applications in Statistics and Econometrics, Jan R. Magnus and Heinz Neudecker
- Matrix Cookbook
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.