2
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ By the way I don't think you need ReplaceRepated here but I left it in my answer since that wasn't the focus. $\endgroup$ – Mr.Wizard Mar 8 '13 at 3:52
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – QuantumDot Mar 8 '13 at 5:34
  • $\begingroup$ @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. :-) $\endgroup$ – Mr.Wizard Mar 8 '13 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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