10
$\begingroup$

I have list of pure functions (All functions are InterpolatingFunction) i.e

{{a, b}, {c, d}, {e, f}, ...}

and I would like to end up with

{ (a[#]/b[#])&, (c[#]/d[#])&,(e[#]/f[#])&,...}

the closest I have got is to do

(Divide @@ Through[#[x]]) & /@ {{a, b}, {c, d}, {e, f}}
{a[x]/b[x], c[x]/d[x], e[x]/f[x]}

but these are not pure functions.

$\endgroup$
11
$\begingroup$

This perhaps:

Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}}
(* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *)

Mr.Wizard's way of writing it (see comment) looks like this in the frontend:

frontend

$\endgroup$
  • 5
    $\begingroup$ Can also be written: ({x, y} \[Function] x[#]/y[#] &) @@@ {{a, b}, {c, d}, {e, f}} $\endgroup$ – Mr.Wizard Oct 21 '14 at 17:23
  • 1
    $\begingroup$ @Mr.Wizard Thanks, I hadn't seen \[Function] before! I added an image to the answer so everyone can see how it looks there. $\endgroup$ – C. E. Oct 21 '14 at 17:34
7
$\begingroup$

You can almost always turn to replacement patterns when you need to transform expressions:

Cases[
  {{a, b}, {c, d}, {e, f}},
  {x_, y_} :> (x[#]/y[#] &)
]
{a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &}

Cases defaults to levelspec {1} so this is safer than using /..

$\endgroup$
4
$\begingroup$

Also:

With[{a = #1, b = #2}, a[#]/b[#] &] & @@@ {{a, b}, {c, d}, {e, f}}

or

x[#]/y[#] & /. {x -> #1, y -> #2} & @@@ {{a, b}, {c, d}, {e, f}}

(* {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *)
$\endgroup$

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.