Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Streamlining Replace in Mathematica 8

  1. May 31, 2012 #1
    Hi guys,

    i have expressions of the type (X*a)(X*b). I want replace this by X^2(a*b).

    So i tried building a block which does nothing but

    %//.(X*a_)(X*b_)->X^2(a*b).

    However, this works only if a is distinct from b. If a and b are equal if HAVE to use the replacement command

    %//.(X*a_)(X*a_)->X^2(a*a).

    Can I tweak the replace command s.t. Mathematica does the replacement even if a is equal to b, too, without having to write a new replacement command? That would be cool :)

    Cheers,
    earth2
     
  2. jcsd
  3. May 31, 2012 #2
    What you see displayed in a notebook is very often different from what the internal form of the expression actually is. Pattern matching ALWAYS works on the internal form and NOT the displayed form.

    Looking at the FullForm of an expression that doesn't pattern match shows you why the pattern match and what you need to match. (But that only works for simple pattern problems. For really hard pattern matching problems I have no advice for you.)

    In[1]:= FullForm[(X*a)(X*b)]

    Out[1]= Times[a,b,Power[X,2]]

    This matches your pattern, believe it or not.

    In[2]:= FullForm[(X*a)(X*a)]

    Out[2]= Times[Power[a,2],Power[X,2]]

    And that looks nothing like your pattern, which is why it doesn't match.

    A less problematic method for this particular example that avoids all the problems with writing patterns

    In[1]:= Collect[(X*a)(X*b),X]

    Out[1]= a b X^2

    In[2]:= Collect[(X*a)(X*a),X]

    Out[2]= a^2 X^2
     
  4. May 31, 2012 #3
    Hi and thanks for your answer. Ok, i just realize i have been too vague.

    Acutally my expressions are of the type

    MP[X,a_1]...MP[X,a_{2n}] where MP is the Minkowski scalar product. Some of the a_n might be the some. This can be replaced in my case (under an integral over X) by

    X^{2n}*Symmetrized Product(MP[a_1,a_2]MP[a_2,a_3]...MP[a_{n-1},a_n]).

    By hand this replacement can be easily done but how can i teach it to Mathematica? As i've said my goal is to have just one replacement rule which also takes into account if some of the a_n are not distinct.

    Cheers.

    EDIT: What would help me tremendously is a way to force Mathematica to write powers as products, i.e a^n as a*a*....*a (n times). :)
     
    Last edited: May 31, 2012
  5. May 31, 2012 #4
    We routinely see "how do I do this?" followed by "do this" followed by "I didn't actually want to do that, how about this?"

    Post the simplest small notebook with the handful of examples that would solve all your problems if they could be done. Include what you have and exactly what you want the replacement to be that you have done by hand. Please don't include any typos.

    Perhaps someone should create a really well written "READ ME" for the Math & Science Software section that encourages posters to do that. It might help streamline the process.

    Reaching inside function arguments to make pattern substitutions raises the level of difficulty, sometimes substantially, but we can see what happens. Since Mathematica is an "infinite evaluation" system and evaluates things "all the way to the bottom" it is sometimes difficult to stop function evaluation until a pattern substitution can be done. It might be easier if your arguments could just be contained in a list. Then the substitutions could be done and finally the result then handed off to MP, but I don't know the specifics of your problem.

    General rule: Almost nobody successfully forces Mathematica to do anything. Every time you want to subvert and thwart what Mathematica wants to do you are waging an uphill battle. Trying to convince Mathematica to not do all the automatic things it does is probably going to lead you to a life of grief and failure. Get a FORTRAN compiler and write your own code and you will probably be happier.
     
    Last edited: May 31, 2012
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Streamlining Replace in Mathematica 8
  1. In mathematica 8 (Replies: 5)

Loading...