Madabaru
Madabaru

Reputation: 23

Python: Gradient of matrix function

I want to calculate the gradient of the following function h(x) = 0.5 x.T * A * x + b.T + x.

For now I set A to be just a (2,2) Matrix.

def function(x):
    return 0.5 * np.dot(np.dot(np.transpose(x), A), x) + np.dot(np.transpose(b), x)

where

A = A = np.zeros((2, 2))
n = A.shape[0]
A[range(n), range(n)] = 1

a (2,2) Matrix with main diagonal of 1 and

b = np.ones(2) 

For a given Point x = (1,1) numpy.gradient returns an empty list.

x = np.ones(2)  
result = np.gradient(function(x))

However shouldn't I get something like that: grad(f((1,1)) = (x1 + 1, x2 + 1) = (2, 2).

Appreciate any help.

Upvotes: 2

Views: 3256

Answers (1)

Jacques Kvam
Jacques Kvam

Reputation: 3056

It seems like you want to perform symbolic differentiation or automatic differentiation which np.gradient does not do. sympy is a package for symbolic math and autograd is a package for automatic differentiation for numpy. For example, to do this with autograd:

import autograd.numpy as np
from autograd import grad

def function(x):
    return 0.5 * np.dot(np.dot(np.transpose(x), A), x) + np.dot(np.transpose(b), x)

A = A = np.zeros((2, 2))
n = A.shape[0]
A[range(n), range(n)] = 1
b = np.ones(2)
x = np.ones(2)
grad(function)(x)

Outputs:

array([2., 2.])

Upvotes: 1

Related Questions