list of functions



On 6/16/07, Martin DRUON <martin.druon at wanadoo.fr> wrote:
>
> I would like to create a list of Legendre polynomials... so, I tried this
> :
>
> list_of_poly(degree):=(
>   load(orthopoly), ...
>

Well, first of all, you should move the "load(orthopoly)" out of the
function; otherwise it re-loads the package *each time* you call the
function.

but I can't evaluate polynomials :
>
> l:list_of_poly(3)
> l[2](5)


If you look at l, you will see that it consists of a list of *expressions*,
not of functions.  Maxima has no way of knowing that when you apply the
expression (x+y) to 3 that you want x (and not y) to take the value 3.

There are two ways to deal with this.  Either you convert the results to
functions -- probably anonymous ones using lambda (see below) -- or you
evaluate the expressions at a given point using subst:

       subst(x=5,l[2]) => 5

I would recommend working with expressions rather than functions, but it is
your choice.

      l:cons(lambda([x], legendre_p(i,x)),l)
>

If you construct the function this way, the evaluation of legendre_p(i,x)
will be deferred until you call the function. Since Maxima has dynamic scope
(not static scope), it will use the value of i at the time you call the
function.  You can deal with this writing instead

       l:cons(  subst(i,'i, lambda([x], legendre_p(i,x))), l)

In this case, the legendre polynomial will be calculated at the time you
call the function, not at the time you run list_of_poly.  If you want to
calculate the polynomial at the time you run list_of_poly, use

      l:cons(   funmake('lambda,cons([x], legendre_p(i,x))), l )
or
                      subst(q=legendre_p(i,x),lambda([x],q))

Also, instead of cons'ing and reversing yourself, you might want to use the
makelist function:

         makelist( legendre_p(i,x), i, 0, degree)

replaces the body of your program.

Hope this helps,

            -s