New Reply

Streamlining Replace in Mathematica 8

 
Share Thread Thread Tools
May31-12, 07:16 AM   #1
 

Streamlining Replace in Mathematica 8


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
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Galaxies fed by funnels of fuel
>> The better to see you with: Scientists build record-setting metamaterial flat lens
>> Google eyes emerging markets networks
May31-12, 09:43 AM   #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
May31-12, 10:01 AM   #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). :)
May31-12, 11:20 AM   #4
 

Streamlining Replace in Mathematica 8


Quote by earth2 View Post
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.
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.

Quote by earth2 View Post
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). :)
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.
New Reply
Thread Tools


Similar Threads for: Streamlining Replace in Mathematica 8
Thread Forum Replies
Mathematica Pattern matching and replace repeated Math & Science Software 2
Identify and replace LED Electrical Engineering 3
What will replace oil? Engineering, Comp Sci, & Technology Homework 6
Streamlining the alphabet. General Discussion 29
Alternative way to replace CFC? General Engineering 5