Re: More universal way of writing gradient

*To*: mathgroup at smc.vnet.net*Subject*: [mg112925] Re: More universal way of writing gradient*From*: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>*Date*: Wed, 6 Oct 2010 03:15:20 -0400 (EDT)*References*: <i8erhh$hv6$1@smc.vnet.net>

Wouldn't mapping at the deepest level work? foo[u_] := Map[bar, u, {-1}] or in your example: grad[u_] := Map[{Derivative[1, 0][#], Derivative[0, 1][#]} &, u, {-1}] Cheers -- Sjoerd On Oct 5, 11:35 am, Sam Takoy <sam.ta... at yahoo.com> wrote: > Hi, > > My question is not related to the gradient at all, but rather strictly > the grammar of Mathematica. Gradient is just an example. > > My question is: what's the elegant way to write the following function > so that it applies to single functions as well as ("rectangular") lists > of functions? > > grad[u_] := {Derivative[1, 0][u], Derivative[0, 1][u]} > gradList[u_] := {Map[Derivative[1, 0], u, {2}], > Map[Derivative[0, 1], u, {Length[Dimensions[u]]}]} > > f[x_, y_] := Sin[x] Exp[y] > Through[grad[f][x, y]] // MatrixForm > gradList[{{f, f}, {f, f}}] // > Map[Apply[#, {x, y}] &, #, {Length[Dimensions[#]]}] & // MatrixFor= m > > I'm sure I could wrap grad and gradList into a function with an If, but > I'm sure there is a more natural way. > > Thank you in advance, > > Sam > > PS: Using Map[Apply[]] in the second case because Through doesn't seem > to work with Lists of Lists. This is the subject of an earlier post...