How this calculation works
Matrix multiplication is associative, so changing parentheses preserves an ordered product when the dimensions are compatible. Its computational cost can change substantially because intermediate matrix shapes differ. This calculator takes only the dimension sequence and uses interval dynamic programming to choose the cheapest final split for each contiguous subchain. It then reconstructs an optimal parenthesization of the original matrix order. Costs count ordinary dense scalar multiplications and remain exact integers even when they exceed the range represented exactly by JavaScript floating-point numbers.
M[i,j]=min over i≤k<j of M[i,k]+M[k+1,j]+d(i−1)d(k)d(j); M[i,i]=0.A worked example
Let A be 10×100, B be 100×5 and C be 5×50. Computing (AB)C costs 10×100×5=5,000 plus 10×5×50=2,500, totaling 7,500. Computing A(BC) costs 100×5×50=25,000 plus 10×100×50=50,000, totaling 75,000. The entries of the matrices are unnecessary for that comparison. The longer default sequence 30,35,15,5,10,20,25 has a minimum count of 15,125; several subchain decisions contribute to that optimum.
Good to know
Supports 1–60 matrices described by 2–61 positive whole dimensions, each at most 1,000,000. Consecutive shapes are implied by the shared sequence, so their inner dimensions match by construction. Matrices cannot be reordered. Costs assume the ordinary abc scalar-multiplication model for an a×b times b×c product. Addition counts, memory, sparsity, parallelism, fast matrix-multiplication methods, floating-point reassociation effects and measured hardware time are not optimized. A one-matrix chain has zero multiplication cost.
Reference: MIT 6.006 — dynamic-programming parenthesization notes (opens in a new tab) ↗
The last multiplication determines the two subproblems
For a subchain from matrix i through matrix j, suppose the last multiplication splits after k. The left product has dimensions d(i−1) by d(k), while the right product has dimensions d(k) by d(j). Their final multiplication costs the product of those three dimensions. Add the already optimized costs of the two shorter subchains and compare all permitted k values. This covers every parenthesization because every complete expression has one last split.
The dynamic program fills shorter intervals before longer ones, making each required subproblem available when it is used. The table shows those subchain costs and the selected split. Reconstruction follows that split tree recursively; it does not multiply the actual arrays or replace an expression with a mere count. Equal-cost choices use a deterministic first split, so a different parenthesization can still be optimal if its independently calculated operation count agrees.
Cheapest arithmetic count is not automatically fastest execution
Two products with the same scalar count can have very different memory traffic and cache behavior. A large intermediate array may also be undesirable even when it lowers arithmetic work. This page’s objective is deliberately the classical multiplication count. It does not estimate seconds from a processor model or claim to choose the best schedule for a particular numerical library, sparse format or distributed machine.
Associativity of exact mathematics also does not make floating-point reassociation bit-for-bit identical. Changing parentheses can change rounding and numerical conditioning, even though the exact product is the same. The Matrix multiplication calculator performs one supplied matrix product and checks compatible dimensions; this page optimizes the order of a whole compatible chain without seeing entries. Keeping these tasks separate avoids implying that an efficient parenthesization certifies numerical accuracy or that a computed product proves its chosen order was cheapest.
A few questions, answered
Can the algorithm rearrange A, B and C?
No. Matrix multiplication is generally noncommutative. Only parentheses change; the original left-to-right matrix order is preserved.
Why are the reported operation counts strings for very large values?
They are exact integers. String output preserves every digit instead of rounding a count beyond the safe integer range of ordinary floating-point numbers.