6
$\begingroup$

As an example, consider the three vectors:

{b1, b2, b3}, {h1, h2, h3}, {yc1, yc2, yc3}.

How do I get b1*h1*yc1 + b2*h2*yc2 + b3*h3*yc3 ?

I mean to get the sum of the products of the corresponding components of all vectors.

$\endgroup$
2
  • 7
    $\begingroup$ Total[{b1, b2, b3} {h1, h2, h3} {yc1, yc2, yc3}] $\endgroup$
    – MarcoB
    Commented Apr 30, 2020 at 17:19
  • $\begingroup$ Nice thank you very much! I just got on Mathematica recently. $\endgroup$ Commented Apr 30, 2020 at 17:22

5 Answers 5

7
$\begingroup$
a = {b1, b2, b3};
b = {h1, h2, h3};
c = {yc1, yc2, yc3};    
(a b).c

(* b1 h1 yc1 + b2 h2 yc2 + b3 h3 yc3 *)
$\endgroup$
6
$\begingroup$
a = {b1, b2, b3};
b = {h1, h2, h3};
c = {y1, y2, y3};

Plus @@ Times @@ {a, b, c}

b1 h1 y1 + b2 h2 y2 + b3 h3 y3

$\endgroup$
4
$\begingroup$

Variations using *Thread functions:

a = {b1, b2, b3};
b = {h1, h2, h3};
c = {y1, y2, y3};

Total@MapThread[Times, {a, b, c}]

Thread[Times[a, b, c]] // Total

or

Using Fold:

Plus @@ Fold[Times, {a, b, c}]

Result:

b1 h1 yc1 + b2 h2 yc2 + b3 h3 yc3

$\endgroup$
2
$\begingroup$
a = {b1, b2, b3};
b = {h1, h2, h3};
c = {y1, y2, y3};

Using Outer:

Total@Nest[Diagonal, Outer[Times, a, b, c], 2]

(*b1 h1 y1 + b2 h2 y2 + b3 h3 y3*)
$\endgroup$
2
$\begingroup$
☺ = +## & @@ (1 ##) &;


☺[{b1, b2, b3}, {h1, h2, h3}, {yc1, yc2, yc3}]
b1 h1 yc1 + b2 h2 yc2 + b3 h3 yc3
$\endgroup$
0

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.