pub struct Tensor<T, const D: usize> { /* private fields */ }
Expand description
A named tensor of some type T
and number of
dimensions D
.
Tensors are a generalisation of matrices; whereas Matrix only
supports 2 dimensions, and vectors are represented in Matrix by making either the rows or
columns have a length of one, Tensor supports an arbitary number of dimensions,
with 0 through 6 having full API support. A Tensor<T, 2>
is very similar to a Matrix<T>
except that this type associates each dimension with a name, and favor names to refer to
dimensions instead of index order.
Like Matrix, the type of the data in this Tensor may implement no traits, in which case the tensor will be rather useless. If the type implements Clone most storage and accessor methods are defined and if the type implements Numeric then the tensor can be used in a mathematical way.
Like Matrix, a Tensor must always contain at least one element, and it may not not have more
elements than std::isize::MAX
. Concerned readers should note that on a 64 bit computer this
maximum value is 9,223,372,036,854,775,807 so running out of memory is likely to occur first.
When doing numeric operations with Tensors you should be careful to not consume a tensor by accidentally using it by value. All the operations are also defined on references to tensors so you should favor &x + &y style notation for tensors you intend to continue using.
See also:
Implementations§
source§impl<T, const D: usize> Tensor<T, D>
impl<T, const D: usize> Tensor<T, D>
sourcepub fn from(shape: [(Dimension, usize); D], data: Vec<T>) -> Self
pub fn from(shape: [(Dimension, usize); D], data: Vec<T>) -> Self
Creates a Tensor with a particular number of dimensions and lengths in each dimension.
The product of the dimension lengths corresponds to the number of elements the Tensor
will store. Elements are stored in what would be row major order for a Matrix.
Each step in memory through the N dimensions corresponds to incrementing the rightmost
index, hence a shape of [("row", 5), ("column", 5)]
would mean the first 6 elements
passed in the Vec would be for (0,0), (0,1), (0,2), (0,3), (0,4), (1,0) and so on to (4,4)
for the 25th and final element.
§Panics
- If the number of provided elements does not match the product of the dimension lengths.
- If a dimension name is not unique
- If any dimension has 0 elements
Note that an empty list for dimensions is valid, and constructs a 0 dimensional tensor with a single element (since the product of an empty list is 1).
sourcepub fn from_fn<F>(shape: [(Dimension, usize); D], producer: F) -> Self
pub fn from_fn<F>(shape: [(Dimension, usize); D], producer: F) -> Self
Creates a Tensor with a particular shape initialised from a function.
The product of the dimension lengths corresponds to the number of elements the Tensor
will store. Elements are stored in what would be row major order for a Matrix.
Each step in memory through the N dimensions corresponds to incrementing the rightmost
index, hence a shape of [("row", 5), ("column", 5)]
would mean the first 6 elements
passed in the Vec would be for (0,0), (0,1), (0,2), (0,3), (0,4), (1,0) and so on to (4,4)
for the 25th and final element. These same indexes will be passed to the producer function
to initialised the values for the Tensor.
use easy_ml::tensors::Tensor;
let tensor = Tensor::from_fn([("rows", 4), ("columns", 4)], |[r, c]| r * c);
assert_eq!(
tensor,
Tensor::from([("rows", 4), ("columns", 4)], vec![
0, 0, 0, 0,
0, 1, 2, 3,
0, 2, 4, 6,
0, 3, 6, 9,
])
);
§Panics
- If a dimension name is not unique
- If any dimension has 0 elements
Note that an empty list for dimensions is valid, and constructs a 0 dimensional tensor with a single element (since the product of an empty list is 1).
sourcepub fn shape(&self) -> [(Dimension, usize); D]
pub fn shape(&self) -> [(Dimension, usize); D]
The shape of this tensor. Since Tensors are named Tensors, their shape is not just a list of lengths along each dimension, but instead a list of pairs of names and lengths.
See also
sourcepub fn try_from(
shape: [(Dimension, usize); D],
data: Vec<T>
) -> Result<Self, InvalidShapeError<D>>
pub fn try_from( shape: [(Dimension, usize); D], data: Vec<T> ) -> Result<Self, InvalidShapeError<D>>
A non panicking version of from which returns Result::Err
if the input
is invalid.
Creates a Tensor with a particular number of dimensions and lengths in each dimension.
The product of the dimension lengths corresponds to the number of elements the Tensor
will store. Elements are stored in what would be row major order for a Matrix.
Each step in memory through the N dimensions corresponds to incrementing the rightmost
index, hence a shape of [("row", 5), ("column", 5)]
would mean the first 6 elements
passed in the Vec would be for (0,0), (0,1), (0,2), (0,3), (0,4), (1,0) and so on to (4,4)
for the 25th and final element.
Returns the Err variant if
- If the number of provided elements does not match the product of the dimension lengths.
- If a dimension name is not unique
- If any dimension has 0 elements
Note that an empty list for dimensions is valid, and constructs a 0 dimensional tensor with a single element (since the product of an empty list is 1).
source§impl<T> Tensor<T, 0>
impl<T> Tensor<T, 0>
sourcepub fn from_scalar(value: T) -> Tensor<T, 0>
pub fn from_scalar(value: T) -> Tensor<T, 0>
Creates a 0 dimensional tensor from some scalar
sourcepub fn into_scalar(self) -> T
pub fn into_scalar(self) -> T
Returns the sole element of the 0 dimensional tensor.
source§impl<T, const D: usize> Tensor<T, D>
impl<T, const D: usize> Tensor<T, D>
pub fn view(&self) -> TensorView<T, &Tensor<T, D>, D>
pub fn view_mut(&mut self) -> TensorView<T, &mut Tensor<T, D>, D>
pub fn view_owned(self) -> TensorView<T, Tensor<T, D>, D>
sourcepub fn index_by(
&self,
dimensions: [Dimension; D]
) -> TensorAccess<T, &Tensor<T, D>, D>
pub fn index_by( &self, dimensions: [Dimension; D] ) -> TensorAccess<T, &Tensor<T, D>, D>
Returns a TensorAccess which can be indexed in the order of the supplied dimensions to read values from this tensor.
§Panics
If the set of dimensions supplied do not match the set of dimensions in this tensor’s shape.
sourcepub fn index_by_mut(
&mut self,
dimensions: [Dimension; D]
) -> TensorAccess<T, &mut Tensor<T, D>, D>
pub fn index_by_mut( &mut self, dimensions: [Dimension; D] ) -> TensorAccess<T, &mut Tensor<T, D>, D>
Returns a TensorAccess which can be indexed in the order of the supplied dimensions to read or write values from this tensor.
§Panics
If the set of dimensions supplied do not match the set of dimensions in this tensor’s shape.
sourcepub fn index_by_owned(
self,
dimensions: [Dimension; D]
) -> TensorAccess<T, Tensor<T, D>, D>
pub fn index_by_owned( self, dimensions: [Dimension; D] ) -> TensorAccess<T, Tensor<T, D>, D>
Returns a TensorAccess which can be indexed in the order of the supplied dimensions to read or write values from this tensor.
§Panics
If the set of dimensions supplied do not match the set of dimensions in this tensor’s shape.
sourcepub fn index(&self) -> TensorAccess<T, &Tensor<T, D>, D>
pub fn index(&self) -> TensorAccess<T, &Tensor<T, D>, D>
Creates a TensorAccess which will index into the dimensions this Tensor was created with in the same order as they were provided. See TensorAccess::from_source_order.
sourcepub fn index_mut(&mut self) -> TensorAccess<T, &mut Tensor<T, D>, D>
pub fn index_mut(&mut self) -> TensorAccess<T, &mut Tensor<T, D>, D>
Creates a TensorAccess which will index into the dimensions this Tensor was created with in the same order as they were provided. The TensorAccess mutably borrows the Tensor, and can therefore mutate it. See TensorAccess::from_source_order.
sourcepub fn index_owned(self) -> TensorAccess<T, Tensor<T, D>, D>
pub fn index_owned(self) -> TensorAccess<T, Tensor<T, D>, D>
Creates a TensorAccess which will index into the dimensions this Tensor was created with in the same order as they were provided. The TensorAccess takes ownership of the Tensor, and can therefore mutate it. See TensorAccess::from_source_order.
sourcepub fn iter_reference(&self) -> TensorReferenceIterator<'_, T, Tensor<T, D>, D> ⓘ
pub fn iter_reference(&self) -> TensorReferenceIterator<'_, T, Tensor<T, D>, D> ⓘ
Returns an iterator over references to the data in this Tensor.
sourcepub fn iter_reference_mut(
&mut self
) -> TensorReferenceMutIterator<'_, T, Tensor<T, D>, D> ⓘ
pub fn iter_reference_mut( &mut self ) -> TensorReferenceMutIterator<'_, T, Tensor<T, D>, D> ⓘ
Returns an iterator over mutable references to the data in this Tensor.
sourcepub fn iter_owned(self) -> TensorOwnedIterator<T, Tensor<T, D>, D> ⓘwhere
T: Default,
pub fn iter_owned(self) -> TensorOwnedIterator<T, Tensor<T, D>, D> ⓘwhere
T: Default,
Creates an iterator over the values in this Tensor.
sourcepub fn rename(&mut self, dimensions: [Dimension; D])
pub fn rename(&mut self, dimensions: [Dimension; D])
Renames the dimension names of the tensor without changing the lengths of the dimensions in the tensor or moving any data around.
use easy_ml::tensors::Tensor;
let mut tensor = Tensor::from([("x", 2), ("y", 3)], vec![1, 2, 3, 4, 5, 6]);
tensor.rename(["y", "z"]);
assert_eq!([("y", 2), ("z", 3)], tensor.shape());
§Panics
- If a dimension name is not unique
sourcepub fn rename_owned(self, dimensions: [Dimension; D]) -> Tensor<T, D>
pub fn rename_owned(self, dimensions: [Dimension; D]) -> Tensor<T, D>
Renames the dimension names of the tensor and returns it without changing the lengths of the dimensions in the tensor or moving any data around.
use easy_ml::tensors::Tensor;
let tensor = Tensor::from([("x", 2), ("y", 3)], vec![1, 2, 3, 4, 5, 6])
.rename_owned(["y", "z"]);
assert_eq!([("y", 2), ("z", 3)], tensor.shape());
§Panics
- If a dimension name is not unique
sourcepub fn rename_view(
&self,
dimensions: [Dimension; D]
) -> TensorView<T, TensorRename<T, &Tensor<T, D>, D>, D>
pub fn rename_view( &self, dimensions: [Dimension; D] ) -> TensorView<T, TensorRename<T, &Tensor<T, D>, D>, D>
Returns a TensorView with the dimension names of the shape renamed to the provided dimensions. The data of this tensor and the dimension lengths and order remain unchanged.
This is a shorthand for constructing the TensorView from this Tensor.
use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorRename};
let abc = Tensor::from([("a", 3), ("b", 3), ("c", 3)], (0..27).collect());
let xyz = abc.rename_view(["x", "y", "z"]);
let also_xyz = TensorView::from(TensorRename::from(&abc, ["x", "y", "z"]));
assert_eq!(xyz, also_xyz);
assert_eq!(xyz, Tensor::from([("x", 3), ("y", 3), ("z", 3)], (0..27).collect()));
§Panics
If a dimension name is not unique
sourcepub fn reshape_mut(&mut self, shape: [(Dimension, usize); D])
pub fn reshape_mut(&mut self, shape: [(Dimension, usize); D])
Changes the shape of the tensor without changing the number of dimensions or moving any data around.
§Panics
- If the number of provided elements in the new shape does not match the product of the dimension lengths in the existing tensor’s shape.
- If a dimension name is not unique
- If any dimension has 0 elements
use easy_ml::tensors::Tensor;
let mut tensor = Tensor::from([("width", 2), ("height", 2)], vec![
1, 2,
3, 4
]);
tensor.reshape_mut([("batch", 1), ("image", 4)]);
assert_eq!(tensor, Tensor::from([("batch", 1), ("image", 4)], vec![ 1, 2, 3, 4 ]));
sourcepub fn reshape_owned<const D2: usize>(
self,
shape: [(Dimension, usize); D2]
) -> Tensor<T, D2>
pub fn reshape_owned<const D2: usize>( self, shape: [(Dimension, usize); D2] ) -> Tensor<T, D2>
Consumes the tensor and changes the shape of the tensor without moving any data around. The new Tensor may also have a different number of dimensions.
§Panics
- If the number of provided elements in the new shape does not match the product of the dimension lengths in the existing tensor’s shape.
- If a dimension name is not unique
- If any dimension has 0 elements
use easy_ml::tensors::Tensor;
let tensor = Tensor::from([("width", 2), ("height", 2)], vec![
1, 2,
3, 4
]);
let flattened = tensor.reshape_owned([("image", 4)]);
assert_eq!(flattened, Tensor::from([("image", 4)], vec![ 1, 2, 3, 4 ]));
sourcepub fn range<R, const P: usize>(
&self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, &Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn range<R, const P: usize>(
&self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, &Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a range taken in P dimensions, hiding the values outside the range from view. Error cases are documented on TensorRange.
This is a shorthand for constructing the TensorView from this Tensor.
use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorRange, IndexRange};
let samples = Tensor::from([("batch", 5), ("x", 7), ("y", 7)], (0..(5 * 7 * 7)).collect());
let cropped = samples.range([("x", IndexRange::new(1, 5)), ("y", IndexRange::new(1, 5))])?;
let also_cropped = TensorView::from(
TensorRange::from(&samples, [("x", 1..6), ("y", 1..6)])?
);
assert_eq!(cropped, also_cropped);
assert_eq!(
cropped.select([("batch", 0)]),
Tensor::from([("x", 5), ("y", 5)], vec![
8, 9, 10, 11, 12,
15, 16, 17, 18, 19,
22, 23, 24, 25, 26,
29, 30, 31, 32, 33,
36, 37, 38, 39, 40
])
);
sourcepub fn range_mut<R, const P: usize>(
&mut self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, &mut Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn range_mut<R, const P: usize>(
&mut self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, &mut Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a range taken in P dimensions, hiding the values outside the range from view. Error cases are documented on TensorRange. The TensorRange mutably borrows this Tensor, and can therefore mutate it
This is a shorthand for constructing the TensorView from this Tensor.
sourcepub fn range_owned<R, const P: usize>(
self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn range_owned<R, const P: usize>(
self,
ranges: [(Dimension, R); P]
) -> Result<TensorView<T, TensorRange<T, Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a range taken in P dimensions, hiding the values outside the range from view. Error cases are documented on TensorRange. The TensorRange takes ownership of this Tensor, and can therefore mutate it
This is a shorthand for constructing the TensorView from this Tensor.
sourcepub fn mask<R, const P: usize>(
&self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, &Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn mask<R, const P: usize>(
&self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, &Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a mask taken in P dimensions, hiding the values inside the range from view. Error cases are documented on TensorMask.
This is a shorthand for constructing the TensorView from this Tensor.
use easy_ml::tensors::Tensor;
use easy_ml::tensors::views::{TensorView, TensorMask, IndexRange};
let samples = Tensor::from([("batch", 5), ("x", 7), ("y", 7)], (0..(5 * 7 * 7)).collect());
let corners = samples.mask([("x", IndexRange::new(3, 2)), ("y", IndexRange::new(3, 2))])?;
let also_corners = TensorView::from(
TensorMask::from(&samples, [("x", 3..5), ("y", 3..5)])?
);
assert_eq!(corners, also_corners);
assert_eq!(
corners.select([("batch", 0)]),
Tensor::from([("x", 5), ("y", 5)], vec![
0, 1, 2, 5, 6,
7, 8, 9, 12, 13,
14, 15, 16, 19, 20,
35, 36, 37, 40, 41,
42, 43, 44, 47, 48
])
);
sourcepub fn mask_mut<R, const P: usize>(
&mut self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, &mut Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn mask_mut<R, const P: usize>(
&mut self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, &mut Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a mask taken in P dimensions, hiding the values inside the range from view. Error cases are documented on TensorMask. The TensorMask mutably borrows this Tensor, and can therefore mutate it
This is a shorthand for constructing the TensorView from this Tensor.
sourcepub fn mask_owned<R, const P: usize>(
self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
pub fn mask_owned<R, const P: usize>(
self,
masks: [(Dimension, R); P]
) -> Result<TensorView<T, TensorMask<T, Tensor<T, D>, D>, D>, IndexRangeValidationError<D, P>>where
R: Into<IndexRange>,
Returns a TensorView with a mask taken in P dimensions, hiding the values inside the range from view. Error cases are documented on TensorMask. The TensorMask takes ownership of this Tensor, and can therefore mutate it
This is a shorthand for constructing the TensorView from this Tensor.
sourcepub fn elementwise_reference<S, I, M>(
&self,
rhs: I,
mapping_function: M
) -> Tensor<T, D>
pub fn elementwise_reference<S, I, M>( &self, rhs: I, mapping_function: M ) -> Tensor<T, D>
Creates and returns a new tensor with all value pairs of two tensors with the same shape
mapped by a function. The value pairs are not copied for you, if you’re using Copy
types
or need to clone the values anyway, you can use
Tensor::elementwise
instead.
§Generics
This method can be called with any right hand side that can be converted to a TensorView,
which includes Tensor
, &Tensor
, &mut Tensor
as well as references to a TensorView
.
§Panics
If the two tensors have different shapes.
sourcepub fn elementwise_reference_with_index<S, I, M>(
&self,
rhs: I,
mapping_function: M
) -> Tensor<T, D>
pub fn elementwise_reference_with_index<S, I, M>( &self, rhs: I, mapping_function: M ) -> Tensor<T, D>
Creates and returns a new tensor with all value pairs of two tensors with the same shape
mapped by a function. The mapping function also receives each index corresponding to the
value pairs. The value pairs are not copied for you, if you’re using Copy
types
or need to clone the values anyway, you can use
Tensor::elementwise_with_index
instead.
§Generics
This method can be called with any right hand side that can be converted to a TensorView,
which includes Tensor
, &Tensor
, &mut Tensor
as well as references to a TensorView
.
§Panics
If the two tensors have different shapes.
sourcepub fn transpose_view(
&self,
dimensions: [Dimension; D]
) -> TensorView<T, TensorTranspose<T, &Tensor<T, D>, D>, D>
pub fn transpose_view( &self, dimensions: [Dimension; D] ) -> TensorView<T, TensorTranspose<T, &Tensor<T, D>, D>, D>
Returns a TensorView which makes the order of the data in this tensor appear to be in a different order. The order of the dimension names is unchanged, although their lengths may swap.
This is a shorthand for constructing the TensorView from this Tensor.
See also: transpose, TensorTranspose
§Panics
If the set of dimensions in the tensor does not match the set of dimensions provided. The order need not match.
source§impl<T, const D: usize> Tensor<T, D>where
T: Clone,
impl<T, const D: usize> Tensor<T, D>where
T: Clone,
sourcepub fn empty(shape: [(Dimension, usize); D], value: T) -> Self
pub fn empty(shape: [(Dimension, usize); D], value: T) -> Self
Creates a tensor with a particular number of dimensions and length in each dimension with all elements initialised to the provided value.
§Panics
- If a dimension name is not unique
- If any dimension has 0 elements
sourcepub fn first(&self) -> T
pub fn first(&self) -> T
Gets a copy of the first value in this tensor.
For 0 dimensional tensors this is the only index []
, for 1 dimensional tensors this
is [0]
, for 2 dimensional tensors [0,0]
, etcetera.
sourcepub fn transpose(&self, dimensions: [Dimension; D]) -> Tensor<T, D>
pub fn transpose(&self, dimensions: [Dimension; D]) -> Tensor<T, D>
Returns a new Tensor which has the same data as this tensor, but with the order of data changed. The order of the dimension names is unchanged, although their lengths may swap.
For example, with a [("x", x), ("y", y)]
tensor you could call
transpose(["y", "x"])
which would return a new tensor with a shape of
[("x", y), ("y", x)]
where every (x,y) of its data corresponds to (y,x) in the original.
This method need not shift all the dimensions though, you could also swap the width
and height of images in a tensor with a shape of
[("batch", b), ("h", h), ("w", w), ("c", c)]
via transpose(["batch", "w", "h", "c"])
which would return a new tensor where all the images have been swapped over the diagonal.
See also: TensorAccess, reorder
§Panics
If the set of dimensions in the tensor does not match the set of dimensions provided. The order need not match (and if the order does match, this function is just an expensive clone).
sourcepub fn transpose_mut(&mut self, dimensions: [Dimension; D])
pub fn transpose_mut(&mut self, dimensions: [Dimension; D])
Modifies this tensor to have the same data as before, but with the order of data changed. The order of the dimension names is unchanged, although their lengths may swap.
For example, with a [("x", x), ("y", y)]
tensor you could call
transpose_mut(["y", "x"])
which would edit the tensor, updating its shape to
[("x", y), ("y", x)]
, so every (x,y) of its data corresponds to (y,x) before the
transposition.
The order swapping will try to be in place, but this is currently only supported for square tensors with 2 dimensions. Other types of tensors will not be transposed in place.
§Panics
If the set of dimensions in the tensor does not match the set of dimensions provided. The order need not match (and if the order does match, this function is just an expensive clone).
sourcepub fn reorder(&self, dimensions: [Dimension; D]) -> Tensor<T, D>
pub fn reorder(&self, dimensions: [Dimension; D]) -> Tensor<T, D>
Returns a new Tensor which has the same data as this tensor, but with the order of the dimensions and corresponding order of data changed.
For example, with a [("x", x), ("y", y)]
tensor you could call
reorder(["y", "x"])
which would return a new tensor with a shape of
[("y", y), ("x", x)]
where every (y,x) of its data corresponds to (x,y) in the original.
This method need not shift all the dimensions though, you could also swap the width
and height of images in a tensor with a shape of
[("batch", b), ("h", h), ("w", w), ("c", c)]
via reorder(["batch", "w", "h", "c"])
which would return a new tensor where every (b,w,h,c) of its data corresponds to (b,h,w,c)
in the original.
See also: TensorAccess, transpose
§Panics
If the set of dimensions in the tensor does not match the set of dimensions provided. The order need not match (and if the order does match, this function is just an expensive clone).
sourcepub fn reorder_mut(&mut self, dimensions: [Dimension; D])
pub fn reorder_mut(&mut self, dimensions: [Dimension; D])
Modifies this tensor to have the same data as before, but with the order of the dimensions and corresponding order of data changed.
For example, with a [("x", x), ("y", y)]
tensor you could call
reorder_mut(["y", "x"])
which would edit the tensor, updating its shape to
[("y", y), ("x", x)]
, so every (y,x) of its data corresponds to (x,y) before the
transposition.
The order swapping will try to be in place, but this is currently only supported for square tensors with 2 dimensions. Other types of tensors will not be reordered in place.
§Panics
If the set of dimensions in the tensor does not match the set of dimensions provided. The order need not match (and if the order does match, this function is just an expensive clone).
sourcepub fn iter(&self) -> TensorIterator<'_, T, Tensor<T, D>, D> ⓘ
pub fn iter(&self) -> TensorIterator<'_, T, Tensor<T, D>, D> ⓘ
Returns an iterator over copies of the data in this Tensor.
sourcepub fn map<U>(&self, mapping_function: impl Fn(T) -> U) -> Tensor<U, D>
pub fn map<U>(&self, mapping_function: impl Fn(T) -> U) -> Tensor<U, D>
Creates and returns a new tensor with all values from the original with the function applied to each. This can be used to change the type of the tensor such as creating a mask:
use easy_ml::tensors::Tensor;
let x = Tensor::from([("a", 2), ("b", 2)], vec![
0.0, 1.2,
5.8, 6.9
]);
let y = x.map(|element| element > 2.0);
let result = Tensor::from([("a", 2), ("b", 2)], vec![
false, false,
true, true
]);
assert_eq!(&y, &result);
sourcepub fn map_with_index<U>(
&self,
mapping_function: impl Fn([usize; D], T) -> U
) -> Tensor<U, D>
pub fn map_with_index<U>( &self, mapping_function: impl Fn([usize; D], T) -> U ) -> Tensor<U, D>
Creates and returns a new tensor with all values from the original and the index of each value mapped by a function.
sourcepub fn map_mut(&mut self, mapping_function: impl Fn(T) -> T)
pub fn map_mut(&mut self, mapping_function: impl Fn(T) -> T)
Applies a function to all values in the tensor, modifying the tensor in place.
sourcepub fn map_mut_with_index(
&mut self,
mapping_function: impl Fn([usize; D], T) -> T
)
pub fn map_mut_with_index( &mut self, mapping_function: impl Fn([usize; D], T) -> T )
Applies a function to all values and each value’s index in the tensor, modifying the tensor in place.
sourcepub fn elementwise<S, I, M>(&self, rhs: I, mapping_function: M) -> Tensor<T, D>
pub fn elementwise<S, I, M>(&self, rhs: I, mapping_function: M) -> Tensor<T, D>
Creates and returns a new tensor with all value pairs of two tensors with the same shape mapped by a function.
use easy_ml::tensors::Tensor;
let lhs = Tensor::from([("a", 4)], vec![1, 2, 3, 4]);
let rhs = Tensor::from([("a", 4)], vec![0, 1, 2, 3]);
let multiplied = lhs.elementwise(&rhs, |l, r| l * r);
assert_eq!(
multiplied,
Tensor::from([("a", 4)], vec![0, 2, 6, 12])
);
§Generics
This method can be called with any right hand side that can be converted to a TensorView,
which includes Tensor
, &Tensor
, &mut Tensor
as well as references to a TensorView
.
§Panics
If the two tensors have different shapes.
sourcepub fn elementwise_with_index<S, I, M>(
&self,
rhs: I,
mapping_function: M
) -> Tensor<T, D>
pub fn elementwise_with_index<S, I, M>( &self, rhs: I, mapping_function: M ) -> Tensor<T, D>
Creates and returns a new tensor with all value pairs of two tensors with the same shape mapped by a function. The mapping function also receives each index corresponding to the value pairs.
§Generics
This method can be called with any right hand side that can be converted to a TensorView,
which includes Tensor
, &Tensor
, &mut Tensor
as well as references to a TensorView
.
§Panics
If the two tensors have different shapes.
source§impl<T> Tensor<T, 1>
impl<T> Tensor<T, 1>
sourcepub fn scalar_product<S, I>(&self, rhs: I) -> T
pub fn scalar_product<S, I>(&self, rhs: I) -> T
Computes the scalar product of two equal length vectors. For two vectors [a,b,c]
and
[d,e,f]
, returns a*d + b*e + c*f
. This is also known as the dot product.
use easy_ml::tensors::Tensor;
let tensor = Tensor::from([("sequence", 5)], vec![3, 4, 5, 6, 7]);
assert_eq!(tensor.scalar_product(&tensor), 3*3 + 4*4 + 5*5 + 6*6 + 7*7);
§Generics
This method can be called with any right hand side that can be converted to a TensorView,
which includes Tensor
, &Tensor
, &mut Tensor
as well as references to a TensorView
.
§Panics
If the two vectors are not of equal length or their dimension names do not match.
source§impl<T> Tensor<T, 2>
impl<T> Tensor<T, 2>
sourcepub fn determinant(&self) -> Option<T>
pub fn determinant(&self) -> Option<T>
Returns the determinant of this square matrix, or None if the matrix
does not have a determinant. See linear_algebra
sourcepub fn inverse(&self) -> Option<Tensor<T, 2>>
pub fn inverse(&self) -> Option<Tensor<T, 2>>
Computes the inverse of a matrix provided that it exists. To have an inverse a
matrix must be square (same number of rows and columns) and it must also have a
non zero determinant. See linear_algebra
sourcepub fn covariance(&self, feature_dimension: Dimension) -> Tensor<T, 2>
pub fn covariance(&self, feature_dimension: Dimension) -> Tensor<T, 2>
Computes the covariance matrix for this feature matrix along the specified feature
dimension in this matrix. See linear_algebra
.
source§impl<T> Tensor<T, 2>where
T: Numeric,
impl<T> Tensor<T, 2>where
T: Numeric,
sourcepub fn diagonal(shape: [(Dimension, usize); 2], value: T) -> Tensor<T, 2>
pub fn diagonal(shape: [(Dimension, usize); 2], value: T) -> Tensor<T, 2>
Creates a diagonal matrix of the provided size with the diagonal elements set to the provided value and all other elements in the tensor set to 0. A diagonal matrix is always square.
The size is still taken as a shape to facilitate creating a diagonal matrix from the dimensionality of an existing one. If the provided value is 1 then this will create an identity matrix.
A 3 x 3 identity matrix:
[
1, 0, 0
0, 1, 0
0, 0, 1
]
§Panics
- If the shape is not square.
- If a dimension name is not unique
- If any dimension has 0 elements
source§impl<T> Tensor<T, 2>
impl<T> Tensor<T, 2>
sourcepub fn into_matrix(self) -> Matrix<T>
pub fn into_matrix(self) -> Matrix<T>
Converts this 2 dimensional Tensor into a Matrix.
This is a wrapper around the From<Tensor<T, 2>>
implementation.
The Matrix will have the data in the same order, with rows equal to the length of the first dimension in the tensor, and columns equal to the length of the second.
source§impl<T: Numeric + Real> Tensor<T, 1>
impl<T: Numeric + Real> Tensor<T, 1>
Methods for tensors with numerical real valued types, such as f32 or f64.
This excludes signed and unsigned integers as they do not support decimal precision and hence can’t be used for operations like square roots.
Third party fixed precision and infinite precision decimal types should be able to implement all of the methods for Real and then utilise these functions.
sourcepub fn euclidean_length(&self) -> T
pub fn euclidean_length(&self) -> T
Computes the L2 norm of this vector, also referred to as the length or magnitude, and written as ||x||, or sometimes |x|.
||a|| = sqrt(a12 + a22 + a32…) = sqrt(aT * a)
This is a shorthand for (x.iter().map(|x| x * x).sum().sqrt()
, ie
the square root of the dot product of a vector with itself.
The euclidean length can be used to compute a unit vector, that is, a vector with length of 1. This should not be confused with a unit matrix, which is another name for an identity matrix.
use easy_ml::tensors::Tensor;
let a = Tensor::from([("data", 3)], vec![ 1.0, 2.0, 3.0 ]);
let length = a.euclidean_length(); // (1^2 + 2^2 + 3^2)^0.5
let unit = a.map(|x| x / length);
assert_eq!(unit.euclidean_length(), 1.0);
source§impl<T> Tensor<T, 6>
impl<T> Tensor<T, 6>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 6>, 6, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 6>, 6, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 6>, 6, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 6>, 6, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 6>, 6, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 6>, 6, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 5>
impl<T> Tensor<T, 5>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 5>, 5, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 5>, 5, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 5>, 5, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 5>, 5, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 5>, 5, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 5>, 5, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 4>
impl<T> Tensor<T, 4>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 4>, 4, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 4>, 4, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 4>, 4, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 4>, 4, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 4>, 4, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 4>, 4, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 3>
impl<T> Tensor<T, 3>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 3>, 3, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 3>, 3, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 3>, 3, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 3>, 3, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 3>, 3, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 3>, 3, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 2>
impl<T> Tensor<T, 2>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 2>, 2, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 2>, 2, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 2>, 2, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 2>, 2, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 2>, 2, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 2>, 2, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 1>
impl<T> Tensor<T, 1>
sourcepub fn select(
&self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &Tensor<T, 1>, 1, 1>, { _ }>
pub fn select( &self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &Tensor<T, 1>, 1, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values.
This is a shorthand for manually constructing the TensorView and TensorIndex
Note: due to limitations in Rust’s const generics support, this method is only
implemented for provided_indexes
of length 1 and D
from 1 to 6. You can fall
back to manual construction to create TensorIndex
es with multiple provided
indexes if you need to reduce dimensionality by more than 1 dimension at a time.
sourcepub fn select_mut(
&mut self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 1>, 1, 1>, { _ }>
pub fn select_mut( &mut self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, &mut Tensor<T, 1>, 1, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See select
sourcepub fn select_owned(
self,
provided_indexes: [(Dimension, usize); 1]
) -> TensorView<T, TensorIndex<T, Tensor<T, 1>, 1, 1>, { _ }>
pub fn select_owned( self, provided_indexes: [(Dimension, usize); 1] ) -> TensorView<T, TensorIndex<T, Tensor<T, 1>, 1, 1>, { _ }>
Selects the provided dimension name and index pairs in this Tensor, returning a TensorView which has fewer dimensions than this Tensor, with the removed dimensions always indexed as the provided values. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See select
source§impl<T> Tensor<T, 0>
impl<T> Tensor<T, 0>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 0>, 0, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 0>, 0, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 0>, 0, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 0>, 0, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 0>, 0, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 0>, 0, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
source§impl<T> Tensor<T, 1>
impl<T> Tensor<T, 1>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 1>, 1, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 1>, 1, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 1>, 1, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 1>, 1, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 1>, 1, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 1>, 1, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
source§impl<T> Tensor<T, 2>
impl<T> Tensor<T, 2>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 2>, 2, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 2>, 2, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 2>, 2, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 2>, 2, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 2>, 2, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 2>, 2, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
source§impl<T> Tensor<T, 3>
impl<T> Tensor<T, 3>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 3>, 3, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 3>, 3, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 3>, 3, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 3>, 3, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 3>, 3, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 3>, 3, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
source§impl<T> Tensor<T, 4>
impl<T> Tensor<T, 4>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 4>, 4, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 4>, 4, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 4>, 4, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 4>, 4, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 4>, 4, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 4>, 4, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
source§impl<T> Tensor<T, 5>
impl<T> Tensor<T, 5>
sourcepub fn expand(
&self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &Tensor<T, 5>, 5, 1>, { _ }>
pub fn expand( &self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &Tensor<T, 5>, 5, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor.
This is a shorthand for manually constructing the TensorView and TensorExpansion
Note: due to limitations in Rust’s const generics support, this method is only
implemented for extra_dimension_names
of length 1 and D
from 0 to 5. You can
fall back to manual construction to create TensorExpansion
s with multiple provided
indexes if you need to increase dimensionality by more than 1 dimension at a time.
sourcepub fn expand_mut(
&mut self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 5>, 5, 1>, { _ }>
pub fn expand_mut( &mut self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, &mut Tensor<T, 5>, 5, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex mutably borrows this Tensor, and can therefore mutate it
See expand
sourcepub fn expand_owned(
self,
extra_dimension_names: [(usize, Dimension); 1]
) -> TensorView<T, TensorExpansion<T, Tensor<T, 5>, 5, 1>, { _ }>
pub fn expand_owned( self, extra_dimension_names: [(usize, Dimension); 1] ) -> TensorView<T, TensorExpansion<T, Tensor<T, 5>, 5, 1>, { _ }>
Expands the dimensionality of this tensor by adding dimensions of length 1 at a particular position within the shape, returning a TensorView which has more dimensions than this Tensor. The TensorIndex takes ownership of this Tensor, and can therefore mutate it
See expand
Trait Implementations§
source§impl<T: Numeric, const D: usize> Add<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Add<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Add<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Add<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by value and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T, const D: usize> Add<&Tensor<T, D>> for &Tensor<T, D>
impl<T, const D: usize> Add<&Tensor<T, D>> for &Tensor<T, D>
Elementwise addition for two referenced tensors
source§impl<T, S, const D: usize> Add<&Tensor<T, D>> for &TensorView<T, S, D>
impl<T, S, const D: usize> Add<&Tensor<T, D>> for &TensorView<T, S, D>
Elementwise addition for a referenced tensor view and a referenced tensor
source§impl<T, const D: usize> Add<&Tensor<T, D>> for Tensor<T, D>
impl<T, const D: usize> Add<&Tensor<T, D>> for Tensor<T, D>
Elementwise addition for two tensors with one referenced
source§impl<T, S, const D: usize> Add<&Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> Add<&Tensor<T, D>> for TensorView<T, S, D>
Elementwise addition for a tensor view and a referenced tensor
source§impl<T, S, const D: usize> Add<&TensorView<T, S, D>> for &Tensor<T, D>
impl<T, S, const D: usize> Add<&TensorView<T, S, D>> for &Tensor<T, D>
Elementwise addition for a referenced tensor and a referenced tensor view
source§impl<T, S, const D: usize> Add<&TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> Add<&TensorView<T, S, D>> for Tensor<T, D>
Elementwise addition for a tensor and a referenced tensor view
source§impl<T: Numeric, const D: usize> Add<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Add<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by reference and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Add<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Add<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T, const D: usize> Add<Tensor<T, D>> for &Tensor<T, D>
impl<T, const D: usize> Add<Tensor<T, D>> for &Tensor<T, D>
Elementwise addition for two tensors with one referenced
source§impl<T, S, const D: usize> Add<Tensor<T, D>> for &TensorView<T, S, D>
impl<T, S, const D: usize> Add<Tensor<T, D>> for &TensorView<T, S, D>
Elementwise addition for a referenced tensor view and a tensor
source§impl<T, S, const D: usize> Add<Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> Add<Tensor<T, D>> for TensorView<T, S, D>
Elementwise addition for a tensor view and a tensor
source§impl<T, S, const D: usize> Add<TensorView<T, S, D>> for &Tensor<T, D>
impl<T, S, const D: usize> Add<TensorView<T, S, D>> for &Tensor<T, D>
Elementwise addition for a referenced tensor and a tensor view
source§impl<T, S, const D: usize> Add<TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> Add<TensorView<T, S, D>> for Tensor<T, D>
Elementwise addition for a tensor and a tensor view
source§impl<T: Clone, const D: usize> Clone for Tensor<T, D>
impl<T: Clone, const D: usize> Clone for Tensor<T, D>
Any tensor of a Cloneable type implements Clone.
source§impl<T: Display, const D: usize> Display for Tensor<T, D>
impl<T: Display, const D: usize> Display for Tensor<T, D>
Any tensor of a Displayable type implements Display
You can control the precision of the formatting using format arguments, i.e.
format!("{:.3}", tensor)
source§impl<T: Numeric, const D: usize> Div<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Div<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Div<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Div<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by value and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Div<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Div<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by reference and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Div<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Div<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<'a, T, const D: usize> From<&'a Tensor<T, D>> for TensorView<T, &'a Tensor<T, D>, D>
impl<'a, T, const D: usize> From<&'a Tensor<T, D>> for TensorView<T, &'a Tensor<T, D>, D>
A reference to a Tensor can be converted to a TensorView of that referenced Tensor.
source§impl<'a, T, const D: usize> From<&'a mut Tensor<T, D>> for TensorView<T, &'a mut Tensor<T, D>, D>
impl<'a, T, const D: usize> From<&'a mut Tensor<T, D>> for TensorView<T, &'a mut Tensor<T, D>, D>
A mutable reference to a Tensor can be converted to a TensorView of that mutably referenced Tensor.
source§impl<T> From<Tensor<T, 2>> for Matrix<T>
impl<T> From<Tensor<T, 2>> for Matrix<T>
Any 2 dimensional tensor can be converted to a matrix with rows equal to the length of the first dimension in the tensor, and columns equal to the length of the second.
source§impl<T, const D: usize> From<Tensor<T, D>> for TensorView<T, Tensor<T, D>, D>
impl<T, const D: usize> From<Tensor<T, D>> for TensorView<T, Tensor<T, D>, D>
A Tensor can be converted to a TensorView of that Tensor.
source§impl<T: Numeric, const D: usize> Mul<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Mul<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Mul<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Mul<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by value and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T> Mul<&Tensor<T, 2>> for &Tensor<T, 2>
impl<T> Mul<&Tensor<T, 2>> for &Tensor<T, 2>
Matrix multiplication for two 2-dimensional referenced tensors
source§impl<T, S> Mul<&Tensor<T, 2>> for &TensorView<T, S, 2>
impl<T, S> Mul<&Tensor<T, 2>> for &TensorView<T, S, 2>
Matrix multiplication for a 2-dimensional referenced tensor view and a referenced tensor
source§impl<T> Mul<&Tensor<T, 2>> for Tensor<T, 2>
impl<T> Mul<&Tensor<T, 2>> for Tensor<T, 2>
Matrix multiplication for two 2-dimensional tensors with one referenced
source§impl<T, S> Mul<&Tensor<T, 2>> for TensorView<T, S, 2>
impl<T, S> Mul<&Tensor<T, 2>> for TensorView<T, S, 2>
Matrix multiplication for a 2-dimensional tensor view and a referenced tensor
source§impl<T, S> Mul<&TensorView<T, S, 2>> for &Tensor<T, 2>
impl<T, S> Mul<&TensorView<T, S, 2>> for &Tensor<T, 2>
Matrix multiplication for a 2-dimensional referenced tensor and a referenced tensor view
source§impl<T, S> Mul<&TensorView<T, S, 2>> for Tensor<T, 2>
impl<T, S> Mul<&TensorView<T, S, 2>> for Tensor<T, 2>
Matrix multiplication for a 2-dimensional tensor and a referenced tensor view
source§impl<T: Numeric, const D: usize> Mul<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Mul<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by reference and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Mul<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Mul<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T> Mul<Tensor<T, 2>> for &Tensor<T, 2>
impl<T> Mul<Tensor<T, 2>> for &Tensor<T, 2>
Matrix multiplication for two 2-dimensional tensors with one referenced
source§impl<T, S> Mul<Tensor<T, 2>> for &TensorView<T, S, 2>
impl<T, S> Mul<Tensor<T, 2>> for &TensorView<T, S, 2>
Matrix multiplication for a 2-dimensional referenced tensor view and a tensor
source§impl<T, S> Mul<Tensor<T, 2>> for TensorView<T, S, 2>
impl<T, S> Mul<Tensor<T, 2>> for TensorView<T, S, 2>
Matrix multiplication for a 2-dimensional tensor view and a tensor
source§impl<T, S> Mul<TensorView<T, S, 2>> for &Tensor<T, 2>
impl<T, S> Mul<TensorView<T, S, 2>> for &Tensor<T, 2>
Matrix multiplication for a 2-dimensional referenced tensor and a tensor view
source§impl<T, S> Mul<TensorView<T, S, 2>> for Tensor<T, 2>
impl<T, S> Mul<TensorView<T, S, 2>> for Tensor<T, 2>
Matrix multiplication for a 2-dimensional tensor and a tensor view
source§impl<T, S, const D: usize> PartialEq<Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> PartialEq<Tensor<T, D>> for TensorView<T, S, D>
A TensorView and a Tensor can be compared for equality. The tensors are equal if they have an equal shape and all their elements are equal.
source§impl<T, S, const D: usize> PartialEq<TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> PartialEq<TensorView<T, S, D>> for Tensor<T, D>
A Tensor and a TensorView can be compared for equality. The tensors are equal if they have an equal shape and all their elements are equal.
source§fn eq(&self, other: &TensorView<T, S, D>) -> bool
fn eq(&self, other: &TensorView<T, S, D>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T: PartialEq, const D: usize> PartialEq for Tensor<T, D>
impl<T: PartialEq, const D: usize> PartialEq for Tensor<T, D>
Two Tensors are equal if they have an equal shape and all their elements are equal
use easy_ml::tensors::Tensor;
let one = Tensor::from([("a", 2), ("b", 3)], vec![
1, 2, 3,
4, 5, 6
]);
let two = Tensor::from([("b", 3), ("a", 2)], vec![
1, 4,
2, 5,
3, 6
]);
let three = Tensor::from([("b", 2), ("a", 3)], vec![
1, 2, 3,
4, 5, 6
]);
assert_eq!(one, one);
assert_eq!(two, two);
assert_eq!(three, three);
assert_ne!(one, two); // similar, but dimension order is not the same
assert_eq!(one, two.reorder(["a", "b"])); // reordering b to the order of a makes it equal
assert_ne!(one, three); // elementwise data is same, but dimensions are not equal
assert_ne!(two, three); // dimensions are not equal, and elementwise data is not the same
source§impl<T, S, const D: usize> Similar<Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> Similar<Tensor<T, D>> for TensorView<T, S, D>
A TensorView and a Tensor can be compared for similarity. The tensors are similar if they have the same set of dimension names and lengths even if two shapes are not in the same order, and all their elements are equal when comparing both tensors via the same dimension ordering.
source§impl<T, S, const D: usize> Similar<TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> Similar<TensorView<T, S, D>> for Tensor<T, D>
A Tensor and a TensorView can be compared for similarity. The tensors are similar if they have the same set of dimension names and lengths even if two shapes are not in the same order, and all their elements are equal when comparing both tensors via the same dimension ordering.
source§impl<T: PartialEq, const D: usize> Similar for Tensor<T, D>
impl<T: PartialEq, const D: usize> Similar for Tensor<T, D>
Two Tensors are similar if they have the same set of dimension names and lengths even if two shapes are not in the same order, and all their elements are equal when comparing both tensors via the same dimension ordering.
Elements are compared by iterating through the right most index of the left tensor and the corresponding index in the right tensor when accessed using the dimension order of the left. When both tensors have their dimensions in the same order, this corresponds to the right most index of the right tensor as well, and will be an elementwise comparison.
If two Tensors are similar, you can reorder one of them to the dimension order of the other and they will be equal.
use easy_ml::tensors::Tensor;
use easy_ml::tensors::operations::Similar;
let one = Tensor::from([("a", 2), ("b", 3)], vec![
1, 2, 3,
4, 5, 6
]);
let two = Tensor::from([("b", 3), ("a", 2)], vec![
1, 4,
2, 5,
3, 6
]);
let three = Tensor::from([("b", 2), ("a", 3)], vec![
1, 2, 3,
4, 5, 6
]);
assert!(one.similar(&one));
assert!(two.similar(&two));
assert!(three.similar(&three));
assert!(one.similar(&two)); // similar, dimension order is not the same
assert!(!one.similar(&three)); // elementwise data is same, but dimensions are not equal
assert!(!two.similar(&three)); // dimension lengths are not the same, and elementwise data is not the same
source§impl<T: Numeric, const D: usize> Sub<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Sub<&T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Sub<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Sub<&T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by value and scalar by reference. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T, const D: usize> Sub<&Tensor<T, D>> for &Tensor<T, D>
impl<T, const D: usize> Sub<&Tensor<T, D>> for &Tensor<T, D>
Elementwise subtraction for two referenced tensors
source§impl<T, S, const D: usize> Sub<&Tensor<T, D>> for &TensorView<T, S, D>
impl<T, S, const D: usize> Sub<&Tensor<T, D>> for &TensorView<T, S, D>
Elementwise subtraction for a referenced tensor view and a referenced tensor
source§impl<T, const D: usize> Sub<&Tensor<T, D>> for Tensor<T, D>
impl<T, const D: usize> Sub<&Tensor<T, D>> for Tensor<T, D>
Elementwise subtraction for two tensors with one referenced
source§impl<T, S, const D: usize> Sub<&Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> Sub<&Tensor<T, D>> for TensorView<T, S, D>
Elementwise subtraction for a tensor view and a referenced tensor
source§impl<T, S, const D: usize> Sub<&TensorView<T, S, D>> for &Tensor<T, D>
impl<T, S, const D: usize> Sub<&TensorView<T, S, D>> for &Tensor<T, D>
Elementwise subtraction for a referenced tensor and a referenced tensor view
source§impl<T, S, const D: usize> Sub<&TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> Sub<&TensorView<T, S, D>> for Tensor<T, D>
Elementwise subtraction for a tensor and a referenced tensor view
source§impl<T: Numeric, const D: usize> Sub<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Sub<T> for &Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor by reference and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T: Numeric, const D: usize> Sub<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
impl<T: Numeric, const D: usize> Sub<T> for Tensor<T, D>where
for<'a> &'a T: NumericRef<T>,
Operation for a tensor and scalar by value. The scalar is applied to all elements, this is a shorthand for map().
source§impl<T, const D: usize> Sub<Tensor<T, D>> for &Tensor<T, D>
impl<T, const D: usize> Sub<Tensor<T, D>> for &Tensor<T, D>
Elementwise subtraction for two tensors with one referenced
source§impl<T, S, const D: usize> Sub<Tensor<T, D>> for &TensorView<T, S, D>
impl<T, S, const D: usize> Sub<Tensor<T, D>> for &TensorView<T, S, D>
Elementwise subtraction for a referenced tensor view and a tensor
source§impl<T, S, const D: usize> Sub<Tensor<T, D>> for TensorView<T, S, D>
impl<T, S, const D: usize> Sub<Tensor<T, D>> for TensorView<T, S, D>
Elementwise subtraction for a tensor view and a tensor
source§impl<T, S, const D: usize> Sub<TensorView<T, S, D>> for &Tensor<T, D>
impl<T, S, const D: usize> Sub<TensorView<T, S, D>> for &Tensor<T, D>
Elementwise subtraction for a referenced tensor and a tensor view
source§impl<T, S, const D: usize> Sub<TensorView<T, S, D>> for Tensor<T, D>
impl<T, S, const D: usize> Sub<TensorView<T, S, D>> for Tensor<T, D>
Elementwise subtraction for a tensor and a tensor view
source§impl<T, const D: usize> TensorMut<T, D> for Tensor<T, D>
impl<T, const D: usize> TensorMut<T, D> for Tensor<T, D>
source§fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T>
fn get_reference_mut(&mut self, indexes: [usize; D]) -> Option<&mut T>
source§unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T
unsafe fn get_reference_unchecked_mut(&mut self, indexes: [usize; D]) -> &mut T
source§impl<T, const D: usize> TensorRef<T, D> for Tensor<T, D>
impl<T, const D: usize> TensorRef<T, D> for Tensor<T, D>
A Tensor implements TensorRef.
source§fn get_reference(&self, indexes: [usize; D]) -> Option<&T>
fn get_reference(&self, indexes: [usize; D]) -> Option<&T>
source§fn view_shape(&self) -> [(Dimension, usize); D]
fn view_shape(&self) -> [(Dimension, usize); D]
source§unsafe fn get_reference_unchecked(&self, indexes: [usize; D]) -> &T
unsafe fn get_reference_unchecked(&self, indexes: [usize; D]) -> &T
source§fn data_layout(&self) -> DataLayout<D>
fn data_layout(&self) -> DataLayout<D>
source§impl<T> TryFrom<(Matrix<T>, [&'static str; 2])> for Tensor<T, 2>
impl<T> TryFrom<(Matrix<T>, [&'static str; 2])> for Tensor<T, 2>
Any matrix and two different dimension names can be converted to a 2 dimensional tensor with the same number of rows and columns.
Conversion will fail if the dimension names for self.rows()
and self.columns()
respectively
are the same.