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>
impl<'a> TensorView<'a>
Sourcepub unsafe fn new(
ptr: *const u8,
shape: &[usize],
strides: &[usize],
dtype: ScalarType,
) -> Result<Self, DiffsolRtError>
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.
Sourcepub unsafe fn new_col_major(
ptr: *const u8,
shape: &[usize],
dtype: ScalarType,
) -> Self
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.
Sourcepub fn from_slice<T: Scalar>(values: &'a [T], shape: &[usize]) -> Self
pub fn from_slice<T: Scalar>(values: &'a [T], shape: &[usize]) -> Self
Sourcepub fn strides(&self) -> &[usize]
pub fn strides(&self) -> &[usize]
The stride of each axis, in bytes. The Tensor docs give the data layout.
Sourcepub fn data_ptr(&self) -> *const u8
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.
Sourcepub fn dtype(&self) -> ScalarType
pub fn dtype(&self) -> ScalarType
The scalar type of the values.
Sourcepub fn index<T: Scalar>(
&self,
index: impl AsRef<[usize]>,
) -> Result<&'a T, DiffsolRtError>
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.
Sourcepub fn column<T: Scalar>(
&self,
index: &[usize],
) -> Result<&'a [T], DiffsolRtError>
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.
Sourcepub fn columns<T: Scalar>(
&self,
) -> Result<impl Iterator<Item = &'a [T]> + 'a, DiffsolRtError>
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>
impl<'a> Clone for TensorView<'a>
impl<'a> Copy for TensorView<'a>
Source§impl<'a> Debug for TensorView<'a>
impl<'a> Debug for TensorView<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.