5

I have a segment of code that uses several functions from numpy and scipy that I need to use in C++, but my knowledge of C++ is much too limited to be able to transfer this code into this language.

What C++ functions should I need to have something like this:

import numpy as np
from scipy.integrate import quad
from scipy.optimize import minimize

minor = 2.23
major = 3.05
encoderdistance = 2.53141952655
print minimize(lambda x: abs(quad(func, 0, x)[0] - encoderdistance), 1).x
min = 90-((180/np.pi) * np.arctan(((np.tan(minimize(lambda x: abs(quad(func, 0, x)[0] - encoderdistance), 1).x))*minor)/major))
print min

Thanks!

3
  • 2
    I think this is not the correct question format for StackOverflow. You should ask questions related to specific issues. As the code has only a few lines, it is not so difficult to convert to C++. I suggest that you first try to convert it by yourself, reading the numpy and scipy documentation to understand each function. If you have some doubt when doing it, I think the StackOverflow community will be happy to help you with specific issues. Commented Jul 7, 2017 at 21:18
  • stackoverflow.com/questions/16512817/numerical-integration-in-c try that for integration, and the rest are in the cmath library i think Commented Jul 7, 2017 at 21:22
  • Possible duplicate of Convert Python program to C/C++ code? Commented Nov 21, 2019 at 13:32

1 Answer 1

2

I just know Cython which can steer you the right direction. Quad http://quadpackpp.sourceforge.net can easily call from C++. Most of the numpy functions are in libc.math (abs, probably arctan, etc.). Pi you can just hardcode the value to a double. Minimize you'll have to search for the scipy source code https://github.com/scipy/scipy/blob/master/scipy/optimize/optimize.py to see what libs are called. Most numpy / scipy functions rely on Blas / Lapack or use the MKL C++ variants.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.