2008/12/30 Danny <dannytar...@gmail.com>: > > Hi Ondrej, > > Thanks for the quick response. I took a look at the Sum class tests, > but there is still one part I'm not clear on. > > In defining a summation, I would like to have the sum over an array of > symbols, indexed by an iterate. > > For example, In the sum > > \sum_{i=0}^4 tau_i, > > I'd like each tau[i] to itself be a symbol, since I later want to > differentiate with respect to these symbols. > > I'd like to do this: > In [17]: tau = [] > > In [18]: for i in range(10): > ....: tau.append(Symbol('tau_%s' % i)) > > In [19]: Sum(tau[i]**2, (i, 0, 9)) > --------------------------------------------------------------------------- > ValueError Traceback (most recent call > last) > > or > > In [20]: Sum(i**2, (i, tau)) > --------------------------------------------------------------------------- > ValueError Traceback (most recent call > last) > > > Ideally, this would give some compact representation of > tau[0]**2 + tau[1]**2 + ... + tau[9]**2
One way of doing it is this: In [1]: var("i") Out[1]: i In [2]: tau = Function("tau") In [3]: Sum(tau(i)**2, (i, 0, 9)) Out[3]: Sum(tau(i)**2, (i, 0, 9)) In [4]: Sum(tau(i)**2, (i, 0, 9)).doit() Out[4]: 2 2 2 2 2 2 2 2 2 2 τ (0) + τ (1) + τ (2) + τ (3) + τ (4) + τ (5) + τ (6) + τ (7) + τ (8) + τ (9) You can differentiate it like this: In [7]: Sum(tau(i)**2, (i, 0, 9)).doit().subs(tau(6), x).diff(x).subs(x, tau(6)) Out[7]: 2⋅τ(6) (One needs to substitute the function tau(i) for a symbol in order to differentiate.) If you don't call .doit(), it will fail: In [8]: Sum(tau(i)**2, (i, 0, 9)).subs(tau(6), x).diff(x).subs(x, tau(6)) Out[8]: 0 becuase there is no tau(6) in the Sum expression. This could probably be fixed. > > Is there something I'm missing? Let me know if the above helps, or if we should try to find a different solution. Ondrej --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---