Streamlining Replace in Mathematica 8

  • Mathematica
  • Thread starter earth2
  • Start date
  • Tags
    Mathematica
In summary, the conversation discusses a problem with pattern matching and replacement in Mathematica. The individual is trying to replace expressions of the form (X*a)(X*b) with X^2(a*b), but is having trouble when a and b are equal. They also mention the use of Minkowski scalar products and how some of the arguments may be repeated. They are looking for a way to teach Mathematica to make this replacement automatically, but it may be difficult due to the structure of the function arguments. They also mention trying to force Mathematica to write powers as products, but this is not recommended as it goes against the automatic nature of the software.
  • #1
earth2
86
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
  • #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
 
  • #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:
  • #4
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:
  • #5
mars

Hello earth2mars,

Thank you for sharing your question about streamlining replace in Mathematica 8. It seems like you are trying to find a more efficient way to perform a certain type of replacement in your expressions. I think there may be a couple of possible approaches to this problem.

One option could be to use the "ReplaceAll" function with a pattern that includes both a and b as distinct variables, but also allows them to be equal. For example, you could try something like this:

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

This way, the pattern will match both cases where a and b are distinct, and where they are equal.

Another option could be to use the "ReplaceRepeated" function, which will continue to perform the replacement until the expression no longer changes. For example:

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

This way, the replacement will continue to be applied until it reaches the desired form, regardless of the values of a and b.

I hope this helps and provides some ideas for how to streamline your replacement process. Mathematica is a powerful tool for solving complex problems, and I'm sure you will be able to find a solution that works for your specific needs. Keep exploring and experimenting, and don't hesitate to reach out to the Mathematica community for more support and guidance. Best of luck in your research!
 

Related to Streamlining Replace in Mathematica 8

1. What is "Streamlining Replace" in Mathematica 8?

"Streamlining Replace" is a feature in Mathematica 8 that allows for efficient and simplified replacement of expressions in mathematical equations or code. It can be used to simplify complex expressions, perform pattern matching, and make code more readable.

2. How does "Streamlining Replace" work in Mathematica 8?

"Streamlining Replace" works by using a set of rules to find specific expressions in an equation or code and replace them with other expressions. These rules can be defined by the user or can be automatically generated by Mathematica based on the structure of the expression.

3. Can "Streamlining Replace" be used for pattern matching?

Yes, "Streamlining Replace" can be used for pattern matching by specifying a pattern in the rule to be replaced. This allows for more precise and targeted replacements in mathematical equations or code.

4. What are the benefits of using "Streamlining Replace" in Mathematica 8?

The main benefit of using "Streamlining Replace" in Mathematica 8 is that it allows for more efficient and simplified replacement of expressions. This can save time and effort when working with complex equations or code. Additionally, it can make the code more readable and easier to understand.

5. Are there any limitations to using "Streamlining Replace" in Mathematica 8?

One limitation of using "Streamlining Replace" in Mathematica 8 is that it may not work as intended if the expressions being replaced are too complex or if there are multiple possible matches for the specified rule. It is important to carefully define and test rules before using them for replacements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
0
Views
588
Back
Top