Expand description
Arrays that Rust and the host language share without a copy.
This module defines two types, a bridge between numeric arrays in Rust and in the host language (Python, C, Wasm/JS). Both store a raw data pointer together with shape, strides (in bytes) and scalar type metadata.
- Tensor is an output. It always owns the value that holds its data (a
Box<dyn Any>), so the data stays valid while theTensorexists. It is the return type of SolutionWrapper::get_ys, SolutionWrapper::get_ts, SolutionWrapper::get_sens and of the OdeWrapper methods that give values. - TensorView is an input, for example
paramsandt_evalof the OdeWrapper solve methods. It does not own its data, and the lifetime of the view is that of the data. It is small,Copy, only on the stack, and has at most tensor::MAX_NDIM (3) axes. - TensorViewMut is a mutable view, the output
yof a C callback (see ode_callbacks). No two of its indices give the same value.
TensorView and TensorViewMut have the C layout of tensor::RawLayout (the fields ptr,
dtype, ndim, shape and strides), so a C callback reads their fields directly.
§Batches
A diffsol vector or dense matrix can have more than one lane (nbatch). The arrays use the
column-major order of diffsol, and the last axis of an array of lanes is always the lane,
also when nbatch is 1. For a diffsol vector, the
shape is [n, nbatch]. For a dense matrix, the shape is [nrows, ncols, nbatch]. If an input
array has one lane, all the lanes get its values.
§Key methods
- Tensor::from_vec — make an array that owns a
Vec, in column-major order. - Tensor::new — make an array that owns any value that holds the data on the heap
(
unsafe). - Tensor::view — a TensorView of an array, with no copy.
- TensorView::from_slice — view a slice, in column-major order.
- TensorView::new — view data with strides that obey the layout rules (
unsafe: the caller keeps the data valid while the view exists). It gives an error for other layouts, for example a row-major array. - Tensor::column, Tensor::columns (TensorView::column, TensorView::columns) — the
columns
[:, j, k]as slices, with no copy. Each column is contiguous, by the layout rules. - Tensor::index (TensorView::index) — a reference to one value. The Tensor docs give the data layout and the layout rules.
- Tensor::shape, Tensor::strides, Tensor::dtype, Tensor::data_ptr (and the same methods of TensorView) — the metadata. With these, a host language can show the data with no copy, for example as a NumPy array with the same shape and strides.
Structs§
- RawLayout
- The data pointer, the scalar type, the shape and the byte strides of a Tensor, a
TensorView or a TensorViewMut. A
RawLayoutalways obeys the layout rules (the Tensor docs give the rules): each constructor checks them. It does not own or borrow the data, so the reads areunsafe. - Tensor
- An N-D array of
f32orf64values that Rust owns, and that the host language (C, Python) reads without a copy. The outputs of this crate areTensorvalues. The inputs are TensorView values. - Tensor
View - A view of an N-D array of
f32orf64values that the view does not own. The inputs of this crate areTensorViewvalues. - Tensor
View Mut - A mutable view of an N-D array of
f32orf64values that the view does not own.
Constants§
- MAX_
NDIM - The maximum number of axes of a Tensor or a TensorView.
Traits§
- Tensor
Layout - A value that a Tensor can show with no copy of the data.
- Tensor
Layout Mut - A diffsol vector that a TensorViewMut can show, so that a C callback can write its values
with no copy. The layout is
[n, nbatch], the same as TensorLayout::tensor_layout.