2
$\begingroup$

I tried to search around but could not find a good solution.

What I want to do is simple, for example, if at the beginning I define the three variables as:

vectora    = {ax, ay, az}
vectorb    = {bx, by, bz}
scalara    = Norm[vectora]
directiona = vectora/scalara

Then I do the operation:

Cross[vectorb, directiona]

Mathematica will displace it as

{(az by)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2] 
- (ay bz)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2],
-((az bx)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2])
+ (ax bz)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2], 
  (ay bx)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2]
- (ax by)/Sqrt[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2]}

I want it to be as concise as possible:

{(az by)/scalara - (ay bz)/scalara, -(ax bx)/scalara + (ax bz)/scalara, (ay bx)/scalara - (ax by)/scalara}

Which function should I use?

Thank you all for your help.

$\endgroup$
0

3 Answers 3

2
$\begingroup$

There are many ways I suppose one could do this. The problem is that you have already assign to scalara variable. So each time M sees scalara it will replace it by it value.

So if you want to assign scalara and still do what you want, one way is to clear it before the end.

vectora={ax,ay,az};
vectorb={bx,by,bz};
scalara=Norm[vectora];
directiona=vectora/scalara;
expr=Cross[vectorb,directiona]

Mathematica graphics

Now

expr/. 1/scalara :> {Clear[scalara]; 1/scalara} 

Gives what you want

Mathematica graphics

But this clear the variable scalara so you have to reassign it again from the start. But if you do not want to lose the definition of scalara each time, just replace the above with

expr/. 1/scalara -> {tmp=scalara;Clear[scalara]; 1/scalara} 
scalara=tmp;

This restores scalara to its old value right away.

$\endgroup$
1
$\begingroup$

There could be better answers but here is what I found. Basically we look at the FullForm of the expression and do the replacement.

vectora = {ax, ay, az}
vectorb = {bx, by, bz}
scalara = Norm[vectora]
directiona = vectora/scalara
crs = Cross[vectorb, directiona]

This comes from the FullForm of the expression of scalara

replace = Power[Abs[ax]^2 + Abs[ay]^2 + Abs[az]^2, Rational[-1, 2]]

crs /. replace -> 1/sca

(*  {(az by)/sca - (ay bz)/sca, -((az bx)/sca) + (ax bz)/sca, (ay bx)/
  sca - (ax by)/sca}  *
$\endgroup$
1
$\begingroup$
expr /. Power[_, Rational[-1, _] ]:> 1 / Defer[scalara]
FullSimplify[expr scalara] / Defer[scalara]

both give

{(az by - ay bz)/scalara, (-az bx + ax bz)/scalara, (ay bx - ax by)/scalara}

Both work if Defer is replaced with HoldForm.

$\endgroup$

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.