Expand description
This crate provides Rust interfaces for NumPy C APIs, especially for the ndarray class.
It uses pyo3
for Rust bindings to CPython, and uses
ndarray
as the Rust matrix library.
To resolve its dependency on NumPy, it calls import numpy.core
internally.
This means that this crate should work if you can use NumPy in your Python environment,
e.g. after installing it by pip install numpy
. It does not matter whether you use
the system environment or a dedicated virtual environment.
Loading NumPy is done automatically and on demand. So if it is not installed, the functions provided by this crate will panic instead of returning a result.
Integration with nalgebra
is provided via an implementation of ToPyArray
for nalgebra::Matrix
to convert nalgebra matrices into NumPy arrays
as well as the PyReadonlyArray::try_as_matrix
and PyReadwriteArray::try_as_matrix_mut
methods to treat NumPy array as nalgebra matrix slices.
§Example
use numpy::pyo3::Python;
use numpy::ndarray::array;
use numpy::{ToPyArray, PyArray, PyArrayMethods};
Python::with_gil(|py| {
let py_array = array![[1i64, 2], [3, 4]].to_pyarray(py);
assert_eq!(
py_array.readonly().as_array(),
array![[1i64, 2], [3, 4]]
);
});
use numpy::pyo3::Python;
use numpy::nalgebra::Matrix3;
use numpy::{pyarray, ToPyArray, PyArrayMethods};
Python::with_gil(|py| {
let py_array = pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];
let py_array_square;
{
let py_array = py_array.readwrite();
let mut na_matrix = py_array.as_matrix_mut();
na_matrix.add_scalar_mut(1);
py_array_square = na_matrix.pow(2).to_pyarray(py);
}
assert_eq!(
py_array.readonly().as_matrix(),
Matrix3::new(1, 2, 3, 4, 5, 6, 7, 8, 9)
);
assert_eq!(
py_array_square.readonly().as_matrix(),
Matrix3::new(30, 36, 42, 66, 81, 96, 102, 126, 150)
);
});
Re-exports§
pub use crate::array::get_array_module;
pub use crate::array::PyArray;
pub use crate::array::PyArray0;
pub use crate::array::PyArray0Methods;
pub use crate::array::PyArray1;
pub use crate::array::PyArray2;
pub use crate::array::PyArray3;
pub use crate::array::PyArray4;
pub use crate::array::PyArray5;
pub use crate::array::PyArray6;
pub use crate::array::PyArrayDyn;
pub use crate::array::PyArrayMethods;
pub use crate::borrow::PyReadonlyArray;
pub use crate::borrow::PyReadonlyArray0;
pub use crate::borrow::PyReadonlyArray1;
pub use crate::borrow::PyReadonlyArray2;
pub use crate::borrow::PyReadonlyArray3;
pub use crate::borrow::PyReadonlyArray4;
pub use crate::borrow::PyReadonlyArray5;
pub use crate::borrow::PyReadonlyArray6;
pub use crate::borrow::PyReadonlyArrayDyn;
pub use crate::borrow::PyReadwriteArray;
pub use crate::borrow::PyReadwriteArray0;
pub use crate::borrow::PyReadwriteArray1;
pub use crate::borrow::PyReadwriteArray2;
pub use crate::borrow::PyReadwriteArray3;
pub use crate::borrow::PyReadwriteArray4;
pub use crate::borrow::PyReadwriteArray5;
pub use crate::borrow::PyReadwriteArray6;
pub use crate::borrow::PyReadwriteArrayDyn;
pub use crate::convert::IntoPyArray;
pub use crate::convert::NpyIndex;
pub use crate::convert::ToNpyDims;
pub use crate::convert::ToPyArray;
pub use crate::npyffi::PY_ARRAY_API;
pub use crate::npyffi::PY_UFUNC_API;
pub use ndarray;
pub use pyo3;
pub use nalgebra;
Modules§
- array
- Safe interface for NumPy’s N-dimensional arrays
- borrow
- Types to safely create references into NumPy arrays
- convert
- Defines conversion traits between Rust types and NumPy data types.
- datetime
- Support datetimes and timedeltas
- npyffi
- Low-Level bindings for NumPy C API.
- prelude
- A prelude
Macros§
- array
- Create an
Array
with one, two, three, four, five, or six dimensions. - einsum
- Return the Einstein summation convention of given tensors.
- einsum_
bound Deprecated - Deprecated name for [
einsum!
]. - pyarray
- Create a
PyArray
with one, two or three dimensions. - pyarray_
bound Deprecated - Deprecated name for
pyarray
.
Structs§
- Allow
Type Change - Marker type to indicate that the element type received via
PyArrayLike
can be cast to the specified type by NumPy’sasarray
. - From
VecError - Represents that given
Vec
cannot be treated as an array. - NotContiguous
Error - Represents that the given array is not contiguous.
- PyArray
Descr - Binding of
numpy.dtype
. - PyArray
Like - Receiver for arrays or array-like types.
- PyFixed
String - A newtype wrapper around
[u8; N]
to handlebyte
scalars while satisfying coherence. - PyFixed
Unicode - A newtype wrapper around
[PyUCS4; N]
to handlestr_
scalars while satisfying coherence. - PyUntyped
Array - A safe, untyped wrapper for NumPy’s
ndarray
class. - Type
Must Match - Marker type to indicate that the element type received via
PyArrayLike
must match the specified type exactly.
Enums§
- Borrow
Error - Inidcates why borrowing an array failed.
Traits§
- Element
- Represents that a type can be an element of
PyArray
. - PyArray
Descr Methods - Implementation of functionality for
PyArrayDescr
. - PyUntyped
Array Methods - Implementation of functionality for
PyUntypedArray
.
Functions§
- Ix1
- Create a one-dimensional index
- Ix2
- Create a two-dimensional index
- Ix3
- Create a three-dimensional index
- Ix4
- Create a four-dimensional index
- Ix5
- Create a five-dimensional index
- Ix6
- Create a six-dimensional index
- IxDyn
- Create a dynamic-dimensional index
- dot
- Return the dot product of two arrays.
- dot_
bound Deprecated - Deprecated name for
dot
. - dtype
- Returns the type descriptor (“dtype”) for a registered type.
- dtype_
bound Deprecated - Deprecated name for
dtype
. - einsum
- Return the Einstein summation convention of given tensors.
- einsum_
bound Deprecated - Deprecated name for
einsum
. - inner
- Return the inner product of two arrays.
- inner_
bound Deprecated - Deprecated name for
inner
.
Type Aliases§
- Complex32
- Alias for a
Complex<f32>
- Complex64
- Alias for a
Complex<f64>
- Ix1
- one-dimensional
- Ix2
- two-dimensional
- Ix3
- three-dimensional
- Ix4
- four-dimensional
- Ix5
- five-dimensional
- Ix6
- six-dimensional
- IxDyn
- dynamic-dimensional
- PyArray
Like0 - Receiver for zero-dimensional arrays or array-like types.
- PyArray
Like1 - Receiver for one-dimensional arrays or array-like types.
- PyArray
Like2 - Receiver for two-dimensional arrays or array-like types.
- PyArray
Like3 - Receiver for three-dimensional arrays or array-like types.
- PyArray
Like4 - Receiver for four-dimensional arrays or array-like types.
- PyArray
Like5 - Receiver for five-dimensional arrays or array-like types.
- PyArray
Like6 - Receiver for six-dimensional arrays or array-like types.
- PyArray
Like Dyn - Receiver for arrays or array-like types whose dimensionality is determined at runtime.