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 7742681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:20:12+00:00 2026-06-01T09:20:12+00:00

Numpy has a basic pxd that declares its c interface to cython. Is there

  • 0

Numpy has a basic pxd that declares its c interface to cython. Is there such a pxd for scipy components (especially scipy.integrate.quadpack)?

Alternatively, could someone give an example of how to link directly with c/fortran functions included in scipy from cython? So far I have always used pyximport … will that work here, or will I have to link using distutils (or make..)?

Thanks!

—- UPDATE —-

The cython code below compiles; however, I get

ImportError: Building module failed: ['ImportError: dlopen(/Users/shauncutts/.pyxbld/lib.macosx-10.7-intel-2.7/factfiber/stat/pmodel/c/meer.so, 2): Symbol not found: _DQAGSE\n Referenced from: /Users/shauncutts/.pyxbld/lib.macosx-10.7-intel-2.7/factfiber/stat/pmodel/c/meer.so\n Expected in: flat namespace\n in /Users/shauncutts/.pyxbld/lib.macosx-10.7-intel-2.7/factfiber/stat/pmodel/c/meer.so\n']

So I think I’ve focused my problem on actually “pointing” my reference of fortran QDAGSE to the _quadpack.so in scipy.integrate. (N.B. tried lowercase version as well.)… without having to hack scipy by putting a pxd there. Is there some way I can do this? Perhaps dynamically? (Can I load the .so dynamically and bind the appropriate function pointer onto a global variable, perhaps?)

cdef extern from "stdlib.h":
    void free(void* ptr)
    void* malloc(size_t size)
    void* realloc(void* ptr, size_t size)


ctypedef double (*qagfunc)( double* )

cdef extern:
    # in scipy.integrate._quadpack
    cdef void dqagse(
        qagfunc f, double *a, double *b, double *epsabs, double *epsrel,
        int *limit, 
        double *result, double *abserr, int *neval, int *ier,
        double *alist, double *blist, double *rlist, double *elist,
        int *iord, int *last
        )

class QAGError( ValueError ):
    code = None

cdef double qags( 
    qagfunc quad_function, double a, double b,
    double epsabs=1.49e-8, double epsrel=1.49e-8, 
    int limit = 50 
    ):

    '''
    wrapper for QUADPACK quags/quagse
    '''
    cdef double result, abserr
    cdef int neval = 0, ier = 0, last = 0
    cdef int *iord = <int *>malloc( limit * sizeof( double ) )
    cdef double *alist = <double *>malloc( limit * sizeof( double ) )
    cdef double *blist = <double *>malloc( limit * sizeof( double ) )
    cdef double *rlist = <double *>malloc( limit * sizeof( double ) )
    cdef double *elist = <double *>malloc( limit * sizeof( double ) )

    try:
        DQAGSE(
            quad_function, &a, &b, &epsabs, &epsrel, &limit, 
            &result, &abserr, &neval, &ier, 
            alist, blist, rlist, elist, iord, &last);

    finally:
        free( iord )
        free( alist )
        free( blist )
        free( rlist )
        free( elist )

    if ier > 0:
        raise QAGError( QUAGS_IER_MAP[ ier ] )
    return result
  • 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-06-01T09:20:14+00:00Added an answer on June 1, 2026 at 9:20 am

    Thanks to: http://codespeak.net/pipermail/cython-dev/2009-October/007239.html

    Adapting, the following seems to work.

    from os import path
    import ctypes
    
    from scipy.integrate import quadpack
    
    ctypedef double (*qagfunc)( double* )
    
    ctypedef void (*quadpack_qagse_fp)( 
        qagfunc f, double *a, double *b, double *epsabs, double *epsrel,
        int *limit, 
        double *result, double *abserr, int *neval, int *ier,
        double *alist, double *blist, double *rlist, double *elist,
        int *iord, int *last
        )
    
    quadpack_path = path.dirname( quadpack.__file__ )
    quadpack_ext_path = path.join( quadpack_path, '_quadpack.so' ) 
    quadpack_lib = ctypes.CDLL( quadpack_ext_path )
    
    cdef quadpack_qagse_fp quadpack_qagse
    quadpack_qagse = ( 
        <quadpack_qagse_fp*><size_t> ctypes.addressof( quadpack_lib.dqagse_ ) )[ 0 ]
    

    With this prepended, the code above seems to work… well except that, annoyingly enough, the answer is a bit different than the python quad, and also quadpack dblquad (I’m evaluating a double integral). I assume I either need more resolution, or need to specify a parameter with a non-default value. (Also it won’t port to Windows perhaps? But that would just mean changing “.so” to “.dll”)

    It gets me just under 5x speed up from previous code that used python callback, but otherwise was as optimized as I could get it.

    For extra credit… how do I smuggle in extra parameters to the c callback? I assume its impossible without global variables? or I could use weave?

    In any case — thanks for your attention — this is an obscure example, but has more general applicability to calling undeclared c functions in extension modules from cython. If anyone has a more elegant way of doing it, I’d be eager to hear.

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

Sidebar

Related Questions

Has anyone tried compiling SciPy 0.7.1 on Windows using numpy-1.3.0 that was built with
I would like to create a two dimensional numpy array of arrays that has
I realize that there is a griddata for NumPy via Matplotlib , but is
Is there a simple way to create an immutable NumPy array? If one has
Is there a way in numpy to create an array of booleans that uses
has anyone used unladen-swallow with numpy/scipy for numeric/scientific applications? Is it significantly faster in
I'm having a problem sorting a numpy array that has numbers as strings. I
numpy has a function to set the specified elements to be some value a=numpy.zeros(10)
My work PC has restrictions that stop me from adding programs to the start
I use numpy for numerical linear algebra. I suspect that I can get much

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.