# ReplaceAll with serveral variables. get all combinations

How can achieve something like this?

a * b /. {a -> {1, 2, 3}, b -> {10, 100}}


I want {10,100,20,200,30,300} or something like that (I don't care the order, or sublist) to be returned. What is the right syntaxis?

In my real problem, I have a more complex expresion

ComputeMean[S0_, r_, s_, T_, NPaths_] :=
Sum[f[S0 Exp[(r - 1/2 s^2) T  +
s Sqrt[T] RandomVariate@NormalDistribution[0, 1]]], {i, 1,
NPaths}] /NPaths

ComputePrice[S0_, r_, s_, T_, NPaths_] :=
Exp[-r T] ComputeMean[s0, r, s, T, NPaths]

ComputePrice[S0,r,s,T,NPaths] /. { S0 -> {values for S0}, r -> {values for r}, ... }

• Join @@ Outer[Times, {1, 2, 3}, {10, 100}] is one way to get the result you appear to be after. The replace you are doing is correct, but its result is invalid - you cant multiply lists with differing dimensions. – ciao Feb 16 '15 at 9:00
• That was just a simple example. In the real problem, I have a complex expresion and 6 variables. I cannot extend your solution to my real problem, I think. – José D. Feb 16 '15 at 9:01
• Of course it can. Nonetheless, a more concrete example? – ciao Feb 16 '15 at 9:03
• @rasher I have updated the question. – José D. Feb 16 '15 at 9:09
• So why do you claim you can't extend it? – Kuba Feb 16 '15 at 9:17

As I said in the comment, it's not the replace that's the problem, it's that you create an invalid expression as a result. Here's one way to do this kind of thing, many others...

eq = a + b^c - 2 d/e;

vars ={a,b,c,d,e};

alist = {1, 2, 3};
blist = {2, 4, 6};
clist = {3, 5, 7};
dlist = {1, 2};
elist = {-1, -2, 2};

eq /. Map[Rule @@@ Transpose[{vars, #}] &, Tuples[{alist, blist, clist, dlist, elist}]];

%//Short

(*

{11,10,8,13,11,7,35,34,32,37<<142>>,7778,7783,7781,7777,279941,279940,279938,279943,279941,279937}

*)


The entries in the result correspond to the tuples' order. You can get the replacements corresponding to the result like so:

sets = Map[Rule @@@ Transpose[{vars, #}] &, Tuples[{alist, blist, clist, dlist, elist}]];
sets // Short

(*
{{a->1,b->2,c->3,d->1,e->-1},{a->1,b->2,c->3,d->1,e->-2},<<159>>,{a->3,b->6,c->7,d->2,e->2}}
*)


And of course, you can arrange/sort/whatever the tuples to prearrange the output order.

The use of Outer, as in comment, is also still a possibility...

f[a_, b_, c_, d_, e_] := a + b^c - 2 d/e
Flatten[Outer[f, alist, blist, clist, dlist, elist]]


With same result as above...

• I'd restrict Flatten in case the result from f is a list :) +1. – Kuba Feb 16 '15 at 9:32
• @rasher +1 nice to "see" you around...I have been away myself :) – ubpdqn Feb 16 '15 at 9:48
• @Kuba True, but if fn returns list of lists, or.... so kind of up to user. – ciao Feb 16 '15 at 20:47
• @ubpdqn Back at you! Life has vacillated between bouts of research and bouts of leisure. In the latter mode right now... – ciao Feb 16 '15 at 20:48

You can do:

Times @@@ Tuples[{{1, 2, 3}, {10, 100}}]


or

Times @@@ Tuples@({a, b} /. {a -> {1, 2, 3}, b -> {10, 100}})


The issue is multiplication not replacement.

For the more general case:

f @@@ Tuples[lst]


e.g.

• In my real problem I don't have just a Times operation, but a more complex expresion. I have updated the question. – José D. Feb 16 '15 at 9:09