After reading this and that, it occurs to me that both "quad" and "quadrature" should be interchangeable*, atleast syntax-wise. Strangely it does seem they are not:
from scipy.integrate import quad as q
#from scipy.integrate import quadrature as q
def myfunc(x):
return x
def integr():
return q(myfunc, 0, 1)[0]
print integr()
def myfunc2(x, y):
return x + y
def integr2(y):
return q(myfunc2, 0, 1, args=(y))[0]
#return q(myfunc2, 0, 1, args=[y])[0]
print integr2(10)
... the example runs fine for "quad", but not for "quadrature" - I end up with:
Traceback (most recent call last):
File "./test.py", line 38, in <module>
print integr2(10)
File "./test.py", line 36, in integr2
return q(myfunc2, 0, 1, args=(y))[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 136, in quadrature
newval = fixed_quad(vfunc, a, b, (), n)[0]
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 48, in fixed_quad
return (b-a)/2.0*sum(w*func(y,*args),0), None
File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadrature.py", line 77, in vfunc
return func(x, *args)
TypeError: myfunc2() argument after * must be a sequence, not int
I have to switch the args tuple to a list (cf. commented line in integr2) even though the documentation says it should be a tuple. It seemed this is what the interpreter complains about ... (right?)
Is this intended? Or am I doing something wrong? In the end I'd like to be able to choose integration methods afterwards without having to change too much of the rest of the code.
*Actually I don't really get how to choose between the two. I do understand the difference between Gaussian quadrature and adaptive quadrature, but I don't know what "adaptive Gaussian quadrature" is supposed to mean - is the number of nodes adapted, if so how!?
The quad function returns the two values, in which the first number is the value of integral and the second value is the estimate of the absolute error in the value of integral. Note − Since quad requires the function as the first argument, we cannot directly pass exp as the argument.
quad uses "a technique from the Fortran library QUADPACK". QUADPACK provides several routines using different techniques.
The function quad is provided to integrate a function of one variable between two points. The points can be ( inf ) to indicate infinite limits. For example, suppose you wish to integrate a bessel function jv(2.5, x) along the interval. I = ∫ 0 4.5 J 2.5 ( x ) d x .
The problem is in the line return q(myfunc2, 0, 1, args=(y))[0]
, specifically in the args=(y)
part. What you want is args=(y,)
(notice the comma after y
) or args=[y]
.
The issue is that in Python tuples are created with commas, not with parentheses. Look:
>>> a = (1,)
>>> b = (1)
>>> print a, type(a)
(1,) <type 'tuple'>
>>> print b, type(b)
1 <type 'int'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With