nalgebra-numpy
This crate provides conversion between nalgebra
and numpy
.
Currently, only the conversion from numpy to nalgebra is implemented, but the other direction will be added soon.
Conversion from numpy to nalgebra.
It is possible to create either a view or a copy of a numpy array.
You can use matrix_from_python
to copy the data into a new matrix,
or one of matrix_slice_from_python
or matrix_slice_mut_from_python
to create a view.
Keep in mind though that the borrow checker can not enforce rules on data managed by a Python object. You could potentially keep an immutable view around in Rust, and then modify the data from Python. For this reason, creating any view -- even an immutable one -- is unsafe.
Examples.
Copy a numpy array to a new fixed size matrix:
use ;
use ;
let gil = acquire_gil;
let context = new_with_gil.unwrap;
python!
let matrix = context.globals.get_item.unwrap;
let matrix : Matrix3 = matrix_from_python?;
assert_eq!;
Dynamic matrices are also supported:
use DMatrix;
#
// <snip>
let matrix : = matrix_from_python?;
assert_eq!;
And so are partially dynamic matrices:
use ;
// <snip>
let matrix : = matrix_from_python?;
assert_eq!;