Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor { /* private fields */ }
Expand description

An N-D array of f32 or f64 values that Rust owns, and that the host language (C, Python) reads without a copy. The outputs of this crate are Tensor values. The inputs are TensorView values.

§Data layout

A Tensor has a data pointer, a scalar type (dtype), a shape and a strides value for each axis. The strides are in bytes. The value at the index (i0, i1, ...) is at the byte offset i0 * strides[0] + i1 * strides[1] + ... from the data pointer.

The arrays use the column-major (Fortran) order of diffsol: the first axis changes fastest, and the lane axis is the last axis. The arrays that this crate makes from diffsol values have these layouts (s is the size of one value, and c is the column stride):

  • A diffsol vector is [n, nbatch], with the strides [s, c]. Each lane is one column.
  • A diffsol dense matrix is [nrows, ncols, nbatch], with the strides [s, c, ncols * c]. Each lane is a column-major matrix.
  • A Rust Vec<T>, for example ts, is [n], with the stride [s].

c is nrows * s for nalgebra. It can be larger for faer, which pads its columns. The padding is not part of the array, and its memory is not initialized. A solution can also have spare columns after the last time of each lane, which are not part of the array.

§Layout rules

Each Tensor and TensorView obeys these rules, which the constructors check:

  • It has 1 to MAX_NDIM axes.
  • The values of a column, [:, j, k], are adjacent: strides[0] is the size of one value (or the first axis has 1 value or none).
  • The data pointer is aligned, and each stride is a multiple of the size of one value.

So each column is one slice, which Tensor::column and Tensor::columns give with no copy. The padding is only after a column, so a column never contains it. Tensor::index gives one value.

§Ownership

A Tensor always owns the value that holds its data (the owner), so the data stays valid while the array exists.

Implementations§

Source§

impl Tensor

Source

pub unsafe fn new<O: 'static>( owner: O, ptr: *const u8, shape: &[usize], strides: &[usize], dtype: ScalarType, ) -> Self

Make an array that owns owner, with a shape and strides (in bytes) that obey the layout rules. The Tensor docs give the data layout and the rules.

§Panics

If the layout does not obey the layout rules.

§Safety

ptr must point into data that owner holds on the heap, so that the data does not move when owner moves into the array. For each index in shape, ptr plus the byte offset from strides must point to a valid value of the type dtype. The data must not change while the array exists.

Source

pub fn from_vec<T: Scalar + 'static>(values: Vec<T>, shape: &[usize]) -> Self

Make an array that owns values, with the shape shape in column-major (Fortran) order.

§Panics

If the number of values is not the product of shape, or shape does not have 1 to MAX_NDIM axes.

Source

pub fn view(&self) -> TensorView<'_>

A view of the array, with no copy.

Source

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

The length of each axis.

Source

pub fn strides(&self) -> &[usize]

The stride of each axis, in bytes. The Tensor docs give the data layout.

Source

pub fn data_ptr(&self) -> *const u8

The pointer to the first value. It is valid while self exists, and the data must not be changed through it.

Source

pub fn dtype(&self) -> ScalarType

The scalar type of the values.

Source

pub fn index<T: Scalar>( &self, index: impl AsRef<[usize]>, ) -> Result<&T, DiffsolRtError>

A reference to the value at index, with one value for each axis, for example tensor.index::<f64>([0, i, 0]). The scalar type must be T.

§Panics

If index does not have one value for each axis, or a value is out of range.

Source

pub fn column<T: Scalar>(&self, index: &[usize]) -> Result<&[T], DiffsolRtError>

The column at index: the values [:, index[0], index[1], ...], as one slice of shape[0] values, with no copy. index has one value for each axis after the first. The scalar type must be T.

For a vector [n, nbatch], column(&[b]) is lane b. For a matrix [nrows, ncols, nbatch], column(&[j, b]) is column j of lane b. For a 1D array, column(&[]) is all the values.

§Panics

If index does not have one value for each axis after the first, or a value is out of range.

Source

pub fn columns<T: Scalar>( &self, ) -> Result<impl Iterator<Item = &[T]> + '_, DiffsolRtError>

All the columns (see Tensor::column), in column-major order: the index on axis 1 changes fastest. Together, they give all the values in column-major order. The scalar type must be T.

Trait Implementations§

Source§

impl<'a> From<&'a Tensor> for TensorView<'a>

Source§

fn from(tensor: &'a Tensor) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Tensor

§

impl !Send for Tensor

§

impl !Sync for Tensor

§

impl !UnwindSafe for Tensor

§

impl Forget for Tensor

§

impl Freeze for Tensor

§

impl Move for Tensor

§

impl Unpin for Tensor

§

impl UnsafeUnpin for Tensor

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.