2
$\begingroup$

It is a really simple question, but I'm a bit stuck. I have a matrix

J = ( {
    {e - a y - 2 x e, -a x},
    {b e y, b e x - 1 - 2 y}
   } );

and a list of pairs

listS = {{0, 0}, {0, -1}, {1, 
    0}, {(e + a)/(e (1 + b a)), (e b - 1)/(
    b a + 1)}};

And I want to replace (x,y) in J by values of listS and to get a list of four matrix. The way I do it:

  J /. {x -> listS[[All, 1]], y -> listS[[All, 2]]}
  J /. {x,y}-> listS

doesn't satisfy me, because in first case I get the list of four elements with the same matrix indexes in each list element, but in the second case it replace nothing at all.

$\endgroup$
2
  • 2
    $\begingroup$ So what exactly is the desired output in this case? $\endgroup$
    – Mr.Wizard
    Commented May 24, 2017 at 16:16
  • 2
    $\begingroup$ Do you by any chance want J /. (Thread[{x, y} -> #] & /@ listS)? $\endgroup$
    – jjc385
    Commented May 24, 2017 at 16:25

2 Answers 2

4
$\begingroup$

The problem with your first approach is that it directly replaces x (and y) by a list. It seems like you rather want each of x and y replaced by a single value, and to return a list of such results. The way to do this is by supplying ReplaceAll (/.) with a list of lists of replacement rules, e.g.,

x /. {{x->1},{x->2},{x->3}}
{1, 2, 3}

In your case, you can achieve this using Threadand Map(/@) :

Thread[{x, y} -> #] & /@ listS
{{x -> 0, y -> 0}, {x -> 0, y -> -1}, {x -> 1, y -> 0}, {x -> (a + e)/((1 + a b) e), y -> (-1 + b e)/(1 + a b)}}

and then perform the replacement:

J /. (Thread[{x, y} -> #] & /@ listS)

A nice way to view the output is

MatrixForm /@ %

MatrixForm/@%

$\endgroup$
3
$\begingroup$

Does this give the output you seek? f[i_] := J /. {x -> listS[[i, 1]], y -> listS[[i, 2]]}; Table[f[i], {i, Length[listS]}]

$\endgroup$
1
  • $\begingroup$ It solves my problem too, but the solution of jjc385 does the same and is shorter. $\endgroup$ Commented May 24, 2017 at 16:42

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.