从数组创建MPF [英] Create mpf from array
问题描述
我正在尝试将fsolve
与mpmath
软件包结合使用.
但是,出现错误cannot create mpf from array([mpf('1.0')], dtype=object)
.
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)
.
这是重现该错误的最小示例.对于此示例,从技术上讲,我不需要mpmath
软件包,但是我的实际函数包含具有超融合功能的函数.
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
我发现了一个类似的问题,建议使用np.frompyfunc
(如何对数组进行mpf?),但它告诉我该函数不可调用(当我将其应用于vn
时).
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
).
推荐答案
诀窍是将np.frompyfunc
应用于函数而不是值.我认为以下修改将使您的功能正常工作:
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
这篇关于从数组创建MPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!