Streamlining Replace in Mathematica 8

  • Context: Mathematica 
  • Thread starter Thread starter earth2
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the challenges of using replacement rules in Mathematica, specifically for expressions involving products of terms with a common factor. Participants explore how to create a single replacement rule that can handle cases where terms may or may not be distinct, particularly in the context of Minkowski scalar products.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks a way to replace expressions of the form (X*a)(X*b) with X^2(a*b) while accommodating cases where a equals b without needing separate rules.
  • Another participant notes that pattern matching in Mathematica operates on the internal form of expressions, which can differ from their displayed form, and provides examples illustrating this point.
  • A participant suggests using the Collect function as an alternative method to achieve the desired result without complex pattern matching.
  • Further clarification reveals that the expressions in question are Minkowski scalar products, and the participant expresses a desire for a single replacement rule that accounts for potential duplicates among the terms.
  • There is a suggestion that restructuring the arguments into a list might simplify the pattern substitution process, although the specifics of the problem remain unclear.
  • One participant warns that attempting to force Mathematica to behave in a certain way often leads to frustration, suggesting that it may be more effective to use traditional programming methods instead.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to create a replacement rule that accommodates both distinct and non-distinct terms. Multiple competing views and methods are presented, and the discussion remains unresolved.

Contextual Notes

Participants express limitations in their current approaches, including the complexity of pattern matching and the challenges of forcing Mathematica to represent powers as products. There is also mention of the need for clearer examples to facilitate understanding and problem-solving.

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 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K