I'm trying to use fsolve
in combination with the mpmath
package.
However, I get the error cannot create mpf from array([mpf('1.0')], dtype=object)
.
Here is a minimal example reproducing the error. For this example, I technically do not need the mpmath
package, but my actual function contains hyperconfluent functions that do.
from scipy.optimize import fsolve
#from mpmath import hyp1f1 as hyp1f1mp
#from mpmath import gamma as gammamp
import mpmath as mp
#import numpy as np
mp.dps = 250; mp.pretty = True
def cosFunc(p):
vn = p
output = mp.sin(vn)
return output
estimate = mp.mpf(1)
value = fsolve(cosFunc,estimate)
print value
I found a similar question suggesting to use np.frompyfunc
(How to mpf an array?), but it tells me that the function is not callable (when I apply it on vn
).
The trick is to apply
np.frompyfunc
to a function instead of a value. I think the following modification would make your function work:The specific cause of the error you is this:
What happens is that
fsolve
tries to convert yourestimate
to array and numpy does not know how to handle mpmath objects.Changing how
fsolve
works is not very productive, so your best bet seems to be to teach your function to handle arrays of mpmath objects