On Jul 9, 3:54 am, Jason Grout <jason-s...@creativetrax.com> wrote: > On 7/8/10 12:26 PM, David Sanders wrote: > > > > > Hi, > > > How can I manipulate objects with indices? > > I need to do things like > > > a_i + b_i * c_{j+1} > > > using a TeX style notation (so that in the last term, the index is j > > +1) > > > where i and j are symbolic. > > > In Mathematica this would be something like > > a[i] + b[i] * c[j+1] > > > but something like that presumably cannot work in Sage, since [] are > > reserved for other things! > > Here's one way to get such expressions: > > sage: a=function('a') > sage: b=function('b') > sage: c=function('c') > sage: var('j') > j > sage: a(j)*b(j)*c(j+1) > a(j)*b(j)*c(j + 1)
Perfect, that's exactly what I was looking for, thanks! > > > I then need to be able to manipulate such expressions to act on them > > with functions according to the values of different indices in each > > term: if there is a product a_i b_i with two equal indices then I need > > to apply a different rule than if the indices are different( a_i b_j > > with j not equal to i ). > > > Could somebody please tell me what syntax I need (if indeed this is > > possible...)? > > How would you manipulate such expressions in Mathematica according to > the rules you give above? It is code I wrote a while ago, but the basic idea is, in light of your response to another question I asked, to do some kind of pattern matching. In fact, the operation is an expectation, so the Mathematica code has things like ExpectationExpression[b_ + c_] := ExpectationExpression[b] + ExpectationExpression[c] to express that the expectation is linear. Of course, Mathematica manages to do the pattern matching in a rather intuitive way. Then ExpectationExpression[a_ b_] /; FreeQ[a, e[i_]] && FreeQ[a, sp[i_]] && FreeQ[a, sm[i_]] && FreeQ[a, lp[i_]] && FreeQ[a, lm[i_]] && FreeQ[a, m[i]] := a*ExpectationExpression[b] to say that it's multiplicative as long as none of the random variables of interest (e_i etc.) occur in the expression. I guess the key part of the code is this kind of thing, a whole series of pattern matching for different possibilities of multiplicative terms: Ex[sp[i_]^alpha_ sm[i_] ^beta_] := ExpectationOfS[i, alpha, beta] Ex[sp[i_] sm[i_] ^beta_] := ExpectationOfS[i, 1, beta] and finally ExpectationOfS[i_, alpha_, beta_] := e[i]^(alpha + beta) FullSimplify[ Gamma[m[i]] / Gamma[m[i] + alpha + beta]] Expand[ FullSimplify[ Gamma[lp[i] + alpha] Gamma[ lm[i] + beta] / (Gamma[lp[i]] Gamma[lm[i]])]] which is the end of the road where the values are finally calculated. I am happy to make the full code available if that is of interest. (I doubt it!) As I say, however, I think it is now clear to me that this is basically a pattern matching exercise, so with your kind assistance I believe that I now know at least the direction that I need to take to do this kind of calculation. Many thanks! David. Here, Ex is another operator that applies once the expression has been reduced further, > Thanks, > > Jason -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org