ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 30 Jul 2022 14:29:44 +0200Matrix dimensions being symbolichttps://ask.sagemath.org/question/63436/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)`
Thu, 28 Jul 2022 18:12:02 +0200https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/Comment by Pickle for <p>Is there any way to define a matrix in sage that has variables in place for the dimensions?</p>
<p>For instance, I want M with dimension n by p. I try the code below to illustrate </p>
<p><code>sage: var('n p')</code></p>
<p><code>sage: matrix(nrows=n,ncols=p)</code></p>
https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63458#post-id-63458One 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.)Sat, 30 Jul 2022 14:29:44 +0200https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63458#post-id-63458Comment by John Palmieri for <p>Is there any way to define a matrix in sage that has variables in place for the dimensions?</p>
<p>For instance, I want M with dimension n by p. I try the code below to illustrate </p>
<p><code>sage: var('n p')</code></p>
<p><code>sage: matrix(nrows=n,ncols=p)</code></p>
https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63446#post-id-63446What sort of things do you want to do with these matrices? Maybe there are other options.Fri, 29 Jul 2022 00:49:21 +0200https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63446#post-id-63446Answer by jesuslop for <p>Is there any way to define a matrix in sage that has variables in place for the dimensions?</p>
<p>For instance, I want M with dimension n by p. I try the code below to illustrate </p>
<p><code>sage: var('n p')</code></p>
<p><code>sage: matrix(nrows=n,ncols=p)</code></p>
https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?answer=63439#post-id-63439This 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
Thu, 28 Jul 2022 19:36:04 +0200https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?answer=63439#post-id-63439Comment by Emmanuel Charpentier for <p>This is not shiny but works</p>
<pre><code> 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)
</code></pre>
<p>adapt for n, p > 9</p>
https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63444#post-id-63444Nope. 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...).Fri, 29 Jul 2022 00:07:30 +0200https://ask.sagemath.org/question/63436/matrix-dimensions-being-symbolic/?comment=63444#post-id-63444