On Jan 13, 2009, at 11:12 PM, Jason Grout wrote: > I've been working on trac #3941 trying to figure out a nice syntax for > computing the total (matrix) derivative of a multivariable function > (e.g., the Jacobian matrix and the Hessian matrix). Relevant > discussion > for the current diff behavior is at > > http://groups.google.com/group/sage-devel/browse_thread/thread/ > 67ab8c7eef8c3f96/eccc976c767b2a25 > > (and thanks to David Harvey for his massive work on the diff > function). > > Currently, if a list or tuple is passed in to the diff function, as in > diff(f,(x,y)), it is treated exactly as diff(f,x,y) is treated. I > propose that a list or tuple of variables instead is handled as a > variable and the function is threaded over the list (and furthermore, > that diff is first threaded over the function if the function > happens to > be a list or tuple). This lets us do things like the following. > > First, define mydiff to be: > > def mydiff(f,*args): > if isinstance(f, (list, tuple)): > return [mydiff(component,*args) for component in f] > else: > if isinstance(args[0], (list, tuple)): > if len(args)==1: > return [mydiff(f,variable) for variable in args[0]] > else: > return mydiff([mydiff(f,variable) for variable in > args[0]], *args[1:]) > else: > return sage.all.diff(f,*args) > > > sage: g(x,y)=function('g',x,y) > sage: mydiff(g(x,y),(x,y)) # Compute the Jacobian matrix > [diff(g(x, y), x, 1), diff(g(x, y), y, 1)] > sage: mydiff(g(x,y),(x,y), (x,y)) # Compute the Hessian matrix > [[diff(g(x, y), x, 2), diff(g(x, y), x, 1, y, 1)], [diff(g(x, y), > x, 1, > y, 1), diff(g(x, y), y, 2)]] > > The following won't work, but should compute the Hessian matrix as > well. > The problem is in the diff parsing function. > > mydiff(g(x,y), (x,y), 2) > > > Mathematica has these commands implemented, but requires an extra > level > of braces to access the functions. The above commands in > Mathematica are: > > In[1]:= D[g[x, y], {{x, y}}] > > (1,0) (0,1) > Out[1]= {g [x, y], g [x, y]} > > In[2]:= D[g[x, y], {{x, y}}, {{x, y}}] > > (2,0) (1,1) (1,1) (0,2) > Out[2]= {{g [x, y], g [x, y]}, {g [x, y], g [x, y]}} > > In[3]:= D[g[x, y], {{x, y}, 2}] > > (2,0) (1,1) (1,1) (0,2) > Out[3]= {{g [x, y], g [x, y]}, {g [x, y], g [x, y]}} > > Thoughts? Comments?
If I understand right, you want diff(g, vars) to return [diff(g, v) for v in vars]. This does seem a bit odd to me, especially as the list-comprehension notation is so simple, but I can see it'd be useful for iterating. One thing I would add to your proposal that an actual matrix/vector be returned, instead of a list of lists that is supposed to be interpreted as such. - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---