Engineering/Mathematics/Matrices
Matrix Multiplication, Entry-by-Entry
The entry in row and column of a product is the sum , taken across the dimension the two matrices share. This formula is a consequence of the column-by-column definition rather than a separate rule, and it is the form in which every proof, every hand computation and every implementation of matrix multiplication is actually carried out.
- Core level
- Stream: computation
- Reading time 13 min
- Ref KVS-ENG-MATH-0040
- Taxonomy
- Engineering / Mathematics
- Prerequisite
- Matrix-vector product; matrix multiplication by columns
- Requires
- of size , of size
- Result size
- Terms per entry
- — the shared dimension
- Cost
- multiplications, flops
Overview
Matrix multiplication is defined column by column: column of is the matrix-vector product of with column of . That definition is the right one conceptually, because it exhibits the product as a collection of linear combinations of the columns of and connects immediately to column spaces and to systems of equations. It is not, however, the form in which anyone computes. Unpacking a single entry of that column gives a scalar formula, and it is that formula which does the day-to-day work.
The result is a sum of products, where is the dimension the two matrices share. Entry draws on row of and column of and on nothing else. Every other row of and column of is irrelevant to it. This locality is the single most important structural fact about the formula: the entries of the product are independent computations, which is why matrix multiplication parallelises so cleanly and why it is the operation hardware designers optimise first.
The formula also explains the conformability rule. The index runs over the columns of and simultaneously over the rows of , so those two counts must agree or the sum is not defined. The outer indices and range over the rows of and the columns of , which is why the product inherits its row count from the first factor and its column count from the second. Sizes multiply as , with the inner dimensions cancelling.
For proofs, the entry formula converts a statement about whole matrices into a statement about scalars, where the ordinary rules of complex arithmetic apply. Associativity, distributivity, the behaviour of the identity and the transpose rule are all established by writing out the relevant entry, manipulating a finite sum, and appealing to matrix equality. That technique — work entry-by-entry, never with ellipses and general arrays — is the standard method for the entire algebra of matrices.
Definition
Entries of Matrix Products
EMPLet be and be . For and , the entries of the product are
This is a theorem, not the definition. It is derived from the column-wise definition of the product, and many texts reverse the two — taking this as the definition and recovering the column form as a consequence.
Conformability for Multiplication
CFMThe product exists only when the number of columns of equals the number of rows of . That shared count is exactly the range of the summation index, so a mismatch means there is no sum to form. Conformability is directional: may be defined while is not, and even when both exist they need not have the same size.
Row-Column Pairing
RCPWriting for row of regarded as a matrix and for column of , the entry formula reads , a matrix identified with its single entry. Over the real numbers this is the familiar dot product; over it is a bilinear pairing with no conjugation, and it is not the Hermitian inner product.
Concepts
Deriving the formula from the column definition
Entry of is entry of column , which by definition is entry of the matrix-vector product . That product is the linear combination of the columns of . Reading off entry of that combination gives , and rewriting the column-and-entry notation in matrix-entry notation gives . Scalar multiplication in commutes, so the factors may be reordered into the conventional .
Each entry is independent of every other
Nothing in the formula for refers to any other entry of the product. The entries can therefore be computed in any order, concurrently, on separate cores or separate machines, with no synchronisation between them. This is why dense matrix multiplication achieves near-peak throughput on modern hardware while operations with sequential dependencies — triangular solves, for instance — do not. It is also why the operation is the standard benchmark for accelerators.
Three readings of the same sum
Fixing and and summing over gives the inner-product view: an entry is a row paired with a column. Fixing and letting vary gives the column view: a column of the product is a linear combination of the columns of . Fixing and accumulating over all and gives the outer-product view: , a sum of rank-one matrices. All three are the same arithmetic reorganised, and each corresponds to a different loop ordering in an implementation.
No conjugation appears — and that matters
The formula multiplies entries directly, with no complex conjugate anywhere. So for complex matrices the entry of a product is not an inner product in the Hermitian sense. The genuine inner product of two columns appears instead in the adjoint product: . Conflating the two is the source of a large fraction of sign and conjugation errors in complex numerical code, and it is why Gram matrices are written rather than over .
Why proofs use this form
Matrix equality is entry-by-entry equality, so any claimed identity between matrix expressions reduces to a claim about scalars. Associativity, for instance, becomes a double sum whose order of summation can be exchanged because the sums are finite. Distributivity becomes distributivity in . The transpose rule becomes an index swap plus commutativity of scalar multiplication. Each proof is short, complete and free of the ellipsis-laden general arrays that make matrix arguments unreadable.
Cost, and why it is not the whole story
The formula requires multiplications and additions per entry, so multiplications and about floating-point operations in total — for square matrices of order . Arithmetic is rarely the binding constraint, however. A naive triple loop touches memory in a pattern that defeats the cache, and a well-blocked implementation of the same operations can run an order of magnitude faster. Asymptotically faster algorithms exist, Strassen's being the best known at roughly , but they trade numerical stability and only repay the added complexity at large sizes.
Procedure: computing a product entry-by-entry
Equations
Entry of a matrix product
EQ-EMP-01The summation index runs over the shared dimension. The free indices and are the row and column of the entry being formed.
Expanded form
EQ-EMP-02The same sum written out. Counting the terms is the quickest check that the shared dimension has been identified correctly.
Size and conformability rule
EQ-EMP-03The inner dimensions must match and are consumed by the summation; the outer dimensions survive as the shape of the result.
Row-column pairing
EQ-EMP-04Row of against column of , producing a matrix identified with its single entry.
Where the true inner product lives
EQ-EMP-05Entries of carry no conjugate. The Hermitian inner product of two columns appears only when one factor is adjointed, which is why Gram matrices are written .
Outer-product accumulation
EQ-EMP-06Reordering the loops turns the product into a sum of rank-one updates. This is the ordering that underlies blocked and out-of-core implementations.
Operation count
EQ-EMP-07Cubic growth in the square case: doubling the order multiplies the work by eight, which is why blocking, precision choice and asymptotically faster algorithms all matter at scale.
Variable Definitions
| Symbol | Name | Meaning | Domain / type |
|---|---|---|---|
| First factor | The left matrix, supplying rows to each entry sum | m x n matrix | |
| Second factor | The right matrix, supplying columns to each entry sum | n x p matrix | |
| Rows of the result | Row count of , inherited by | positive integer | |
| Shared dimension | Columns of and rows of ; the number of terms in each sum | positive integer | |
| Columns of the result | Column count of , inherited by | positive integer | |
| Row index | Selects the row of and the row of the result | 1 to m | |
| Column index | Selects the column of and the column of the result | 1 to p | |
| Summation index | Runs across the shared dimension and is consumed by the sum | 1 to n | |
| Product entry | The scalar in row , column of the product | complex scalar |
Worked Numerical Example
Problem statement
A plant builds three products from four subassemblies. Matrix records how many of each subassembly goes into each product; matrix records the mass in kilograms and the cost in hundreds of currency units of each subassembly. Compute the product entry-by-entry to obtain the mass and cost of each finished product, and verify the result independently.
Set up the two factors and check conformability
is : rows are products, columns are subassemblies. is : rows are subassemblies, columns are the two resources. The shared dimension is , the four subassembly types, so each entry of the product will be a sum of four terms and the result will be .
Compute one entry in full
Take , the cost of product 1. Pair row of with column of , term by term across the four subassemblies. Each product is a quantity multiplied by a unit cost, so the sum is a total cost — the units confirm the pairing is the right way round.
Compute a second entry to fix the pattern
Entry is the cost of product 2. Row of is and column of is the unit cost column. Note that only these two vectors are consulted — nothing about products 1 and 3, and nothing about the mass column, enters this number.
Fill the remaining entries
The other four entries follow the same rule: , , and . Each used one row of and one column of and nothing else.
Cross-check against the column definition
Column of the product should equal applied to column of , that is . Evaluating the linear combination gives , agreeing with the first column above. Two routes, one answer.
Apply the row-sum check
Multiplying by the all-ones vector must commute with the product: . Row sums of are , and applying to that vector gives , and — exactly the row sums of the computed product. This single check tests every entry at a fraction of the cost of recomputing.
Interpret the result
The two columns of are the total mass and total cost of each product, rolled up from the subassembly level in one operation. Because the entry formula is linear in , updating a supplier price means changing one entry of and recomputing one column — the bill of materials in is untouched.
Product 1 weighs and costs ; product 2 weighs and costs ; product 3 weighs and costs . Each of the six numbers required a four-term sum, multiplications in all, matching the count. Products 1 and 3 have equal mass but very different cost, which the aggregated figures expose immediately and the raw bill of materials does not.
Applications & Industry Use
Bill-of-materials roll-up
Multiplying a product-by-component matrix with a component-by-resource matrix converts a bill of materials into totals for mass, cost, labour or carbon in a single operation. Chaining a further factor propagates the roll-up through sub-tiers of the supply chain, with associativity guaranteeing the answer does not depend on the grouping.
Counting walks of a given length
For an adjacency matrix , the entry counts walks of length from node to node , and the entry formula is precisely the statement that a walk of length decomposes into one of length plus a final edge. Reachability, path enumeration and centrality measures all reduce to repeated products.
GEMM as the benchmark kernel
General matrix-matrix multiply is the highest arithmetic-intensity dense kernel, performing operations on data. That ratio is why it reaches near-peak throughput, why every accelerator ships a tuned implementation, and why deep learning frameworks reshape their workloads into GEMM calls wherever possible.
Leontief inter-industry models
The technical coefficient matrix multiplied by an output vector gives intermediate demand, and powers of that matrix accumulate indirect requirements through successive tiers of supply. The entry formula gives the interpretation directly: each term is one industry's requirement passed through one intermediate sector.
Cascaded filters and transform matrices
A cascade of linear filters or transform stages is represented by the product of their matrices, and entry of the product is the total contribution of input to output summed over all intermediate paths. Collapsing the cascade into one matrix trades storage for a single pass at run time.
Stoichiometry combined with flow rates
A species-by-reaction stoichiometric matrix multiplied by a reaction-by-time-step rate matrix gives species production over each interval. The shared dimension is the reaction set, so each entry sums the contributions of every reaction to one species in one interval.
Design Considerations
Choose the loop order for the memory system, not the formula
All six orderings of the triple loop compute the same operations, but they differ by an order of magnitude in speed. The ordering that walks contiguous memory in the innermost loop wins; on row-major storage that is typically , , . Never assume the textbook , , ordering is a reasonable implementation.
Exploit the independence of entries
Because no entry depends on another, the result can be partitioned arbitrarily across threads, cores or nodes with no communication during the computation. Tiling the result into blocks that fit in cache, and reusing each loaded block for many operations, is the single largest performance lever available.
Reassociate long chains before computing
For a chain the parenthesisation changes only the cost, never the answer. Multiplying right to left costs multiplications against for the other grouping. Where a chain ends in a vector, always work from the vector outwards.
Do not form a product you only need to apply
If is wanted for a handful of vectors, forming explicitly is wasted work and wasted memory. Apply the factors in sequence instead. This principle governs the use of factored representations, implicit operators and matrix-free methods throughout scientific computing.
Respect sparsity
The dense entry formula sums over all terms whether or not they are zero. For sparse operands the product must be computed by iterating over stored non-zeros, and the result may be far denser than either factor. Estimate the fill before allocating, and consider whether the product is needed at all.
Decide on the accumulator precision
Summing terms accumulates rounding error growing like . Where the operands are low precision — binary32, bfloat16 or integer — the accumulator should be wider than the operands. Mixed-precision hardware makes this choice explicit, and getting it wrong is a common source of silently degraded results in machine learning pipelines.
Standards & Codes
Notation, interchange and numerical standards that govern how this material is written down, stored and computed in production systems.
| Reference | Title | Relevance to this topic |
|---|---|---|
BLAS Level 3 | Basic Linear Algebra Subprograms, matrix-matrix operations | The xGEMM interface computes with transpose flags on either operand, and is the reference against which all dense multiplication implementations are measured. |
ISO 80000-2 | Quantities and units — Part 2: Mathematics | Fixes the index convention with row before column, without which the entry formula is ambiguous and the transpose rule cannot be stated. |
IEEE 754-2019 | IEEE Standard for Floating-Point Arithmetic | Governs the accumulation of the -term sum, including the fused multiply-add operation that halves the rounding error per term and is the basis of modern GEMM kernels. |
ISO/IEC 1539 | Programming languages — Fortran | Standardises MATMUL and DOT_PRODUCT as intrinsics, so the entry formula is part of the language rather than a library, and compilers are free to substitute a tuned kernel. |
ISO/IEC 40314 | Mathematical Markup Language (MathML) Version 3.0 | Encodes the summation and subscripted index notation on this page semantically, keeping the distinction between free and bound indices available to search and assistive technology. |
Material Selection
For a mathematical topic, "material" is the numeric representation: the scalar field, storage format and precision the computation is built from.
| Representation | Select when | Trade-off |
|---|---|---|
| Exact integer arithmetic | Combinatorial counts, bills of materials, adjacency powers and stoichiometry, where entries are whole numbers. | Exact and auditable, but repeated products grow entries rapidly — adjacency powers overflow 64-bit counters within a few multiplications on a moderately connected graph. |
| IEEE 754 binary64 | General scientific and engineering computation. | The default; roughly relative error per entry, negligible for most problems, and universally supported by tuned kernels. |
| IEEE 754 binary32 | Graphics, signal processing and inference workloads where throughput and memory bandwidth dominate. | Doubles effective bandwidth and often more than doubles throughput, at seven significant digits; adequate when the product feeds a tolerant downstream stage. |
| Reduced precision with wide accumulator | Machine learning training and inference on tensor-core class hardware. | bfloat16 or FP8 operands with binary32 accumulation give large speed-ups while keeping the summation stable; results are not bitwise reproducible across hardware generations. |
| Sparse storage with sparse-sparse product | Graph adjacency, finite element connectivity and other structurally sparse operands. | Cost scales with non-zeros rather than , but the product typically fills in, and symbolic analysis is needed before allocation. |
| Fixed-point with saturating accumulation | Embedded DSP and quantised inference on integer-only hardware. | Deterministic and cheap in silicon, but the accumulator width must cover products and a saturation policy must be defined, or overflow wraps silently. |
Manufacturing Notes
Implementation notes — how the result is actually produced by hand, by algorithm and by library, including cost and numerical behaviour.
Operation count and arithmetic intensity
The product performs flops while touching matrix entries. For square matrices this is operations on data, an intensity that grows with and is what allows blocked implementations to approach the arithmetic peak of a processor. Few other linear algebra kernels have this property, which is why so much of numerical computing is arranged to end in a GEMM call.
Falk's scheme for hand computation
Write above and to the right, below and to the left, and fill the rectangle where a row of meets a column of . The layout places each operand pair in line of sight, removing the index bookkeeping that causes most manual errors. Complete one row of the result at a time so that a slip is confined to a single row.
Verification without recomputation
Two cheap checks catch most errors. Row sums must satisfy and column sums must satisfy ; both require only matrix-vector work. For a probabilistic check on large products, compare with for a random — Freivalds' algorithm — which detects an error with probability at least one half per trial at cost.
Library behaviour
NumPy's @ operator and numpy.matmul dispatch to the installed BLAS xGEMM; numpy.dot does the same for two-dimensional inputs but has different broadcasting semantics for higher ranks. MATLAB's * and Eigen's operator* likewise call tuned kernels. Because different BLAS implementations block and reorder the summation differently, results can differ in the last bits between machines while both remain correct.
Beyond the cubic count
Strassen's algorithm computes a block product with seven multiplications instead of eight, giving overall. It becomes competitive above roughly order and is used inside some production libraries with a cut-off to conventional blocking at small sizes. The trade is weaker element-wise error bounds, so it is inappropriate where entry-level accuracy guarantees are required.
Failure Modes & Common Mistakes
| Failure mode / mistake | Impact | Root cause | Prevention & detection |
|---|---|---|---|
| Multiplying non-conformable matrices | high | Assuming any two matrices can be multiplied, or transposing one operand mentally without doing so in the data. | Write the sizes as before computing and confirm the inner pair matches; treat a shape error at run time as a modelling error, not a coding slip. |
| Pairing rows with rows or columns with columns | high | Reversing the role of the two factors, so entry is formed from row of and row of . | State the rule aloud as row-of-first with column-of-second, and sanity-check with a case where , and are all different so a mistake produces a shape error. |
| Wrong number of terms in the sum | medium | Summing over the row count of or the column count of instead of the shared dimension. | Count the terms in the first entry computed and compare with ; every entry of the product must have the same number of terms. |
| Assuming entries of a complex product are inner products | high | Reading as and inserting or expecting a conjugate. | Remember that the entry formula has no conjugation; use when Hermitian inner products of columns are what is wanted. |
| Assuming commutativity | high | Rewriting as inside a derivation, which is false in general and may not even be conformable. | Preserve operand order in every algebraic step; when a swap seems necessary, use the transpose rule instead. |
| Integer overflow in repeated products | medium | Computing high powers of an adjacency or count matrix in fixed-width integers, where walk counts grow exponentially. | Use arbitrary-precision integers, work modulo a prime when only existence matters, or switch to floating point and accept approximate counts. |
| Catastrophic loss of precision in long sums | medium | Accumulating terms of alternating sign in the same precision as the operands, so leading digits cancel. | Use a wider accumulator or a fused multiply-add, and prefer blocked summation, which reduces error growth from to roughly . |
| Materialising a product that is only ever applied | low | Forming explicitly when only is needed for a few vectors. | Apply the factors right to left and keep the operator in factored form; reserve explicit formation for cases where the product itself is inspected or reused many times. |
FAQs
Is the entry formula the definition of matrix multiplication?
Not here. The product is defined column by column, as the matrix-vector product of with each column of , and the entry formula is derived from it. Many texts reverse the order and take the entry formula as the definition. The two are equivalent, but the column-wise definition connects more directly to linear combinations, column spaces and systems of equations.
Why must the number of columns of equal the number of rows of ?
Because the summation index runs simultaneously over both. Each term pairs an entry from row of with an entry from column of , and there is no sensible pairing unless the two sequences have the same length. The shared count is consumed by the sum, which is why it does not appear in the size of the result.
Does the entry formula involve a complex conjugate?
No. Entries are multiplied directly, so for complex matrices is a bilinear pairing rather than a Hermitian inner product. The genuine inner product of columns appears in , whose entry is . Confusing the two is a frequent source of conjugation errors in complex code.
How many operations does a matrix product take?
Exactly multiplications and additions, so about floating-point operations, or for square matrices of order . In practice the time is governed by memory traffic rather than arithmetic, which is why blocked implementations of the same operation count can be many times faster than a naive triple loop.
Can I compute just one entry of a product without computing the rest?
Yes, and it costs only . Entry depends solely on row of and column of . This is worth exploiting whenever only a diagonal, a single row, or a scattered set of entries of a large product is needed.
What is the quickest way to check a product by hand?
Compare row sums. The identity means the row sums of the product must equal applied to the vector of row sums of , and the analogous statement holds for columns. Both checks cost a matrix-vector product each and together test every entry.
Why do different machines give slightly different results for the same product?
Because tuned implementations block and reorder the summation to suit the cache and vector width, and floating-point addition is not associative. Every ordering is equally valid and each is accurate to within its error bound, but the last bits can differ. Bitwise reproducibility requires a library that explicitly guarantees a fixed summation order.
References
- Beezer, R. A. A First Course in Linear Algebra, Version 0.70. University of Puget Sound, 2006. Section MM, subsection MMEE. Licensed under the GNU Free Documentation License v1.2.
- ISO 80000-2:2019, Quantities and units — Part 2: Mathematics. International Organization for Standardization.
- Dongarra, J. J., Du Croz, J., Duff, I. S. and Hammarling, S. A Set of Level 3 Basic Linear Algebra Subprograms. ACM Transactions on Mathematical Software, 1990.
- Golub, G. H. and Van Loan, C. F. Matrix Computations, 4th edition. Johns Hopkins University Press, 2013.
- Strassen, V. Gaussian Elimination is not Optimal. Numerische Mathematik, 1969.
- IEEE 754-2019, IEEE Standard for Floating-Point Arithmetic. Institute of Electrical and Electronics Engineers.
AI Suggested Questions
- Trace all six loop orderings of a triple-loop matrix multiply and explain which memory layout each one favours.
- Show how the entry formula proves that in three lines of index manipulation.
- For an adjacency matrix, prove by induction that counts walks of length from node to node .
- Compare the cost of against for matrices of sizes 500x5, 5x500 and 500x1, and explain the difference.
- How does Freivalds' algorithm verify a matrix product in quadratic time, and what is its failure probability after three trials?
- At what matrix order does Strassen's algorithm overtake blocked conventional multiplication in practice, and what accuracy is given up?
Related Calculators
Multiply two conformable matrices with a per-entry expansion of every summation term, for real or complex data.
Single Product Entry CalculatorCompute one chosen entry from a single row and column, showing the term-by-term accumulation and operation count.
Matrix Product VerifierCheck a claimed product using row-sum, column-sum and randomised Freivalds tests without recomputing the full result.
