Joachim De Beule | 13 May 15:22
Picon
Favicon

second order functions?

Hi all,

I was wondering how to define a second order function in maxima, i.e. a 
function returning a function.

More concrete: I want to define a function make_fn(n) which should return a 
function fn(x), where the way in which fn(x) is computed depends on the 
initially provided n.

I tried it as follows:

make_fn(n) := lambda([x],n+x)$
fn3: make_fn(3)

but this evaluates to lambda([x], x + n), whereas I want it to evaluate to 
lambda([x], x + 3)

Thanks, Joachim.
Barton Willis | 13 May 15:42
Favicon

Re: second order functions?

maxima-bounces <at> math.utexas.edu wrote on 05/13/2008 08:26:13 AM:

> More concrete: I want to define a function make_fn(n) which should 
return a 
> function fn(x), where the way in which fn(x) is computed depends on the 
> initially provided n.
> 
> I tried it as follows:
> 
> make_fn(n) := lambda([x],n+x)$

Try using funmake; here is an example:

  (%i1) make_fn(n) := funmake('lambda, [[x], n+x])$
  (%i2) make_fn(3)(w);
  (%o2) w+3

For info on funmake, enter "? funmake" on a command line.

Barton
Stavros Macrakis | 13 May 16:32
Picon
Favicon

Re: second order functions?

On Tue, May 13, 2008 at 9:26 AM, Joachim De Beule
<joachim <at> arti.vub.ac.be> wrote:
> Hi all,
>
>  I was wondering how to define a second order function in maxima, i.e. a
>  function returning a function.

There are several ways.  Unfortunately, depending on lexical scope is
not one of them, since Maxima doesn't support lexical scope.

As Barton says, you can use funmake.  Another way is to use "subst":

        subst([val=n], lambda([x],val+x) )

This has the advantage/disadvantage of specifying exactly which
variables you want to substitute.

What exactly are you trying to accomplish using second-order
functions?  Though they're certainly a powerful and appropriate
technique for many applications, often in Maxima it is easier to use
expressions than functions.

           -s
Joachim De Beule | 13 May 17:13
Picon
Favicon

Re: second order functions?

On Tuesday 13 May 2008 16:32:52 you wrote:
> [...]
>
> What exactly are you trying to accomplish using second-order
> functions?  Though they're certainly a powerful and appropriate
> technique for many applications, often in Maxima it is easier to use
> expressions than functions.
>

Maybe I should first say that I am a lisp programmer and new to maxima, so it 
could well be that I am abusing maxima by trying to use it like lisp or 
sometrhing. (By the way, is there a good reference on how to use lisp in 
maxima? I can't seem to find one.)

What I am trying to do is the following. I want to run a number of 
simulations. The number of variables and hence ode's per simulation depends 
on a certain parameter n; and given n the set of ode's can easily be 
generated. That is what I want to do as opposed to each time having to type 
the set of ode's myself for different values of n.

J.
Robert Dodier | 13 May 21:06
Picon

Re: second order functions?

On Tue, May 13, 2008 at 9:13 AM, Joachim De Beule
<joachim <at> arti.vub.ac.be> wrote:

>  Maybe I should first say that I am a lisp programmer and new to maxima, so it
>  could well be that I am abusing maxima by trying to use it like lisp or
>  sometrhing. (By the way, is there a good reference on how to use lisp in
>  maxima? I can't seem to find one.)

The code = data paradigm carries over from Maxima into Lisp,
so you should be fairly comfortable, but there are various
departures from Lisp, notably the lack of lexical scope.
Also Maxima is not exactly a Lisp-1 nor a Lisp-2.
Sorry for the confusion.

About Lisp & Maxima, there is section in the reference manual
about that; ?? lisp at the input prompt should find it.
See also "Minimal Maxima" at: http://maxima.sourceforge.net/documentation.html
Also: http://maxima.sourceforge.net/wiki/index.php/Maxima%20internals

Good luck, & welcome to Maxima.

Robert Dodier
Barton Willis | 13 May 17:52
Favicon

Re: second order functions?

maxima-bounces <at> math.utexas.edu wrote on 05/13/2008 09:32:52 AM:

> 
> As Barton says, you can use funmake.  Another way is to use "subst":
> 
>         subst([val=n], lambda([x],val+x) )

You do need to be careful with such things:

(%i9)  f1(val) := funmake('lambda, [[x], val+x])$
(%i10) f2(val) := funmake('lambda,[['x], val + 'x])$
(%i11) f3(n) := subst([val=n], lambda([x], val+x))$
(%i12) f4(n) := subst(['val=n], lambda([x], val+x))$
(%i13) val : 7$
(%i14) [f1(val), f2(val), f3(val), f4(val)];
(%o14) [lambda([42],49),lambda([x],x+7),lambda([x],val+x),lambda([x],x+7)]

(%i15) x : 42$
(%i16) [f1(val), f2(val), f3(val), f4(val)];
(%o16) [lambda([42],49),lambda([x],x+7),lambda([x],val+x),lambda([x],x+7)]

There is also variants that use buildq.

Barton
Robert Dodier | 13 May 21:00
Picon

Re: second order functions?

On Tue, May 13, 2008 at 9:52 AM, Barton Willis <willisb <at> unk.edu> wrote:

>  There is also variants that use buildq.

Yeah --- I prefer buildq because the result is more predictable
(everything is quoted except designated variables).
buildq is essentially a reimplementation of `(,foo) macrology.

In this case, maybe s.t. like

foo(n) := buildq ([n], lambda ([x], n + x));

foo(7);
 => lambda([x], 7 + x)

HTH

Robert Dodier

Gmane