mpmath和scipy之间的关系:类型错误 [英] Relation between mpmath and scipy: Type Error
问题描述
Scipy具有许多特殊功能,特别是Bessel函数 (***)在网站 但是,如果我使用从numpy导入的J_n(x): 然后我遇到以下错误(添加了回溯) 另一方面,如果我使用 那么我该如何在mpmath的 以同样的方式,可以在 您可以使用以下方式转换mpmath正常的python编号: Scipy have a lot of special functions, in particular Bessel functions (***) In the site SymPy find by "Oscillatory quadrature (quadosc)", this example come from there. But if I use J_n(x) imported from numpy: then I got the following error (Edited: added the Traceback) On the other hand, if I use So how can I use a function In the same way, a You can convert the mpmath normal python number with: Does 这篇关于mpmath和scipy之间的关系:类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!jn
(始终由大写字母J_n(x)表示)和球形Bessel函数spherical_jn
(由小写字母j_n(x)表示).另一方面,mpmath具有quadosc
,这是一种用于集成快速振荡功能(如jn
和spherical_jn
)的特殊方法. 我得到的问题似乎是 quadosc
来自mpmath的不支持,例如,来自scipy的jn
作为使该积分不可或缺的输入.我的意思是,如果我使用从numpy导入的quad
,则不会获得TypeError的任何错误,但是from mpmath import findroot, quadosc, inf, j0
j0zero = lambda n: findroot(j0, pi*(n-0.25)) # ***
I = quadosc(j0, [0, inf], zeros=j0zero)
print(I)
I = 1.0 # OK, this is the correct answer.
from scipy.special import jn
f = lambda x: jn(0,x)
j0zero = lambda n: findroot(f, pi*(n-0.25))
II = quadosc(f, [0, inf], zeros=j0zero)
print(II)
TypeError Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
927 try:
--> 928 fx = f(*x0)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
<ipython-input-449-aeebd9a1e908> in <lambda>(x)
2
----> 3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-449-aeebd9a1e908> in <module>
3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
----> 5 II = quadosc(f, [0, inf], zeros=j0zero)
6 print(II)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/quadrature.py in quadosc(ctx, f, interval, omega, period, zeros)
998 # raise ValueError("zeros do not appear to be correctly indexed")
999 n = 1
-> 1000 s = ctx.quadgl(f, [a, zeros(n)])
1001 def term(k):
1002 return ctx.quadgl(f, [zeros(k), zeros(k+1)]
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
930 except TypeError:
--> 931 fx = f(x0[0])
932 multidimensional = False
933 if 'multidimensional' in kwargs:
quad
,那么我得到了from scipy.integrate import quad
f = lambda x: jn(0,x)
III = quad(f,0,inf)[0]
print(III)
III = -21.154674722694516 # What is an incorrect answer.
quadosc
中使用来自scipy的函数jn
?如何解决此错误?感谢您的所有帮助.In [31]: one,two = mpmath.mpmathify(1), mpmath.mpmathify(2)
In [32]: one,two
Out[32]: (mpf('1.0'), mpf('2.0'))
In [33]: one+two
Out[33]: mpf('3.0')
In [34]: jn(1,2)
Out[34]: 0.5767248077568736
In [35]: jn(one,two)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-ec48c25f686b> in <module>
----> 1 jn(one,two)
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
mpmath
数字不能在jn/jv
中使用.mpmath
函数中使用mpmath
数字,但不能使用等效的numpy
:In [41]: mpmath.sin(one)
Out[41]: mpf('0.8414709848078965')
In [42]: np.sin(1)
Out[42]: 0.8414709848078965
In [43]: np.sin(one)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'mpf' object has no attribute 'sin'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-43-38fc918b311a> in <module>
----> 1 np.sin(one)
TypeError: loop of ufunc does not support argument 0 of type mpf which has no callable sin method
In [44]: float(one)
Out[44]: 1.0
In [45]: jn(float(one),float(two))
Out[45]: 0.5767248077568736
np.float64(one)
也可以,但是jn
不喜欢np.float128(one)
.显然,jn
已针对C double进行了编译,但没有针对精度更高的浮点数进行编译.mpmath
是否谈论将其与numpy
一起使用?我见过mpmath
与sympy
一起使用,但没有与numpy
一起使用.jn
(always denoted by uppercase letter J_n(x)) and spherical Bessel functions spherical_jn
(denoted by lowercase letter j_n(x)). On the other hand mpmath have quadosc
, a special method for integrate rapidly oscillating functions, like jn
and spherical_jn
. The problem I had obtained is that seems that quadosc
from mpmath not support, e.g, jn
from scipy as an input to make this integral. I means, if I use quad
imported from numpy, so nothing error of TypeError is obtained, but quad
is not very appropriate to evaluate integrals of J_n(x) or j_n(x) when x is very large.from mpmath import findroot, quadosc, inf, j0
j0zero = lambda n: findroot(j0, pi*(n-0.25)) # ***
I = quadosc(j0, [0, inf], zeros=j0zero)
print(I)
I = 1.0 # OK, this is the correct answer.
from scipy.special import jn
f = lambda x: jn(0,x)
j0zero = lambda n: findroot(f, pi*(n-0.25))
II = quadosc(f, [0, inf], zeros=j0zero)
print(II)
TypeError Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
927 try:
--> 928 fx = f(*x0)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
<ipython-input-449-aeebd9a1e908> in <lambda>(x)
2
----> 3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-449-aeebd9a1e908> in <module>
3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
----> 5 II = quadosc(f, [0, inf], zeros=j0zero)
6 print(II)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/quadrature.py in quadosc(ctx, f, interval, omega, period, zeros)
998 # raise ValueError("zeros do not appear to be correctly indexed")
999 n = 1
-> 1000 s = ctx.quadgl(f, [a, zeros(n)])
1001 def term(k):
1002 return ctx.quadgl(f, [zeros(k), zeros(k+1)]
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
930 except TypeError:
--> 931 fx = f(x0[0])
932 multidimensional = False
933 if 'multidimensional' in kwargs:
quad
then I gotfrom scipy.integrate import quad
f = lambda x: jn(0,x)
III = quad(f,0,inf)[0]
print(III)
III = -21.154674722694516 # What is an incorrect answer.
jn
that come from scipy inside a quadosc
of mpmath? How can I fix this error? Thanks for all help.In [31]: one,two = mpmath.mpmathify(1), mpmath.mpmathify(2)
In [32]: one,two
Out[32]: (mpf('1.0'), mpf('2.0'))
In [33]: one+two
Out[33]: mpf('3.0')
In [34]: jn(1,2)
Out[34]: 0.5767248077568736
In [35]: jn(one,two)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-ec48c25f686b> in <module>
----> 1 jn(one,two)
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
mpmath
numbers cannot be use in jn/jv
.mpmath
number can be used in a mpmath
function, but not the equivalent numpy
:In [41]: mpmath.sin(one)
Out[41]: mpf('0.8414709848078965')
In [42]: np.sin(1)
Out[42]: 0.8414709848078965
In [43]: np.sin(one)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'mpf' object has no attribute 'sin'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-43-38fc918b311a> in <module>
----> 1 np.sin(one)
TypeError: loop of ufunc does not support argument 0 of type mpf which has no callable sin method
In [44]: float(one)
Out[44]: 1.0
In [45]: jn(float(one),float(two))
Out[45]: 0.5767248077568736
np.float64(one)
also works, but jn
does not like np.float128(one)
. Evidently jn
has been compiled for C doubles, but not higher precision floats.mpmath
does talk about using it with numpy
? I've seen mpmath
used with sympy
, but not with numpy
.