Re: A simple question?
- To: mathgroup at smc.vnet.net
- Subject: [mg3654] Re: A simple question?
- From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
- Date: Sun, 31 Mar 1996 00:53:30 -0500
- Sender: owner-wri-mathgroup at wolfram.com
In article <4jd5ej$pmr at dragonfly.wolfram.com>, nader at math.chalmers.se (Nader Tajvidi) writes: > For a function f[x,y] I want to create a > 2x2x2x2 matrix called fourthDs. The elements of fourthDs will be the > fourth derivatives of f with respect to x and y in the following form: > fourthDs[[1,1,1,1]]=D[f[x,y],{x,4}] > fourthDs[[2,2,2,2]]=D[f[x,y],{y,4}] > fourthDs[[1,2,1,1]]=D[f[x,y],{x,3},{y,1}] For a function f with arguments vars, and maxord = maximum order of derivative needed in the matrix, the function dmatrix will create such a matrix. The function G stores its values for unique argument lists: G[F_, vars__][args__] /; OrderedQ[{args}] := G[F, vars][args] = D[F[vars], args] G[z__][args__] := G[z] @@ Sort[{args}] dmatrix[f_Symbol, vars__Symbol, maxord_Integer] := Outer[ G[f, vars], Sequence @@ Table[{vars}, {maxord}] ] For example: In[2]:= f1[x_, y_] := x^4 y^3 In[3]:= mat1 = dmatrix[f1, x, y, 4] 3 2 2 2 2 2 2 3 Out[3]= {{{{24 y , 72 x y }, {72 x y , 72 x y}}, {{72 x y , 72 x y}, {72 x y, 24 x }}}, 2 2 2 3 2 3 3 {{{72 x y , 72 x y}, {72 x y, 24 x }}, {{72 x y, 24 x }, {24 x , 0}}}} The storage by G is 5 elements: In[4]:= ?G Global`G G[f1, x, y][x, x, x, x] = 24*y^3 G[f1, x, y][x, x, x, y] = 72*x*y^2 G[f1, x, y][x, x, y, y] = 72*x^2*y G[f1, x, y][x, y, y, y] = 24*x^3 G[f1, x, y][y, y, y, y] = 0 G[F_, vars__][args__] /; OrderedQ[{args}] := G[F, vars][args] = D[F[vars], args] G[z__][args__] := Apply[G[z], Sort[{args}]] In[4]:= mat1[[1, 2, 1, 1]] == D[f1[x, y], {x, 3}, {y, 1}] Out[4]= True In[5]:= f2[x_, y_, z_] := Sin[x y z] In[6]:= mat2 = dmatrix[f2, x, y, z, 5]; In[7]:= Dimensions[mat2] Out[7]= {3, 3, 3, 3, 3} In[8]:= mat2[[2, 1, 3, 1, 2]] == D[f2[x, y, z], {x, 2}, {y, 2}, {z, 1}] Out[8]= True The matrix mat2 in this example has 243 elements but the required storage by G is 21. It should be possible to use G directly. If it is called with different arrangements of arguments, G will not have to recalculate beyond the maximum 21 functions needed: In[9]:= G[f2, x, y, z][y, x, z, x, x] 2 2 2 4 4 3 3 Out[9]= -9 y z Cos[x y z] + x y z Cos[x y z] + 7 x y z Sin[x y z] -- ___________________________________________________________________________________ Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering ==== [MESSAGE SEPARATOR] ====