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$
0

3 Answers 3

12
$\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$
2
  • 6
    $\begingroup$ Can also be written: ({x, y} \[Function] x[#]/y[#] &) @@@ {{a, b}, {c, d}, {e, f}} $\endgroup$ Commented Oct 21, 2014 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$ Commented Oct 21, 2014 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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.