*To*: mathgroup@smc.vnet.net*Subject*: [mg11866] Re: Altering terms of an Expression*From*: "C. Woll" <carlw@u.washington.edu>*Date*: Fri, 3 Apr 1998 03:45:26 -0500*Organization*: University of Washington*References*: <6fsjtm$gpn@smc.vnet.net>

Hi, How about the following. 1) If your terms are being added together, why don't you just do Do[ answer=Expand[gm[[k,l]]*f[k]*f[l]];, {k,1,n},{l,k+1,n}] To answer you specific question about Length, you could try creating your own function SRlength[a_Plus]:=Length[a] SRlength[_]:=1 2,3) I think the following code does what you ask. Define a function g which takes two arguments, the index and the string, with the following properties g /: g[n_,"newstring"] g[n_,a_] := g[n,a] g /: g[n_,a_] g[n_,b_] := g[n, a<>b] You could also use format to have the output appear the way you wanted, that is Format[g[n_,a_]] := a[n] Then, define a fill variable fill = Product[g[n,"newstring"],{n,4}] where you can replace 4 with the maximum allowed index. Now, g[3,"astring"] g[1,"bstring"] g[1,"cstring"] fill produces bstringcstring[1] newstring[2] astring[3] newstring[4] Internally, the output is represented as g[1,"bstringcstring"] g[2,"newstring"] g[3,"astring"] g[4,"newstring"] Is this what you were looking for? Carl Woll Dept. of Physics U. of Washington On 1 Apr 1998, S. Rempe wrote: > Hi, > There are 3 things I want to do with an expression. > The expression may have 1 or more terms added together, each > term is composed of a product of elements. > > > 1). I want to multiply each term in the expression by a product of > functions. For example, the expression labelled by integers k,l > might look like > > gm[[k,l]]= a*b*c > > I want it to become > > gm[[k,l]]= a*b*c*f[k]*f[l] > > > I have written a Do loop that doesn't work because I wish that > Length would output the number of terms added in the expression > (1 term in above expression), but Length gives 3 for the expression > above. > > Do[{ > answer=gm[[k,l]][[i]]*f[k]*f[l]; > },{k,1,n},{l,k+1,n},{i,1,Length[gm[[k,l]]]} ]; > > > This works fine when the expression contains 2 or more terms. > How do I make it work (automatically) even in the case of 1 > term in the expression? > > 2). I want to order each term in the expression by the index. > For example, say the expression is in alphabetical order, > > astring[3]*bstring[1] > > I want it ordered numerically > > bstring[1]*astring[3] > > > 3). Then in each term of the expression, I want to combine > the strings that refer to the same index and multiply in > a new string if the index is missing. > > For example, the expression in numerical order from the > last step might be, > > bstring[1]*cstring[1]*astring[3] > > I want it to become > > bstringcstring[1]*newstring[2]*astring[3] > > > To summarize: > > In Step 1 Length[expression] gives the number of terms > summed together in expression except when there is only 1 term. > I need to know how to get around that. > > The result of Step 3, merging strings with the same index, is the > other thing I want. Numerical ordering of the indices is not > required in the result, I just thought it might make Step 3 > easier to accomplish. > > Thanks. > > Susan Rempe > rempe@u.washington.edu > > >