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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:04:59+00:00 2026-06-16T13:04:59+00:00

I want to write a Boost-Python program to take a symbolic python function from

  • 0

I want to write a Boost-Python program to take a symbolic python function from user and evaluate its derivative in my program.

For example the User provide a python file (Function.py) which defines a function like
F = sin(x)*cos(x).

Then I want to have access to F'(x) (derivative of F(x)) using symbolic differentiation ability of Sympy. I don’t want to use numerical differentiation.

Is there a way to make such a function F'(x) accessible in the C++ using Boost-Python.

  • 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-16T13:05:00+00:00Added an answer on June 16, 2026 at 1:05 pm

    I am not a SymPy expert but maybe this can help you:

    You can define a Python method like:

    def f(x):
      return sin(x)*cos(x)
    

    You can create an evaluable function f1 as the derivative of f using:

    from sympy import *
    x = symbols('x')
    f1 = lambdify(x, diff(f(x)))
    

    This function f1 can be called from C++ using boost::python. You may create an object to function f1, call the function using the () operator and convert the result to double using extract<>.

    Here is an example:

      namespace py = boost::python;
    
      Py_Initialize();
    
      py::object main_module = py::import("__main__");
      py::object main_dict   = main_module.attr("__dict__");
    
      py::exec(
        "def f(x):\n"
        "  return  sin(x)*cos(x)\n",
        main_dict
      );
    
      py::exec(
        "from sympy import *\n"
        "x = symbols('x')\n"
        "f1 = lambdify(x, diff(f(x)))\n",
         main_dict
      );
    
      py::object f1 = main_dict["f1"];
      std::cout << py::extract<double>(f1(0.0)) << std::endl;
      std::cout << py::extract<double>(f1(1.0)) << std::endl;
    
      return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple server application that will take commands from a
I want write a simple query which will fetch data from a table (which
I want to write a program in Prolog that confirms if a b-tree of
I want to write <a href=product.php?id=5> as product/5.I am not talking about user to
I want to write a program to contact a PHP script online, and show
So I want to write a... well... not-so-simple parser with boost::spirit::qi. I know the
I wanted to write a function with a variable argument list. I want to
For convenient usage I want to write formatting function similar to sprintf just returning
I am new to metafunctions. I want to write a function that replaces all
For performance reasons I want to port parts of my python program to C++

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.