Silmarilon
Silmarilon

Reputation: 1

Using scipy.fsolve for a multidimensional numpy array

Lets assume I have an array of shape (36,100) and I would like to solve (using scipy.optimize.fsolve) each of these functions individually. I could do it with a for loop very easy but this is too slow for me... Is there a way I can do this in one single step?

Here is a simple example what I am trying to do:

import numpy as np
from scipy.optimize import fsolve
from scipy.interpolate import RectBivariateSpline

m_array = np.geomspace(1e13, 1e15, 36)[:,None]
rbin = np.geomspace(1e-3, 50, 100)[None,:]
mhga = rbin**2 * np.sqrt(m_array)
ff = RectBivariateSpline(m_array[:,0], rbin[0,:], mhga)
def func(x):
    return (1-x) + (ff(m_array[:,0], x*rbin[0,:]))

xx = fsolve(func, 1.0)

xx should have the shape of m_array in the end, but I get this error:

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

Upvotes: 0

Views: 37

Answers (0)

Related Questions