Mathematica Streamlining Replace in Mathematica 8

  • Thread starter Thread starter earth2
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around creating a single replacement rule in Mathematica to transform expressions of the form (X*a)(X*b) into X^2(a*b), even when a and b are not distinct. Users highlight that pattern matching in Mathematica operates on the internal representation of expressions, which can complicate replacements. A suggested alternative is to use the Collect function to achieve the desired result without complex pattern matching. The conversation also touches on the challenges of forcing Mathematica to represent powers as products and the difficulties of manipulating function arguments for pattern substitutions. Ultimately, users are encouraged to provide clear examples to facilitate better assistance.
earth2
Messages
82
Reaction score
0
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
 
Physics news on Phys.org
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
 
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:
earth2 said:
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.

earth2 said:
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.
 
Last edited:

Similar threads

Replies
1
Views
1K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
2
Views
3K
Replies
1
Views
1K
Back
Top