17

There is a function I am trying to integrate in Python using scipy.integrate.quad. This particular function takes two arguments. There is only one argument I want to integrate over. An example is shown below.

from scipy import integrate as integrate
def f(x,a):  #a is a parameter, x is the variable I want to integrate over
    return a*x

result = integrate.quad(f,0,1)

This example doesn't work (as is likely clear to you) since, as Python reminds me when I try it:

TypeError: f() takes exactly 2 arguments (1 given)

I am wondering how to use integrate.quad() to integrate in a single variable sense when the function given is, in general, a multi-variable function, with the extra variables providing parameters to the function.

2 Answers 2

24

Found the answer in the scipy documentation.

You can do the following:

from scipy import integrate as integrate
def f(x,a):  #a is a parameter, x is the variable I want to integrate over
    return a*x

result = integrate.quad(f,0,1,args=(1,))

The args=(1,) argument in the quad method will make a=1 for the integral evalution.

This can also be carried to functions with more than two variables:

from scipy import integrate as integrate
def f(x,a,b,c):  #a is a parameter, x is the variable I want to integrate over
    return a*x + b + c

result = integrate.quad(f,0,1,args=(1,2,3))

This will make a=1, b=2, c=3 for the integral evaluation.

The important thing to remember for the function you want to integrate this way is to make the variable you want to integrate over the first argument to the function.

6
  • 1
    args=(1) is the same as args=1; i.e. without a trailing comma to force the expression to be a tuple, those parentheses don't do anything useful. According to the quad docstring, args is expected to be a tuple, but it appears to handle a single value. To make args a tuple of length 1, use args=(1,). Jan 22, 2015 at 22:40
  • Would you say the "most correct" way to pass a single argument to args is a tuple args=(1,), based on the documentation? Jan 22, 2015 at 22:45
  • Based on the documentation, yes. However, if you check the source code, you'll find that if args is not a tuple, the code does args = (args,), so I guess it is "safe" to use args=1 because of this undocumented feature. (Note that this means you can't use any sequence other than a tuple to hold multiple arguments. For example, for your second f with four arguments, you can't use args=[1, 2, 3], because internally it will be treated as ([1, 2, 3],).) Jan 22, 2015 at 23:18
  • 2
    And what will happen if we integrate a product of two functions, one say f(x,a,b) and the other g(x,c). How would args work in this case? Feb 4, 2017 at 11:32
  • What if I have a function f(x,y,z) and that the one I need to integrate over a range for x and another time over a range of y. is this not possible because I can only integrate over the first argument?
    – Adriaan
    Jul 11, 2018 at 13:47
8

Use the args argument (see the scipy documentation):

result = integrate.quad(f,0,1, args=(a,))

The comma in args=(a,) is required because a tuple must be passed.

2
  • I got it to work without including the comma. Which seems strange. But work it did. Jan 22, 2015 at 22:39
  • 2
    Yup: the source includes the lines: if not isinstance(args, tuple): args = (args,), so a single value is fine. If it's not documented, there's no guarantee it won't change in future, though...
    – xnx
    Jan 22, 2015 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.