On Fri, Jul 27, 2012 at 6:05 PM, fulio <[email protected]> wrote:

> Hi all,
>
> is it possible to increase the index of a symbolic variable.
>
> I tried this.
>
> var('b x')
> var('i n', integer=True)
> print(var('x:5'))
> print(summation(b_i*x(n-i),(i, 0, 4)))
>
>
> The output should look like this:
> b4*x(n - 4) + b3*x(n - 3) + b2*x(n - 2) + b1x(n - 1) + b0x(n)
>
>
 I'm showing the steps I used to give an answer:

>>> [w for w in dir() if 'Index' in w]
['Indexed', 'IndexedBase']
>>> help(Indexed)
Help on class Indexed in module sympy.tensor.indexed:

class Indexed(sympy.core.expr.Expr)
 |  Represents a mathematical object with indices.
 |
 |  >>> from sympy.tensor import Indexed, IndexedBase, Idx
 |  >>> from sympy import symbols
 |  >>> i, j = map(Idx, ['i', 'j'])
 |  >>> Indexed('A', i, j)
 |  A[i, j]
 |
 |  It is recommended that Indexed objects are created via IndexedBase:
 |
 |  >>> A = IndexedBase('A')
 |  >>> Indexed('A', i, j) == A[i, j]
 |  True
 |

>>> b=IndexedBase('b')
>>> summation(b[i]*f(n-i),(i,0,4))
f(n)*b[0] + f(n - 4)*b[4] + f(n - 3)*b[3] + f(n - 2)*b[2] + f(n - 1)*b[1]

You can also create the symbols as needed and use Add rather than summation:

>>> Add(*(Symbol('b'+str(i))*f(n-i) for i in range(5)))
b0*f(n) + b1*f(n - 1) + b2*f(n - 2) + b3*f(n - 3) + b4*f(n - 4)

What do you want to do after you have the expression? The Indexed doesn't
respond to subs -- seems to me that this is an error -- while the symbol
does:

>>> summation(b[i]*f(n-i),(i,0,4))
f(n)*b[0] + f(n - 4)*b[4] + f(n - 3)*b[3] + f(n - 2)*b[2] + f(n - 1)*b[1]
>>> _.subs(b[0],1)
f(n)*b[0] + f(n - 4)*b[4] + f(n - 3)*b[3] + f(n - 2)*b[2] + f(n - 1)*b[1]

-------> notice that the b[0] is still there

>>> Add(*(Symbol('b'+str(i))*f(n-i) for i in range(5)))
b0*f(n) + b1*f(n - 1) + b2*f(n - 2) + b3*f(n - 3) + b4*f(n - 4)
>>> _.subs('b0',1)
b1*f(n - 1) + b2*f(n - 2) + b3*f(n - 3) + b4*f(n - 4) + f(n)

-------> the b0 is now 1

HTH,
 Chris

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to