Calculus, Linear Algebra & Discrete Math Matrices & Linear Algebra Row-by-column (Cayley) matrix product

Matrix Multiplication Calculator

Enter two matrices row by row and this calculator multiplies them, showing every entry of the answer as the explicit dot product of a row of A with a column of B. Sizes are read from what you type, so a matrix times a vector is just a B with one column. Switch to power mode to raise a square matrix to an integer power. If the inner dimensions do not match, you get a plain explanation of which number would have to equal which, instead of a silent failure.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Matrix A, row 1Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.1, 2
Matrix B, row 1Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.5, 6
Matrix A, row 2Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.3, 4
Matrix B, row 2Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.7, 8
Matrix A, row 3Only needed for a three-row A. Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.
Matrix B, row 3Only needed for a three-row B. Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix has fewer rows.
What to computePower mode ignores matrix B and requires A to be square.The product A × B
Exponent n for AⁿA whole number from 0 to 12. A⁰ is the identity matrix.3

It returns

  • First entry of the result, row 1 column 1 — Row 1 of the left matrix dotted with column 1 of the right. The whole result matrix is in the table below.
  • Trace of the result — Sum of the diagonal. Defined only when the result is square.
  • Frobenius norm of the result — The square root of the sum of the squares of every entry.
  • Largest entry by magnitude
  • Sum of all entries

The formula

(AB)ij=k=1naikbkj
(m×n)(n×p)(m×p)

In plain text: (AB)ᵢⱼ = Σₖ aᵢₖ · bₖⱼ

  • AThe left matrix, of shape m×n (—)
  • BThe right matrix, of shape n×p — its row count must equal A's column count (—)
  • (AB)ᵢⱼRow i of A dotted with column j of B (—)
  • kThe summation index running across A's columns and down B's rows (—)

The product is m×p. Matrix multiplication is associative and distributive but not commutative.

Updated Category Matrices & Linear Algebra Verified against published test cases Reading time 10 min

What matrix multiplication actually does

Matrix multiplication looks arbitrary until you know what it is for: it is composition of linear maps. If B transforms space and then A transforms the result, the single matrix that does both in one step is AB. The strange-looking row-times-column rule is forced by that requirement — it is the only definition that makes composing maps agree with multiplying their matrices.

That explains every rule you have to remember. The inner dimensions must match because B's output lives in the space A takes as input. The order matters because doing B then A is not the same as doing A then B — rotate a book 90° about one axis and then another, then repeat in the opposite order, and the book ends up somewhere different. And the product is m×p because the composite map takes p-dimensional input to m-dimensional output.

Concretely, entry (i, j) of AB is the dot product of row i of A with column j of B. Each entry answers one narrow question: how much does the j-th input direction contribute to the i-th output coordinate?

The dimension rule, and how to check it in one glance

Write the shapes side by side: (m×n)(n×p). The two inner numbers must be equal, and they cancel; the two outer numbers survive as the shape of the answer. A 2×3 times a 3×2 gives a 2×2. A 3×2 times a 2×3 gives a 3×3. Same two matrices, different order, different shape — which is the fastest way to see that AB and BA are not the same object even when both exist.

Two special cases are worth naming because they are most of the multiplication anyone does in practice. A matrix times a column vector is an (m×n)(n×1) product, giving an m×1 column: this is the operation Ax that a linear system is built from. A row vector times a matrix is (1×m)(m×n), giving a 1×n row.

Powers only make sense for square matrices, since A² needs A's columns to match A's own rows. A⁰ is defined as the identity, exactly as x⁰ = 1. Powers of a matrix have real meaning: if A is the transition matrix of a Markov chain, Aⁿ gives the n-step transition probabilities; if A is the adjacency matrix of a graph, entry (i, j) of Aⁿ counts the walks of length n from node i to node j.

The cost of a straightforward product is m·n·p multiplications and about the same number of additions. For two n×n matrices that is n³, which is why multiplying two 1,000×1,000 matrices is a billion operations and why faster algorithms such as Strassen's exist at all.

Worked example: [[1, 2], [3, 4]] × [[5, 6], [7, 8]]

Both matrices are 2×2, so the inner dimensions match and the answer is 2×2. Four entries, four dot products.

  1. Entry (1,1). Row 1 of A is (1, 2); column 1 of B is (5, 7). Dot them: 1×5 + 2×7 = 5 + 14 = 19.
  2. Entry (1,2). Row 1 of A against column 2 of B, which is (6, 8): 1×6 + 2×8 = 6 + 16 = 22.
  3. Entry (2,1). Row 2 of A is (3, 4), against column 1 of B: 3×5 + 4×7 = 15 + 28 = 43.
  4. Entry (2,2). Row 2 of A against column 2 of B: 3×6 + 4×8 = 18 + 32 = 50.

So AB = [[19, 22], [43, 50]]. The trace is 19 + 50 = 69, the entries sum to 134, and the Frobenius norm is √(19² + 22² + 43² + 50²) = √(361 + 484 + 1849 + 2500) = √5194 = 72.069411.

Now reverse the order. BA has entry (1,1) equal to row 1 of B against column 1 of A: 5×1 + 6×3 = 23, not 19. Working the rest out gives BA = [[23, 34], [31, 46]], with trace 23 + 46 = 69. The traces agree — that is the identity tr(AB) = tr(BA), which holds for every pair whose product is defined both ways — but the matrices themselves are completely different. This is what non-commutativity looks like in four numbers.

Reading the summary figures

The headline figure is the (1,1) entry — row 1 of the left matrix dotted with column 1 of the right. It is the first thing you compute by hand and the first thing to check against someone else's answer, and unlike the trace it exists for every product, including a matrix times a column vector. The full result matrix sits in the table below it.

The trace is the sum of the diagonal, and it exists only when the result is square. It is invariant under a change of basis and equals the sum of the eigenvalues, which makes it a quick fingerprint of a transformation. The identity tr(AB) = tr(BA) holds even when AB and BA have different sizes, which is used constantly in proofs.

The Frobenius norm is the square root of the sum of the squares of every entry — the ordinary Euclidean length of the matrix read as a long vector. It is the standard way to answer “how big is this matrix?” and the standard way to measure the gap between two matrices as ‖A − B‖F.

The largest entry and the sum of entries are practical sanity checks. If you multiply two matrices of entries around 10 with an inner dimension of 3, expect entries around 300; a result of 3 or 30,000 means something is wrong with the shapes or the transcription.

Watch for a result that is all zeros without either input being zero. That is genuinely possible — [[1,0],[0,0]] times [[0,0],[0,1]] is the zero matrix — and it is one of the properties that makes matrices unlike numbers. A zero product does not imply a zero factor.

Which shapes multiply, and what comes out

The inner dimensions must agree. Every row can be reproduced in the calculator above.
ABAB defined?Shape of ABBA defined?
2×22×2Yes2×2Yes, usually different
2×33×2Yes2×2Yes, but 3×3
3×22×3Yes3×3Yes, but 2×2
2×22×1Yes2×1No
1×33×1Yes1×1 (a scalar)Yes, but 3×3
2×23×2No — 2 ≠ 3Yes, 3×2
3×33×3Yes3×3Yes, usually different

The 1×3 times 3×1 row is the dot product; the 3×1 times 1×3 in the other order is the outer product, a rank-one 3×3 matrix.

The rules that are not what you expect

  • AB ≠ BA in general. This is the big one. Both products may exist, have different shapes, or one may not exist at all. Some special pairs do commute — any matrix with the identity, any two diagonal matrices, any matrix with its own powers — but the general rule is that order matters.
  • AB = 0 does not mean A = 0 or B = 0. Two non-zero matrices can multiply to nothing, because one can map everything into the null space of the other.
  • AB = AC does not mean B = C. Cancellation is only valid when A is invertible, since you are really multiplying by A⁻¹.
  • (AB)ᵀ = BᵀAᵀ and (AB)⁻¹ = B⁻¹A⁻¹. Transposing and inverting both reverse the order.
  • The product is associative: (AB)C = A(BC). The grouping is free but the order is not, and the grouping you choose can change the operation count enormously for chains of differently-shaped matrices.
  • Multiplying element by element is a different operation. The entrywise product used in spreadsheets and array languages is the Hadamard product, not this. It requires identical shapes and has no connection to composing maps.

Powers of a matrix count paths

Let A be the adjacency matrix of a graph, with a 1 wherever two nodes are joined. Then entry (i, j) of An is exactly the number of walks of length n from node i to node j, because the sum over intermediate nodes in the multiplication rule is the enumeration of those walks. Applying the same idea to the matrix [[1,1],[1,0]] gives the Fibonacci numbers: its n-th power is [[Fn+1, Fn], [Fn, Fn−1]], which you can verify above by setting the exponent to 3 and reading off [[3,2],[2,1]].

Multiplication is the check on almost every other matrix computation. Multiply a matrix by its claimed inverse and you should get the identity — that is exactly the residual the matrix inverse calculator reports. Multiply A by a claimed eigenvector and you should get a scalar multiple of that vector, which is the definition the eigenvalue calculator works from.

The determinant is multiplicative across products: det(AB) = det(A)·det(B), so composing two maps multiplies their volume factors. Check it with the determinant calculator on A, on B and on AB. For solving Ax = b rather than forming products, go to the RREF calculator, and for factoring a matrix into a product of triangular pieces, the LU decomposition calculator.

Each individual entry of the product is a dot product, so if you want to see one of them in isolation with the geometry attached — magnitudes, angle, orthogonality — the dot product calculator takes the row and the column directly.

Frequently asked questions

Why does the order of multiplication matter?

Because matrices represent transformations, and doing one transformation then another is generally not the same as doing them in the opposite order. Rotating then reflecting lands somewhere different from reflecting then rotating. Algebraically you can see it in one line: for A = [[1,2],[3,4]] and B = [[5,6],[7,8]], AB = [[19,22],[43,50]] while BA = [[23,34],[31,46]]. The traces happen to agree at 69, but nothing else does.

What sizes of matrix can I multiply here?

Up to three rows each, with as many columns as you like to type, and the sizes are read from what you enter rather than chosen from a menu. A matrix times a vector is just a B with one entry per row. The only requirement is the standard one: the number of columns in A must equal the number of rows in B, and the calculator tells you exactly which two numbers failed to match when they do not.

Can I multiply a matrix by a single number?

That is scalar multiplication, not matrix multiplication, and it works differently: you multiply every entry by that number and the shape is unchanged. There is no dimension rule to satisfy. If you want it here, use a diagonal matrix with the scalar repeated down the diagonal — multiplying by 3·I has exactly the effect of multiplying by 3.

What does the trace tell me?

It is the sum of the diagonal entries, and it equals the sum of the eigenvalues. It is unchanged by a change of basis, so it identifies the transformation rather than the particular coordinates you wrote it in. The identity tr(AB) = tr(BA) is true whenever both products are defined, even though the products themselves usually differ, and it is the workhorse of a great many proofs.

Why is my product a different size from both inputs?

Because the shape rule discards the inner dimensions and keeps the outer ones: (m×n)(n×p) gives m×p. A 2×3 times a 3×2 collapses to a 2×2, while a 3×2 times a 2×3 expands to a 3×3. Nothing has gone wrong — the shape is telling you the dimensions of the input and output spaces of the composite map.

How do I raise a matrix to a power efficiently?

By repeated squaring: A⁸ = ((A²)²)², which is three multiplications instead of seven. For large exponents the professional route is diagonalisation: if A = PDP⁻¹ then Aⁿ = PDⁿP⁻¹, and raising a diagonal matrix to a power just raises each diagonal entry, so the cost stops growing with n. The eigenvalue calculator produces the P and D you need.

Is matrix multiplication associative?

Yes: (AB)C = A(BC) always, whenever the shapes allow. The grouping is free but the cost is not. Take a 1000×1 column A, a 1×1000 row B and a 1000×1 column C. Grouping as (AB)C forms the full 1000×1000 outer product first, at 1000 × 1 × 1000 = 1,000,000 multiplications, then multiplies that by C for 1000 × 1000 × 1 = 1,000,000 more — 2,000,000 in all. Grouping as A(BC) collapses BC to a single number at 1 × 1000 × 1 = 1,000 multiplications, then scales the column at 1000 × 1 × 1 = 1,000 more — 2,000 in all. Same answer, a thousand times the work.

What is the identity matrix and what does multiplying by it do?

The identity I has ones down the diagonal and zeros elsewhere, and multiplying by it changes nothing: AI = IA = A for any A of matching size. It is the matrix equivalent of the number 1, and it is what A⁰ evaluates to in power mode. It is also the target of the inverse: A⁻¹ is defined as the matrix for which the product is I.

References

  • Introduction to Linear Algebra, 5th ed., §2.4 (Rules for Matrix Operations) — Gilbert Strang, Wellesley-Cambridge Press
  • Matrix Computations, 4th ed., §1.1 (Basic Algorithms and Notation) — Gene H. Golub and Charles F. Van Loan, Johns Hopkins University Press
  • Linear Algebra and Its Applications, 5th ed., §2.1 (Matrix Operations) — David C. Lay, Steven R. Lay and Judi J. McDonald, Pearson