Skip to content

9.2 Gaussian elimination and LU with partial pivoting

The previous section, Vectors, matrices, row-major layout, and matmul, let you multiply matrices. It did not let you undo a multiplication: given AA and bb, find the xx with Ax=bAx = b. That is the core step of every second-order method you will meet. Logistic regression by IRLS, in Logistic regression (IRLS), ROC-AUC, calibration (the classifier behind the usage-policy head), solves one linear system per iteration. Newton’s method solves one per step. And the section after this one, Eigenvalues, power iteration, and the spectral radius, finds the eigenvalue of a matrix nearest a chosen number by solving with the same matrix again and again.

Concepts from other sections (11)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Calling np.linalg.solve would work. This section builds the method from first principles instead, so you can see why it is fast (factor once, solve many times) and why the naive version silently returns garbage on perfectly ordinary matrices. By the end you will have lu(A), which returns P,L,UP, L, U with PA=LUPA = LU, and lu_solve(P, L, U, b), which solves Ax=bAx = b by two triangular solves. The solution is lu.py and its contract is lu.pyi. Nothing needs to be built first, but the matrix product and row-major layout from the previous section are assumed.

Learning objectives

  • Carry out Gaussian elimination with partial pivoting by hand and read off PP, LL, and UU with PA=LUPA = LU.
  • Explain why the multipliers are exactly the entries of LL, and why pivoting bounds them by 1.
  • Show, with a concrete 10−2010^{-20} pivot, how elimination without pivoting loses every digit of the answer.
  • Solve Ax=bAx = b by forward and back substitution, and say why a zero pivot must raise.
  • Compare the cost of factoring (23n3\frac{2}{3}n^3) with the cost of each solve (2n22n^2).

The section uses a small set of symbols throughout:

SymbolMeaningType / shape
AAa square matrix, aija_{ij} its entriesfloat64[n, n]
x,bx, bthe unknown and the right-hand side of Ax=bAx = bfloat64[n] or [n, k]
LLunit lower triangular: ones on the diagonal, zeros abovefloat64[n, n]
UUupper triangular: zeros below the diagonalfloat64[n, n]
PPa permutation matrix: the identity with its rows reorderedfloat64[n, n]
ℓik\ell_{ik}the multiplier that eliminates entry (i,k)(i, k), stored in LLscalar
ukku_{kk}the kk-th pivotscalar
ε\varepsilonfloat64 unit roundoff, 2−53≈1.1×10−162^{-53} \approx 1.1 \times 10^{-16}

Elimination is a sequence of row operations

Section titled “Elimination is a sequence of row operations”

Subtracting ℓ\ell times row kk from row ii does not change the solution set of Ax=bAx = b, provided you do the same to bb: it combines two true equations into a third. Gaussian elimination uses that operation to clear the entries below the diagonal, one column at a time. For column kk, the pivot is ukku_{kk}, and for each row i>ki > k,

ℓik=aikukk,rowi←rowi−ℓik rowk.\ell_{ik} = \frac{a_{ik}}{u_{kk}}, \qquad \text{row}_i \leftarrow \text{row}_i - \ell_{ik}\, \text{row}_k .

After n−1n - 1 columns the matrix is upper triangular. That is UU.

Elimination looks like a procedure, but it is also a factorization. The row operation “row ii minus ℓ\ell times row kk” is multiplication on the left by E=I−ℓ eiek⊤E = I - \ell\, e_i e_k^\top, the identity with −ℓ-\ell at position (i,k)(i, k). Its inverse is I+ℓ eiek⊤I + \ell\, e_i e_k^\top: add the row back. Elimination computes Elast⋯E1A=UE_{\text{last}} \cdots E_1 A = U, so

A=E1−1⋯Elast−1 U=LU,A = E_1^{-1} \cdots E_{\text{last}}^{-1}\, U = L U,

and the product of those inverses, taken in this order, is simply the identity with every multiplier ℓik\ell_{ik} placed at position (i,k)(i, k). No arithmetic is needed to form LL: you write each multiplier where it was used. LL is the record of elimination, which is what lets you replay it on any bb later.

So far the procedure divides by ukku_{kk}, and nothing guarantees that is non-zero. The matrix (0111)\begin{pmatrix} 0 & 1 \\ 1 & 1 \end{pmatrix} is invertible, yet its first pivot is 0. Swapping the two rows fixes it, since equations may be listed in any order.

A pivot that is merely small is worse, because nothing fails visibly. Take ϵ=10−20\epsilon = 10^{-20}:

(ϵ111)x=(12),x≈(1,1).\begin{pmatrix} \epsilon & 1 \\ 1 & 1 \end{pmatrix} x = \begin{pmatrix} 1 \\ 2 \end{pmatrix}, \qquad x \approx (1, 1).

Pivoting on ϵ\epsilon gives ℓ21=1020\ell_{21} = 10^{20}, and u22=1−1020u_{22} = 1 - 10^{20} rounds to −1020-10^{20}. The 1 in a22a_{22} is lost, because the spacing of float64 numbers near 102010^{20} is about 16 00016\,000. Back substitution then gets x2=1x_2 = 1 and x1=(1−x2)/ϵ=0x_1 = (1 - x_2)/\epsilon = 0. Every digit of x1x_1 is wrong.

Partial pivoting fixes both problems. For column kk it chooses the row p≥kp \ge k with the largest ∣upk∣\lvert u_{pk} \rvert and swaps it into row kk before eliminating. Then every multiplier satisfies ∣ℓik∣≤1\lvert \ell_{ik} \rvert \le 1, row operations never amplify entries by more than a factor 2 per step, and in practice elimination with partial pivoting is backward stable: the computed xx solves a system (A+δA)x=b(A + \delta A)x = b with ∥δA∥\lVert \delta A \rVert a small multiple of ε∥A∥\varepsilon \lVert A \rVert. On the ϵ\epsilon system, the 1, 1 row becomes the pivot, ℓ21=10−20\ell_{21} = 10^{-20}, u22=1−10−20≈1u_{22} = 1 - 10^{-20} \approx 1 keeps its information, and back substitution returns x≈(1,1)x \approx (1, 1).

Three details make pivoting right, and each is a place where implementations go wrong:

  • Search rows kk and below only. Rows above kk are finished pivot rows.
  • Compare absolute values. −3-3 is a better pivot than 11.
  • Swap the whole rows of the working matrix, and also the multipliers already stored in LL for those two rows (columns 00 to k−1k - 1). A multiplier belongs to its equation, and the equation moved. LL’s diagonal stays where it is.

Ties go to the lowest row index (np.argmax returns the first maximum), which makes PP deterministic. If the best ∣upk∣\lvert u_{pk} \rvert is 0, the whole column below the diagonal is already zero: there is nothing to eliminate, so skip it. The matrix is then singular, and UU carries a 0 on its diagonal.

With pivoting in place, you need a way to record the swaps and use them when solving. Keep an array perm where row ii of the permuted matrix is row perm[i] of AA. The permutation matrix has Pi,perm[i]=1P_{i,\mathrm{perm}[i]} = 1, so (PA)i,:=Aperm[i],:(PA)_{i,:} = A_{\mathrm{perm}[i],:}. PP is orthogonal, P−1=P⊤P^{-1} = P^\top, and in general P≠P⊤P \ne P^\top; a 3-cycle is the smallest example.

With PA=LUPA = LU, the system Ax=bAx = b becomes LUx=PbLUx = Pb, solved in two triangular steps:

forward:yi=(Pb)i−∑j<iℓij yj(i=0,1,… ),back:xi=yi−∑j>iuij xjuii(i=n−1,…,0).\text{forward:}\quad y_i = (Pb)_i - \sum_{j < i} \ell_{ij}\, y_j \quad (i = 0, 1, \dots), \qquad \text{back:}\quad x_i = \frac{y_i - \sum_{j > i} u_{ij}\, x_j}{u_{ii}} \quad (i = n-1, \dots, 0).

Forward substitution divides by nothing, because LL has a unit diagonal. Back substitution divides by each pivot, so a zero pivot means the system has no unique solution and lu_solve raises. Several right-hand sides at once (bb of shape [n,k][n, k]) use the same formulas column by column.

Eliminating column kk updates an (n−k−1)×(n−k)(n-k-1) \times (n-k) block with one multiply and one subtract per entry, so the factorization costs ∑k2(n−k)2≈23n3\sum_k 2(n-k)^2 \approx \frac{2}{3} n^3 floating-point operations. Each triangular solve costs about n2n^2. For n=1000n = 1000, that is about 6.7×1086.7 \times 10^8 operations to factor and 2×1062 \times 10^6 per solve. That 300-fold gap is why inverse iteration in Eigenvalues, power iteration, and the spectral radius factors once and solves at every step.

Now put it all together on a 3 by 3 system:

A=(2114−60−272),b=(5−29).A = \begin{pmatrix} 2 & 1 & 1 \\ 4 & -6 & 0 \\ -2 & 7 & 2 \end{pmatrix}, \qquad b = \begin{pmatrix} 5 \\ -2 \\ 9 \end{pmatrix}.

Column 0. The candidates are 2,4,−22, 4, -2; the largest absolute value is 44 in row 1, so swap rows 0 and 1 (perm = [1, 0, 2]). The pivot is 4, and the multipliers are ℓ10=2/4=0.5\ell_{10} = 2/4 = 0.5 and ℓ20=−2/4=−0.5\ell_{20} = -2/4 = -0.5:

row1:(2,1,1)−0.5 (4,−6,0)=(0,4,1),row2:(−2,7,2)+0.5 (4,−6,0)=(0,4,2).\text{row}_1: (2, 1, 1) - 0.5\,(4, -6, 0) = (0, 4, 1), \qquad \text{row}_2: (-2, 7, 2) + 0.5\,(4, -6, 0) = (0, 4, 2).

Column 1. The candidates in rows 1 and 2 are 44 and 44: a tie, so row 1 stays. ℓ21=4/4=1\ell_{21} = 4/4 = 1 and row2:(0,4,2)−(0,4,1)=(0,0,1)\text{row}_2: (0, 4, 2) - (0, 4, 1) = (0, 0, 1). The factors are

P=(010100001),L=(1000.510−0.511),U=(4−60041001).P = \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{pmatrix}, \quad L = \begin{pmatrix} 1 & 0 & 0 \\ 0.5 & 1 & 0 \\ -0.5 & 1 & 1 \end{pmatrix}, \quad U = \begin{pmatrix} 4 & -6 & 0 \\ 0 & 4 & 1 \\ 0 & 0 & 1 \end{pmatrix}.

Check one entry of LULU: row 2 of LL times column 0 of UU is −0.5⋅4=−2=(PA)20-0.5 \cdot 4 = -2 = (PA)_{20}.

Solve. Pb=(−2,5,9)Pb = (-2, 5, 9). Forward: y0=−2y_0 = -2, y1=5−0.5⋅(−2)=6y_1 = 5 - 0.5 \cdot (-2) = 6, y2=9−(−0.5)(−2)−1⋅6=2y_2 = 9 - (-0.5)(-2) - 1 \cdot 6 = 2. Back: x2=2/1=2x_2 = 2 / 1 = 2, x1=(6−1⋅2)/4=1x_1 = (6 - 1 \cdot 2) / 4 = 1, x0=(−2−(−6)⋅1−0⋅2)/4=1x_0 = (-2 - (-6) \cdot 1 - 0 \cdot 2) / 4 = 1. So x=(1,1,2)x = (1, 1, 2), and indeed 2+1+2=52 + 1 + 2 = 5, 4−6+0=−24 - 6 + 0 = -2, −2+7+4=9-2 + 7 + 4 = 9. Every number here is a small dyadic fraction, so the solution reproduces them exactly.

The interface splits factoring from solving, because that split is what makes the factor-once pattern possible:

def lu(A: ArrayLike) -> tuple[NDArray, NDArray, NDArray]:
"""P, L, U (float64 [n, n]) with P @ A == L @ U; |L| <= 1; ties to the lowest
row; zero columns skipped (singular A still factors). A is not modified."""
def lu_solve(P: ArrayLike, L: ArrayLike, U: ArrayLike, b: ArrayLike) -> NDArray:
"""x with A @ x == b for b of shape [n] or [n, k]. ValueError on a zero pivot."""

lu never raises on a singular matrix: a singular matrix still has a valid factorization with a zero on UU‘s diagonal, and that zero is useful information, since it makes the rank deficiency visible. Only the solve needs to divide by the pivot, so that is where the error belongs. Neither function modifies its inputs, because callers reuse AA.

The reasoning chain runs straight from the principles. Elimination only ever subtracts multiples of a pivot row from rows below it, so the work is a loop over columns k=0,…,n−2k = 0, \dots, n-2, each updating the trailing block U[k+1:, k:]. The multipliers are exactly the entries of LL, so the solution writes each one into L[i, k] at the moment it is computed; no separate pass forms LL. Dividing by the pivot is safe and stable only if the pivot is the largest ∣⋅∣\lvert\cdot\rvert among the rows still being worked on. That forces three things: search U[k:, k] (not the whole column), compare absolute values, and swap the stored multipliers in L[:, :k] with the rows. A column whose best pivot is 0 has nothing to eliminate; skipping it, rather than raising, lets a singular matrix factor and leaves the 0 on UU‘s diagonal where lu_solve can see it. Swaps are cheap to record as an index array perm and convert to PP once at the end. Solving replays elimination on PbPb and then undoes UU from the bottom row up, checking for a zero pivot before any division.

The factorization works on a float64 copy so the caller’s A survives:

def lu(A: ArrayLike) -> tuple[NDArray, NDArray, NDArray]:
U = np.array(A, dtype=np.float64) # a copy: the caller's A is never touched
if U.ndim != 2 or U.shape[0] != U.shape[1]:
raise ValueError(f"lu: A must be a square matrix, got shape {U.shape}")
n = U.shape[0]
L = np.eye(n)
perm = np.arange(n) # row i of P A is row perm[i] of A

Each column picks its pivot from rows kk and below, by absolute value, ties to the first. np.argmax on the slice returns an offset into that slice, so adding k converts it back to a row index of U:

for k in range(n - 1):
p = k + int(np.argmax(np.abs(U[k:, k]))) # offset by k: rows above are finished
if U[p, k] == 0.0:
continue # the column is already zero below k: nothing to eliminate

The swap moves the working rows, the multipliers already stored for those rows, and the permutation record. Only columns k: of U need to move, since columns left of k are already zero in both rows; only columns :k of L hold multipliers, and the unit diagonal stays in place:

if p != k:
U[[k, p], k:] = U[[p, k], k:]
L[[k, p], :k] = L[[p, k], :k] # a multiplier belongs to its equation
perm[[k, p]] = perm[[p, k]]

Elimination of the whole column is one vectorised rank-1 update. On the worked example at k=0k = 0 this computes m = [0.5, -0.5] and turns rows 1 and 2 into (0,4,1)(0, 4, 1) and (0,4,2)(0, 4, 2):

m = U[k + 1 :, k] / U[k, k] # |m| <= 1 thanks to the pivot choice
L[k + 1 :, k] = m # store with a + sign: row_i -= m * row_k
U[k + 1 :, k:] -= np.outer(m, U[k, k:])
U[k + 1 :, k] = 0.0 # exactly zero by construction, not just rounded
P = np.zeros((n, n))
P[np.arange(n), perm] = 1.0 # P[i, perm[i]] = 1, so (P A)[i] = A[perm[i]]
return P, L, U

The solve validates shapes, refuses a zero pivot before dividing, and runs the two substitutions. Because y is indexed by its first axis only, the same loop handles b of shape [n] and [n, k]:

d = np.diag(U)
if np.any(d == 0.0):
raise ValueError("lu_solve: U has a zero pivot, A is singular")
# Forward substitution, L y = P b: unit diagonal, no division.
y = P @ b # P, not P.T: replays the row swaps on b
for i in range(n):
y[i] = y[i] - L[i, :i] @ y[:i]
# Back substitution, U x = y, from the last row up.
x = y # in place: y[i] is no longer needed once x[i] is set
for i in range(n - 1, -1, -1):
x[i] = (x[i] - U[i, i + 1 :] @ x[i + 1 :]) / d[i]
return x

P @ b allocates a new array, so the caller’s b is not modified either. The full solution is lu.py.

The cost is 23n3\frac{2}{3}n^3 flops to factor, about 2n22n^2 per right-hand side, and O(n2)O(n^2) memory for the factors. Separating lu from lu_solve is what lets inverse iteration and IRLS pay the cubic cost once.

You might be tempted to invert AA instead. Forming A−1A^{-1} costs about 2n32n^3 and is less accurate than solving with the factors, and it throws away the zero-pivot signal. Complete pivoting, which searches the whole trailing block for the largest entry, gives a tighter growth bound but costs O(n3)O(n^3) comparisons and a column permutation as well; partial pivoting is backward stable in practice and is what LAPACK uses. Returning PP as a dense matrix matches the contract and makes PA=LUPA = LU directly checkable; production code returns the perm vector instead, and the solution keeps it internally for the same reason. The one tradeoff knowingly accepted is speed: the loops are Python-level over kk and ii, with NumPy doing each row or block. That is slow for large nn but makes every step of the algorithm visible.

The test cases a correct solution must pass map directly onto the ideas above. The hand example must reproduce PP, LL, UU and x=(1,1,2)x = (1, 1, 2) exactly, which pins the pivot and tie rules and the order of forward then back substitution. On random matrices of sizes 1 to 12, the structural properties every caller relies on must hold: PA=LUPA = LU, PP a permutation, LL unit lower with ∣L∣≤1\lvert L \rvert \le 1, and UU upper. The answers must agree with np.linalg.solve for one and three right-hand sides with nn up to 40, which is what IRLS needs.

The boundary cases are where naive code breaks. The zero leading pivot (0111)\begin{pmatrix} 0 & 1 \\ 1 & 1 \end{pmatrix} must factor and solve, which shows pivoting is required, not optional. The ϵ=10−20\epsilon = 10^{-20} system must give (1,1)(1, 1). A negative candidate −3-3 must beat 11 as the pivot. A large entry in a finished row must not be chosen, because the search covers rows kk and below only. A rank-2 matrix and the zero matrix must factor while solving with them raises. AA must be unchanged after the call, and non-square inputs or a wrong-length bb must raise. Finally, a 3-cycle permutation checks that the solve uses PbPb and not P⊤bP^\top b: P⊤P^\top is the inverse permutation and gives the same result only when PP is its own inverse (a product of disjoint swaps).

The common mistakes, their symptoms, and the line that prevents each are collected here:

PitfallSymptomHow the solution avoids it
no pivoting at alldivision by zero on invertible matrices, or every digit lost to a tiny pivotalways swaps the largest ∣⋅∣\lvert\cdot\rvert into the pivot row
choosing the largest value instead of the largest absolute valuemultipliers above 1 and error growthsearches np.abs(U[k:, k])
swapping rows of UU but not the multipliers already in LLPA≠LUPA \ne LU as soon as a later column swapsswaps L[[k, p], :k] with the rows
applying P⊤P^\top to bb instead of PPright answers whenever PP is its own inverse, wrong otherwisestarts the forward pass from y = P @ b
forgetting to divide by the pivot in back substitutionxx scaled wrongly except where pivots are 1divides each back-substitution row by d[i]
storing the multiplier with the wrong signLU≠PAL U \ne PAstores m itself in L and subtracts outer(m, row_k)
dividing by a zero pivot when solvinginf and nan instead of an errorchecks np.any(d == 0.0) before any division and raises
searching the whole column for the pivota finished row is swapped back downsearches only U[k:, k] and offsets by k
eliminating in the caller’s arraythe next solve with AA uses UUworks on np.array(A, dtype=np.float64), a copy

Warning: Forgetting to swap the stored multipliers passes every test in which only one swap happens. The bug shows up only when a later column swaps again, so test with matrices that need several swaps.

The next consumer is Eigenvalues, power iteration, and the spectral radius, whose inverse_iteration factors A−σIA - \sigma I once and calls lu_solve at every step. Later, Logistic regression (IRLS), ROC-AUC, calibration solves (X⊤WX) δ=X⊤(y−p)(X^\top W X)\,\delta = X^\top (y - p) at every iteration of logistic regression, and Newton steps in optimization solve with the Hessian.

  • LAPACK dgetrf (SRC/dgetrf.f, dgetrf2.f): a blocked, right-looking factorization that spends almost all its time in matrix-matrix products, so it runs near the speed of GEMM.
  • LAPACK dgetrs and scipy.linalg.lu_solve (scipy/linalg/_decomp_lu.py): triangular solves with many right-hand sides at once.
  • Rook and complete pivoting (Higham, ch. 9): stronger growth bounds at extra search cost.
  • Sparse LU (SuperLU, UMFPACK; scipy.sparse.linalg.splu): reorders rows and columns to keep LL and UU sparse.
  • Cholesky for symmetric positive definite systems (LAPACK dpotrf): half the work and no pivoting needed; the natural choice for IRLS’s X⊤WXX^\top W X.
  • Trefethen and Bau, Numerical Linear Algebra, lectures 20 to 22 (elimination, pivoting, stability); Strang, Introduction to Linear Algebra, ch. 2; Higham, Accuracy and Stability of Numerical Algorithms, ch. 9.
  • Pivot: the diagonal entry ukku_{kk} that elimination divides by in column kk.
  • Multiplier: ℓik=aik/ukk\ell_{ik} = a_{ik}/u_{kk}, the amount of the pivot row subtracted from row ii; stored in LL.
  • Unit lower triangular: lower triangular with ones on the diagonal.
  • Partial pivoting: swapping the row with the largest absolute entry (among rows kk and below) into the pivot position before eliminating.
  • Permutation matrix: the identity with its rows reordered; orthogonal, with P−1=P⊤P^{-1} = P^\top.
  • Backward stable: the computed answer is the exact answer to a slightly perturbed problem, (A+δA)x=b(A + \delta A)x = b with ∥δA∥\lVert \delta A \rVert a small multiple of ε∥A∥\varepsilon \lVert A \rVert.
  • Gaussian elimination turns Ax=bAx = b into an upper triangular system by subtracting multiples of a pivot row from the rows below it; the multipliers form a unit lower triangular LL and the result is UU, with A=LUA = LU when no rows move.
  • Partial pivoting swaps the row with the largest ∣entry∣\lvert\text{entry}\rvert into the pivot position, so every ∣ℓik∣≤1\lvert \ell_{ik} \rvert \le 1; the swaps form PP and PA=LUPA = LU.
  • Without pivoting, a zero pivot stops elimination on an invertible matrix and a tiny one destroys the answer.
  • Factor once in 23n3\frac{2}{3}n^3 operations; each new right-hand side then costs only 2n22n^2: forward substitution Ly=PbLy = Pb, back substitution Ux=yUx = y.
  • A singular matrix still factors, with a zero on UU‘s diagonal; solving with it must fail loudly.
  1. Why does the pivot search use U[k:, k] and add k to the result?

    Answer

    Rows above kk are finished pivot rows; swapping one of them back down would undo earlier elimination. np.argmax on the slice returns an offset into that slice, so k converts it back to a row index of U.

  2. Why must L[[k, p], :k] be swapped along with the rows of U?

    Answer

    Each stored multiplier says how much of an earlier pivot row was subtracted from a particular equation. When that equation moves to a different row, its history must move with it, or LULU reconstructs the wrong rows and PA≠LUPA \ne LU after the first later swap.

  3. On the ϵ=10−20\epsilon = 10^{-20} system, what does pivoting change, number by number?

    Answer

    Row 1 (the 1, 1 row) becomes the pivot, so ℓ21=10−20\ell_{21} = 10^{-20} instead of 102010^{20}. Then u22=1−10−20≈1u_{22} = 1 - 10^{-20} \approx 1 keeps its information, and back substitution gives x≈(1,1)x \approx (1, 1) instead of (0,1)(0, 1).

  4. Why does lu skip a zero column instead of raising, while lu_solve raises?

    Answer

    A singular matrix still has a valid factorization PA=LUPA = LU with a zero on UU‘s diagonal; that is useful information (the rank deficiency is visible). Only the solve needs to divide by the pivot, so that is where the error belongs.

  5. Why y = P @ b and not P.T @ b?

    Answer

    PA=LUPA = LU means the factored equations are in the order perm; Ax=bAx = b becomes LUx=PbLUx = Pb. P⊤P^\top is the inverse permutation and gives the same result only when PP is its own inverse (a product of disjoint swaps), so a 3-cycle exposes the difference.

  6. For n=1000n = 1000, roughly how many operations does factoring cost compared with one solve, and what design does that justify?

    Answer

    About 6.7×1086.7 \times 10^8 to factor and 2×1062 \times 10^6 per solve, a 300-fold gap. It justifies separating lu from lu_solve so a caller that solves many times with the same matrix factors only once.