By Jamal Hussein · Last updated:
Hadamard Product Calculator cij = aij × bij
Type two matrices of the same shape, one row per line, with numbers separated by commas or spaces (up to 8 × 8). You get the element-wise product, the ordinary matrix product beside it for comparison, a heat map of the result and the working for every cell.
The Hadamard product is the element-wise product of two matrices of the same shape: each entry of the result is the product of the two entries in the same row and column. This Hadamard product calculator returns that matrix, shows the ordinary matrix product next to it when the shapes allow one, and also handles element-wise division and powers.
What is the Hadamard product?
Take two matrices with identical dimensions and multiply them position by position. Nothing from row 1 ever meets anything from column 2; the top-left entry of A is multiplied by the top-left entry of B and by nothing else. The result has the same shape as the inputs. The operation is also called the element-wise product, the entrywise product or the Schur product, and it is written \(A \circ B\) or \(A \odot B\).
It behaves like ordinary multiplication of numbers, because that is all it is, done many times in parallel. It is commutative, associative and distributes over addition, and its identity element is the matrix filled with ones, not the identity matrix.
Hadamard product formula
Here \(a_{ij}\) and \(b_{ij}\) are the entries in row \(i\) and column \(j\) of A and B, \(m\) is the number of rows and \(n\) the number of columns. Both matrices must be \(m \times n\), and so is the result. The two other operations in the tool follow the same pattern:
Element-wise division has no value wherever B holds a zero, and the calculator flags those cells individually instead of printing NaN. The element-wise power raises each entry to the scalar \(k\) and works for any shape, unlike a matrix power, which needs a square matrix.
Worked example: two 2 × 2 matrices
The default input is A with rows (1, 2) and (3, 4), and B with rows (5, 6) and (7, 8). Multiply matching positions:
The ordinary matrix product of the same pair combines rows with columns. Its top-left entry is 1 × 5 + 2 × 7 = 19, and the full result is a different matrix:
Add up the four Hadamard entries and you get 5 + 12 + 21 + 32 = 70. That total is the Frobenius inner product of A and B, which is what you would get by flattening both matrices into vectors and taking their dot product.
What is the difference between the Hadamard product and matrix multiplication?
They answer different questions. The Hadamard product scales each entry independently; the matrix product composes two linear maps. The table lists the practical differences.
| Property | Hadamard product A ∘ B | Matrix product AB |
|---|---|---|
| Shape rule | A and B have the same shape, m × n | Columns of A = rows of B (m × n times n × p) |
| Result shape | m × n | m × p |
| Entry (i, j) | aij bij | Sum over k of aik bkj |
| Commutative? | Yes, always | No, in general AB ≠ BA |
| Identity element | Matrix of all ones | Identity matrix I |
| NumPy operator | A * B or np.multiply(A, B) | A @ B or np.matmul(A, B) |
| Default example | [[5, 12], [21, 32]] | [[19, 22], [43, 50]] |
The two shape rules only overlap for square matrices of the same size. A 2 × 3 pair has a Hadamard product but no matrix product, while a 2 × 2 times a 2 × 3 has a matrix product but no Hadamard product. For the row-by-column version, use the matrix multiplication calculator. A third operation, the Kronecker product, multiplies every entry of A by the whole of B and returns a much larger block matrix.
Why does A * B give the wrong answer in NumPy?
Because * on NumPy arrays is the Hadamard product, not the matrix product. Code written from a textbook formula such as \(y = Wx\) needs W @ x. With square arrays W * x raises no error at all; it returns a matrix of the right shape holding the wrong numbers, which is why this bug survives so long.
Broadcasting makes it easier to miss. NumPy compares shapes from the last axis backwards, and two axes are compatible when they are equal or when one of them is 1; the axis of length 1 is stretched to match. So a 2 × 3 array times a length-3 vector multiplies every row by that vector, which is useful. But a length-3 vector times a 3 × 1 column silently produces a 3 × 3 array. The strict mathematical Hadamard product, and this calculator, require equal shapes.
Where is the Hadamard product used in machine learning?
Masks. Multiplying by a matrix of zeros and ones keeps some entries and removes the rest. Dropout draws a random 0/1 mask and multiplies the activations by it, then rescales the survivors: with a drop rate of 0.5, activations (0.5, −1.2, 2) under the mask (1, 0, 1) become (1, 0, 4). Padding masks zero out the loss at padded positions in the same way. Attention masks are usually applied by adding a large negative number to the masked scores before the softmax, which drives those attention weights to zero.
LSTM and GRU gates. A gate is a vector of values between 0 and 1 produced by a sigmoid function, and it acts through an element-wise product. The LSTM cell state update is
where the forget gate \(f_t\) decides, component by component, how much of the previous cell state \(c_{t-1}\) survives, and the input gate \(i_t\) decides how much of the candidate \(\tilde{c}_t\) is written.
Backpropagation. The error signal of a layer is the upstream gradient multiplied element-wise by the derivative of the activation:
The first factor is a true matrix product with a transposed weight matrix; the second step is a Hadamard product. Both appear in one line, so mixing up the operators here breaks training.
Per-feature scaling. Multiplying a data matrix by a row vector of scale factors, one per column, is a broadcast Hadamard product. Standardisation, feature weighting and learned scale parameters in normalisation layers all work this way.
Common mistakes
- Using
*where@was intended. Check the shape of the result and, for square matrices, check one entry by hand. - Squaring a matrix with
**.A ** 2squares each entry and gives [[1, 4], [9, 16]] for the default A. The matrix squareA @ Ais [[7, 10], [15, 22]]. - Relying on broadcasting by accident. A shape of (3,) against (3, 1) produces a 3 × 3 result instead of an error. Assert shapes before multiplying.
- Treating element-wise division as multiplication by an inverse.
A / Bdivides entry by entry and has nothing to do with the inverse matrix of B. - Expecting the identity matrix to leave A unchanged. Under the Hadamard product, I zeroes every off-diagonal entry. The neutral element is the all-ones matrix.
Hadamard product in Python with NumPy
The code reproduces the worked example and the comparison with the matrix product.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A * B) # [[ 5 12] [21 32]] Hadamard product
print(np.multiply(A, B)) # same result
print(A @ B) # [[19 22] [43 50]] matrix product
print((A * B).sum()) # 70, the Frobenius inner product
print(A ** 2) # [[ 1 4] [ 9 16]] element-wise power
print(np.linalg.matrix_power(A, 2)) # [[ 7 10] [15 22]]
# Broadcasting: scale each column of a 2 x 3 array
X = np.array([[1, 2, 3], [4, 5, 6]])
print(X * np.array([1, 10, 100])) # [[1 20 300] [4 50 600]]
The numpy.multiply reference confirms that it is equivalent to the * operator on arrays, and the NumPy broadcasting guide gives the full compatibility rules. For division, A / B returns inf or nan with a runtime warning where B is zero.
Frequently asked questions
Is the Hadamard product commutative?
Yes. Each entry is a product of two numbers, and a × b = b × a, so A ∘ B = B ∘ A for every pair of same-shape matrices. It is also associative and distributes over addition. The ordinary matrix product is not commutative in general.
Can you take the Hadamard product of matrices with different shapes?
No. The definition needs an entry of B for every entry of A, so both must be m × n. NumPy will still multiply some unequal shapes by broadcasting an axis of length 1, but that is a convenience of the library, not part of the mathematical definition.
Is A * B in NumPy matrix multiplication?
No. On NumPy arrays A * B is the element-wise (Hadamard) product. Matrix multiplication is A @ B, np.matmul(A, B) or, for 2-D arrays, np.dot(A, B).
What is the Hadamard product of a matrix with the identity matrix?
It keeps the diagonal of the matrix and sets every other entry to zero, because the identity matrix has ones on the diagonal and zeros elsewhere. For A with rows (1, 2) and (3, 4) the result has rows (1, 0) and (0, 4).
Related calculators
Browse the full linear algebra calculators hub, or continue with the tools closest to this one: