Skip to content
Glacius
Linear algebraConcept reference

Entrywise Products

The Hadamard product multiplies equal-shaped matrices entry by entry.

On this page 7 sections
  1. Overview
  2. Multiply matching entries
  3. Keep the operation straight
  4. Match shapes and positions
  5. Key takeaway
  6. Sources & further reading
  7. Concept connections

01Multiply matching entries#

The Hadamard product multiplies equal-shaped matrices entry by entry. It keeps their shape. In ML, this operation can apply a supplied mask or gate separately to each coordinate.

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

Suppose AA has rows (2,1),(3,0)(2,-1),(3,0) and BB has rows (4,5),(0,2)(4,5),(0,-2). The four products are 8,5,0,08,-5,0,0, so ABA\odot B has rows (8,5),(0,0)(8,-5),(0,0).

02Keep the operation straight#

Each output entry uses exactly one entry from each input. No row-column sum appears. A row-by-column product ABAB is a different operation, with a different shape rule and generally different numbers.

Check your reasoning

Rows of A: (2,−1),(0,3). Rows of B: (4,2),(5,−2). Rows of A ⊙ B?

  1. A(8,−2),(0,−6)
  2. B(3,6),(15,−6)
  3. C(8,2),(0,6)
Show answer and explanation
(8,−2),(0,−6)

Multiply corresponding entries and retain their positions.

A mask MM containing zeros and ones sets an entry to zero when the corresponding mask is zero and preserves it when the mask is one. For row (6,3,2)(6,-3,2) and mask (1,0,1)(1,0,1), the result is (6,0,2)(6,0,2).

03Match shapes and positions#

A 2×32\times3 matrix can have a Hadamard product with another 2×32\times3 matrix. Equal numbers of entries are insufficient if their shapes differ. Reshaping or broadcasting is a separate convention that must be stated explicitly.

Check your reasoning

A draft computes an entry of A ⊙ B by summing a left row against a right column. What should change?

  1. ASum only the diagonal
  2. BMultiply just the two matching entries
  3. CTranspose the final matrix
Show answer and explanation
Multiply just the two matching entries

The draft used ordinary matrix multiplication.

Changing one entry of BB affects only its matching entry in ABA\odot B. If the matching AA entry is zero, the output stays zero. Position carries the meaning of this operation.

Key takeaway

Compute the Hadamard product of two equal-shaped real matrices.

  • Compute the Hadamard product of two equal-shaped real matrices.

Sources & further reading

  1. [1]
  2. [2]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Entrywise Products.” Math behind ML. /learn/la-hadamard