Ask Your Question

implicitly defining a sequence of variables

asked 2011-06-21 23:48:29 -0500

parzan gravatar image

updated 2011-06-22 11:24:58 -0500

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?

edit retag flag offensive close merge delete


How 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.

Jason Grout gravatar imageJason Grout ( 2011-07-04 14:09:30 -0500 )edit

I 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.

parzan gravatar imageparzan ( 2011-07-13 12:20:14 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2011-06-23 02:24:33 -0500

niles gravatar image

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 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
edit flag offensive delete link more


a = list(var("a%d" % i) for i in range(4))?

DSM gravatar imageDSM ( 2011-06-23 04:18:03 -0500 )edit

By 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...

kcrisman gravatar imagekcrisman ( 2011-06-23 05:37:05 -0500 )edit

then maybe someone will improve `var` to automatically generate a sequence of variables ;)

niles gravatar imageniles ( 2011-06-23 05:44:57 -0500 )edit

Thanks 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'.

parzan gravatar imageparzan ( 2011-06-23 10:52:36 -0500 )edit

Maybe this would be possible with a generator...

kcrisman gravatar imagekcrisman ( 2011-06-23 13:42:58 -0500 )edit

answered 2011-07-04 13:58:15 -0500

Jason Grout gravatar image

updated 2011-07-04 13:58:33 -0500

How about something like this?

class VariableGenerator(object):
    def __init__(self, prefix):
        self.__prefix = prefix

    def __getitem__(self, key):
        return SR.var("%s%s"%(self.__prefix,key))
a[0], a[1], a[2] # all variables
edit flag offensive delete link more


Note 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.

Jason Grout gravatar imageJason Grout ( 2011-07-04 14:08:01 -0500 )edit

This 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?

parzan gravatar imageparzan ( 2011-07-13 11:34:11 -0500 )edit

answered 2011-07-13 12:08:30 -0500

parzan gravatar image

updated 2011-07-13 23:34:52 -0500

I 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
sage: p.taylor(x,0,3)                  
sage: p.diff(x)


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:
 -- an error. To debug this try: debugmode(true);

whereas in maple this works -


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) $.

edit flag offensive delete link more

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


Asked: 2011-06-21 23:48:29 -0500

Seen: 1,551 times

Last updated: Jul 13 '11