Search code examples
pythonmathscipynumerical-integration

How to do this integral using tplquad, dblquad in python?


How to perform this kind of integral in python using scipy:

$$\int_{0}^{1} f(x) \, dx \int_{0}^{x} g(y) \, dy \int_{0}^{x} h(z) \,dz $$

Tried using tplquad, but I think the fact that the two inner integrals are independent functions of x is not something I'm able to code.


Solution

  • SciPy's tplquad expects a function of at least three arguments in the order (z, y, x). Nothing easier than that:

    def fgh(z, y, x):
        return f(x)*g(y)*h(z)
    

    To specify bounds lambda functions are quite useful. Example code for the quadrature then reads:

    tplquad(fgh, a=0,               b=1, \
                 gfun=lambda x:0,   hfun=lambda x:x, \
                 qfun=lambda x,y:0, rfun=lambda x,y:x)