Type Alias PyReadonlyArray3

Source
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>
where N: Scalar + Element, D: Dimension,

Source

pub fn try_as_matrix<R, C, RStride, CStride>( &self, ) -> Option<MatrixView<'_, N, R, C, RStride, CStride>>
where R: Dim, C: Dim, RStride: Dim, CStride: Dim,

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>
where T: Element, D: Dimension,

Source

pub fn as_array(&self) -> ArrayView<'_, T, D>

Provides an immutable array view of the interior of the NumPy array.

Source

pub fn as_slice(&self) -> Result<&[T], NotContiguousError>

Provide an immutable slice view of the interior of the NumPy array if it is contiguous.

Source

pub fn get<I>(&self, index: I) -> Option<&T>
where I: NpyIndex<Dim = D>,

Provide an immutable reference to an element of the NumPy array if the index is within bounds.

Trait Implementations

Source§

impl<'py, T, D> Clone for PyReadonlyArray<'py, T, D>
where T: Element, D: Dimension,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'py, T, D> Debug for PyReadonlyArray<'py, T, D>
where T: Element, D: Dimension,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'py, T, D> Deref for PyReadonlyArray<'py, T, D>
where T: Element, D: Dimension,

Source§

type Target = Bound<'py, PyArray<T, D>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'py, T, D> Drop for PyReadonlyArray<'py, T, D>
where T: Element, D: Dimension,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'py, T, D> From<PyReadwriteArray<'py, T, D>> for PyReadonlyArray<'py, T, D>
where T: Element, D: Dimension,

Source§

fn from(value: PyReadwriteArray<'py, T, D>) -> Self

Converts to this type from the input type.
Source§

impl<'py, T: Element, D: Dimension> FromPyObject<'py> for PyReadonlyArray<'py, T, D>

Source§

fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult<Self>

Extracts Self from the bound smart pointer obj. Read more