0

I'm using the package numddifftools to calculate the hessians of a multidimensional function (from R^n to R^n). While changing the code to use numpy arrays instead of lists, I discovered that doing so broke the code. Specifically:

import numpy as np 
import numdifftools as nd

def function(x):

    out = np.zeros(2)
    out[0] = x[0] - x[1]**2/2.0

    return float(out[0])

tempHessian = nd.Hessian(function, method='complex')
tempHessian([0.4,0.4])

Produces the error: ...\Continuum\Anaconda3\lib\site-packages\ipykernel_launcher.py:8: ComplexWarning: Casting complex values to real discards the imaginary part

and gives a zero hessian.

However, this one works fine:

import numpy as np 
import numdifftools as nd

def function(x):

    return x[0] - x[1]**2/2.0

tempHessian = nd.Hessian(function, method='complex')
tempHessian([0.4,0.4])

Any ideas what could be the problem? Thanks!

  • You should try to figure out what line in your code is triggering the warning. You can turn warnings into exceptions via the warnings module. https://stackoverflow.com/questions/5644836/in-python-how-does-one-catch-warnings-as-if-they-were-exceptions – ngoldbaum Aug 01 '17 at 15:29
  • The problem is "float(out[0])" you are converting the matrix in a "real" one. – axaroth Aug 01 '17 at 15:33

1 Answers1

0

When out is created like this:

    out = np.zeros(2)

it has type numpy.float64. You can't assign a complex value to such an array. Depending on which version of numpy that you are using, you'll get a warning or an error.

The complex step method for numerical differentiation requires that your function works with complex values. Forcing out to be numpy.float64 breaks that method. Converting the return value to floating point with float(out[0]) also breaks the method.

You can try something like this:

def function(x):

    out = np.zeros(2, dtype=x.dtype)
    out[0] = x[0] - x[1]**2/2.0

    return out[0]

This creates out with the same data type as x. So if x is complex, the return value is also complex, as required by the complex step method.

(Of course, we don't know why you create out to have size 2, when only the first element is used. Presumably this is a simplification of the actual code that you are working with.)

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214