gasar8
gasar8

Reputation: 314

Scipy nquad integration with arguments

I tried to integrate in python using nquad. The problem is that when I try to pass extra arguments to the function which is being integrated in nquad, it wants to pass those parameters to bounds instead to function. I searched the internet and found that it was a bug in scipy.__version__ < 0.18.0, and was fixed then, but I have got version 1.1.0 and the problem persists. What should I do? The simplified example is below

>>> from scipy.integrate import nquad
>>> ranges0 = lambda x: [x, 2 * x]
>>> ranges = [ranges0, [1, 2]]
>>> func = lambda x0, x1, t0, t1: x0 + x1 + t0 + t1
>>> nquad(func, ranges, args=(1,2))

>>> TypeError: <lambda>() takes exactly 1 argument (3 given)

Upvotes: 0

Views: 1037

Answers (1)

Adam Dadvar
Adam Dadvar

Reputation: 394

I did some digging in the documentation for nquad and I have found this excerpt:

If an element of ranges is a callable, then it will be called with all of the integration arguments available, as well as any parametric arguments. e.g. if func = f(x0, x1, x2, t0, t1), then ranges[0] may be defined as either (a, b) or else as (a, b) = range0(x1, x2, t0, t1).

In other words, when defining a func with 4 parameters you must define your range to take exactly 4-pos parameters. In other words, since your ranges0 is in the first place in the range list, it will be passed 4-1=3 parameters, if you put it in the next place in the list it will be passed 4-2=2 parameters. The same is true for all further places in the array. It stays at 2 because it is called with:

all of the integration arguments available

Here the problem is not related to scipy but it more relating to a logical error on your part.

In summary, a function in the range list can never accept one and only one argument when there are 2 time variables.

Upvotes: 1

Related Questions