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, 21 Nov 2020 20:41:15 +0100implicitly defining a sequence of variableshttps://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/To define a general polynomial in Maple one writes
p := sum(a[i]*x^i,i=0..n);
and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.
So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.
Trying to accomplish this in sage I reached
sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:
sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
In Maple they all give good results.
Am I going at this the right way? Is there a way to implicitly define variables as in Maple?
Wed, 22 Jun 2011 06:48:29 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/Comment by Jason Grout for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21503#post-id-21503How does Maple deal with formal polynomials? I think we'd need some other sort of formal polynomial class to deal with them, as what you're doing above in Sage is only constructing an explicit symbolic polynomial.Mon, 04 Jul 2011 21:09:30 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21503#post-id-21503Comment by parzan for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21475#post-id-21475I really don't know how Maple does this. I am certain it is not by a class of formal polynomials, but simply a symbolic sum of symbolic expressions (involving indexing). One could also take products instead of sums, or combinations of such. I assume Maplesoft would not explain how this is implemented, but Maxima also knows how to do this (see my answer below), and it is open source, so hopefully one day someone would implement this in sage, or at least wrap the maxima implementation.Wed, 13 Jul 2011 19:20:14 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21475#post-id-21475Answer by niles for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=12462#post-id-12462I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for `diff`, `series`, and `taylor`:
sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
Thu, 23 Jun 2011 09:24:33 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=12462#post-id-12462Comment by DSM for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21557#post-id-21557a = list(var("a%d" % i) for i in range(4))? Thu, 23 Jun 2011 11:18:03 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21557#post-id-21557Comment by kcrisman for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21551#post-id-21551Maybe this would be possible with a generator...Thu, 23 Jun 2011 20:42:58 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21551#post-id-21551Comment by parzan for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21476#post-id-21476I think the real thing would be to implement in the symbolic expression class the __getitem__ method as in Jason's answer, but allowing a symbolic expression as index (and returning a third symbolic expression, a[i], which upon assigning i=3 for example evaluates to a[3]). Of course this is just wishful thinking, I have no idea how to actually implement this. On the other hand maxima does this, so it is somehow possible!Wed, 13 Jul 2011 18:53:20 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21476#post-id-21476Comment by parzan for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21552#post-id-21552Thanks for the ideas! This is nice, but it's not exactly what I want, since it bounds the polynomial degree. What I am looking for is a way to represent general polynomials, i.e., the "4" in the example should somehow be replaced by a variable 'n'.Thu, 23 Jun 2011 17:52:36 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21552#post-id-21552Comment by kcrisman for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21555#post-id-21555By the way, in Python 3 this is going to get even more confusing, with the {0} notation we'll need looking vaguely like subscript notation...Thu, 23 Jun 2011 12:37:05 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21555#post-id-21555Comment by niles for <p>I think the best way to do this is to use python's list comprehension/generator syntax to define the sequence of variables. Something like the following provides a formal sum which works as expected for <code>diff</code>, <code>series</code>, and <code>taylor</code>:</p>
<pre><code>sage: a = var(','.join('a%s'%i for i in range(4))); a
(a0, a1, a2, a3)
sage: p = sum(a[i]*x^i for i in range(4)); p
a3*x^3 + a2*x^2 + a1*x + a0
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21554#post-id-21554then maybe someone will improve `var` to automatically generate a sequence of variables ;)Thu, 23 Jun 2011 12:44:57 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21554#post-id-21554Answer by Emmanuel Charpentier for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=54329#post-id-54329You may define a *symbolic function* implicitly accepting an integer argument ; bonus : you may also define print and latex_print functins to get a "pleasant" output.
See `function?`.Sat, 21 Nov 2020 20:41:15 +0100https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=54329#post-id-54329Answer by Jason Grout for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=11982#post-id-11982How about something like this?
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')
a[0], a[1], a[2] # all variables
Mon, 04 Jul 2011 20:58:15 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=11982#post-id-11982Comment by Jason Grout for <p>How about something like this?</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')
a[0], a[1], a[2] # all variables
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21504#post-id-21504Note that this doesn't solve the "number of variables is a variable" problem, only the "how do I easily generate a number of variables" problem.Mon, 04 Jul 2011 21:08:01 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21504#post-id-21504Comment by parzan for <p>How about something like this?</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')
a[0], a[1], a[2] # all variables
</code></pre>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21478#post-id-21478This is nice. Perhaps you could make "VariableGenerator" inherit "sage.symbolic.expression.Expression" so that it would be a variable with indexing? and even better, if the key could be a symbolic expression itself - maybe that way we could achieve maple's functionality?Wed, 13 Jul 2011 18:34:11 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?comment=21478#post-id-21478Answer by parzan for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=12518#post-id-12518I found out that maxima does offer this capability, so one can use it in sage:
sage: p = maxima('sum(a[i]*x^i,i,0,n)')
sage: p
'sum(a[i]*x^i,i,0,n)
sage: p.taylor(x,0,3)
a[0]+a[1]*x+a[2]*x^2+a[3]*x^3
sage: p.diff(x)
'sum(i*a[i]*x^(i-1),i,0,n)
sadly:
sage: p.sage()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
...
TypeError: unable to make sense of Maxima expression 'sum(a[i]*x^i,i,0,n)' in Sage
Also, it is not as strong as maple:
sage: p = maxima('product(1+a[i]*x+b[i]*x^2,i,0,n)')
sage: p.taylor(x,0,3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
taylor: unable to expand at a point specified in:
'product(b[i]*x^2+a[i]*x+1,i,0,n)
-- an error. To debug this try: debugmode(true);
whereas in maple this works -
series(product(1+a[i]*x+b[i]*x^2,i=1..k),x=0,3);
gives the desired answer $1+\sum _{i=1}^{k}a_{{i}}x+ \left( \sum _{i=1}^{k}b_{{i}}-1/2\cdot{a_{{i}
}}^{2}+1/2\cdot \left( \sum _{i=1}^{k}a_{{i}} \right) ^{2} \right) {x}^{2
}+O \left( {x}^{3} \right) $.
Wed, 13 Jul 2011 19:08:30 +0200https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=12518#post-id-12518Answer by Sylvain for <p>To define a general polynomial in Maple one writes</p>
<pre><code>p := sum(a[i]*x^i,i=0..n);
</code></pre>
<p>and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.</p>
<p>So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.</p>
<p>Trying to accomplish this in sage I reached</p>
<pre><code>sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
</code></pre>
<p>Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:</p>
<pre><code>sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
</code></pre>
<p>In Maple they all give good results.</p>
<p>Am I going at this the right way? Is there a way to implicitly define variables as in Maple?</p>
https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=54320#post-id-54320I use the following:
sage: x = SR.var("x", 10)
sage: sage: p = sum(a[i]*x^i for i in range(10)); p
a9*x^9 + a8*x^8 + a7*x^7 + a6*x^6 + a5*x^5 + a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0
Fri, 20 Nov 2020 21:12:39 +0100https://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/?answer=54320#post-id-54320