# Create Matrix in RREF with indeterminates

Hi all,

I would like to be able to create a Matrix over a multivariable polynomial ring (in particular over R = PolynomialRing(QQ, 'x',(n-k)*n)) in Sage that has a particular form. I would like to create a $k \times n$ matrices that is in RREF form, where I can choose where the pivots should be and fill in the rest with variables from my polynomial ring. (I am assuming full rank, so there will be $k$ pivots with $n \choose k$ possible choices)

$\begin{bmatrix} 1 & x1 & 0 & x2 & \cdots\\ 0 & 0 & 1 & x3 & \cdots\\ 0 & 0 & 0 & 0 &\cdots \\ \vdots & \vdots & \vdots & \vdots & \ddots \end{bmatrix}$

Is there any way I can do this in Sage by just indicating which columns I want to have the pivots in?

edit retag close merge delete

Sort by » oldest newest most voted Here's a way. First we define our variables:

k = 2
n = 3
R = PolynomialRing(QQ, 'x',(n-k)*k)


Then we introduce the following function:

def indet_rref(k, n, pivots, ring):
assert len(pivots) == k and sorted(pivots) == list(pivots)
M = Matrix(ring, k, n)
taken = []
for (row, pivot) in enumerate(pivots):
M[row, pivot] = 1
taken.extend((i, pivot) for i in range(k))
taken.extend((row,j) for j in range(pivot))
indet_indices = [(i,j) for i in range(k) for j in range(n) if not (i,j) in taken]
for (idx, y) in zip(indet_indices, ring.gens()):
M[idx] = y
return M


The pivots argument is a list of k column indices (one per row), and the generators of ring are taken as the indeterminates.

For example:

sage: indet_rref(k, n, range(k), R)
[ 1  0 x0]
[ 0  1 x1]


We can test all the combinations:

import itertools
for pivots in itertools.combinations(range(n), k):
M = indet_rref(k, n, pivots, R)
assert M.rref() == M
assert M.rank() == k
print M
print


Output:

[ 1  0 x0]
[ 0  1 x1]

[ 1 x0  0]
[ 0  0  1]

[0 1 0]
[0 0 1]

more