Skip to main content

TensorView

Struct TensorView 

Source
pub struct TensorView<'a> { /* private fields */ }
Expand description

A view of an N-D array of f32 or f64 values that the view does not own. The inputs of this crate are TensorView values.

A TensorView is small and Copy, and it is only on the stack: the shape and the strides are fixed-size arrays. It has the same data layout and the same layout rules as Tensor (strides in bytes, column-major order, lane axis last, adjacent values in each column). So a view of a row-major array is not possible: make a column-major (Fortran-ordered) copy first. The lifetime 'a is the lifetime of the data.

A TensorView has the C layout of RawLayout, so a C callback that gets a const TensorView * can read the fields ptr, dtype, ndim, shape and strides.

Implementations§

Source§

impl<'a> TensorView<'a>

Source

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

Make a view of data, with a shape and strides (in bytes) that obey the layout rules. The Tensor docs give the data layout and the rules.

§Errors

If the layout does not obey the layout rules: for example, the strides of a row-major array, more than MAX_NDIM axes, or data that is not aligned.

§Safety

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 stay valid, and must not change, during 'a.

Source

pub unsafe fn new_col_major( ptr: *const u8, shape: &[usize], dtype: ScalarType, ) -> Self

Make a view of data with the shape shape in column-major (Fortran) order.

§Panics

If shape does not have 1 to MAX_NDIM axes, or ptr is not aligned for dtype.

§Safety

ptr must point to product(shape) valid, contiguous values of the type dtype. The data must stay valid, and must not change, during 'a.

Source

pub fn from_slice<T: Scalar>(values: &'a [T], shape: &[usize]) -> Self

Make a view of 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 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 during 'a, 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<&'a T, DiffsolRtError>

A reference to the value at index, with one value for each axis. 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<&'a [T], DiffsolRtError>

The column at index: the values [:, index[0], index[1], ...], as one slice of shape[0] values, with no copy. Tensor::column gives the details.

§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 = &'a [T]> + 'a, DiffsolRtError>

All the columns, in column-major order. Tensor::columns gives the details.

Trait Implementations§

Source§

impl<'a> Clone for TensorView<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for TensorView<'a>

Source§

impl<'a> Debug for TensorView<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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<'a> !Send for TensorView<'a>

§

impl<'a> !Sync for TensorView<'a>

§

impl<'a> Forget for TensorView<'a>

§

impl<'a> Freeze for TensorView<'a>

§

impl<'a> Move for TensorView<'a>

§

impl<'a> RefUnwindSafe for TensorView<'a>

§

impl<'a> Unpin for TensorView<'a>

§

impl<'a> UnsafeUnpin for TensorView<'a>

§

impl<'a> UnwindSafe for TensorView<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.