acalculator

What is the matrix multiplication?

Type two matrices to get their product A × B, with every entry worked out as a row of A times a column of B.

Your numbers

Matrix A
Matrix B
Give B as many rows as A has columns.
A × B
[58, 64; 139, 154]

A × B = [58, 64; 139, 154].

Size of A × B
2 × 2
Working
c11 = 1×7 + 2×9 + 3×11 = 58; c12 = 1×8 + 2×10 + 3×12 = 64; c21 = 4×7 + 5×9 + 6×11 = 139; c22 = 4×8 + 5×10 + 6×12 = 154

A × B: [58, 64; 139, 154]. A × B = [58, 64; 139, 154].

Each entry, row by column

How to calculate

Multiplies an m × n matrix A by an n × p matrix B in exact decimal arithmetic: each entry of A × B is a row of A times a column of B, with the working.

Example with the default inputs (Matrix A [1, 2, 3; 4, 5, 6], Matrix B [7, 8; 9, 10; 11, 12]): A × B = [58, 64; 139, 154].

Method: The entry in row i, column j of A × B is row i of A times column j of B: cᵢⱼ = Σₖ aᵢₖbₖⱼ.

  • A has m rows and n columns, and B has n rows and p columns, so A × B has m rows and p columns (1 to 6 each). Otherwise there is no product.
  • Every entry is read exactly as typed, so the product is exact: 0.1 × 0.2 is 0.02.
  • The order matters: A × B and B × A are usually different.

Machine-readable copies: Markdown, JSON.

Worked examples

Each example is checked against the calculator on every build.

  1. Matrix A 1, 2, 3; 4, 5, 6, Matrix B 7, 8; 9, 10; 11, 12 gives A × B [58, 64; 139, 154], Size of A × B 2 × 2.Source: hand calculation in content.mdx: 1×7 + 2×9 + 3×11 = 58, …, 4×8 + 5×10 + 6×12 = 154
  2. Matrix A 1, 2; 3, 4, Matrix B 5, 6; 7, 8 gives A × B [19, 22; 43, 50], Working c11 = 1×5 + 2×7 = 19; c12 = 1×6 + 2×8 = 22; c21 = 3×5 + 4×7 = 43; c22 = 3×6 + 4×8 = 50.Source: hand calculation in content.mdx: the row-times-column rule, DLMF equation 1.2.34
  3. Matrix A 5, 6; 7, 8, Matrix B 1, 2; 3, 4 gives A × B [23, 34; 31, 46].Source: hand calculation in content.mdx: B × A differs from A × B (5×1 + 6×3 = 23)
  4. Matrix A 1, 2, 3, Matrix B 4; 5; 6 gives A × B [32], Size of A × B 1 × 1.Source: hand calculation in content.mdx: a row times a column is the dot product, 1×4 + 2×5 + 3×6 = 32
  5. Matrix A 0.1, 0.2; 0.3, 0.4, Matrix B 0.5, 0; 0, 0.5 gives A × B [0.05, 0.1; 0.15, 0.2].Source: hand calculation in content.mdx: B is 0.5 × I, so A × B halves every entry of A; decimals are exact

How it works

A has m rows and n columns, and B has n rows and p columns. The product C = A × B has m rows and p columns. The entry in row i and column j is row i of A times column j of B:

cᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ + … + aᵢₙbₙⱼ

For example, the entry in row 2, column 1 multiplies the second row of A with the first column of B, pair by pair, and adds the products.

The calculator shows the product, its size, and the working for the first 16 entries.

Assumptions

  • A needs as many columns as B has rows. If the sizes do not match, there is no product, and the calculator says so.
  • Each matrix has 1 to 6 rows and 1 to 6 columns.
  • Every entry is read exactly as typed, and the arithmetic is exact. Sums and products of decimals always end, so every entry of the product is shown as an exact decimal.
  • The order matters: A × B and B × A are usually different.
  • The working lists the entries row by row, separated by "; ", at most 16, then "and N more entries the same way". Each entry reads "c12 = 1×6 + 2×8 = 22": c, the row and column numbers from 1 run together, then each product of a row entry and a column entry joined by " + ", with "×" and no spaces around it, and a negative factor in brackets: "c11 = (-1.5)×3.2 + 2×1 = -2.8". Numbers are exact decimals as in the product.
  • The product is written row by row in square brackets, rows separated by "; " and entries by ", ": [58, 64; 139, 154]. Each entry is its exact decimal with no trailing zeros and a plain hyphen for negatives (0.05, -7). The size is written "2 × 2".

Worked examples by hand

A 2 × 3 matrix times a 3 × 2 matrix. A = [1, 2, 3; 4, 5, 6] and B = [7, 8; 9, 10; 11, 12]. The product is 2 × 2:

  • row 1 × column 1: 1 × 7 + 2 × 9 + 3 × 11 = 7 + 18 + 33 = 58
  • row 1 × column 2: 1 × 8 + 2 × 10 + 3 × 12 = 8 + 20 + 36 = 64
  • row 2 × column 1: 4 × 7 + 5 × 9 + 6 × 11 = 28 + 45 + 66 = 139
  • row 2 × column 2: 4 × 8 + 5 × 10 + 6 × 12 = 32 + 50 + 72 = 154

So A × B = [58, 64; 139, 154].

[1, 2; 3, 4] × [5, 6; 7, 8]. 1 × 5 + 2 × 7 = 19, 1 × 6 + 2 × 8 = 22, 3 × 5 + 4 × 7 = 43, 3 × 6 + 4 × 8 = 50: [19, 22; 43, 50].

The other order, [5, 6; 7, 8] × [1, 2; 3, 4]. 5 × 1 + 6 × 3 = 23, 5 × 2 + 6 × 4 = 34, 7 × 1 + 8 × 3 = 31, 7 × 2 + 8 × 4 = 46: [23, 34; 31, 46], which is not the same as above.

A row times a column. [1, 2, 3] × [4; 5; 6] = 1 × 4 + 2 × 5 + 3 × 6 = 4 + 10 + 18 = 32, a 1 × 1 matrix.

Decimals. B = [0.5, 0; 0, 0.5] is 0.5 times the identity matrix, so A × B halves every entry of A: [0.1, 0.2; 0.3, 0.4] × B = [0.05, 0.1; 0.15, 0.2].

Other questions people ask

How do I multiply two matrices?

Each entry of A × B is a row of A times a column of B: multiply the pairs of numbers and add. The entry in row 1, column 1 of [1, 2; 3, 4] × [5, 6; 7, 8] is 1 × 5 + 2 × 7 = 19. Repeat for every row of A and every column of B.

When can two matrices be multiplied?

When A has as many columns as B has rows. A 2 × 3 matrix times a 3 × 2 matrix works and gives a 2 × 2 matrix. A 2 × 3 matrix times a 2 × 3 matrix does not, because a row of 3 numbers cannot pair with a column of 2.

What size is the product?

Rows of A by columns of B. An m × n matrix times an n × p matrix is m × p. A 1 × 3 row times a 3 × 1 column is a single number (1 × 1); a 3 × 1 column times a 1 × 3 row is a 3 × 3 matrix.

Is A × B the same as B × A?

Usually not. [1, 2; 3, 4] × [5, 6; 7, 8] = [19, 22; 43, 50], but [5, 6; 7, 8] × [1, 2; 3, 4] = [23, 34; 31, 46]. Often only one of the two products exists at all.

What is the difference between matrix multiplication and the dot product?

Each entry of a matrix product is a dot product of a row and a column. Multiplying a 1 × n row by an n × 1 column gives just that one dot product: [1, 2, 3] × [4; 5; 6] = 32.

Why is the answer exact?

Each number is read exactly as typed, so decimals like 0.1 do not pick up binary rounding error. Ordinary floating-point arithmetic gives 0.1 × 0.2 = 0.020000000000000004; this calculator gives 0.02.