andres
andres

Reputation: 1109

scipy.optimize.fsolve 'proper array of floats' error

I need to compute the root of a function and I'm using scipy.optimize.fsolve. However when I call fsolve, sometimes it outputs an error that says 'Result from function call is not a proper array of floats.'

Here's an example of the inputs I'm using:

In [45]: guess = linspace(0.1,1.0,11)

In [46]: alpha_old = 0.5

In [47]: n_old = 0

In [48]: n_new = 1

In [49]: S0 = 0.9

In [50]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: array cannot be safely cast to required type
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/home/andres/Documents/UdeA/Proyecto/basis_analysis/<ipython-input-50-f1e9a42ba072> in <module>()
----> 1 fsolve(bb.alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))

/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
    123             maxfev = 200*(n + 1)
    124         retval = _minpack._hybrd(func, x0, args, full_output, xtol,
--> 125                 maxfev, ml, mu, epsfcn, factor, diag)
    126     else:
    127         _check_func('fsolve', 'fprime', Dfun, x0, args, n, (n,n))

error: Result from function call is not a proper array of floats.

In [51]: guess = linspace(0.1,1.0,2)

In [52]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
Out[52]: array([ 0.54382423,  1.29716005])

In [53]: guess = linspace(0.1,1.0,3)

In [54]: fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
Out[54]: array([ 0.54382423,  0.54382423,  1.29716005])

There you can see that for 'guess' as defined in In[46] it outputs an error, however for 'guess' as defined in In[51] and in In[53] it works ok. As far as I know both In[46], In[51] and In[53] are the same type of arrays so what's the reason for the error I'm getting in In[50]?

Here are the functions I'm calling in case they're the reason of the problem:

def alpha_eq(alpha2,n1,alpha1,n2,S0):
    return overlap(n1,alpha1,n2,alpha2) - S0

def overlap(n1,alpha1,n2,alpha2):
    aux1 = sqrt((2.0*alpha1)**(2*n1+3)/factorial(2*n1+2))
    aux2 = sqrt((2.0*alpha2)**(2*n2+3)/factorial(2*n2+2))
    return aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)

(the functions linspace, sqrt and factorial are imported from scipy)

This is a plot of the function for which I'm trying to find the roots. plot

It seems to me like this is a bug of fsolve, however I want to make sure I'm not making a stupid mistake before reporting it.

If there's something wrong with my code please let me know. Thanks!

Upvotes: 2

Views: 4311

Answers (1)

Jaime
Jaime

Reputation: 67427

I have modified your overlap function for debugging as follows:

def overlap(n1,alpha1,n2,alpha2):
    print n1, alpha1, n2, alpha2
    aux1 = sqrt((2.0*alpha1)**(2*n1 + 3)/factorial(2*n1 + 2))
    aux2 = sqrt((2.0*alpha2)**(2*n2 + 3)/factorial(2*n2 + 2))
    ret = aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)
    print ret, ret.dtype
    return ret

And when I try to reproduce your error, here's what happens:

>>> scipy.optimize.fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
0 0.5 1 [ 0.1   0.19  0.28  0.37  0.46  0.55  0.64  0.73  0.82  0.91  1.  ]
[ 0.11953652  0.34008953  0.54906314  0.71208678  0.82778065  0.90418052
  0.95046505  0.97452352  0.98252708  0.97911263  0.96769965] float64

...

0 0.5 1 [ 0.45613162  0.41366639  0.44818267  0.49222515  0.52879856  0.54371741
  0.50642005  0.28700652 -3.72580492  1.81152096  1.41975621]
[ 0.82368346+0.j          0.77371428+0.j          0.81503304+0.j
  0.85916030+0.j          0.88922137+0.j          0.89992643+0.j
  0.87149667+0.j          0.56353606+0.j          0.00000000+1.21228156j
  0.75791881+0.j          0.86627491+0.j        ] complex128

So in the process of solving your equation, the square root of a negative number is being calculated, which leads to the complex128 dtype and your error.

With your function, if you are only interested in the zeros, I think you can get rid of the sqrts if you raise S0 to the 4th power:

def alpha_eq(alpha2,n1,alpha1,n2,S0):
    return overlap(n1,alpha1,n2,alpha2) - S0**4

def overlap(n1,alpha1,n2,alpha2):
    aux1 = (2.0*alpha1)**(2*n1 + 3)/factorial(2*n1 + 2)
    aux2 = (2.0*alpha2)**(2*n2 + 3)/factorial(2*n2 + 2)
    ret = aux1 * aux2 * factorial(n1+n2+2) / (alpha1+alpha2)**(n1+n2+3)
    return ret

And now:

>>> scipy.optimize.fsolve(alpha_eq,guess,args=(n_old,alpha_old,n_new,S0))
array([ 0.92452239,  0.92452239,  0.92452239,  0.92452239,  0.92452239,
        0.92452239,  0.92452239,  0.92452239,  0.92452239,  0.92452239,
        0.92452239])

Upvotes: 2

Related Questions