Ask Your Question

Matrix dimensions being symbolic

asked 2022-07-28 18:12:02 +0100

Pickle gravatar image

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 flag offensive close merge delete


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

John Palmieri gravatar imageJohn Palmieri ( 2022-07-29 00:49:21 +0100 )edit

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.)

Pickle gravatar imagePickle ( 2022-07-30 14:29:44 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-07-28 19:36:04 +0100

jesuslop gravatar image

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

edit flag offensive delete link 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...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-07-29 00:07:30 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-07-28 18:12:02 +0100

Seen: 122 times

Last updated: Jul 28 '22