ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 30 Nov 2018 17:42:57 -0600Creating an array of variableshttps://ask.sagemath.org/question/8390/creating-an-array-of-variables/Here is a very very basic question.
I want to create a polynomial, say
a_0*x^0 + a_1*x + a_2*x^2+ \cdots + a_{20} x^{20}.
I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers.
There must be some very basic command that I don't know, but I can't find it in the documentation.Mon, 17 Oct 2011 05:16:43 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/Comment by parzan for <p>Here is a very very basic question.</p>
<p>I want to create a polynomial, say
a_0<em>x^0 + a_1</em>x + a_2*x^2+ \cdots + a_{20} x^{20}.</p>
<p>I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers. </p>
<p>There must be some very basic command that I don't know, but I can't find it in the documentation.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21105#post-id-21105See also http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables and the answers there.Tue, 18 Oct 2011 01:38:53 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21105#post-id-21105Answer by Jason Grout for <p>Here is a very very basic question.</p>
<p>I want to create a polynomial, say
a_0<em>x^0 + a_1</em>x + a_2*x^2+ \cdots + a_{20} x^{20}.</p>
<p>I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers. </p>
<p>There must be some very basic command that I don't know, but I can't find it in the documentation.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?answer=12779#post-id-12779You could use the method shown here: http://groups.google.com/group/sage-support/browse_thread/thread/18525d8c5bca0afa/8f9c64cbefa9ac67?lnk=gst&q=generate+symbolic+variables+jason#8f9c64cbefa9ac67
See, for example: http://sagenb.org/home/pub/3352/
class VariableGenerator(object):
def __init__(self, prefix):
self.__prefix = prefix
@cached_method
def __getitem__(self, key):
return SR.var("%s%s"%(self.__prefix,key))
a=VariableGenerator('a')
p = sum(a[i]*x**i for i in range(10))
Mon, 17 Oct 2011 10:09:48 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?answer=12779#post-id-12779Comment by slelievre for <p>You could use the method shown here: <a href="http://groups.google.com/group/sage-support/browse_thread/thread/18525d8c5bca0afa/8f9c64cbefa9ac67?lnk=gst&q=generate+symbolic+variables+jason#8f9c64cbefa9ac67">http://groups.google.com/group/sage-s...</a></p>
<p>See, for example: <a href="http://sagenb.org/home/pub/3352/">http://sagenb.org/home/pub/3352/</a></p>
<pre><code>class VariableGenerator(object):
def __init__(self, prefix):
self.__prefix = prefix
@cached_method
def __getitem__(self, key):
return SR.var("%s%s"%(self.__prefix,key))
a=VariableGenerator('a')
p = sum(a[i]*x**i for i in range(10))
</code></pre>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=44527#post-id-44527The sagenb.org server was shut down, but the public worksheet is at
- [https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/3301-3401/3352.sagews?viewer=share](https://share.cocalc.com/share/19575ea0-317e-402b-be57-368d04c113db/pub/3301-3401/3352.sagews?viewer=share)Fri, 30 Nov 2018 17:42:57 -0600https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=44527#post-id-44527Comment by Jason Grout for <p>You could use the method shown here: <a href="http://groups.google.com/group/sage-support/browse_thread/thread/18525d8c5bca0afa/8f9c64cbefa9ac67?lnk=gst&q=generate+symbolic+variables+jason#8f9c64cbefa9ac67">http://groups.google.com/group/sage-s...</a></p>
<p>See, for example: <a href="http://sagenb.org/home/pub/3352/">http://sagenb.org/home/pub/3352/</a></p>
<pre><code>class VariableGenerator(object):
def __init__(self, prefix):
self.__prefix = prefix
@cached_method
def __getitem__(self, key):
return SR.var("%s%s"%(self.__prefix,key))
a=VariableGenerator('a')
p = sum(a[i]*x**i for i in range(10))
</code></pre>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21108#post-id-21108I've been hoping someone will make a patch and shepherd it through the review process!Mon, 17 Oct 2011 17:20:17 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21108#post-id-21108Comment by DSM for <p>You could use the method shown here: <a href="http://groups.google.com/group/sage-support/browse_thread/thread/18525d8c5bca0afa/8f9c64cbefa9ac67?lnk=gst&q=generate+symbolic+variables+jason#8f9c64cbefa9ac67">http://groups.google.com/group/sage-s...</a></p>
<p>See, for example: <a href="http://sagenb.org/home/pub/3352/">http://sagenb.org/home/pub/3352/</a></p>
<pre><code>class VariableGenerator(object):
def __init__(self, prefix):
self.__prefix = prefix
@cached_method
def __getitem__(self, key):
return SR.var("%s%s"%(self.__prefix,key))
a=VariableGenerator('a')
p = sum(a[i]*x**i for i in range(10))
</code></pre>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21112#post-id-21112@Jason Grout: should something like your recipe be made canonical [i.e. added to the Sage stdlib]? Overriding __getitem__ -- and remembering to cache the method so that a[3] is a[3] -- is a little beyond what beginners should need to understand to get the outcome, IMHO.Mon, 17 Oct 2011 10:20:48 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21112#post-id-21112Answer by DSM for <p>Here is a very very basic question.</p>
<p>I want to create a polynomial, say
a_0<em>x^0 + a_1</em>x + a_2*x^2+ \cdots + a_{20} x^{20}.</p>
<p>I could define these a_i one at a time, but it would be much better to have a way to create an array A of length 20 where A[i] is the coefficient a_i. The idea is that I want to do some operations and solve for these coefficients, which will end up being rational numbers. </p>
<p>There must be some very basic command that I don't know, but I can't find it in the documentation.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?answer=12772#post-id-12772I don't know that there's a command in particular to construct a polynomial with symbolic coefficients, but here's what I'd do:
sage: N = 20
sage: x = var("x")
sage: aa = list(var('a_%d' % i) for i in (0..N))
sage: p = sum(a*x**i for i,a in enumerate(aa))
sage: p
a_20*x^20 + a_19*x^19 + a_18*x^18 + a_17*x^17 + a_16*x^16 + a_15*x^15 + a_14*x^14 + a_13*x^13 + a_12*x^12 + a_11*x^11 + a_10*x^10 + a_9*x^9 + a_8*x^8 + a_7*x^7 + a_6*x^6 + a_5*x^5 + a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
It's easy enough to change where x and the coefficients a are living (right now they're in the Symbolic Ring) if needed.
Mon, 17 Oct 2011 05:29:46 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?answer=12772#post-id-12772Comment by Xaver for <p>I don't know that there's a command in particular to construct a polynomial with symbolic coefficients, but here's what I'd do:</p>
<pre><code>sage: N = 20
sage: x = var("x")
sage: aa = list(var('a_%d' % i) for i in (0..N))
sage: p = sum(a*x**i for i,a in enumerate(aa))
sage: p
a_20*x^20 + a_19*x^19 + a_18*x^18 + a_17*x^17 + a_16*x^16 + a_15*x^15 + a_14*x^14 + a_13*x^13 + a_12*x^12 + a_11*x^11 + a_10*x^10 + a_9*x^9 + a_8*x^8 + a_7*x^7 + a_6*x^6 + a_5*x^5 + a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
</code></pre>
<p>It's easy enough to change where x and the coefficients a are living (right now they're in the Symbolic Ring) if needed.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21118#post-id-21118Why does one need the list? Can't one just do: sum([var(join(['a',str(i)],sep=''))*x^i for i in (0..N)])Mon, 17 Oct 2011 06:25:05 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21118#post-id-21118Comment by DSM for <p>I don't know that there's a command in particular to construct a polynomial with symbolic coefficients, but here's what I'd do:</p>
<pre><code>sage: N = 20
sage: x = var("x")
sage: aa = list(var('a_%d' % i) for i in (0..N))
sage: p = sum(a*x**i for i,a in enumerate(aa))
sage: p
a_20*x^20 + a_19*x^19 + a_18*x^18 + a_17*x^17 + a_16*x^16 + a_15*x^15 + a_14*x^14 + a_13*x^13 + a_12*x^12 + a_11*x^11 + a_10*x^10 + a_9*x^9 + a_8*x^8 + a_7*x^7 + a_6*x^6 + a_5*x^5 + a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
</code></pre>
<p>It's easy enough to change where x and the coefficients a are living (right now they're in the Symbolic Ring) if needed.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21117#post-id-21117@Xaver: sure, but then it's much less convenient to refer to the variables by themselves later. If you store them in a list or a dict or something, then you can write aa[10] and get the variable a_10.Mon, 17 Oct 2011 06:29:26 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21117#post-id-21117Comment by Xaver for <p>I don't know that there's a command in particular to construct a polynomial with symbolic coefficients, but here's what I'd do:</p>
<pre><code>sage: N = 20
sage: x = var("x")
sage: aa = list(var('a_%d' % i) for i in (0..N))
sage: p = sum(a*x**i for i,a in enumerate(aa))
sage: p
a_20*x^20 + a_19*x^19 + a_18*x^18 + a_17*x^17 + a_16*x^16 + a_15*x^15 + a_14*x^14 + a_13*x^13 + a_12*x^12 + a_11*x^11 + a_10*x^10 + a_9*x^9 + a_8*x^8 + a_7*x^7 + a_6*x^6 + a_5*x^5 + a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
</code></pre>
<p>It's easy enough to change where x and the coefficients a are living (right now they're in the Symbolic Ring) if needed.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21114#post-id-21114@DSM: for sage: p=sum([var(join(['a',str(i)],sep=''))*x^i for i in (0..N)]), couldn't you still use p.operands()[N-10].operands()[0] and get a10, so you can still address them in a list fashion ... but I agree this now looks ugly (and with karma 51 I'd better shut up ;) ) Mon, 17 Oct 2011 08:47:05 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21114#post-id-21114Comment by DSM for <p>I don't know that there's a command in particular to construct a polynomial with symbolic coefficients, but here's what I'd do:</p>
<pre><code>sage: N = 20
sage: x = var("x")
sage: aa = list(var('a_%d' % i) for i in (0..N))
sage: p = sum(a*x**i for i,a in enumerate(aa))
sage: p
a_20*x^20 + a_19*x^19 + a_18*x^18 + a_17*x^17 + a_16*x^16 + a_15*x^15 + a_14*x^14 + a_13*x^13 + a_12*x^12 + a_11*x^11 + a_10*x^10 + a_9*x^9 + a_8*x^8 + a_7*x^7 + a_6*x^6 + a_5*x^5 + a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
</code></pre>
<p>It's easy enough to change where x and the coefficients a are living (right now they're in the Symbolic Ring) if needed.</p>
https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21111#post-id-21111@Xaver: I think it's worse than that, as what if you add new operands to p (or zero a term)? Then you've got no guarantee that the ith operand is a_i. So you have to keep a copy of the original p around just in case, in which case it's simpler to keep the variables around. But pay no attention to karma, lots of main devels around here have only middling karma. :^)Mon, 17 Oct 2011 10:28:21 -0500https://ask.sagemath.org/question/8390/creating-an-array-of-variables/?comment=21111#post-id-21111