MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A simple question?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3653] Re: A simple question?
  • From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
  • Date: Sun, 31 Mar 1996 00:53:20 -0500
  • Organization: University of Colorado, Boulder
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4jino9$c4a at dragonfly.wolfram.com>,
Lou D'Andria <lou at wolfram.com> wrote:
>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.

    << several clever solutions snipped >>

Rather than wracking one's brains trying to come up with a clever functional
way around this problem, why not just use dynamic programming.  First,
create a function of four indices that computes the required derivative
and memorizes the value.  Then, pass *that* function to Array.

    Clear[scratch]
    scratch[i_,j_,k_,l_] := scratch[i,j,k,l] =
	With[{nx = Count[{i,j,k,l},1]}, D[f[x,y], {x,nx}, {y,4-nx}] ]

    Array[scratch, {2,2,2,2}]

Also note that this solution (and every other that I have seen) will
break if x or y has a value (f[x,y] will evaluate before the derivative
is taken).  You can avoid this problem by using temporary variables to
take the derivative, and then substituting the real variables:

    scratch[i_,j_,k_,l_] := scratch[i,j,k,l] =
	Module[{xx, yy},
	    With[{nx = Count[{i,j,k,l},1]},
		D[f[xx,yy], {xx,nx}, {yy,4-nx}] /. {xx->x, yy->y} ]
	]

but here is a really elegant way to do it without introducing temporary
variables for differentiation:

    scratch[i_,j_,k_,l_] := scratch[i,j,k,l] =
	With[{nx = Count[{i,j,k,l},1]}, Derivative[nx,4-nx][f][x,y] ]

Remember to Clear[scratch] and redefine it each time you want to use it.


		Dave Wagner
		Principia Consulting
		(303) 786-8371
		dbwagner at princon.com
		http://www.princon.com/princon

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Creating and Opening Files on a Mac...
  • Next by Date: Re: substitue expression for function f as argument in mma
  • Previous by thread: Re: A simple question?
  • Next by thread: Font changes on plots??