微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

只有大小为 1 的数组可以转换为 Python 标量:Integrate.quad Vs Integrate.quadrature

如何解决只有大小为 1 的数组可以转换为 Python 标量:Integrate.quad Vs Integrate.quadrature

我正在尝试计算一个积分。当我使用函数 scipy.integrate.quad 时,代码运行良好。但是,当我使用 scipy.integrate.quadrature(我需要使用 this 或 fixed.quad 方法)时,它返回“只有大小为 1 的数组可以转换为 Python 标量”错误。我该怎么办?

我的代码

import cmath as c
import math  as m
import numpy as np
import scipy as sp
import os
import csv
import time
import scipy.integrate as integrate

from scipy import special

k = 0.1 

def solnum(x,chi,y): 

    Bk = ((2*abs(k))**(3/2))*((1+(1.5*abs(k))))*(m.gamma((1/(2*abs(k)))+(3/4))/m.gamma((1/(2*abs(k)))-(3/4)))

    C = (chi/(2*m.sqrt(m.pi)))

    R1 = (1/(1+(y**2)))

    R2 = (((k**2)*(-((chi**2)*((x-y)**2))/4))-(m.sqrt(1+((k**2)*(-((chi**2)*((x-y)**2))/4)**2))))/(k**2 - 1)

    R3 = ((((m.sqrt(1+((k**2)*(-((chi**2)*((x-y)**2))/4)**2)))) + ((k*(-((chi**2)*((x-y)**2))/4))))**(1/k))  

    sn = (C*Bk*R1*R2*R3)

    return sn

def g(x,chi):   
    f1 = lambda y: solnum(x,y)
    f2 = integrate.quadrature(f1,-200,200)
    return f2[0]

print(g(0.,0.05))  #for instance

错误信息:

Traceback (most recent call last):
  File "c:/Users/pwadmin/Google Drive/Pós-Doc/Py2/Completão/cacete de agulha.py",line 34,in <module>
    print(g(0.,0.05))

File "c:/Users/pwadmin/Google Drive/Pós-Doc/Py2/Completão/cacete de agulha.py",line 32,in g
    f2 = integrate.quadrature(f1,200)
  
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_quadrature.py",line 238,in quadrature
    newval = fixed_quad(vfunc,a,b,(),n)[0]
  
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_quadrature.py",line 119,in fixed_quad
    return (b-a)/2.0 * np.sum(w*func(y,*args),axis=-1),None
  

File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_quadrature.py",line 149,in vfunc
    return func(x,*args)
  
File "c:/Users/pwadmin/Google Drive/Pós-Doc/Py2/Completão/cacete de agulha.py",line 31,in <lambda>
    f1 = lambda y: solnum(y,x,chi)
  
File "c:/Users/pwadmin/Google Drive/Pós-Doc/Py2/Completão/cacete de agulha.py",line 22,in solnum
    R2 = (((k**2)*(-((chi**2)*((x-y)**2))/4))-(m.sqrt(1+((k**2)*(-((chi**2)*((x-y)**2))/4)**2))))/(k**2 - 1)
TypeError: only size-1 arrays can be converted to Python scalars

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。