Interpolation function



On Tue, Dec 13, 2005 at 10:09:11AM +1300, Glenn Ramsey wrote:
> Albert Reiner wrote:
> >[Daniel Lakeland , Fri, 9 Dec 2005 14:34:47 
> >-0800]:
> >
> >>I was disappointed when I tried to do something like this and found
> >>that maxima uses dynamic scope instead of building a closure. I was
> >>looking to write a function which takes data as an argument and
> >>returns a function that does the interpolation, this is a standard
> >>trick in LISP, but without closures it was not trivial.
> >
> 
> >I find the following idiom to be easy to use and reliable:
> >
> >    (%i1) make_inc(x) := buildq([x], lambda([y], x+y)) $
> >    
> >    (%i2) f : make_inc(2+3);
> >    
> >    (%o2)                         LAMBDA([y], 5 + y)
> >    (%i3) f(3);
> >    
> >    (%o3)                                  8
> 
> I don't get this, can someone explain how the above relates to creating 
> a function to interpolate from a set of data pairs?


Imagine that you want to write a function which works like this:

myfunc : splineinterpolate(xes,yes);

myfunc(newx); /* gives the interpolated value based on the xes and yes
that were passed to splineinterpolate*/

In other words what splineinterpolate should return is a function that
knows how to compute interpolated numbers. The first thought would be
something like this.

splineinterpolate(xes,yes) := lambda([x], dosomeinterpolationstuff(xes,yes));

but now, myfunc will be a function which refers to variables named
"xes" and "yes". Because maxima is dynamically scoped, those values
will be determined at call time. You have no idea what's going to go on.

instead you want the xes and yes to be remembered privately inside
"myfunc". This is what common lisp does by default, as does scheme and
most modern lisp dialects. The suggestion was to use maxima's pattern
matching substitution to do the substitution inside splineinterpolate
and therefore get the effect of remembering the values that were
passed in.

splineinterpolate(xes,yes) :=
buildq([xes,yes], lambda([x],dosomeinterpolationstuff(xes,yes)));


to see what would happen consider the following:

----------


(%i1) xes:[1,2,3];

(%o1) 				   [1, 2, 3]
(%i2) yes:[2,4,6];

(%o2) 				   [2, 4, 6]
(%i3) buildq([xes,yes],lambda([x],dointerp(xes,yes)));

(%o3) 		  lambda([x], dointerp([1, 2, 3], [2, 4, 6]))

(%i4) lambda([x],dointerp(xes,yes));

(%o4) 		        lambda([x], dointerp(xes, yes))
(%i5) 



-- 
Daniel Lakeland
dlakelan@street-artists.org
http://www.street-artists.org/~dlakelan