Search code examples
pythonnumpyscipynumerical-methodsnumerical-integration

Calculate Integral over array in Python with output array


I'd like to calculate an integral of the form

where I want the results as an array (to eventually plot them as a function of omega). I have

    import numpy as np
    import pylab as plt
    from scipy import integrate
    
    w = np.linspace(-5, 5, 1000)

    def g(x):
        return np.exp(-2*x)

    def complexexponential(x, w):
        return np.exp(-1j*w*x)

    def integrand(x, w):
        return g(x)*complexexponential(x, w)

    integrated = np.real(integrate.quad(integrand, 0, np.inf, args = (w)))

which gives me the error "supplied function does not return a valid float". I am not very familiar with the integrate-function of Scipy. Many thanks for your help in advance!


Solution

  • Scipy integrate.quad doesn't seem to support vector output. If you loop over all your values of w and only give one of them at a time as args your code seems to work fine.

    Also it doesn't handle complex integration, which you can get around using the procedure outlined in this answer.