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§
Sourcefn as_array_ptr(&self) -> *mut PyArrayObject
fn as_array_ptr(&self) -> *mut PyArrayObject
Returns a raw pointer to the underlying PyArrayObject
.
Sourcefn dtype(&self) -> Bound<'py, PyArrayDescr>
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::with_gil(|py| {
let array = PyArray::from_vec(py, vec![1_i32, 2, 3]);
assert!(array.dtype().is_equiv_to(&dtype::<i32>(py)));
});
Provided Methods§
Sourcefn is_contiguous(&self) -> bool
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::with_gil(|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)?))?
.downcast_into::<PyArray1<i32>>()?;
assert!(!view.is_contiguous());
})
Sourcefn is_fortran_contiguous(&self) -> bool
fn is_fortran_contiguous(&self) -> bool
Returns true
if the internal data of the array is Fortran-style/column-major contiguous.
Sourcefn is_c_contiguous(&self) -> bool
fn is_c_contiguous(&self) -> bool
Returns true
if the internal data of the array is C-style/row-major contiguous.
Sourcefn ndim(&self) -> usize
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::with_gil(|py| {
let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);
assert_eq!(arr.ndim(), 3);
});
Sourcefn strides(&self) -> &[isize]
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::with_gil(|py| {
let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);
assert_eq!(arr.strides(), &[240, 48, 8]);
});
Sourcefn shape(&self) -> &[usize]
fn shape(&self) -> &[usize]
Returns a slice which contains dimmensions of the array.
See also [ndarray.shape
][ndaray-shape] and PyArray_DIMS
.
§Example
use numpy::{PyArray3, PyUntypedArrayMethods};
use pyo3::Python;
Python::with_gil(|py| {
let arr = PyArray3::<f64>::zeros(py, [4, 5, 6], false);
assert_eq!(arr.shape(), &[4, 5, 6]);
});