Create mpf from array

2019-03-06 04:32发布

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).

2条回答
闹够了就滚
2楼-- · 2019-03-06 04:52

The trick is to apply np.frompyfunc to a function instead of a value. I think the following modification would make your function work:

def cosFunc(p):
  vn = p
  np_sin = np.frompyfunc(mp.sin, 1, 1)
  output = np_sin(vn)
  return float(output)

value = fsolve(cosFunc, 1)
print value
查看更多
疯言疯语
3楼-- · 2019-03-06 04:53

The specific cause of the error you is this:

(Pdb) x0
array([mpf('1.0')], dtype=object)
(Pdb) mp.sin(x0)
*** TypeError: cannot create mpf from array([mpf('1.0')], dtype=object)

What happens is that fsolve tries to convert your estimate to array and numpy does not know how to handle mpmath objects.

>>> np.asarray(mp.mpf(1))
>>> array(mpf('1.0'), dtype=object)

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

def cos_func(p):
   vn = p
   if isinstance(p, np.ndarray):
         if p.size == 0: 
             vn = p[0]
         else:
             raise ValueError  # or whatever you want to do here"
   return mp.sin(vn)
查看更多
登录 后发表回答