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);

[Maple Math]

> f(t);

[Maple Math]

Here it is with vnapply :

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

[Maple Math]

> f(t);

[Maple Math]

When the expression contains derivatives with respect to the variables, we don't want unapply to return a result such as [Maple Math] , 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);

[Maple Math]

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

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

[Maple Math]

Here it is with vnapply :

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

[Maple Math]

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);

[Maple Math]

> whattype(f);

[Maple Math]

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

> f(q+1);

[Maple Math]

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);

[Maple Math]

> evalhf(g(3));

[Maple Math]

See also: array , D , diff , evalhf , table , unapply , vnapply

Maple Advisor Database, R. Israel 1998