Ask Your Question

Revision history [back]

Symbolic matrix:

sage: matrix(SR, 3, 2, lambda i,j: var('x_{}_{}'.format(i,j)))
[x_0_0 x_0_1]
[x_1_0 x_1_1]
[x_2_0 x_2_1]

SageMath cannot compute a LU decomposition since the symbolic ring is not exact.

Matrix with entries in a fraction field:

sage: R = PolynomialRing(QQ, 3, 2, var_array='x').fraction_field()
sage: A = matrix(R, 3, 2, R.gens()); A
[x00 x01]
[x10 x11]
[x20 x21]

LU decomposition:

sage: P, L, U = A.LU()
sage: P
[1 0 0]
[0 1 0]
[0 0 1]
sage: L
[                                        1                                         0                                         0]
[                                  x10/x00                                         1                                         0]
[                                  x20/x00 (-x01*x20 + x00*x21)/(-x01*x10 + x00*x11)                                         1]
sage: U
[                     x00                      x01]
[                       0 (-x01*x10 + x00*x11)/x00]
[                       0                        0]
sage: A == P*L*U
True

In the fraction field every nonzero element is invertible, and the LU decomposition algorithm did use some inverses, so the resulting "general formula" in fact has limited applicability when the variables are replaced by numbers (limited to those situations where the inverses exist). For example the inverse of x00 is used, so the formulas are not valid when x00 = 0. But you can repeat the calculation for that case:

sage: B = copy(A)
sage: B[0,0] = 0
sage: B.LU()
...

etc.