First time here? Check out the FAQ!

Ask Your Question
1

Matrix dimensions being symbolic

asked 2 years ago

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)

Preview: (hide)

Comments

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

John Palmieri gravatar imageJohn Palmieri ( 2 years ago )

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 ( 2 years ago )

1 Answer

Sort by » oldest newest most voted
-1

answered 2 years ago

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

Preview: (hide)
link

Comments

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 ( 2 years ago )

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

Stats

Asked: 2 years ago

Seen: 267 times

Last updated: Jul 28 '22