4

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!

KelvinS
  • 2,870
  • 8
  • 34
  • 67
Fairly Factual
  • 161
  • 3
  • 13
  • 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. – KelvinS Jul 07 '17 at 21:18
  • https://stackoverflow.com/questions/16512817/numerical-integration-in-c try that for integration, and the rest are in the cmath library i think – Chad K Jul 07 '17 at 21:22
  • Possible duplicate of [Convert Python program to C/C++ code?](https://stackoverflow.com/q/4650243/608639) – jww Nov 21 '19 at 13:32

1 Answers1

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.

Matt
  • 2,602
  • 13
  • 36