pub struct TensorRange<T, S, const D: usize> { /* private fields */ }
Expand description

A range over a tensor in D dimensions, hiding the values outside the range from view.

The entire source is still owned by the TensorRange however, so this does not permit creating multiple mutable ranges into a single tensor even if they wouldn’t overlap.

See also: TensorMask

use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorRange};
let numbers = Tensor::from([("batch", 4), ("rows", 8), ("columns", 8)], vec![
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 1, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 0, 1, 1, 0, 0, 0,
    0, 0, 1, 1, 1, 1, 0, 0,
    0, 0, 1, 1, 1, 1, 0, 0,

    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 2, 2, 0, 0, 0,
    0, 0, 2, 0, 0, 2, 0, 0,
    0, 0, 0, 0, 0, 2, 0, 0,
    0, 0, 0, 0, 2, 0, 0, 0,
    0, 0, 0, 2, 0, 0, 0, 0,
    0, 0, 2, 0, 0, 0, 0, 0,
    0, 0, 2, 2, 2, 2, 0, 0,

    0, 0, 0, 3, 3, 0, 0, 0,
    0, 0, 3, 0, 0, 3, 0, 0,
    0, 0, 0, 0, 0, 3, 0, 0,
    0, 0, 0, 0, 3, 0, 0, 0,
    0, 0, 0, 0, 3, 0, 0, 0,
    0, 0, 0, 0, 0, 3, 0, 0,
    0, 0, 3, 0, 0, 3, 0, 0,
    0, 0, 0, 3, 3, 0, 0, 0,

    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 4, 4, 0, 0, 0,
    0, 0, 4, 0, 4, 0, 0, 0,
    0, 4, 4, 4, 4, 4, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0,
    0, 0, 0, 0, 4, 0, 0, 0
]);
let one_and_two = TensorView::from(
    TensorRange::from(&numbers, [("batch", 0..2)])
        .expect("Input is constucted so that our range is valid")
);
let framed = TensorView::from(
    TensorRange::from(&numbers, [("rows", [1, 6]), ("columns", [1, 6])])
        .expect("Input is constucted so that our range is valid")
);
assert_eq!(one_and_two.shape(), [("batch", 2), ("rows", 8), ("columns", 8)]);
assert_eq!(framed.shape(), [("batch", 4), ("rows", 6), ("columns", 6)]);
println!("{}", framed.select([("batch", 3)]));
// D = 2
// ("rows", 6), ("columns", 6)
// [ 0, 0, 0, 4, 0, 0
//   0, 0, 4, 4, 0, 0
//   0, 4, 0, 4, 0, 0
//   4, 4, 4, 4, 4, 0
//   0, 0, 0, 4, 0, 0
//   0, 0, 0, 4, 0, 0 ]

Implementations§

source§

impl<T, S, const D: usize> TensorRange<T, S, D>where
    S: TensorRef<T, D>,

source

pub fn from<R, const P: usize>(
    source: S,
    ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, IndexRangeValidationError<D, P>>where
    R: Into<IndexRange>,

Constructs a TensorRange from a tensor and set of dimension name/range pairs.

Returns the Err variant if any dimension would have a length of 0 after applying the ranges, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source.

source

pub fn from_strict<R, const P: usize>(
    source: S,
    ranges: [(Dimension, R); P]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, P>>where
    R: Into<IndexRange>,

Constructs a TensorRange from a tensor and set of dimension name/range pairs.

Returns the Err variant if any dimension would have a length of 0 after applying the ranges, if multiple pairs with the same name are provided, or if any dimension names aren’t in the source, or any range extends beyond the length of that dimension in the tensor.

source

pub fn from_all<R>(
    source: S,
    ranges: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, InvalidShapeError<D>>where
    R: Into<IndexRange>,

Constructs a TensorRange from a tensor and a range for each dimension in the tensor (provided in the same order as the tensor’s shape).

Returns the Err variant if any dimension would have a length of 0 after applying the ranges.

source

pub fn from_all_strict<R>(
    source: S,
    range: [Option<R>; D]
) -> Result<TensorRange<T, S, D>, StrictIndexRangeValidationError<D, D>>where
    R: Into<IndexRange>,

Constructs a TensorRange from a tensor and a range for each dimension in the tensor (provided in the same order as the tensor’s shape), ensuring the range is within the lengths of the tensor.

Returns the Err variant if any dimension would have a length of 0 after applying the ranges or any range extends beyond the length of that dimension in the tensor.

Trait Implementations§

source§

impl<T: Clone, S: Clone, const D: usize> Clone for TensorRange<T, S, D>

source§

fn clone(&self) -> TensorRange<T, S, D>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug, S: Debug, const D: usize> Debug for TensorRange<T, S, D>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T, S, const D: usize> TensorMut<T, D> for TensorRange<T, S, D>where
    S: TensorMut<T, D>,

A TensorRange implements TensorMut, with the dimension lengths reduced to the range the the TensorRange was created with.

source§

fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T>

Gets a mutable reference to the value at the index, if the index is in range. Otherwise returns None.
source§

unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T

Gets a mutable reference to the value at the index without doing any bounds checking. For a safe alternative see get_reference_mut. Read more
source§

impl<T, S, const D: usize> TensorRef<T, D> for TensorRange<T, S, D>where
    S: TensorRef<T, D>,

A TensorRange implements TensorRef, with the dimension lengths reduced to the range the the TensorRange was created with.

source§

fn get_reference(&self, indexes: [usize; D]) -> Option<&T>

Gets a reference to the value at the index if the index is in range. Otherwise returns None.
source§

fn view_shape(&self) -> [(Dimension, usize); D]

The shape this tensor has. See dimensions for an overview. The product of the lengths in the pairs define how many elements are in the tensor (or the portion of it that is visible).
source§

unsafe fn get_reference_unchecked(&self, indexes: [usize; D]) -> &T

Gets a reference to the value at the index without doing any bounds checking. For a safe alternative see get_reference. Read more
source§

fn data_layout(&self) -> DataLayout<D>

The way the data in this tensor is laid out in memory. In particular, Linear has several requirements on what is returned that must be upheld by implementations of this trait. Read more

Auto Trait Implementations§

§

impl<T, S, const D: usize> RefUnwindSafe for TensorRange<T, S, D>where
    S: RefUnwindSafe,
    T: RefUnwindSafe,

§

impl<T, S, const D: usize> Send for TensorRange<T, S, D>where
    S: Send,
    T: Send,

§

impl<T, S, const D: usize> Sync for TensorRange<T, S, D>where
    S: Sync,
    T: Sync,

§

impl<T, S, const D: usize> Unpin for TensorRange<T, S, D>where
    S: Unpin,
    T: Unpin,

§

impl<T, S, const D: usize> UnwindSafe for TensorRange<T, S, D>where
    S: UnwindSafe,
    T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere
    T: Clone,

§

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 Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.