Struct dense_mats::tensor::Tensor
[−]
[src]
pub struct Tensor<N, DimArray, Storage> where Storage: Deref<Target=[N]> {
// some fields omitted
}A simple dense matrix
Methods
impl<N, DimArray> Tensor<N, DimArray, Vec<N>> where DimArray: ArrayLikeMut<usize>
fn zeros(shape: DimArray) -> TensorOwned<N, DimArray> where N: Num + Copy
Create an all-zero tensor in C order
fn zeros_f(shape: DimArray) -> TensorOwned<N, DimArray> where N: Num + Copy
Create an all-zero tensor in F order
fn ones(shape: DimArray) -> TensorOwned<N, DimArray> where N: Num + Copy
Create an all-one tensor in C order
fn ones_c(shape: DimArray) -> TensorOwned<N, DimArray> where N: Num + Copy
Create an all-one tensor in F order
impl<N, DimArray, Storage> Tensor<N, DimArray, Storage> where DimArray: ArrayLike<usize>, Storage: Deref<Target=[N]>
Methods available for all tensors regardless of their dimension count
fn ndims(&self) -> usize
The number of dimensions of this tensor
fn strides(&self) -> DimArray
The strides of the tensor.
Explanations on a matrix.
self.strides()[0] gives the number of elements that must be skipped into self.data() to get to the element of the next row with the same column. self.strides()[1] gives the number of elements that must be skipped into self.data() to get to the element of the next column with the same row.
For a row major matrix of shape (3, 4) with contiguous storage, the strides would be [4, 1].
For alignement reasons, it is possible to have strides that don't match the shape of the matrix (meaning that some elements of the data array are unused).
fn strides_ref(&self) -> &[usize]
Get the strides by reference
fn data(&self) -> &[N]
Access to the tensors's data
Explanations on a matrix.
Getting access to the element located at row i and column j can be done by indexing self.data() at the location computed by i * strides[0] + j * strides[1]
fn shape(&self) -> DimArray
The shape of the tensor
fn shape_ref(&self) -> &[usize]
Get the shape by reference
fn is_contiguous(&self) -> bool
Returns true if the array is contiguous, ie the fastest varying axis has stride 1, and no unused data is present in the array
fn can_ravel(&self) -> bool
Checks whether all dimensions except the fastest varying one
are contiguous. Having that property verified allows flattened
views of the tensor using ravel(). Otherwise copies have to be done.
fn ordering(&self) -> StorageOrder
Get the storage order of this tensor
fn outer_dim(&self) -> Option<usize>
Get the slowest varying dimension index
fn inner_dim(&self) -> Option<usize>
Get the fastest varying dimension index
fn outer_stride(&self) -> Option<usize>
The stride for the outer dimension
fn inner_stride(&self) -> Option<usize>
The stride for the inner dimension
fn outer_shape(&self) -> Option<usize>
The shape of the outer dimension
fn inner_shape(&self) -> Option<usize>
The stride for the inner dimension
fn borrowed(&self) -> TensorView<N, DimArray>
Get a view into this tensor
fn outer_block_iter<'a>(&'a self, block_size: usize) -> ChunkOuterBlocks<'a, N, DimArray, Storage>
Iteration on outer blocks views of size block_size
fn data_index(&self, index: DimArray) -> usize
Index into the data array for the given N-dimensional index
fn to_owned(&self) -> TensorOwned<N, DimArray> where N: Copy
fn iter_axis<'a>(&'a self, axis: Axis) -> Slices<'a, N, DimArray, Storage>
Iteration on the given axis
fn slice_dim<'a>(&'a self, Axis: Axis, index: usize) -> TensorView<'a, N, DimArray::Pred> where DimArray: ArrayLikeMut<usize>
Get a view as a tensor of lower dimension count, by slicing into the given axis at the given index
fn diag_view(&self) -> TensorView<N, [usize; 1]>
Get a view over the tensor's diagonal
The diagonal vector is as long as the smallest dimension.
Panics
Panics if the tensor is zero-dim.
fn ravel(&self) -> TensorView<N, [usize; 1]>
Get a flattened view of the tensor
This only works if the tensor verifies can_ravel()
Panics
If the tensor is not contiguous over all dimensions except the fastest varying one
On zero-dim tensors
impl<N, DimArray, Storage> Tensor<N, DimArray, Storage> where DimArray: ArrayLikeMut<usize>, Storage: Deref<Target=[N]>
fn middle_outer_views<'a>(&'a self, start: usize, count: usize) -> Result<TensorView<'a, N, DimArray>, DMatError>
Slice along the least varying dimension of the matrix, from
index start and taking count vectors.
e.g. for a row major matrix, get a view of count rows starting
from start.
impl<N, DimArray, Storage> Tensor<N, DimArray, Storage> where DimArray: ArrayLikeMut<usize>, Storage: DerefMut<Target=[N]>
fn outer_block_iter_mut<'a>(&'a mut self, block_size: usize) -> ChunkOuterBlocksMut<'a, N, DimArray, Storage>
Iteration on mutable outer blocks views of size block_size
fn borrowed_mut(&mut self) -> TensorViewMut<N, DimArray>
Get a mutable view into this tensor
fn slice_dim_mut<'a>(&'a mut self, Axis: Axis, index: usize) -> TensorViewMut<'a, N, DimArray::Pred> where DimArray: ArrayLikeMut<usize>
Get a mutable view as a tensor of lower dimension count, by slicing into the given axis at the given index
fn iter_axis_mut<'a>(&'a mut self, axis: Axis) -> SlicesMut<'a, N, DimArray, Storage>
fn middle_outer_views_mut<'a>(&'a mut self, start: usize, count: usize) -> Result<TensorViewMut<'a, N, DimArray>, DMatError>
Slice mutably along the least varying dimension of the matrix, from
index start and taking count vectors.
e.g. for a row major matrix, get a view of count rows starting
from start.
fn diag_view_mut(&mut self) -> TensorViewMut<N, [usize; 1]>
Get a mutable view over the tensor's diagonal
The diagonal vector is as long as the smallest dimension.
Panics
Panics if the tensor is zero-dim.
impl<N> Tensor<N, [usize; 2], Vec<N>>
fn new_owned(data: Vec<N>, rows: usize, cols: usize, strides: [usize; 2]) -> MatOwned<N>
Create a dense matrix from owned data
fn eye(dim: usize) -> MatOwned<N> where N: Num + Copy
Return the identity matrix of dimension dim
The storage will be row major, however one can transpose if needed
fn into_data(self) -> Vec<N>
Get the underlying data array as a vector
impl<'a, N: 'a> Tensor<N, [usize; 2], &'a [N]>
fn new_mat_view(data: &'a [N], rows: usize, cols: usize, strides: [usize; 2]) -> MatView<'a, N>
Create a view of a matrix implementing DenseMatView
impl<N, Storage> Tensor<N, [usize; 2], Storage> where Storage: Deref<Target=[N]>
fn rows(&self) -> usize
The number of rows of the matrix
fn cols(&self) -> usize
The number of cols of the matrix
fn row(&self, i: usize) -> Result<VecView<N>, DMatError>
Get a view into the specified row
fn col(&self, j: usize) -> Result<VecView<N>, DMatError>
Get a view into the specified column
impl<N, Storage> Tensor<N, [usize; 2], Storage> where Storage: DerefMut<Target=[N]>
fn data_mut(&mut self) -> &mut [N]
Mutable access to the matrix's data
Getting access to the element located at row i and column j can be done by indexing self.data() at the location computed by i * strides[0] + j * strides[1]
fn row_mut(&mut self, i: usize) -> Result<VecViewMut<N>, DMatError>
Get a mutable view into the specified row
fn col_mut(&mut self, j: usize) -> Result<VecViewMut<N>, DMatError>
Get a mutable view into the specified column
impl<N, Storage> Tensor<N, [usize; 1], Storage> where Storage: Deref<Target=[N]>
fn iter(&self) -> Map<Take<Chunks<N>>, fn(&[N]) -> &N>
Iterate over a dense vector's values by reference
fn dim(&self) -> usize
The number of dimensions
fn stride(&self) -> usize
The stride of this vector
impl<N, Storage> Tensor<N, [usize; 1], Storage> where Storage: DerefMut<Target=[N]>
fn iter_mut(&mut self) -> Map<Take<ChunksMut<N>>, fn(&mut [N]) -> &mut N>
Iterate over a dense vector's values by mutable reference
fn data_mut(&mut self) -> &mut [N]
The underlying data as a mutable slice