2
$\begingroup$

How do I evaluate, e.g. $3x+2y+z$, if I have $(x,y,z)$-values defined by a variable

a = {4, 5, 6}

I could do it with

expr = 3x + 2y + z;
expr /. {x->a[[1]], y->a[[2]], z->a[[3]]}

but there must be a smarter and shorter way to do it.

I tried ways like

expr /. {x, y, z} -> a
expr /. ({x, y, z} -> a)

but it was incorrect. TIA

$\endgroup$
3
  • 4
    $\begingroup$ You need Thread like this: 3 x + 2 y + z /. Thread[{x, y, z} -> a]. But even better would be to store your expression , linear in {x,y,z}, as the vector {3,2,1}. Then you could just do a dot product {3,2,1}.{4,5,6} $\endgroup$ – flinty Oct 4 '20 at 13:56
  • $\begingroup$ Or, if you are 'stuck' with expr (and you are as lazy as I am), you could get the dot product as follows: a.Coefficient[#,Variables@#]&[expr] => 28 $\endgroup$ – user1066 Oct 4 '20 at 17:50
  • $\begingroup$ Probably too late to get it marked as a dupe, but it probably should be, instead just closed. $\endgroup$ – Michael E2 Oct 4 '20 at 18:50

Browse other questions tagged or ask your own question.