Search code examples
pythonarraysnumpyderivative

Partial derivative of a function with numpy.array input


I have a function like:

def fun(A, B, C):
    return numpy.sum(numpy.dot(A, B)) + numpy.sum(C)

Where A, B, C are 2-dimensional numpy.array, and the return value is a float value

How can I get the partial derivative of fun(A,B,C) with regard to A, B, or c? (and the partial derivatives will also be numpy.array)

I'm ready to use libaries like numpy and scipy, but not symbolic libraries.


Solution

  • For numerical differentiation, you can use numdifftools.

    import numpy as np
    import numdifftools
    
    def fun(A, B, C):
        return numpy.sum(numpy.dot(A, B)) + numpy.sum(C)
    
    def fun_A(A, B, C):
        J = numdifftools.Jacobian(lambda z: fun(z.reshape(A.shape), B, C).ravel())
        return J(A.ravel()).reshape(A.shape)
    
    
    np.random.seed(1234)
    A = np.random.rand(30,30)
    B = np.random.rand(30,30)
    C = np.random.rand(30,30)
    print fun_A(A,B,C)[3,5]
    # -> 14.9081790839
    
    # Verify result manually
    Ap = A.copy()
    Ap[3,5] += 1e-6
    print (fun(Ap,B,C) - fun(A,B,C)) / 1e-6
    # -> 14.908178855
    

    You can also easily cook up your own naive numerical differention routine using the latter approach just by incrementing each matrix element at a time by a small amount.