7
$\begingroup$

Suppose I have a list of functions:

functions = {f, g, h, ...}

And all of those functions are in form of f[x_ , y_]. For example:

f[x_, y_] = x + y;
g[x_, y_] = x^3 + y^2 + 2x;
functions={f,g};
(*Pass x and y for eval?*)
functions[2, 3] (*- Does not work :( *)
Print[MatrixForm[functions[2,3]]] (*The expected result would be a vector
of calculated function values...*)

Is there any easy way to pass arguments to the list of functions, so that they all will be evaluated?

$\endgroup$

3 Answers 3

14
$\begingroup$
f[x_, y_] = x + y;
g[x_, y_] = x^3 + y^2 + 2 x;
functions = {f, g};

Using Map

#[x, y] & /@ functions

(* {x + y, 2 x + x^3 + y^2} *)

Using Through

Through[functions[x, y]] *)

(* {x + y, 2 x + x^3 + y^2}
$\endgroup$
1
$\begingroup$
f[x_, y_] = x + y;
g[x_, y_] = x^3 + y^2 + 2 x;
functions = {f, g};

Using ComapApply (new in 14.0)

ComapApply[functions] @ {x, y}

{x + y, 2 x + x^3 + y^2}

$\endgroup$
1
$\begingroup$
f[x_, y_] = x + y;
g[x_, y_] = x^3 + y^2 + 2  x;
functions = {f, g};

Using Thread:

h = Activate[#1 @@@ #2 & @@@ Thread[{#1, Inactive[#2]}]] &;

h[functions, {x, y}]

(*{x + y, 2 x + x^3 + y^2}*)
$\endgroup$

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.