Mathematica : Noncommutative Multiply and Replacement Rules

In summary, with the Val function, the code can be rewritten to order the noncommutative multiplication of GH[a].GH[b].GH[c].GH[d].GH[b] in increasing character value. The rule in the second replacement does not apply because it is evaluating the position right before the last two, and once that is evaluated, it does not re-evaluate it. Using the Sort function, the code can also be simplified to achieve the same result.
  • #1
Hepth
Gold Member
464
40
Code:
Val[a_] := ToCharacterCode[ToString[a]][[1]];
GH[a].GH[b].GH[c].GH[d].GH[b] //. {GH[a_].GH[b_] :> If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]]}

I have the noncommutative multiply of GH[a].GH.GH[c].GH[d].GH

My goal is to order these in increasing charcter value. (Val is the function to convert to basically ascii code).

Shouldn't this work? And give GH[a].GH.GH.GH[c].GH[d] at the end?


Notice also that
Code:
GH[a].GH[b].GH[c].GH[d].GH[b]

GH[a].GH[b].GH[c].GH[d].GH[b] //. {
(A_).GH[a_].GH[b_].(B_) :> 
   A.If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]].B,
  (A_).GH[a_].GH[b_] :> 
   A.If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]],
  GH[a_].GH[b_].(B_) :> 
   If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]].B,
  GH[a_].GH[b_] :> If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]]}


GH[a].GH[b].GH[c].GH[d].GH[b] //. {
  (A_).GH[a_].GH[b_] :> A.If[Val[a] > Val[b], GH[b].GH[a], GH[a].GH[b]]
  }

Even though the rule in the 2nd one exists in the replacements of the first, it doesn't apply. I think the problem comes in the fact that its applying a rule to the position right before the last two, and once that is evaluated, does not re-evaluate it, even though no real action was taken. Though I guess if you consider it replacing itself an action, then that's what it did.

Any ideas? Even if its a different method. This is something that I'm trying to write in a much more complicated way, and I need to figure out how these work.

Thanks!
 
Physics news on Phys.org
  • #2
Code:
In[1]:= Val[a_] := ToCharacterCode[ToString[a]][[1]]; 
GH[a].GH[b].GH[c].GH[d].GH[b] //. Dot[h___, GH[a_], GH[b_], t___] /;
   Val[a]>Val[b]:>Dot[h,GH[b],GH[a],t]

Out[1]= GH[a].GH[b].GH[b].GH[c].GH[d]

Or

Code:
In[2]:= Sort[GH[a].GH[b].GH[c].GH[d].GH[b]]

Out[2]= GH[a].GH[b].GH[b].GH[c].GH[d]
 
Last edited:

FAQ: Mathematica : Noncommutative Multiply and Replacement Rules

1. What is a noncommutative multiplication in Mathematica?

A noncommutative multiplication in Mathematica is a type of multiplication where the order of the operands matters. This means that the result of multiplying A and B is not always the same as multiplying B and A.

2. How do I perform noncommutative multiplication in Mathematica?

To perform noncommutative multiplication in Mathematica, you can use the NonCommutativeMultiply function, represented by **. For example, A ** B represents the noncommutative multiplication of A and B.

3. What are replacement rules in Mathematica?

Replacement rules in Mathematica are a way to transform expressions by replacing certain patterns with other expressions. These rules are represented by -> and can be used to replace specific terms or variables within an expression.

4. How do I use replacement rules in Mathematica?

To use replacement rules in Mathematica, you can use the ReplaceAll function, represented by /. This function takes in an expression and a set of replacement rules and returns the transformed expression. For example, expr /. {x -> 2, y -> 3} will replace all instances of x with 2 and y with 3 in the expression expr.

5. Can I combine noncommutative multiplication and replacement rules in Mathematica?

Yes, you can combine noncommutative multiplication and replacement rules in Mathematica to perform more complex calculations and transformations. You can use the NonCommutativeMultiply function and ReplaceAll function together to perform noncommutative multiplication and then replace certain terms or variables with specific values or expressions.

Similar threads

Replies
4
Views
2K
Replies
4
Views
2K
Replies
15
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
3
Views
704
Replies
1
Views
1K
Replies
1
Views
2K
2
Replies
52
Views
12K
Back
Top