ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 14 Apr 2017 17:16:30 -0500Multivariate symbolicshttp://ask.sagemath.org/question/37292/multivariate-symbolics/Consider this piece of code:
sage: n=4 # dimension of state-space
sage: x = polygens(QQ, ['x'+str(i) for i in [1..n]]); x # vector of state variables
(x1, x2, x3, x4)
There is a handy keyword with formal power series, the number of generators (`num_gens`), and it works like:
sage: PowerSeriesRing(QQ, 'x', num_gens=5)
Multivariate Power Series Ring in x0, x1, x2, x3, x4 over Rational Field
There is a kind of analogue with polynomials, but not keyworded:
sage: PolynomialRing(QQ, 'x', 5)
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Rational Field
Is it possible to use any of these to define a vector of polynomial variables in the object `x` as the beginning (but without list comprehension)?
---
As a side note, i think the `var` constructor also lacks a vector form?
sage: var(['x'+str(i) for i in [1..n]])
(x1, x2, x3, x4)
This could be useful to simplify constructions as:
sage: A = matrix(SR, [['a'+str(i)+str(j) for j in [1..n]] for i in [1..n]]); A
[a11 a12 a13 a14]
[a21 a22 a23 a24]
[a31 a32 a33 a34]
[a41 a42 a43 a44]
In Matlab this is `A = sym('a', n)`, but i didn't find a similar shortcut in Sage.Fri, 14 Apr 2017 01:59:58 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/Answer by nbruin for <p>Consider this piece of code:</p>
<pre><code>sage: n=4 # dimension of state-space
sage: x = polygens(QQ, ['x'+str(i) for i in [1..n]]); x # vector of state variables
(x1, x2, x3, x4)
</code></pre>
<p>There is a handy keyword with formal power series, the number of generators (<code>num_gens</code>), and it works like:</p>
<pre><code>sage: PowerSeriesRing(QQ, 'x', num_gens=5)
Multivariate Power Series Ring in x0, x1, x2, x3, x4 over Rational Field
</code></pre>
<p>There is a kind of analogue with polynomials, but not keyworded:</p>
<pre><code>sage: PolynomialRing(QQ, 'x', 5)
Multivariate Polynomial Ring in x0, x1, x2, x3, x4 over Rational Field
</code></pre>
<p>Is it possible to use any of these to define a vector of polynomial variables in the object <code>x</code> as the beginning (but without list comprehension)? </p>
<hr>
<p>As a side note, i think the <code>var</code> constructor also lacks a vector form?</p>
<pre><code>sage: var(['x'+str(i) for i in [1..n]])
(x1, x2, x3, x4)
</code></pre>
<p>This could be useful to simplify constructions as:</p>
<pre><code>sage: A = matrix(SR, [['a'+str(i)+str(j) for j in [1..n]] for i in [1..n]]); A
[a11 a12 a13 a14]
[a21 a22 a23 a24]
[a31 a32 a33 a34]
[a41 a42 a43 a44]
</code></pre>
<p>In Matlab this is <code>A = sym('a', n)</code>, but i didn't find a similar shortcut in Sage.</p>
http://ask.sagemath.org/question/37292/multivariate-symbolics/?answer=37293#post-id-37293 sage: PolynomialRing(QQ, 'x', 5).gens()
(x0, x1, x2, x3, x4)
For your symbolic matrix, perhaps this is more to your liking?
sage: matrix(SR,4,4, lambda i,j: SR.symbol("a%s%s"%(i,j)))
[a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33]
Fri, 14 Apr 2017 03:11:49 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/?answer=37293#post-id-37293Comment by mforets for <pre><code>sage: PolynomialRing(QQ, 'x', 5).gens()
(x0, x1, x2, x3, x4)
</code></pre>
<p>For your symbolic matrix, perhaps this is more to your liking?</p>
<pre><code>sage: matrix(SR,4,4, lambda i,j: SR.symbol("a%s%s"%(i,j)))
[a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33]
</code></pre>
http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37302#post-id-37302follow up in [#22813](https://trac.sagemath.org/ticket/22813) !Fri, 14 Apr 2017 17:16:30 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37302#post-id-37302Comment by nbruin for <pre><code>sage: PolynomialRing(QQ, 'x', 5).gens()
(x0, x1, x2, x3, x4)
</code></pre>
<p>For your symbolic matrix, perhaps this is more to your liking?</p>
<pre><code>sage: matrix(SR,4,4, lambda i,j: SR.symbol("a%s%s"%(i,j)))
[a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33]
</code></pre>
http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37301#post-id-37301`var(*args)` already means the same thing as `for a in args: var(a)`, so the signature is not available for different interpretation.Fri, 14 Apr 2017 14:17:27 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37301#post-id-37301Comment by mforets for <pre><code>sage: PolynomialRing(QQ, 'x', 5).gens()
(x0, x1, x2, x3, x4)
</code></pre>
<p>For your symbolic matrix, perhaps this is more to your liking?</p>
<pre><code>sage: matrix(SR,4,4, lambda i,j: SR.symbol("a%s%s"%(i,j)))
[a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33]
</code></pre>
http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37296#post-id-37296for the symbolic vector/matrix, i would take your solution and embed it into `calculus.var` so that `var('x', 4)` and `var('x', 4, 4)` work and produce a vector/matrix respectively .. what do you think?Fri, 14 Apr 2017 04:59:29 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37296#post-id-37296Comment by mforets for <pre><code>sage: PolynomialRing(QQ, 'x', 5).gens()
(x0, x1, x2, x3, x4)
</code></pre>
<p>For your symbolic matrix, perhaps this is more to your liking?</p>
<pre><code>sage: matrix(SR,4,4, lambda i,j: SR.symbol("a%s%s"%(i,j)))
[a00 a01 a02 a03]
[a10 a11 a12 a13]
[a20 a21 a22 a23]
[a30 a31 a32 a33]
</code></pre>
http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37295#post-id-37295yes, indeed `gens` answers my question! i had a closer look at `polygens` code, and seemingly a trivial change allows to do `polygens(QQ, 'x', 4)`. this is [#22809](https://trac.sagemath.org/ticket/22809)Fri, 14 Apr 2017 04:38:56 -0500http://ask.sagemath.org/question/37292/multivariate-symbolics/?comment=37295#post-id-37295