# LU decomposition for symbolic matrices with many different variables.

I have another question about LU decomposition. I would like to define a matrix $x_i(t)$ as a matrix with $1$'s on the diagonal and with $t$ at $(i,i+1)$ position, and all other entries are $0$. Let $g = x_{i_1}(t_1) \cdots x_{i_m}(t_m) T$, where $i_1, \ldots, i_m$ are some given integers, $T$ is a diagonal matrix with entries $\lambda_1, \ldots, \lambda_n$. I would like to define this matrix in Sage and apply LU decomposition to it. Now $t_1, \ldots, t_m, \lambda_1, \ldots, \lambda_n$ are symbols. How to do this in Sage? Thank you very much.

edit retag close merge delete

Sort by » oldest newest most voted

First, let me suggest that all your indices start at 0, not 1. This might look weird at the beginning, but you will see that it is very convenient in the longer term.

You can define your inteterminates as follows:

sage: m,n = 6,10
sage: R = PolynomialRing(QQ,names=['t{}'.format(i) for i in range(m)] + ['lambda{}'.format(i) for i in range(n)]).fraction_field()
sage: R
Fraction Field of Multivariate Polynomial Ring in t0, t1, t2, t3, t4, t5, lambda0, lambda1, lambda2, lambda3, lambda4, lambda5, lambda6, lambda7, lambda8, lambda9 over Rational Field
sage: R.gens()
(t0,
t1,
t2,
t3,
t4,
t5,
lambda0,
lambda1,
lambda2,
lambda3,
lambda4,
lambda5,
lambda6,
lambda7,
lambda8,
lambda9)

To access the inteterminates easily, you can do:

sage: t = R.gens()[:m]
sage: l = R.gens()[m:]

You can check:

sage: t[1] + l[3]
t1 + lambda3

If you want nice LaTeX rendering in jupyter notebook, you can do:

%display latex

You can define the matrix $x_i(t)$ as follows:

sage: def x(i,t):
....:     m = matrix.identity(R,n)
....:     m[i,i+1] = t
....:     return m

You can check, e.g.

sage: x(1,t[3])

The matrix $T$ can be easily defined:

sage: T = matrix.diagonal(l)

Now, if L is the list of indices $i_0,\dots,i_{m-1}$, you can define the matrix $g$ as follows:

sage: def g(L):
....:     M = matrix.identity(R,n)
....:     for i,l in enumerate(L):
....:         M = M * x(l,t[i])
....:     return M * T

You can check for example:

sage: g([0,1,3,5,6,8])
more