0
$\begingroup$

I have a multivariate function that I want to map into a triple of variables.

x = {0.01`, 0.11`, 0.21000000000000002`, 0.31000000000000005`, 
   0.41000000000000003`, 0.51`, 0.6100000000000001`, 
   0.7100000000000001`, 0.81`, 0.91`};
y = {1.`, 1.`, 1.`, 1.`, 1.`, 0.9804148800744802`, 
   0.8196730260718126`, 0.6189620070242443`, 0.40469250572443105`, 
   0.26901189545273474`};
z = {0.8153370522348216`, 0.8853249359665971`, 0.9223317847713025`, 
   0.9559380865485229`, 0.9999999995185327`, 1.`, 1.`, 
   0.9084506972387802`, 0.7345679056105409`, 0.5989011012000781`};
domain = Partition[Flatten[{x, y, z}], 3];

f[x_, y_, z_] := (x + 1.5*y + 1.2*z)/(1 + x);

Map[f, domain]

I want

{f[0.01, 0.11, 0.21], f[0.31, 0.41, 0.51], f[0.61, 0.71, 0.81], 
 f[0.91, 0.815337, 0.885325], f[0.922332, 0.955938, 1.], 
 f[1., 1., 0.908451], f[0.734568, 0.598901, 1.], f[1., 1., 1.], 
 f[1., 0.980415, 0.819673], f[0.618962, 0.404693, 0.269012]}

But for some reason when I Map[f, domain] I get:

{f[{0.01, 0.11, 0.21}], f[{0.31, 0.41, 0.51}], f[{0.61, 0.71, 0.81}], 
 f[{0.91, 0.815337, 0.885325}], f[{0.922332, 0.955938, 1.}], 
 f[{1., 1., 0.908451}], f[{0.734568, 0.598901, 1.}], f[{1., 1., 1.}], 
 f[{1., 0.980415, 0.819673}], f[{0.618962, 0.404693, 0.269012}]}

I have searched the documentation and other posts, but I still cannot figure what is going on with this very simple problem.

$\endgroup$
  • 1
    $\begingroup$ Either define f[{x_, y_, z_}]:=...., or do Apply[f, domain, 1] (in short-hand notation: f @@@ domain). $\endgroup$ – corey979 Feb 20 '18 at 17:24
  • $\begingroup$ Would say that looks like to form triplets as mydom=Thread[{x,y,z}] $\endgroup$ – Anxon Pués Feb 20 '18 at 17:30
  • 1
    $\begingroup$ Or this domain=Transpose@{x, y, z} $\endgroup$ – OkkesDulgerci Feb 20 '18 at 19:01
1
$\begingroup$

I tested this and work as , I think, you expect

    x = RandomInteger[{0, 9}, 10];
    y = RandomInteger[{0, 9}, 10];
    z = RandomInteger[{0, 9}, 10];
    mydom = Thread[{x, y, z}];
    Apply[f, mydom, 2]
$\endgroup$
  • $\begingroup$ Of course. Thank you so much, @AnxonPues. $\endgroup$ – Laura K Feb 20 '18 at 17:54
  • $\begingroup$ And also thank you for using the RandomInteger to create a better example than mine! $\endgroup$ – Laura K Feb 20 '18 at 18:51
1
$\begingroup$

It is simple. f @@@ domain is all you need.

f @@@ domain

{0.422772, 1.17328, 1.6441, 1.89005, 1.85, 1.49094, 1.27189, 1.81186, 1.85, 1.42995}

$\endgroup$
  • $\begingroup$ Of course! Thank you so much, @m_goldberg! $\endgroup$ – Laura K Feb 20 '18 at 18:50

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.