Re: A simple question?
- To: mathgroup at smc.vnet.net
- Subject: [mg3651] Re: A simple question?
- From: lou (Lou D'Andria)
- Date: Sun, 31 Mar 1996 00:52:59 -0500
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
In article <4jd5ej$pmr at dragonfly.wolfram.com>, nader at math.chalmers.se (Nader Tajvidi) wrote: > I guess this must be a simple question, but I can't seem to >find a good solution to it. 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}] > > This simply means that the number of 1's in the index will >give the number of derivations with respect to x and the number of 2's >in the index will give the number of derivations with respect to y. In >this case there will be 5 different elements in fourthDs. For example, >the last element in the above list will be the same for >fourthDs[[2,1,1,1]], fourthDs[[1,1,2,1]] and so on. Nader, As usual with Mathematica, there are many, many different ways to do what you need. The most straight-forward, given your description in the last paragraph above, is this. Array[D[f[x,y],{x,Count[{##},1]},{y,Count[{##},2]}]&,{2,2,2,2}] There is at least one problem with this though. It calculates most of the derivatives more than once. In fact, D[f[x,y],{x,2},{y,2}] is calculated 6 different times! That's a bit wasteful. Here's a solution that calculates each derivative exactly once. Array[Plus,{2,2,2,2},0] /. Table[i->D[f[x,y],{x,4-i},{y,i}],{i,0,4}] The third argument to Array sets the "origin" of the array. So that instead of elements running from {1,1,1,1} through {2,2,2,2}, the elements will run from {0,0,0,0} to {1,1,1,1}. Here are some more methods which will work - almost one for every day of the week. ;-) I believe that each one uses a different idea, and all of them except the last one are wasteful in their calculation of derivatives. Enjoy! Array[D[f[x,y],##]&@@{x,y}[[{##}]]&,{2,2,2,2}] Array[D[f[x,y],{x,4-#},{y,#}]&[Plus[##]]&,{2,2,2,2},0] Array[Unevaluated[D[f[x,y],##]]/.{1->x,2->y}&,{2,2,2,2}] Table[D[f[x,y],{x,4-i-j-k-l},{y,i+j+k+l}], {i,0,1},{j,0,1},{k,0,1},{l,0,1}] Fold[ Map[Function[{z},Partition[z,#2,1]],#,{-3}]&, Table[D[f[x,y],{x,4-i},{y,i}],{i,0,4}], {4,3,2} ] Lou D'Andria ==== [MESSAGE SEPARATOR] ====