Le 18/11/2012 18:09, Shriramana Sharma a écrit :
On Sun, Nov 18, 2012 at 10:03 PM, Chris Smith <smi...@gmail.com> wrote:
_.replace(f, lambda x: x**2)

No I mean: I have individual values for lamb0, lamb1, lamb2, lamb3. So
what I did was:

from sympy import *
var('i j n')
lamb=Function('lamb')
f=Sum(lamb(i)*(-1)**(n-j)*binomial(i,n-j),(i,n-j,n))
mylamb=list(var('lamb:4'))
nval=3
for jval in range(nval+1):
         print(f.subs(((j,jval),(n,nval))).doit().replace(lamb,lambda
x:mylamb[x]))

which gave the "desired" output of:

-lamb3
lamb2 + 3*lamb3
-lamb1 - 2*lamb2 - 3*lamb3
lamb0 + lamb1 + lamb2 + lamb3

Obviously I'll have to have a proper mylamb list (containing floats)
for my application, but this is just experimentation...

But what's with so many ((( with the subs method?

First, don't use var(), it'll just confuse you. The second line should be "i, j, n = symbols('i j n')" and the fifth:
"mylamb = symbols('lamb:4')".

To do the substitutions, the simplest way in this case is to create a substitution dictionary, and then call subs(), like this (assuming Python 2.7+):
substs = {lamb(k): value for k, value in enumerate(mylamb)}
print f.subs({j: jval, n: nval}).doit().subs(substs)


I'm not sure what you mean. Using summation is ding it with sympy semantics.
Perhaps you can post the way you think is un-sympy-ish.

What I considered *unsympyish* is to do instead of summation(f,(i,a,b)):

s=0
for ival in range(a,b+1): s+=f.subs(i,ival)

BTW can you please clarify the difference between Sum and summation?
The doc says "represents unevaluated summation". So is it just that
until I say doit() it doesn't give me the output but remains a
symbolic function or such?

Yes, that's it. Sum is a Python class that represents symbolic sums. When you write Sum(...), you simply get an object of type 'Sum', with attributes specified by the arguments of the call. summation(...) is effectively just Sum(...).doit(). There are several (function, class) pairs like this in sympy, another example is integrate/Integral.

Another curious thing which I came across in the process of the above:

In [1]: from sympy import *
In [2]: lamb=list(var('lamb:4'))
In [3]: lamb
Out[3]: [lamb0, lamb1, lamb2, lamb3]
In [4]: lamb0=4
In [5]: lamb
Out[5]: [lamb0, lamb1, lamb2, lamb3]
In [6]: lamb[0]
Out[6]: lamb0
In [7]: type(lamb[0])
Out[7]: sympy.core.symbol.Symbol
In [8]: type(lamb0)
Out[8]: builtins.int

I don't get it -- there are *two* objects with name lamb0 in the
current namespace? How can that be?

No there aren't. Don't mix up the python variable named 'lamb0' (which is 4, per your assignment in In[4]) with the object Symbol('lamb0'), which happens to be the value of lamb[0]. See http://docs.sympy.org/dev/gotchas.html#variables-assignment-does-not-create-a-relation-between-expressions for more explanations on this.

Thanks as ever!


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

Reply via email to