Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

scipy - Only size-1 arrays can be converted to Python scalars: Integrate.quad Vs Integrate.quadrature

I'm trying to calculate an integral. When I use the function scipy.integrate.quad the code works perfectly. However, when I use scipy.integrate.quadrature (I need to use this or the fixed.quad methods) it returns "only size-1 arrays can be converted to Python scalars" error. What should I do?

My code

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, chi, y)
    f2 = integrate.quadrature(f1,-200,200)
    return f2[0]

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

The error message:

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,200)
  
File "C:ProgramDataAnaconda3libsite-packagesscipyintegrate\_quadrature.py", line 238, in quadrature
    newval = fixed_quad(vfunc, a, b, (), n)[0]
  
File "C:ProgramDataAnaconda3libsite-packagesscipyintegrate\_quadrature.py", line 119, in fixed_quad
    return (b-a)/2.0 * np.sum(w*func(y, *args), axis=-1), None
  

File "C:ProgramDataAnaconda3libsite-packagesscipyintegrate\_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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...