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...

> How I can store functions in lists and evaluate them ?

Martin, here is a magic incantation which yields the hoped-for result, I think:

load (orthopoly);
make_polynomial_functions (n) :=
   makelist (buildq ([e], lambda ([x], e)), e, makelist (legendre_p
(i, x), i, 1, n));

L : make_polynomial_functions (3);
  => [lambda([x],x), lambda([x],3*(1-x)^2/2-3*(1-x)+1),
  lambda([x],-5*(1-x)^3/2+15*(1-x)^2/2-6*(1-x)+1)]

L[2](3); => 13
plot2d (L, [x, -1, 1]); => nice plot

Notes. (1) I find it easier to use map and makelist instead of
explicit for-loops. I try to construct an initial list and then modify
it into the desired result by applying one or more functions to
each element. In the example above, the inner makelist creates
a list of polynomial expressions, then the outer makelist pastes
the expressions into lambda expressions (nameless functions).
(2) lambda does not evaluate its arguments, so to evaluate
the function body (e in the example) requires some extra work.
buildq evaluates e and pastes that value into the lambda.

There are other ways to achieve the same result.
Hope this helps.

Robert Dodier