API proposal: derivative by list

42 views
Skip to first unread message

Francesco Bonazzi

unread,
Nov 27, 2017, 12:24:15 PM11/27/17
to sympy
Hi, I think we could support a simple notation for array derivatives.

For example, if would be nice if the Gradient and Hessian could be easily calculated as:

In [5]: f(x, y, z).diff([[x, y, z]])
Out[5]:
⎡∂                                        
⎢──(f(x, y, z))  ──(f(x, y, z))  ──(f(x, y, z))⎥
⎣∂x              y              z            

In [6]: f(x, y, z).diff([[x, y, z], 2])
Out[6]:
  2                  2                  2            
                                                   
───(f(x, y, z))   ─────(f(x, y, z))  ─────(f(x, y, z))⎥
  2               y x              z x            
x                                                    
                                                     
  2                  2                  2            
                                                   
⎢─────(f(x, y, z))   ───(f(x, y, z))   ─────(f(x, y, z))⎥
⎢∂y x                 2               z y            
                   y                                
                                                     
  2                  2                  2            
                                                   
⎢─────(f(x, y, z))  ─────(f(x, y, z))   ───(f(x, y, z))
⎢∂z x              z y                 2            
                                      z              

That is, if the argument being derived wrt is a nested list, interpret the derivative as an array derivative.

This behaviour would then be similar to Wolfram Mathematica's one: http://reference.wolfram.com/language/ref/D.html

Currently, it's possible to calculate the array derivative provided that the deriving factor is an Array object.

Anyone objecting to introducing this API addition?

Aaron Meurer

unread,
Nov 27, 2017, 3:53:08 PM11/27/17
to sy...@googlegroups.com
If this is a generalization (same meaning) as the new matrix
derivative stuff, then I think it's good. I think it would be better
to explicitly require a Matrix object, although lists could
potentially be useful for easier input. Assumedly the output would be
a Matrix (a list output would be less useful for further SymPy
operations).

Also, please consider the proposed syntax at
https://github.com/sympy/sympy/issues/5575 (diff(f(x), (x, n)) to mean
d^n/dx^n f(x), for n symbolic) when looking at any new APIs for
diff().

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/0fd53e7c-7ff1-4abc-8c5b-5ec101bca7d1%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Francesco Bonazzi

unread,
Nov 30, 2017, 3:49:16 AM11/30/17
to sympy


On Monday, 27 November 2017 21:53:08 UTC+1, Aaron Meurer wrote:
I think it would be better to explicitly require a Matrix object, although lists could
potentially be useful for easier input.

Matrix is always a two dimensioanl objects, even if they are row- or column- matrices.
 
Assumedly the output would be a Matrix (a list output would be less useful for further SymPy
operations).

The output is an Array, an the dimension may vary. Matrices always have 2 dimensions.

Francesco Bonazzi

unread,
Nov 30, 2017, 11:43:56 AM11/30/17
to sympy
Reply all
Reply to author
Forward
0 new messages