>> From: PierLuigi Zezza />
# Thanks for all suggestions. My final goal was to define first and second
# derivative of multivariate functions as functions, what I got is
# the following (still missing something on the second derivative)
> restart;
# Using list seems to be the best choice
> alpha:=[t-> sin(t^2),t -> t^2];
2 2
alpha := [t -> sin(t ), t -> t ]
# so that
> alpha(t);
2 2
[sin(t ), t ]
> D(alpha);
2
[t -> 2 cos(t ) t, t -> 2 t]
> D(alpha)(t);
2
[2 cos(t ) t, 2 t]
> D(alpha)(3);
[6 cos(9), 6]
# Or, may be less elegant,
> alpha:=map(unapply,vector([sin(t^2),t^2]),t);
[ 2 2]
alpha := [t -> sin(t ), t -> t ]
> D(alpha);
[ 2 ]
[t -> 2 cos(t ) t, t -> 2 t]
> D(alpha)(t);
[ 2 ]
[2 cos(t ) t, 2 t]
> D(alpha)(3);
[6 cos(9), 6]
# moreover if
> f:=(x,y,z)-> x^2*y-y^3*sin(z)+z^4;
2 3 4
f := (x, y, z) -> x y - y sin(z) + z
# then you can define
> Df:=[seq(D[i](f),i=1..3)];
2 2
Df := [(x, y, z) -> 2 x y, (x, y, z) -> x - 3 y sin(z),
3 3
(x, y, z) -> -y cos(z) + 4 z ]
# so that
> Df(x,y,z);
2 2 3 3
[2 x y, x - 3 y sin(z), -y cos(z) + 4 z ]
> Df(1,2,3);
[4, 1 - 12 sin(3), -8 cos(3) + 108]
# or in general
> D1:=proc(f) local x,n,k,var: n:=nops({op(1,eval(f))});
> if n=1 then D(f) else
> [seq(D[h](f),h=1..n)] fi; end;
D1 := proc(f)
local x, n, k, var;
n := nops({op(1, eval(f))});
if n = 1 then D(f) else [seq(D[h](f), h = 1 .. n)] end if
end proc
# so that
> D1(f);
2 2
[(x, y, z) -> 2 x y, (x, y, z) -> x - 3 y sin(z),
3 3
(x, y, z) -> -y cos(z) + 4 z ]
> D1(f)(2,3,a);
3
[12, 4 - 27 sin(a), -27 cos(a) + 4 a ]
# For the second derivative the solution is not completely satisfactory.
# If you define
> D2:=proc(f)local n: n:=nops({op(1,eval(f))}); if n=1 then (D@@2)(f)
> else
> map(unapply,convert([D[1](D1(f)),D[2](D1(f)),D[3](D1(f))](op(1,eval(f)
> )),matrix),op(1,eval(f))) end if end;
D2 := proc(f)
local n;
n := nops({op(1, eval(f))});
if n = 1 then (D@@2)(f)
else map(unapply, convert(
[D[1](D1(f)), D[2](D1(f)), D[3](D1(f))](
op(1, eval(f))), matrix), op(1, eval(f)))
end if
end proc
# then
> D2(f)(x,y,z);
[2 y 2 x 0 ]
[ ]
[ 2 ]
[2 x -6 y sin(z) -3 y cos(z) ]
[ ]
[ 2 3 2]
[ 0 -3 y cos(z) y sin(z) + 12 z ]
> D2(f)(1,a,3);
[2 a 2 0 ]
[ ]
[ 2 ]
[ 2 -6 a sin(3) -3 a cos(3) ]
[ ]
[ 2 3 ]
[ 0 -3 a cos(3) a sin(3) + 108]
> D2(sin)(x);
-sin(x)
# but
> D2(f);
[?[1, 1] ?[1, 2] 0 ]
[ ]
[?[2, 1] ?[2, 2] ?[2, 3]]
[ ]
[ 0 ?[3, 2] ?[3, 3]]
# I do not understand what's happening so I cannot fix it
# If you do not want a matrix as an output but only a list this works
# fine
> DD2:=proc(f)local n: n:=nops({op(1,eval(f))}); if n=1 then (D@@2)(f)
> else
> [D[1](D1(f)),D[2](D1(f)),D[3](D1(f))] end if end;
DD2 := proc(f)
local n;
n := nops({op(1, eval(f))});
if n = 1 then (D@@2)(f)
else [D[1](D1(f)), D[2](D1(f)), D[3](D1(f))]
end if
end proc
# and
> DD2(f);
[[(x, y, z) -> 2 y, (x, y, z) -> 2 x, 0], [(x, y, z) -> 2 x,
2
(x, y, z) -> -6 y sin(z), (x, y, z) -> -3 y cos(z)], [
2 3 2
0, (x, y, z) -> -3 y cos(z), (x, y, z) -> y sin(z) + 12 z ]
]
> DD2(f)(x,y,z);
2
[[2 y, 2 x, 0], [2 x, -6 y sin(z), -3 y cos(z)],
2 3 2
[0, -3 y cos(z), y sin(z) + 12 z ]]
> DD2(f)(1,2,3);
[[4, 2, 0], [2, -12 sin(3), -12 cos(3)],
[0, -12 cos(3), 8 sin(3) + 108]]
>
|