# Matrix dimensions being symbolic

Is there any way to define a matrix in sage that has variables in place for the dimensions?

For instance, I want M with dimension n by p. I try the code below to illustrate

sage: var('n p')

sage: matrix(nrows=n,ncols=p)

edit retag close merge delete

What sort of things do you want to do with these matrices? Maybe there are other options.

One idea is to do matrix manipulations knowing a common dimension. For instance, if I have an M matrix that is n x n, then one idea is to be able to do matrix manipulation that is available for square matrices (i.e. eigenvalue analysis, inversion, etc.)

Sort by » oldest newest most voted

This is not shiny but works

    var('n p')
n = 2
p = 5

vars1 = [var('a_%d%d' %(i,j)) for i in [1..n] for j in [1..p]]
matrix(SR, n, p, vars1)


adapt for n, p > 9

more

Nope. After :

sage: var("n, p")
(n, p)


the Python variable n is indeed bound to a symbolic variable (element of SR) :

sage: n.parent()
Symbolic Ring


But after :

sage: n=2 ; p=5


n is bound to an integer :

sage: n.parent()
Integer Ring


and the rest of your code uses the fixed, integer values of n (and p).

There is currently no way to define a symbolic-dimensioned matrix in Sage (and, unless I'm mistaken (which is possible...), neither in Sympy, Mathematica, Giac or Fricas...).