Quad / Quad Integration from Python / SciPy

After reading this and that , it seems to me that both “square” and “quadrature” should be interchangeable *, at least syntactically. Strange, it seems this is not the case:

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 works fine for "quad", but not for "quadrature" - the result is:

 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 (ba)/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 need to switch the args tuple to a list (cf. commented line in integr2), although the documentation says that it should be a tuple. This seems to be what the interpreter is talking about ... (right?)

Is this intended? Or am I doing something wrong? In the end, I would like to be able to choose integration methods after that, without changing too much of the rest of the code.

* In fact, I do not understand how to choose between them. I understand the difference between a Gaussian quadrature and an adaptive quadrature, but I don’t know what “adaptive Gaussian quadrature” means, is the number of nodes adapted, if so, how !?

+6
source share
1 answer

The problem is the line return q(myfunc2, 0, 1, args=(y))[0] , especially in the part of args=(y) . You want args=(y,) (note the comma after y ) or args=[y] .

The problem is that Python cortettes are created with commas rather than parentheses. Take a look:

 >>> a = (1,) >>> b = (1) >>> print a, type(a) (1,) <type 'tuple'> >>> print b, type(b) 1 <type 'int'> 
+4
source

Source: https://habr.com/ru/post/897234/


All Articles