Skip to content

Commit 9379893

Browse files
committed
Document the Fortran memory layout of NumPy arrays created from nalgebra matrices and test the conversion of the naglebra vector aliases.
1 parent 7eeff58 commit 9379893

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/convert.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ where
194194
type Item = N;
195195
type Dim = crate::Ix2;
196196

197+
/// Note that the NumPy array always has Fortran memory layout
198+
/// matching the [memory layout][memory-layout] used by [`nalgebra`].
199+
///
200+
/// [memory-layout]: https://nalgebra.org/docs/faq/#what-is-the-memory-layout-of-matrices
197201
fn to_pyarray<'py>(&self, py: Python<'py>) -> &'py PyArray<Self::Item, Self::Dim> {
198202
unsafe {
199203
let array = PyArray::<N, _>::new(py, (self.nrows(), self.ncols()), true);

tests/to_py.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,20 @@ fn matrix_to_numpy() {
322322

323323
assert_eq!(array.readonly().as_array(), array![[0, 1, 2]]);
324324
});
325+
326+
let vector = nalgebra::Vector4::<i32>::new(-4, 1, 2, 3);
327+
328+
Python::with_gil(|py| {
329+
let array = vector.to_pyarray(py);
330+
331+
assert_eq!(array.readonly().as_array(), array![[-4], [1], [2], [3]]);
332+
});
333+
334+
let vector = nalgebra::RowVector2::<i32>::new(23, 42);
335+
336+
Python::with_gil(|py| {
337+
let array = vector.to_pyarray(py);
338+
339+
assert_eq!(array.readonly().as_array(), array![[23, 42]]);
340+
});
325341
}

0 commit comments

Comments
 (0)