function which takes functions as arguments



On 10/3/07, U?ur G?ney <ugurguney at gmail.com> wrote:

> # How can I do this? What notation should I use in order to have functions
> in arguments (like in integrate() for example)?

Named or unnamed functions can be arguments.

foo (F) := [F (a), apply (F, [b]), map (F, [a, b, c])];
g (x) := sin (x);

foo (g);
 => [sin(a), sin(b), [sin(a), sin(b), sin(c)]]

foo (lambda ([x], sin (x)));
 => [sin(a), sin(b), [sin(a), sin(b), sin(c)]]

Expressions other than lambda expressions are not recognized
by Maxima as functions.

foo (sin (x));
 => error

For the record, integrate actually does not know what to do with
named functions or lambda expressions, only non-lambda expressions.

> And in the function body, how can I say "differentiate x1
> with respect to first argument of x1"

There is a positional derivative package named pdiff, but maybe
the following is enough for you to write your function find_jacobian.

pdiff1 (f, n) := block ([f_fundef, f_args, f_body],
  f_fundef : apply (fundef, [f]),
  f_args : args (lhs (f_fundef)),
  f_body : rhs (f_fundef),
  diff (f_body, f_args [n]));

F (x, y) := sin (x * y);
pdiff1 (F, 1);
 => y cos(x y)
pdiff1 (F, 2);
 => x cos(x y)

Note that there is already a jacobian function which expects
a list of expressions and a list of variables as its arguments,

jacobian ([sin (x * y), cos (x * y)], [x, y]);
 => matrix (<stuff>)

It appears that jacobian is not documented. Sorry, that's an oversight.

Hope this helps,

Robert Dodier