/CppPyInterop

Interop Examples of Embedding Python within C++, using Python.h only.

Primary LanguageC++MIT LicenseMIT

CppPyInterop

By: J. Brandon George | darth.data410@gmail.com | twitter: @PyFryDay | medium.com: https://darth-data410.medium.com/ | youtube: https://www.youtube.com/@pyfryday

Details:

Interop Examples of Embedding Python within C++, using Python.h only. This includes using vscode as IDE choice on ubunutu (linux). The contents of this project represent work used in medium.com articles for creating how-tos and training materials. Medium.com articles for each section will be linked as pubslihed.

cpy-numpy_pyplot

Within this section of the repo is a vscode IDE targeted project, focused on Pythons numpy and matplotlib modules. The main command line tool to showcase Python being embedded within C++ is called 'cpy'. You may need to adjust the .vscode folder, tasks.json file for YOUR Python implementation.

As-is this project debugs/builds using Visual Studio Code v1.77.3 IDE, running on Ubuntu 22.04.2 LTS, ../include/python3.10 and flag -std=c++17.

The following section list usage information for ./cpy:

  1. -matrix-diag-test: Uses two parameters passed in, the rows and columns of a test NDMatrix. Then NDMatrices of that size are used throughout the test execution. This includes showcase of generating random numbers from numpy.random.random via Python.h within C++. Further the finding of diagonal values of an numpy.NDArray built from the custom cpy::NDMatrix found in this project.

Example(s): ./cpy -matrix-diag-test 4 4 Output example shown in the following screen shot:

  1. -pyplot: Uses three parameters with call: start, stop & step values. Expects these to be floats. Can use *pi in order to adjust one-or-all three values by. Uses the values to load Python modules numpy and matplotlib.pyplot. Generates NDarray values using start, stop and step. Then retrieves the sin(x) values stored in a different NDarray. Calls pyplot to plot(x,y) and show().

Example(s): (Example_1): ./cpy -pyplot 1 11 0.2 - Plot saved: ../numpy_pyplot/saved_plots/Example_1.png

(Example_2): ./cpy -pyplot 1 3*pi 0.48 - Plot saved: ../numpy_pyplot/saved_plots/Example_2.png

  1. -eigvals: Uses two paramters to determine the size of a general NDMatrix. Will then randomly generate matrix values and populate. Finally calls the cpy::EigVals(NDMatrix m) which executes logic converting the NDMatrix to a numpy.array (like) object for calling numpy.lingalg.eigvals(x). The return is a vector of C-type complex values. cpy::EigVals checks to make sure the passed in NDMatrix is "square" (ie: n * rows = n * cols). If not cpy throws a runtime_error() stating NDMatrix must be square. (ie: 2x2, 3x3, 4x4, etc.).

Example(s): ./cpy -eigvals 2 2 Output example shown in the following screen shot: