6
$\begingroup$

If yy and zz are 2x2 Hermitian matrices, is there a way that I can mark them (with a property?) as Hermitian so that Mathematica can assume that it can factor out and simplify scalar multipliers from a dot product expression? In this example, we have -1 * -1 as the multiplier:

ClearAll[a, yy, zz]
a = -(-yy.zz).zz
FullForm[a]

This gives:

-(-yy.zz).zz
Times[-1,Dot[Times[-1,Dot[yy,zz]],zz]]

Can it be made to simplify to just:

yy.zz.zz
$\endgroup$
1
  • $\begingroup$ Dave, just a gentle reminder that, if one of the answers provided below solve your problem, you might want to accept it by clicking on the gray check mark next to it. $\endgroup$
    – MarcoB
    Jul 27, 2015 at 19:44

3 Answers 3

6
$\begingroup$

To factor out numeric factors in any argument of Dot:

(2 yy.(3 zz)).(4 zz) //. Dot[a___, d_?NumericQ b_, c___] :> d Dot[a, b, c]

24 yy.zz.zz


Edit: If you want this to happen automatically, you can add the rule as a new definition for Dot:

Unprotect[Dot];
Dot[a___, d_?NumericQ b_, c___] := d Dot[a, b, c]
Protect[Dot];

Now the factoring happens by itself:

(2 yy.(3 zz)).(4 zz)

24 yy.zz.zz

$\endgroup$
5
  • $\begingroup$ Thanks; this is a nice and straightforward replacement that will do the job. I must say, though, that I was hoping that Mathematica could just "figure it out". $\endgroup$
    – David B
    Jul 27, 2015 at 20:28
  • $\begingroup$ @daveboden You can make it automatic, if you like -- see the edit. $\endgroup$ Jul 27, 2015 at 21:21
  • $\begingroup$ Please, try your code for this string J.Transpose[(-P)].x $\endgroup$
    – dtn
    Sep 9, 2022 at 4:21
  • $\begingroup$ And for this: J.Transpose[1].x, J.Transpose[-1].x and J.Transpose[-P].(-1) $\endgroup$
    – dtn
    Sep 9, 2022 at 5:27
  • $\begingroup$ @dtn, I see you have asked your question under multiple answers but I think it is unclear to people exactly what you mean. In your first problem, do you want Transpose[1] to return the identity, such that J.Transpose[1].x reduces to J.x? I think Mathematica can't do that because it doesn't know the dimensions of J and x so that it can't return identity matrix of proper dimensions. At least as far as I know Mathematica can't do that. Perhaps a Mathematica export can comment on this. $\endgroup$
    – Alwin
    Sep 28, 2022 at 12:50
4
$\begingroup$

Is the following sufficiently general?

t[e_] := e /. Dot[Times[z1_ /;!ArrayQ[z1], Dot[z2__]], z3__] :> z1 Dot[z2, z3]    
Simplify[a, TransformationFunctions -> {Automatic, t}]
(* yy.zz.zz *)
$\endgroup$
3
  • $\begingroup$ Please, try your code for this string J.Transpose[(-P)].x $\endgroup$
    – dtn
    Sep 9, 2022 at 4:22
  • $\begingroup$ @dtn, What are the three quantities in your expression? $\endgroup$
    – bbgodfrey
    Sep 9, 2022 at 4:56
  • $\begingroup$ J,Transpose[-P] and x $\endgroup$
    – dtn
    Sep 9, 2022 at 5:00
3
$\begingroup$

Easier and more generally applicable is to use TensorExpand:

In[]: -(-yy.zz).zz//TensorExpand
Out[]: yy.zz.zz
$\endgroup$
2
  • $\begingroup$ Your code work for this string J.Transpose[(-P)].x, but how to return the standard notation for transpose? $\endgroup$
    – dtn
    Sep 9, 2022 at 4:22
  • $\begingroup$ Is your question that J.Transpose[(-P)].x//TensorExpand returns -J.Transpose[P, {2, 1}].x but you want J.Transpose[P].x? Besides what it would mean mathematically a way to get this output is to use a replacement rule in the following way (J.Transpose[(-P)].x//TensorExpand)//Transpose[a_, {2, 1}] :> Transpose[a]. For a better understanding of what the {2,1} means you can lookup the documentation of Transpose or have a look at this post. $\endgroup$
    – Alwin
    Sep 28, 2022 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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