Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599596
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:33:49+00:00 2026-05-21T23:33:49+00:00

I’m using right now the scipy.integrate.quad to successfully integrate some real integrands. Now a

  • 0

I’m using right now the scipy.integrate.quad to successfully integrate some real integrands. Now a situation appeared that I need to integrate a complex integrand. quad seems not be able to do it, as the other scipy.integrate routines, so I ask: is there any way to integrate a complex integrand using scipy.integrate, without having to separate the integral in the real and the imaginary parts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:33:50+00:00Added an answer on May 21, 2026 at 11:33 pm

    What’s wrong with just separating it out into real and imaginary parts? scipy.integrate.quad requires the integrated function return floats (aka real numbers) for the algorithm it uses.

    import scipy
    from scipy.integrate import quad
    
    def complex_quadrature(func, a, b, **kwargs):
        def real_func(x):
            return scipy.real(func(x))
        def imag_func(x):
            return scipy.imag(func(x))
        real_integral = quad(real_func, a, b, **kwargs)
        imag_integral = quad(imag_func, a, b, **kwargs)
        return (real_integral[0] + 1j*imag_integral[0], real_integral[1:], imag_integral[1:])
    

    E.g.,

    >>> complex_quadrature(lambda x: (scipy.exp(1j*x)), 0,scipy.pi/2)
    ((0.99999999999999989+0.99999999999999989j),
     (1.1102230246251564e-14,),
     (1.1102230246251564e-14,))
    

    which is what you expect to rounding error – integral of exp(i x) from 0, pi/2 is (1/i)(e^i pi/2 – e^0) = -i(i – 1) = 1 + i ~ (0.99999999999999989+0.99999999999999989j).

    And for the record in case it isn’t 100% clear to everyone, integration is a linear functional, meaning that ∫ { f(x) + k g(x) } dx = ∫ f(x) dx + k ∫ g(x) dx (where k is a constant with respect to x). Or for our specific case ∫ z(x) dx = ∫ Re z(x) dx + i ∫ Im z(x) dx as z(x) = Re z(x) + i Im z(x).

    If you are trying to do a integration over a path in the complex plane (other than along the real axis) or region in the complex plane, you’ll need a more sophisticated algorithm.

    Note: Scipy.integrate will not directly handle complex integration. Why? It does the heavy lifting in the FORTRAN QUADPACK library, specifically in qagse.f which explicitly requires the functions/variables to be real before doing its “global adaptive quadrature based on 21-point Gauss–Kronrod quadrature within each subinterval, with acceleration by Peter Wynn’s epsilon algorithm.” So unless you want to try and modify the underlying FORTRAN to get it to handle complex numbers, compile it into a new library, you aren’t going to get it to work.

    If you really want to do the Gauss-Kronrod method with complex numbers in exactly one integration, look at wikipedias page and implement directly as done below (using 15-pt, 7-pt rule). Note, I memoize’d function to repeat common calls to the common variables (assuming function calls are slow as if the function is very complex). Also only did 7-pt and 15-pt rule, since I didn’t feel like calculating the nodes/weights myself and those were the ones listed on wikipedia, but getting reasonable errors for test cases (~1e-14)

    import scipy
    from scipy import array
    
    def quad_routine(func, a, b, x_list, w_list):
        c_1 = (b-a)/2.0
        c_2 = (b+a)/2.0
        eval_points = map(lambda x: c_1*x+c_2, x_list)
        func_evals = map(func, eval_points)
        return c_1 * sum(array(func_evals) * array(w_list))
    
    def quad_gauss_7(func, a, b):
        x_gauss = [-0.949107912342759, -0.741531185599394, -0.405845151377397, 0, 0.405845151377397, 0.741531185599394, 0.949107912342759]
        w_gauss = array([0.129484966168870, 0.279705391489277, 0.381830050505119, 0.417959183673469, 0.381830050505119, 0.279705391489277,0.129484966168870])
        return quad_routine(func,a,b,x_gauss, w_gauss)
    
    def quad_kronrod_15(func, a, b):
        x_kr = [-0.991455371120813,-0.949107912342759, -0.864864423359769, -0.741531185599394, -0.586087235467691,-0.405845151377397, -0.207784955007898, 0.0, 0.207784955007898,0.405845151377397, 0.586087235467691, 0.741531185599394, 0.864864423359769, 0.949107912342759, 0.991455371120813]
        w_kr = [0.022935322010529, 0.063092092629979, 0.104790010322250, 0.140653259715525, 0.169004726639267, 0.190350578064785, 0.204432940075298, 0.209482141084728, 0.204432940075298, 0.190350578064785, 0.169004726639267, 0.140653259715525,  0.104790010322250, 0.063092092629979, 0.022935322010529]
        return quad_routine(func,a,b,x_kr, w_kr)
    
    class Memoize(object):
        def __init__(self, func):
            self.func = func
            self.eval_points = {}
        def __call__(self, *args):
            if args not in self.eval_points:
                self.eval_points[args] = self.func(*args)
            return self.eval_points[args]
    
    def quad(func,a,b):
        ''' Output is the 15 point estimate; and the estimated error '''
        func = Memoize(func) #  Memoize function to skip repeated function calls.
        g7 = quad_gauss_7(func,a,b)
        k15 = quad_kronrod_15(func,a,b)
        # I don't have much faith in this error estimate taken from wikipedia
        # without incorporating how it should scale with changing limits
        return [k15, (200*scipy.absolute(g7-k15))**1.5]
    

    Test case:

    >>> quad(lambda x: scipy.exp(1j*x), 0,scipy.pi/2.0)
    [(0.99999999999999711+0.99999999999999689j), 9.6120083407040365e-19]
    

    I don’t trust the error estimate — I took something from wiki for recommended error estimate when integrating from [-1 to 1] and the values don’t seem reasonable to me. E.g., the error above compared with truth is ~5e-15 not ~1e-19. I’m sure if someone consulted num recipes, you could get a more accurate estimate. (Probably have to multiple by (a-b)/2 to some power or something similar).

    Recall, the python version is less accurate than just calling scipy’s QUADPACK based integration twice. (You could improve upon it if desired).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.