Matrix Chain Multiplication Cost Optimizer
Enter a sequence of compatible matrix dimensions. Find the least expensive grouping, compare left-to-right and right-to-left multiplication, and see how a maximum intermediate size can change the best order.
Choose the least-work order for a chain of matrices
Compare multiplication counts and check which orders stay within an intermediate size limit.
Matrix dimensions and intermediate limit
Each adjacent pair of dimensions gives one matrix. For example, 2, 3, 12 gives a 2 × 3 matrix followed by a 3 × 12 matrix. Enter three to ten dimensions.
Leave blank for no limit. The final product is excluded.
The minimum-work plan is available.
Best plan and capacity
- Minimum scalar multiplications
- 15,125
- Largest intermediate in chosen plan
- 175 cells
- Smallest feasible intermediate limit
- 175 cells
- Final matrix shape
- 30 × 25
Compare work and intermediate sizes
| Order | Work | Cells | Fits |
|---|---|---|---|
| Chosen plan | 15,125 | 175 | Yes |
| Left to right | 40,500 | 600 | Yes |
| Right to left | 47,500 | 875 | Yes |
See which matrices to multiply first
((A × (B × C)) × ((D × E) × F))
Step 1 · B × C
35 × 15 by 15 × 5 gives 35 × 5
2,625 multiplications · 175 intermediate cells
Step 2 · A × B–C
30 × 35 by 35 × 5 gives 30 × 5
5,250 multiplications · 150 intermediate cells
Step 3 · D × E
5 × 10 by 10 × 20 gives 5 × 20
1,000 multiplications · 100 intermediate cells
Step 4 · D–E × F
5 × 20 by 20 × 25 gives 5 × 25
2,500 multiplications · 125 intermediate cells
Step 5 · A–C × D–F
30 × 5 by 5 × 25 gives 30 × 25
3,750 multiplications · 750 final cells
Export the multiplication orders
Save the dimensions, comparison and each order’s multiplication steps.
View CSV text
How to use this calculator
- Enter shared dimensions in order, separated by commas or spaces. Every adjacent pair gives one matrix shape.
- Leave the cap blank for minimum work alone, or enter the largest cell count allowed for a proper intermediate product.
- Compare multiplication work and intermediate sizes. Select a plan to see its parentheses, factor shapes and each merge.
- Download CSV for the exact plan counts and step ledger.
The Formula
The mathematical logic powering this calculation is:
Let the dimension sequence be d₀,…,dₙ, so matrix i uses dᵢ₋₁ rows and dᵢ columns. A split of a contiguous block after matrix k multiplies a dᵢ₋₁ × d_k matrix by a d_k × dⱼ matrix. This adds dᵢ₋₁d_kdⱼ scalar multiplications to the work in the left and right groups. The new result has dᵢ₋₁dⱼ cells.
For every block, examine each split, starting with single matrices that need zero work. A proper intermediate product uses its output-cell count. The two original operands' sizes and the final matrix do not enter the intermediate limit. A split's largest intermediate is the maximum of its child peak sizes and its own proper output size. Reject splits whose peak exceeds the entered cap. Among feasible candidates, choose the least work, then the smallest peak at equal work. Any remaining tie keeps the first split. A second pass chooses the minimum largest intermediate regardless of work to identify the smallest feasible cap.
The comparison counts classical dense scalar multiplications. It preserves the input order and changes only the parentheses, which yields the same product in exact arithmetic. The cell limit applies to a single produced intermediate matrix at a time. Real memory also depends on simultaneously held matrices, array storage and runtime details. The worksheet does not predict runtime or memory in bytes.
Use three to ten whole dimensions from 1 to 1,000,000. Enter no limit or a whole cap from zero through 1 trillion cells. A two-matrix chain has no intermediate product. Counts use exact integer arithmetic even above floating-point integer precision.Worked Example
If you input:
- Dimensions: 30, 35, 15, 5, 10, 20, 25
- Intermediate limit: None
Result:
The grouping (A × (B × C)) × ((D × E) × F) uses 15,125 scalar multiplications. Left to right uses 40,500 and right to left uses 47,500. The chosen order has a largest proper intermediate of 175 cells.
Frequently Asked Questions
Does the optimizer reorder the matrices?
It keeps the matrices in their entered order and chooses which adjacent subproducts to evaluate first. Associativity permits changing this grouping.
What does the intermediate cell limit include?
It covers the largest proper subproduct formed during the plan. The original input matrices and final result are outside that limit. The metric describes one matrix’s size rather than total simultaneous memory.
Why might the limit increase the multiplication count?
The fastest grouping can form an intermediate matrix that exceeds your cap. The best feasible grouping may create smaller intermediate matrices and perform more scalar work.
What if no order fits?
The workbook shows the smallest feasible limit and lets you inspect the baseline orders. Increasing the limit to that minimum permits at least one grouping.
Similar Calculators
Matrix Calculator
Multiply, add, subtract and transpose matrices, or find a determinant and inverse. Get exact fractions, cell explanations and CSV for matrices up to 4 × 4.
Reduced Row Echelon Form Calculator & Rank Factors
Find an exact RREF with Gauss–Jordan steps. Identify pivots and check the matrix’s rank factors and nullspaces.
Polynomial Multiplication Calculator & Term Ledger
Multiply two polynomials in x with exact rational coefficients. Follow each distributed term pair and see which powers cancel.
