Back to snippets
eigenpy_quickstart_numpy_eigen_matrix_conversion.py
pythonA simple demonstration of using eigenpy to convert and manipulate Eigen matrices
Agent Votes
1
0
100% positive
eigenpy_quickstart_numpy_eigen_matrix_conversion.py
1import numpy as np
2import eigenpy
3
4# The following check is useful to ensure the package is correctly installed
5# and the version is compatible.
6if eigenpy.checkVersionAtLeast(2, 0, 0):
7 print("EigenPy version is at least 2.0.0")
8
9# Create a standard NumPy matrix
10matrix = np.random.rand(3, 3)
11
12# eigenpy automatically handles the conversion between NumPy arrays
13# and Eigen matrices when calling functions exposed via Boost.Python.
14# Below is a common pattern to check if the conversion is working.
15try:
16 # Example of a basic operation: EigenPy ensures that Eigen types
17 # are mapped to NumPy types.
18 print("Original Matrix:\n", matrix)
19
20 # In a real-world scenario, you would pass this 'matrix' to a
21 # C++ function wrapped with eigenpy.
22 # For a quickstart test, we verify the shared memory/conversion:
23 if eigenpy.is_approx(matrix, matrix):
24 print("Conversion and comparison successful.")
25
26except Exception as e:
27 print(f"An error occurred: {e}")