nalgebra_numpy/
to_numpy.rs

1use nalgebra::Matrix;
2use numpy::PyArray;
3use pyo3::IntoPy;
4
5/// Copy a nalgebra matrix to a numpy ndarray.
6///
7/// This does not create a view of the nalgebra matrix.
8/// As such, the matrix can be dropped without problem.
9pub fn matrix_to_numpy<'py, N, R, C, S>(py: pyo3::Python<'py>, matrix: &Matrix<N, R, C, S>) -> pyo3::PyObject
10where
11	N: nalgebra::Scalar + numpy::Element,
12	R: nalgebra::Dim,
13	C: nalgebra::Dim,
14	S: nalgebra::storage::Storage<N, R, C>,
15{
16	let array = PyArray::new(py, (matrix.nrows(), matrix.ncols()), false);
17	for r in 0..matrix.nrows() {
18		for c in 0..matrix.ncols() {
19			unsafe {
20				*array.uget_mut((r, c)) = matrix[(r, c)].inlined_clone();
21			}
22		}
23	}
24
25	array.into_py(py)
26}