Ma · Mathematics

How to Differentiate a Hadamard Product

Published By the 9to5Science editorial team

At a glance

  1. 1. Define the product

    The Hadamard product multiplies corresponding entries of two vectors or matrices.

  2. 2. Apply the product rule

    Differentiate each factor while holding the other factor fixed, then add the results.

  3. 3. Use diagonal matrices

    For vectors, multiplication by a fixed vector becomes multiplication by its diagonal matrix.

  4. 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 AA and BB have the same dimensions, their Hadamard product is written

A⊙B,A \odot B,

with entries

(A⊙B)ij=AijBij.(A \odot B)_{ij}=A_{ij}B_{ij}.

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 a(x),b(x)∈Rna(x),b(x)\in\mathbb{R}^n be differentiable vector-valued functions, and define

f(x)=a(x)⊙b(x).f(x)=a(x)\odot b(x).

The iith component is

fi(x)=ai(x)bi(x).f_i(x)=a_i(x)b_i(x).

Applying the scalar product rule to each component gives

df=da⊙b+a⊙db.df=da\odot b+a\odot db.

Equivalently, using diagonal matrices,

df=diag⁡(b) da+diag⁡(a) db.df=\operatorname{diag}(b)\,da+\operatorname{diag}(a)\,db.

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 aa and bb depend on x∈Rmx\in\mathbb{R}^m, let JaJ_a and JbJ_b denote their Jacobian matrices. Then the Jacobian of ff is

Jf(x)=diag⁡(b(x))Ja(x)+diag⁡(a(x))Jb(x).J_f(x)=\operatorname{diag}(b(x))J_a(x)+\operatorname{diag}(a(x))J_b(x).

This formula has dimensions n×mn\times m, as it should.

One factor fixed

If bb is a constant vector and only a(x)a(x) depends on xx, then db=0db=0. Therefore, since db=0db=0,

df=da⊙b=diag⁡(b) da,df=da\odot b=\operatorname{diag}(b)\,da,

and hence

Jf(x)=diag⁡(b)Ja(x).J_f(x)=\operatorname{diag}(b)J_a(x).

In particular, if f(x)=c⊙xf(x)=c\odot x for a fixed vector cc, then

Jf=diag⁡(c).J_f=\operatorname{diag}(c).

The diagonal matrix appears because each output component depends only on the corresponding input component.

Worked vector example

Let

a(x,y)=[x2sin⁡yx+y],b(x,y)=[exy2x−y].a(x,y)=\begin{bmatrix}x^2\\ \sin y\\ x+y\end{bmatrix}, \qquad b(x,y)=\begin{bmatrix}e^x\\ y^2\\ x-y\end{bmatrix}.

Define f=a⊙bf=a\odot b. Its components are

f(x,y)=[x2exy2sin⁡y(x+y)(x−y)].f(x,y)=\begin{bmatrix} x^2e^x\\ y^2\sin y\\ (x+y)(x-y) \end{bmatrix}.

Differentiate directly:

Jf(x,y)=[ex(x2+2x)002ysin⁡y+y2cos⁡y2x−2y].J_f(x,y)= \begin{bmatrix} e^x(x^2+2x) & 0\\ 0 & 2y\sin y+y^2\cos y\\ 2x & -2y \end{bmatrix}.

The same result follows from the Hadamard product rule. First,

Ja=[2x00cos⁡y11],Jb=[ex002y1−1].J_a= \begin{bmatrix} 2x&0\\ 0&\cos y\\ 1&1 \end{bmatrix}, \qquad J_b= \begin{bmatrix} e^x&0\\ 0&2y\\ 1&-1 \end{bmatrix}.

Thus

Jf=diag⁡(b)Ja+diag⁡(a)Jb.J_f=\operatorname{diag}(b)J_a+\operatorname{diag}(a)J_b.

The first row, for example, is

ex[2x0]+x2[ex0]=[ex(x2+2x)0].e^x\begin{bmatrix}2x&0\end{bmatrix}+x^2\begin{bmatrix}e^x&0\end{bmatrix} =\begin{bmatrix}e^x(x^2+2x)&0\end{bmatrix}.

Matrix-valued functions

Let A(t)A(t) and B(t)B(t) be matrices of the same size, depending on a scalar tt. Their Hadamard product satisfies

ddt(A(t)⊙B(t))=A′(t)⊙B(t)+A(t)⊙B′(t).\frac{d}{dt}\bigl(A(t)\odot B(t)\bigr) =A'(t)\odot B(t)+A(t)\odot B'(t).

This is exactly the ordinary product rule applied to every matrix entry.

For example, let

A(t)=[t2sin⁡t1t],B(t)=[ettt2cos⁡t].A(t)=\begin{bmatrix}t^2&\sin t\\1&t\end{bmatrix}, \qquad B(t)=\begin{bmatrix}e^t&t\\t^2&\cos t\end{bmatrix}.

Then

A′(t)=[2tcos⁡t01],B′(t)=[et12t−sin⁡t].A'(t)=\begin{bmatrix}2t&\cos t\\0&1\end{bmatrix}, \qquad B'(t)=\begin{bmatrix}e^t&1\\2t&-\sin t\end{bmatrix}.

Therefore,

ddt(A⊙B)=[et(t2+2t)tcos⁡t+sin⁡t2tcos⁡t−tsin⁡t].\frac{d}{dt}(A\odot B) = \begin{bmatrix} e^t(t^2+2t)&t\cos t+\sin t\\ 2t&\cos t-t\sin t \end{bmatrix}.

For a matrix-valued function depending on a vector xx, the same identity holds with differentials:

d(A⊙B)=dA⊙B+A⊙dB.d(A\odot B)=dA\odot B+A\odot dB.

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,

[1234]⊙[5678]=[5122132],\begin{bmatrix}1&2\\3&4\end{bmatrix} \odot \begin{bmatrix}5&6\\7&8\end{bmatrix} = \begin{bmatrix}5&12\\21&32\end{bmatrix},

whereas ordinary multiplication gives

[1234][5678]=[19224350].\begin{bmatrix}1&2\\3&4\end{bmatrix} \begin{bmatrix}5&6\\7&8\end{bmatrix} = \begin{bmatrix}19&22\\43&50\end{bmatrix}.

Consequently, do not replace the Hadamard product rule with a rule for matrix multiplication. For ordinary matrix products, the derivative is d(AB)=dA B+A dBd(AB)=dA\,B+A\,dB; for Hadamard products, it is d(A⊙B)=dA⊙B+A⊙dBd(A\odot B)=dA\odot B+A\odot dB.

Common mistakes

  • Confusing ⊙\odot 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

  1. Matrix Differential Calculus with Applications in Statistics and Econometrics, Jan R. Magnus and Heinz Neudecker
  2. Matrix Cookbook

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.