0
$\begingroup$

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}, ... }
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – ciao Feb 16 '15 at 9:00
  • $\begingroup$ 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. $\endgroup$ – José D. Feb 16 '15 at 9:01
  • 1
    $\begingroup$ Of course it can. Nonetheless, a more concrete example? $\endgroup$ – ciao Feb 16 '15 at 9:03
  • $\begingroup$ @rasher I have updated the question. $\endgroup$ – José D. Feb 16 '15 at 9:09
  • $\begingroup$ So why do you claim you can't extend it? $\endgroup$ – Kuba Feb 16 '15 at 9:17
4
$\begingroup$

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...

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

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.

enter image description here

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

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.