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

The Archive Base Latest Questions

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

Scipy has many functions that accept a python callable to perform some operation. In

  • 0

Scipy has many functions that accept a python callable to perform some operation. In particular, I’m working with a mathematical optimization function scipy.optimize.leastsq that accepts a Python callable as objective function argument. This objective function can be called by leastsq lots of times during the minimization process.

My profiling shows that a lot of time is spent on that objective function. I have sped up some parts of the function using Cython. However, the function itself is still a Python function and calling it repeatedly (as leastsq does) has some overhead.

I think I could get a further increase in speed if the function was a Cython function (using cdef instead of def). So I put my call to leastsq inside the Cython extension and pass a Cython objective function to it. But when I do this I get a compile error at the leastsq call:

Cannot convert 'object (object, object, object)' to Python object

Is there any way to pass a Cython function as an argument to these Scipy functions that require python callables?

Or, in my case, is there any way to access the underlying leastsq implementation and pass the Cython objective function to it?

  • 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-12T22:12:02+00:00Added an answer on June 12, 2026 at 10:12 pm

    Passing cdef functions in is not possible currently. Passing callback functions to the underlying Fortran code is also not possible, as it’s wrapped with f2py which doesn’t know about Cython.

    What you can do is:

    • Write your function as Cython’s def function. These can be passed in to all Scipy’s routines. This does not remove the additional function call overhead which comes from using Python’s callback mechanism (which might not be significant!), but you can speed up the implementation of your function, and this might well be enough. Just remember to cdef the variables appearing in, as usual when writing Cython code.

    • Copy the MINPACK source codes from Scipy or netlib.org, and use them directly yourself. This gets rid of the remaining function call overhead by replacing Python function callback mechanism by the low-level one.

    (There have been discussions on adding a protocol for passing low-level function pointers around exactly for this purpose, which could be adopted by any Python-based system having a need for it, but AFAIK the design is not completed, and not implemented in Cython & Scipy.)

    • 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
Possible Duplicate: Relationship between scipy and numpy For instance, NumPy has window functions bartlett
So I've been working on a python project and reached the point that I
if I have an application that is written part in Python and has dependencies
So numpy has some convenience functions for combining several arrays into one, e.g. hstack
I wrote a neural network object in python that has a cost function and
Scipy and Numpy have between them three different functions for finding eigenvectors for a
When I try to import the scipy module (version 0.11.0b1) in the Python interpreter
I have installed numpy1.3,scipy 0.7.1,matplotlib 0.99.1.1 and python 2.5 when I import pylab I
I have installed Python 2.7.2 on my Mac, and it's working fine. I downloaded

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.