> > 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 > > >>> var('lamda') lamda >>> e = lambda n, j: Sum(Subs(lamda(x),x,i)*(-1)**(n-j)*binomial(i,n-j),(i,n-j,n)).doit()
That Subs was created to delay substitution. It says "don't replace the function arg until you have a value for the replacement 'i'". e is now a 2-argument function (defined as a 1-liner but could have been defined as def e(n,j): return Sum(Subs(lamda(x),x,i)*(-1)**(n-j)*binomial(i,n-j),(i,n-j,n)).doit() Now let's see e in action: >>> e(5,2) -lamda(3) - 4*lamda(4) - 10*lamda(5) Note: if the "doit" were not part of e's definition then e(5,2) would have given us Sum(-i*(i - 2)*(i - 1)*Subs(lamda(x), (x,), (i,))/6, (i, 3, 5)) and then we would have to apply doit() to get it to evaluate. Now, if you have specific values of lamda you can just substitute them in. Here I only replace lamda(3): >>> _.subs(lamda(3), 2) -4*lamda(4) - 10*lamda(5) - 2 Now here's your loop: >>> n=3 >>> for j in range(n+1): ... print e(n,j) ... -lamda(3) lamda(2) + 3*lamda(3) -lamda(1) - 2*lamda(2) - 3*lamda(3) lamda(0) + lamda(1) + lamda(2) + lamda(3) Looks familiar, right? > But what's with so many ((( with the subs method? > > foo.subs(old, new) or foo.subs( [ (old1, new1), (old2, new2) ] ); the 2nd case gives a list (or tuple) of replacements to apply sequentially, i.e. foo.subs(old1,new1).subs(old2,new2). > 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? > > summation tries to do the summation and if it can't it returns the Sum; if you enter it as a Sum it says "whether you can or not, don't evaluate it yet", i.e. summation *might* evaluate; Sum won't. > 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, one is lamb0 the other is list lamb; lamb[0] is item 0 of list lamb. -- 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.