0
$\begingroup$

Given an expression in x, y, z (for example), and a list with three elements, like {1,2,3}, how can I do the equivalent of expr/.{x->1,y->2,z->3}? So for example suppose

a = {1,2,3}
x^2+y^2+z^2 /.{x->1,y->2,z->3}

but obviously x^2+y^2+z^2/.{x,y,z}->a does not work.

$\endgroup$
2
$\begingroup$

You can do this:

a = {1, 2, 3}
x^2 + y^2 + z^2 /. Rule @@@ Transpose[{{x, y, z}, a}]

You might also use MapThread and the like. But anyways, make sure you understand what @@@ does.

$\endgroup$
1
  • $\begingroup$ Thanks. Beyond your suggestion to understand @@@, I also just learned that -> is really infix shorthand for Rule[]. $\endgroup$ – rogerl May 4 '13 at 1:43
6
$\begingroup$

This can be found in many answers, but I can't find the same question on this site or in the documentation. So:

x^2+y^2+z^2 /.Thread[{x, y, z} -> a]
  (* 14 *)
$\endgroup$

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.