Extending Python with Numerical Recipes
pythonc-plus-plusnumerical-computingextensioninterop
Abstraction: Tutorial for wrapping C++ Numerical Recipes code as callable Python modules
Key points:
- Uses
nr3python.hheader to bridge Python and C++ types: NRpyArgs unpacks Python arguments, NRpyObject wraps return values, NRpyTuple returns multiple values - Vectors and matrices pass by reference (no copy); changes in C++ are immediately visible in Python numpy arrays
- Six examples: scalar variables, vector/matrix/string/dict, encapsulating C++ functions, passing Python functions to C++, persistent OO objects (NRpyCONSTRUCTOR/NRpyCONNECT macros), and sharing objects by name between namespaces
- Compilation produces
.so(Linux) or.pyd(Windows) shared libraries; first-time compile requires locating Python.h and numpy/arrayobject.h include paths - Key motivation: C++ executes orders of magnitude faster than Python for numerical functions, while Python controls the workflow
Connections: Numerical Recipes · Python · Python C Extension · Numerical Computing