EigenPy
EigenPy provides seamless Python bindings between NumPy arrays and Eigen C++ dense matrix types, utilizing Boost.Python. It allows for efficient data exchange and manipulation, exposing Eigen types and operations directly to Python. The current version is 3.12.0, with a release cadence that tends to follow updates in underlying dependencies or related projects like Pinocchio and Crocoddyl.
Warnings
- breaking With the release of EigenPy v3.0.0, the exposed Eigen C++ types moved from a nested namespace (e.g., `eigenpy.hpp.MatrixXd`) directly into the main `eigenpy` module.
- gotcha The function `eigenpy.enableEigenPy()` must be called exactly once in your program's lifecycle before any EigenPy functionality (like type conversions or creating Eigen objects) can be used. Forgetting this will lead to errors.
- gotcha Conversions between Eigen matrices and NumPy arrays have different memory semantics. Converting an Eigen matrix to NumPy using `np.asarray(eigen_matrix)` creates a *view* (no copy), meaning changes to the NumPy array will reflect in the Eigen matrix. Conversely, converting a NumPy array to Eigen using `eigenpy.toEigen(numpy_array)` creates a *copy* by default.
- gotcha EigenPy leverages Boost.Python for its bindings. While transparent for basic use, advanced users developing custom C++ extensions that interact with EigenPy might need to ensure their build system (e.g., CMake) correctly links against Boost.Python and Eigen versions compatible with the installed EigenPy wheel.
Install
-
pip install eigenpy
Imports
- eigenpy
import eigenpy
- enableEigenPy
eigenpy.enableEigenPy()
- MatrixXd
eigenpy.MatrixXd
- toEigen
eigenpy.toEigen
- toNumpy
eigenpy.toNumpy
Quickstart
import eigenpy
import numpy as np
# IMPORTANT: Enable EigenPy functionality
eigenpy.enableEigenPy()
# Create an Eigen MatrixXd instance (from C++ bindings)
mat_eigen = eigenpy.MatrixXd(3, 3)
mat_eigen.setRandom()
print('Eigen MatrixXd (random):\n', mat_eigen)
# Convert Eigen matrix to NumPy array (creates a view by default)
np_view = np.asarray(mat_eigen)
print('\nNumPy view of Eigen matrix:\n', np_view)
# Modifying the NumPy view also modifies the underlying Eigen matrix
np_view[0, 0] = 99.0
print('\nEigen MatrixXd after NumPy view modification:\n', mat_eigen)
# Convert NumPy array to Eigen matrix (creates a copy by default)
numpy_orig = np.array([[1.0, 2.0], [3.0, 4.0]])
mat_eigen_from_np = eigenpy.toEigen(numpy_orig)
print('\nEigen matrix from NumPy array:\n', mat_eigen_from_np)