Skip to main content

PyUntypedArrayMethods

Trait PyUntypedArrayMethods 

Source
pub trait PyUntypedArrayMethods<'py>: Sealed {
    // Required methods
    fn as_array_ptr(&self) -> *mut PyArrayObject;
    fn dtype(&self) -> Bound<'py, PyArrayDescr>;

    // Provided methods
    fn is_contiguous(&self) -> bool { ... }
    fn is_fortran_contiguous(&self) -> bool { ... }
    fn is_c_contiguous(&self) -> bool { ... }
    fn ndim(&self) -> usize { ... }
    fn strides(&self) -> &[isize] { ... }
    fn shape(&self) -> &[usize] { ... }
    fn len(&self) -> usize { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

Implementation of functionality for PyUntypedArray.

Required Methods§

Source

fn as_array_ptr(&self) -> *mut PyArrayObject

Returns a raw pointer to the underlying PyArrayObject.

Source

fn dtype(&self) -> Bound<'py, PyArrayDescr>

Returns the dtype of the array.

See also ndarray.dtype and PyArray_DTYPE.

§Example
use numpy::prelude::*;
use numpy::{dtype, PyArray};
use pyo3::Python;

Python::attach(|py| {
   let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);

   assert!(array.dtype().is_equiv_to(&dtype::<i32>(py)));
});

Provided Methods§

Source

fn is_contiguous(&self) -> bool

Returns true if the internal data of the array is contiguous, indepedently of whether C-style/row-major or Fortran-style/column-major.

§Example
use numpy::{PyArray1, PyUntypedArrayMethods};
use pyo3::{types::{IntoPyDict, PyAnyMethods}, Python, ffi::c_str};

Python::attach(|py| {
    let array = PyArray1::arange(py, 0, 10, 1);
    assert!(array.is_contiguous());

    let view = py
        .eval(c_str!("array[::2]"), None, Some(&[("array", array)].into_py_dict(py)?))?
        .cast_into::<PyArray1<i32>>()?;
    assert!(!view.is_contiguous());
})
Source

fn is_fortran_contiguous(&self) -> bool

Returns true if the internal data of the array is Fortran-style/column-major contiguous.

Source

fn is_c_contiguous(&self) -> bool

Returns true if the internal data of the array is C-style/row-major contiguous.

Source

fn ndim(&self) -> usize

Returns the number of dimensions of the array.

See also ndarray.ndim and PyArray_NDIM.

§Example
use numpy::{PyArray3, PyUntypedArrayMethods};
use pyo3::Python;

Python::attach(|py| {
    let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);

    assert_eq!(arr.ndim(), 3);
});
Source

fn strides(&self) -> &[isize]

Returns a slice indicating how many bytes to advance when iterating along each axis.

See also ndarray.strides and PyArray_STRIDES.

§Example
use numpy::{PyArray3, PyUntypedArrayMethods};
use pyo3::Python;

Python::attach(|py| {
    let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);

    assert_eq!(arr.strides(), &[240, 48, 8]);
});
Source

fn shape(&self) -> &[usize]

Returns a slice which contains dimmensions of the array.

See also ndarray.shape and PyArray_DIMS.

§Example
use numpy::{PyArray3, PyUntypedArrayMethods};
use pyo3::Python;

Python::attach(|py| {
    let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);

    assert_eq!(arr.shape(), &[4, 5, 6]);
});
Source

fn len(&self) -> usize

Calculates the total number of elements in the array.

Source

fn is_empty(&self) -> bool

Returns true if the there are no elements in the array.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'py, T, D> PyUntypedArrayMethods<'py> for Bound<'py, PyArray<T, D>>

Source§

impl<'py> PyUntypedArrayMethods<'py> for Bound<'py, PyUntypedArray>

Implementors§