MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Modifying arguments of sub-parts of an expression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38879] Re: Modifying arguments of sub-parts of an expression
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Fri, 17 Jan 2003 05:38:34 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Gareth Russell had an expression such as 
   Gamma[a]*Gamma[b]/Beta[a,b]*Gamma[a+b]
and wanted to make a new expression where for every subexpression with head
Gamma, 
if the arguments include b, each b is replaced by b+1.  He wanted a function
that 
would make this change.  My solution is below.
-----------------------

In[1]:=
SubPosnQ[list1_,list2_]:=
  (Length[list2]<Length[list1])&&(Take[list1,Length[list2]]===list2)


In[2]:=
Modify[expr_]:=
  With[{ posn = Intersection[ Position[expr, b], Position[expr, _Gamma],
SameTest -> SubPosnQ ] },
     ReplacePart[ expr, b+1, posn ]
  ]

-----------------------
Example:

In[3]:=
   ex=(20*Gamma[a]^2*Gamma[1+a]^2*Gamma[3+b]^3*Gamma[4+b])/
      (Beta[a,b]^3*Gamma[3+a+b]*Gamma[5+a+b]);


In[4]:=
   Modify[ex]

Out[4]=
   (20*Gamma[a]^2*Gamma[1+a]^2*Gamma[4+b]^3*Gamma[5+b])
   (Beta[a,b]^3*Gamma[4+a+b]*Gamma[6+a+b])


------------------------
In the above solution I tried to make it concise and easy to read. 
If speed is more important you could make SubPosnQ a compiled function, 
or use a pure function instead of SubPosnQ.  You could also avoid using 
Intesection to make (posn) since it's rather slow when a non-default 
SameTest is used.

------------------------
Regards,
  Ted Ersek

Download my collection of Mathematica tricks from:  
http://www.verbeia.com/mathematica/tips/Tricks.html
and  
http://www.verbeia.com/mathematica/tips/GraphicsTricks.html




  • Prev by Date: Save as Special XHTML+MathML bug
  • Next by Date: Re: Circle Fit
  • Previous by thread: RE: Modifying arguments of sub-parts of an expression
  • Next by thread: How to convert a series of PICT frames to an animated GIF?