Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function, explicit that takes dot product of two symbols, and replaces it with repeated dummy indices generated by Unique[].

explicit[expr_] := 
  expr //. {Dot[a_, b_] :> (Subscript[a, #] Subscript[b, #]) &[
  Unique[]]};

So that if my input is v.w, then the output is $v_{$3}w_{$3}$ which is good.

The problem is that if there is a single term multiplying two pairs of dot products, I don't know how to get Unique[] to generate a new symbol for each pair. That is, if I input c.d e.f the output is $c_{$3}d_{$3}e_{$3}f_{$3}$, which is bad. I want $c_{$3}d_{$3}e_{$4}f_{$4}$, with new repeated subscripts. How do I modify my code?

share|improve this question
    
By the way I don't think you need ReplaceRepated here but I left it in my answer since that wasn't the focus. –  Mr.Wizard Mar 8 '13 at 3:52

1 Answer 1

up vote 4 down vote accepted

You merely need to watch your operator precedence. Here with brackets to group the RHS properly:

explicit[expr_] := 
  expr //. {Dot[a_, b_] :> ((Subscript[a, #] Subscript[b, #]) &[Unique[]])};

explicit[c.d e.f] 

Mathematica graphics

share|improve this answer
    
How did you know that this was the way to solve the problem? Even with answers to my other questions on SE, I still don't seem to get it... it just seems like magic half the time, and I just move on... –  QuantumDot Mar 8 '13 at 5:34
    
@QuantumDot Well, I know that Unique[] needs to be evaluated for each occurrence of Dot. For that I need some head that holds it unevaluated. You had two in your original code: RuleDelayed and Function. Function is already being used to distribute a single instance of Unique[] to multiple expressions. That leaves RuleDelayed. (Which is exactly the right tool.) Now I must make sure that Unique[] is part of the RHS of RuleDelayed. I can use these methods to check how Mathematica "sees" this, or just my experience. :-) –  Mr.Wizard Mar 8 '13 at 5:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.