I am running some calculations with vectors (lists) and in order to perform them, they need to be flattened. Is there a better or neater way to do this?

ReactionPower = Flatten[RF1].Flatten[RF1vel] + Flatten[RF2].Flatten[RF2vel]; 

All the variables are of the form:

RF1 = {{x},{y}}

x and y are not integers, but long expressions with trigonometric functions.

  • $\begingroup$ What do RF1, RF1vel, RF2, and RF2vel look like before Flatten[]-ing? $\endgroup$
    – J. M.'s torpor
    Apr 15 '17 at 0:56
  • $\begingroup$ @J.M. All are of the form {{ }, { }} $\endgroup$ Apr 15 '17 at 0:59
  • $\begingroup$ Ah, so ReactionPower is a scalar? $\endgroup$
    – J. M.'s torpor
    Apr 15 '17 at 1:01
  • $\begingroup$ @J.M. Correct and the variables on the RHS are x and y components $\endgroup$ Apr 15 '17 at 1:04
  • 1
    $\begingroup$ Maybe a route that is "better ... than using multiple Flatten[]s..." can be had if you modify the process generating those lists to be Flatten[]-ed. $\endgroup$
    – J. M.'s torpor
    Apr 15 '17 at 1:06


rf1 = {{x1}, {y1}};
rf2 = {{x2}, {y2}};
rf1vel = {{vx1}, {vy1}};
rf2vel = {{vx2}, {vy2}};

Here is a way using Total:

reactionPower = Total[rf1*rf1vel + rf2*rf2vel, 2]
(* vx1 x1 + vx2 x2 + vy1 y1 + vy2 y2 *)

reactionPower === Flatten[rf1].Flatten[rf1vel] + Flatten[rf2].Flatten[rf2vel]
(* True *)

This will certainly be less efficient, but is slightly shorter:

ReactionPower = Tr[RF1.Transpose[RF1vel] + RF2.Transpose[RF2vel]];

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.