Re: Combining pure functions to produce a new pure function

• To: mathgroup at smc.vnet.net
• Subject: [mg89726] Re: Combining pure functions to produce a new pure function
• From: dh <dh at metrohm.ch>
• Date: Thu, 19 Jun 2008 05:42:17 -0400 (EDT)
• References: <g3aomo\$3o\$1@smc.vnet.net>

```
Hi Mac,

if you call your function forwardmodel with a symbolic parameter it will

return an expression with an unevaluated variable biomass. This is

because the statement If[b<0,...] did not evaluate. To fix this you must

write:

biomass=If[b<3,3,b];

But I think your approach is much too complicated. If you want to

combine two functions, you can do this on the fly, e.g.:

f1[x_]=x;

f2[x_]=x^2

f12={f1[#],f2[#]}&

THis will not have any problems with unevaluated variables because f1

and f2 are only evaluated when f12 is used. But if for efficiancy you

want to get the expression for f1 and f2 into f12 you could say:

f12=Evaluate[{f1[#],f2[#]}]&

hope this helps, Daniel

Malcolm Davidson wrote:

> Hello Daniel,

>

> I'm not quite sure I understood although I understand that this is the

> model giving the problems. The variable biomass is used internally in

> the code inside an if statement (see below) which checks provides a

> lower limit to biomass.

>

> forwardmodel["letoan", b_] := Module[{\[Gamma]1, \[Gamma]2, biomass},

>    If[b < 3, biomass = 3, biomass = b];

>    \[Gamma]1 = -32 Exp[-Log[10, biomass]] - 10 // N;

>    \[Gamma]2 = (Log[10, biomass] - 4.5563)/0.18;

>    If[\[Gamma]1 <= -15, 10 Log[10, 10^(\[Gamma]1/10) Sin[27 Degree]],

>     10 Log[10, 10^(\[Gamma]2/10) Sin[27 Degree]]]];

>

> Does this help ? How would I change the code to make it work ?

>

> Thanks so much

>

> Malcolm

>

> 2008/6/18 dh <dh at metrohm.ch>:

>> Hi Mac,

>> look at Out[66]: there seems to be a variable biomass\$491 without a

value,

>> that anticipates the evaluation of the result. If I define the function

>> e.g.:

>> forwardmodel[x1_,x2_]:=x2;

>> h[forwardmodel["letoan",#]&,forwardmodel["saatchiphv",#]&][100]

>> everything works as expected.

>> hope this helps, Daniel

>>

>> Mac wrote:

>>> I have a function where one of the arguments is supposed to be a list

>>> of pure functions. Tyhis is useful for an algorithm that can run using

>>> multiple input functions. Based on a previous post some time ago

>>>

>>>

>>>

>>>

>>> 0d169d8992b4

>>>

>>> a method was proposed to address this. If I apply this method to list

>>> arguments I get the following code and results:

>>>

>>> In[60]:= h[f1_, f2_] :=

>>>  Function[x,

>>>  Evaluate[{f1[x], f2[x]} /.

>>>    HoldPattern[Function[a_, b_]] -> Function[a, b][x]]]

>>>

>>> Combine two simple pure functions - works as expected

>>>

>>> In[58]:= h[# + 1 &, # + 2 &]

>>> %[1]

>>>

>>> Out[58]= Function[x\$, {1 + x\$, 2 + x\$}]

>>>

>>> Out[59]= {2, 3}

>>>

>>> Combine two custom functions and I get the internal details of one

>>> function and with the second one evaluated properly

>>>

>>> In[66]:= h[forwardmodel["letoan", #] &, forwardmodel["saatchiphv", #]

>>> &]

>>> %[100]

>>>

>>> Out[66]= Function[x\$, {If[-10. - 32./biomass\$491^0.434294 <= -15,

>>>   10 Log[10, 10^(\[Gamma]1\$491/10) Sin[27 =B0]],

>>>   10 Log[10, 10^(\[Gamma]2\$491/10) Sin[27 =B0]]],

>>>  If[x\$ > 2, -35.01 + 4.296 Log[x\$], -35.01 + 4.296 Log[2]]}]

>>>

>>> Out[67]= {If[-10. - 32./biomass\$491^0.434294 <= -15,

>>>  10 Log[10, 10^(\[Gamma]1\$491/10) Sin[27 =B0]],

>>>  10 Log[10, 10^(\[Gamma]2\$491/10) Sin[27 =B0]]], -15.2262}

>>>

>>> I can see that this effect is due to the evaluations of the function

>>>

>>>  forwardmodel["letoan", #] &

>>>

>>> so that the output no longer depends on the pure function variable \$x.

>>> Note that separately these functions work as expected. Here some

>>>

>>> In[69]:= forwardmodel["letoan", #] &[100]

>>> forwardmodel["saatchiphv", #] &[100]

>>>

>>> Out[69]= -17.6312

>>>

>>> Out[70]= -15.2262

>>>

>>> Can annybody suggest a general solution ?

>>>

>>> Many thanks

>>>

>>> Mac

>>>

>>>

>>>

>>>

>>

>> --

>>

>> Daniel Huber

>> Metrohm Ltd.

>> Oberdorfstr. 68

>> CH-9100 Herisau

>> Tel. +41 71 353 8585

--

Daniel Huber

Metrohm Ltd.

Oberdorfstr. 68

CH-9100 Herisau

Tel. +41 71 353 8585, Fax +41 71 353 8907

E-Mail:<mailto:dh at metrohm.com>

Internet:<http://www.metrohm.com>

```

• Prev by Date: Re: Problems with FindRoot and recursive functions
• Next by Date: Re: Resolve a function
• Previous by thread: Re: Combining pure functions to produce a new pure function
• Next by thread: Manipulate[] data input