pub type PyReadonlyArray3<'py, T> = PyReadonlyArray<'py, T, Ix3>;
Expand description
Read-only borrow of a three-dimensional array.
Aliased Type§
struct PyReadonlyArray3<'py, T> { /* private fields */ }
Implementations
Source§impl<'py, N, D> PyReadonlyArray<'py, N, D>
impl<'py, N, D> PyReadonlyArray<'py, N, D>
Sourcepub fn try_as_matrix<R, C, RStride, CStride>(
&self,
) -> Option<MatrixView<'_, N, R, C, RStride, CStride>>
pub fn try_as_matrix<R, C, RStride, CStride>( &self, ) -> Option<MatrixView<'_, N, R, C, RStride, CStride>>
Try to convert this array into a nalgebra::MatrixView
using the given shape and strides.
Note that nalgebra’s types default to Fortan/column-major standard strides whereas NumPy creates C/row-major strides by default. Furthermore, array views created by slicing into existing arrays will often have non-standard strides.
If you do not fully control the memory layout of a given array, e.g. at your API entry points, it can be useful to opt into nalgebra’s support for dynamic strides, for example
use pyo3::{py_run, ffi::c_str};
use numpy::{get_array_module, PyReadonlyArray2};
use nalgebra::{MatrixView, Const, Dyn};
#[pyfunction]
fn sum_standard_layout<'py>(py: Python<'py>, array: PyReadonlyArray2<'py, f64>) -> Option<f64> {
let matrix: Option<MatrixView<f64, Const<2>, Const<2>>> = array.try_as_matrix();
matrix.map(|matrix| matrix.sum())
}
#[pyfunction]
fn sum_dynamic_strides<'py>(py: Python<'py>, array: PyReadonlyArray2<'py, f64>) -> Option<f64> {
let matrix: Option<MatrixView<f64, Const<2>, Const<2>, Dyn, Dyn>> = array.try_as_matrix();
matrix.map(|matrix| matrix.sum())
}
Python::with_gil(|py| {
let np = py.eval(c_str!("__import__('numpy')"), None, None)?;
let sum_standard_layout = wrap_pyfunction!(sum_standard_layout)(py)?;
let sum_dynamic_strides = wrap_pyfunction!(sum_dynamic_strides)(py)?;
py_run!(py, np sum_standard_layout, r"assert sum_standard_layout(np.ones((2, 2), order='F')) == 4.");
py_run!(py, np sum_standard_layout, r"assert sum_standard_layout(np.ones((2, 2, 2))[:,:,0]) is None");
py_run!(py, np sum_dynamic_strides, r"assert sum_dynamic_strides(np.ones((2, 2), order='F')) == 4.");
py_run!(py, np sum_dynamic_strides, r"assert sum_dynamic_strides(np.ones((2, 2, 2))[:,:,0]) == 4.");
})
Source§impl<'py, T, D> PyReadonlyArray<'py, T, D>
impl<'py, T, D> PyReadonlyArray<'py, T, D>
Trait Implementations
Source§impl<'py, T, D> Clone for PyReadonlyArray<'py, T, D>
impl<'py, T, D> Clone for PyReadonlyArray<'py, T, D>
Source§impl<'py, T, D> Debug for PyReadonlyArray<'py, T, D>
impl<'py, T, D> Debug for PyReadonlyArray<'py, T, D>
Source§impl<'py, T, D> Deref for PyReadonlyArray<'py, T, D>
impl<'py, T, D> Deref for PyReadonlyArray<'py, T, D>
Source§impl<'py, T, D> Drop for PyReadonlyArray<'py, T, D>
impl<'py, T, D> Drop for PyReadonlyArray<'py, T, D>
Source§impl<'py, T, D> From<PyReadwriteArray<'py, T, D>> for PyReadonlyArray<'py, T, D>
impl<'py, T, D> From<PyReadwriteArray<'py, T, D>> for PyReadonlyArray<'py, T, D>
Source§fn from(value: PyReadwriteArray<'py, T, D>) -> Self
fn from(value: PyReadwriteArray<'py, T, D>) -> Self
Converts to this type from the input type.