Bug fix: Problems with unapply

The function unapply , which constructs a procedure from an expression, has three major shortcomings, listed below. To correct them, I have written the alternative procedure vnapply .

unapply does not work with arrays (including matrices and vectors) or tables.

> f:=unapply(vector([1,x,x^2]), x);

> f(t);

Here it is with vnapply :

f:= vnapply(vector([1,x,x^2]), x);

> f(t);

When the expression contains derivatives with respect to the variables, we don't want unapply to return a result such as , because that wouldn't work unless x is a name of a variable. What we do want is to take the derivative of g , and then return the function that evaluates this at its argument. This can be expressed using the D operator.

> unapply(diff(g(x),x), x);

Unfortunately, unapply fails to do this in slightly more complicated circumstances.

> unapply(2*diff(g(x),x), x);

Here it is with vnapply :

> vnapply(2*diff(g(x),x), x);

unapply has a rather strange feature: if the expression is an integer power, then what is returned is the power of a procedure rather than a procedure.

> f:= unapply((x+1)^2,x);

> whattype(f);

In most cases this behaviour is harmless: applying f to any argument v will result in , as it should.

> f(q+1);

However, one place it will not work is in the evalhf environment:

> evalhf(f(3));

Error, unable to evaluate function `f` in evalhf

Here it is using vnapply :

> g:= vnapply((x+1)^2,x);

> evalhf(g(3));

Maple Advisor Database, R. Israel 1998