Trait TensorCreator

Source
pub trait TensorCreator<T>: Sized {
    type Output;

Show 18 methods // Required methods fn empty<S>(shape: S) -> Result<Self::Output, TensorError> where S: Into<Shape>; fn zeros<S>(shape: S) -> Result<Self::Output, TensorError> where S: Into<Shape>; fn ones<S>(shape: S) -> Result<Self::Output, TensorError> where S: Into<Shape>, u8: Cast<T>; fn empty_like(&self) -> Result<Self::Output, TensorError>; fn zeros_like(&self) -> Result<Self::Output, TensorError>; fn ones_like(&self) -> Result<Self::Output, TensorError> where u8: Cast<T>; fn full<S>(val: T, shape: S) -> Result<Self::Output, TensorError> where S: Into<Shape>; fn full_like(&self, val: T) -> Result<Self::Output, TensorError>; fn arange<U>(start: U, end: U) -> Result<Self::Output, TensorError> where usize: Cast<T>, U: Cast<i64> + Cast<T> + Copy; fn arange_step( start: T, end: T, step: T, ) -> Result<Self::Output, TensorError> where T: Cast<f64> + Cast<usize>, usize: Cast<T>; fn eye(n: usize, m: usize, k: usize) -> Result<Self::Output, TensorError>; fn linspace<U>( start: U, end: U, num: usize, include_end: bool, ) -> Result<Self::Output, TensorError> where U: Cast<f64> + Cast<T> + Copy, usize: Cast<T>, f64: Cast<T>; fn logspace( start: T, end: T, num: usize, include_end: bool, base: T, ) -> Result<Self::Output, TensorError> where T: Cast<f64> + Float + NormalOut<Output = T>, usize: Cast<T>, f64: Cast<T>; fn geomspace( start: T, end: T, n: usize, include_end: bool, ) -> Result<Self::Output, TensorError> where f64: Cast<T>, usize: Cast<T>, T: Cast<f64>; fn tri( n: usize, m: usize, k: i64, low_triangle: bool, ) -> Result<Self::Output, TensorError> where u8: Cast<T>; fn tril(&self, k: i64) -> Result<Self::Output, TensorError> where T: NormalOut<bool, Output = T> + Cast<T> + TypeCommon, <T as TypeCommon>::Vec: NormalOut<boolx16, Output = <T as TypeCommon>::Vec>; fn triu(&self, k: i64) -> Result<Self::Output, TensorError> where T: NormalOut<bool, Output = T> + Cast<T> + TypeCommon, <T as TypeCommon>::Vec: NormalOut<boolx16, Output = <T as TypeCommon>::Vec>; fn identity(n: usize) -> Result<Self::Output, TensorError> where u8: Cast<T>;
}
Expand description

A trait defines a set of functions to create tensors.

Required Associated Types§

Source

type Output

the output type of the creator

Required Methods§

Source

fn empty<S>(shape: S) -> Result<Self::Output, TensorError>
where S: Into<Shape>,

Creates a tensor with uninitialized elements of the specified shape.

This function allocates memory for a tensor of the given shape, but the values are uninitialized, meaning they may contain random data.

§Arguments
  • shape - The desired shape of the tensor. The type S must implement Into<Shape>.
§Returns
  • A tensor with the specified shape, but with uninitialized data.
§Panics
  • This function may panic if the requested shape is invalid or too large for available memory.
Source

fn zeros<S>(shape: S) -> Result<Self::Output, TensorError>
where S: Into<Shape>,

Creates a tensor filled with zeros of the specified shape.

This function returns a tensor where every element is initialized to 0, with the shape defined by the input.

§Arguments
  • shape - The desired shape of the tensor. The type S must implement Into<Shape>.
§Returns
  • A tensor filled with zeros, with the specified shape.
§Panics
  • This function may panic if the requested shape is invalid or too large for available memory.
Source

fn ones<S>(shape: S) -> Result<Self::Output, TensorError>
where S: Into<Shape>, u8: Cast<T>,

Creates a tensor filled with ones of the specified shape.

This function returns a tensor where every element is initialized to 1, with the shape defined by the input.

§Arguments
  • shape - The desired shape of the tensor. The type S must implement Into<Shape>.
§Returns
  • A tensor filled with ones, with the specified shape.
§Panics
  • This function may panic if the requested shape is invalid or too large for available memory.
Source

fn empty_like(&self) -> Result<Self::Output, TensorError>

Creates a tensor with uninitialized elements, having the same shape as the input tensor.

This function returns a tensor with the same shape as the calling tensor, but with uninitialized data.

§Arguments

This function takes no arguments.

§Returns
  • A tensor with the same shape as the input, but with uninitialized data.
§Panics
  • This function may panic if the shape is too large for available memory.
Source

fn zeros_like(&self) -> Result<Self::Output, TensorError>

Creates a tensor filled with zeros, having the same shape as the input tensor.

This function returns a tensor with the same shape as the calling tensor, with all elements initialized to 0.

§Arguments

This function takes no arguments.

§Returns
  • A tensor with the same shape as the input, filled with zeros.
§Panics
  • This function may panic if the shape is too large for available memory.
Source

fn ones_like(&self) -> Result<Self::Output, TensorError>
where u8: Cast<T>,

Creates a tensor filled with ones, having the same shape as the input tensor.

This function returns a tensor with the same shape as the calling tensor, with all elements initialized to 1.

§Arguments

This function takes no arguments.

§Returns
  • A tensor with the same shape as the input, filled with ones.
§Panics
  • This function may panic if the shape is too large for available memory.
Source

fn full<S>(val: T, shape: S) -> Result<Self::Output, TensorError>
where S: Into<Shape>,

Creates a tensor filled with a specified value, with the specified shape.

This function returns a tensor where every element is set to val, with the shape defined by the input.

§Arguments
  • val - The value to fill the tensor with.
  • shape - The desired shape of the tensor. The type S must implement Into<Shape>.
§Returns
  • A tensor filled with val, with the specified shape.
§Panics
  • This function may panic if the requested shape is invalid or too large for available memory.
Source

fn full_like(&self, val: T) -> Result<Self::Output, TensorError>

Creates a tensor filled with a specified value, having the same shape as the input tensor.

This function returns a tensor where every element is set to val, with the same shape as the calling tensor.

§Arguments
  • val - The value to fill the tensor with.
§Returns
  • A tensor with the same shape as the input, filled with val.
§Panics
  • This function may panic if the shape is too large for available memory.
Source

fn arange<U>(start: U, end: U) -> Result<Self::Output, TensorError>
where usize: Cast<T>, U: Cast<i64> + Cast<T> + Copy,

Creates a tensor with values within a specified range.

This function generates a 1D tensor with values ranging from start (inclusive) to end (exclusive).

§Arguments
  • start - The start of the range.
  • end - The end of the range.
§Returns
  • A 1D tensor with values ranging from start to end.
§Panics
  • This function will panic if start is greater than or equal to end, or if the range is too large for available memory.
Source

fn arange_step(start: T, end: T, step: T) -> Result<Self::Output, TensorError>
where T: Cast<f64> + Cast<usize>, usize: Cast<T>,

Creates a tensor with values within a specified range with a given step size.

This function generates a 1D tensor with values ranging from start (inclusive) to end (exclusive), incremented by step.

§Arguments
  • start - The start of the range.
  • end - The end of the range (exclusive).
  • step - The step size between consecutive values.
§Returns
  • A 1D tensor with values from start to end, incremented by step.
§Panics
  • This function will panic if step is zero or if the range and step values are incompatible.
Source

fn eye(n: usize, m: usize, k: usize) -> Result<Self::Output, TensorError>

Creates a 2D identity matrix with ones on a diagonal and zeros elsewhere.

This function generates a matrix of size n by m, with ones on the kth diagonal (can be offset) and zeros elsewhere.

§Arguments
  • n - The number of rows in the matrix.
  • m - The number of columns in the matrix.
  • k - The diagonal offset (0 for main diagonal, positive for upper diagonals, negative for lower diagonals).
§Returns
  • A 2D identity matrix with ones on the specified diagonal.
§Panics
  • This function will panic if n or m is zero, or if memory constraints are exceeded.
Source

fn linspace<U>( start: U, end: U, num: usize, include_end: bool, ) -> Result<Self::Output, TensorError>
where U: Cast<f64> + Cast<T> + Copy, usize: Cast<T>, f64: Cast<T>,

Creates a tensor with evenly spaced values between start and end.

This function generates a 1D tensor of num values, linearly spaced between start and end. If include_end is true, the end value will be included as the last element.

§Arguments
  • start - The start of the range.
  • end - The end of the range.
  • num - The number of evenly spaced values to generate.
  • include_end - Whether to include the end value in the generated tensor.
§Returns
  • A 1D tensor with num linearly spaced values between start and end.
§Panics
  • This function will panic if num is zero or if num is too large for available memory.
Source

fn logspace( start: T, end: T, num: usize, include_end: bool, base: T, ) -> Result<Self::Output, TensorError>
where T: Cast<f64> + Float + NormalOut<Output = T>, usize: Cast<T>, f64: Cast<T>,

Creates a tensor with logarithmically spaced values between start and end.

This function generates a 1D tensor of num values spaced evenly on a log scale between start and end. The spacing is based on the logarithm to the given base. If include_end is true, the end value will be included.

§Arguments
  • start - The starting exponent (base base).
  • end - The ending exponent (base base).
  • num - The number of logarithmically spaced values to generate.
  • include_end - Whether to include the end value in the generated tensor.
  • base - The base of the logarithm.
§Returns
  • A 1D tensor with num logarithmically spaced values between start and end.
§Panics
  • This function will panic if num is zero or if base is less than or equal to zero.
Source

fn geomspace( start: T, end: T, n: usize, include_end: bool, ) -> Result<Self::Output, TensorError>
where f64: Cast<T>, usize: Cast<T>, T: Cast<f64>,

Creates a tensor with geometrically spaced values between start and end.

This function generates a 1D tensor of n values spaced evenly on a geometric scale between start and end. If include_end is true, the end value will be included.

§Arguments
  • start - The starting value (must be positive).
  • end - The ending value (must be positive).
  • n - The number of geometrically spaced values to generate.
  • include_end - Whether to include the end value in the generated tensor.
§Returns
  • A 1D tensor with n geometrically spaced values between start and end.
§Panics
  • This function will panic if n is zero, if start or end is negative, or if the values result in undefined behavior.
Source

fn tri( n: usize, m: usize, k: i64, low_triangle: bool, ) -> Result<Self::Output, TensorError>
where u8: Cast<T>,

Creates a 2D triangular matrix of size n by m, with ones below or on the kth diagonal and zeros elsewhere.

This function generates a matrix with a triangular structure, filled with ones and zeros, based on the diagonal offset and the low_triangle flag.

§Arguments
  • n - The number of rows in the matrix.
  • m - The number of columns in the matrix.
  • k - The diagonal offset (0 for main diagonal, positive for upper diagonals, negative for lower diagonals).
  • low_triangle - If true, the matrix will be lower triangular; otherwise, upper triangular.
§Returns
  • A 2D triangular matrix of ones and zeros.
§Panics
  • This function will panic if n or m is zero.
Source

fn tril(&self, k: i64) -> Result<Self::Output, TensorError>
where T: NormalOut<bool, Output = T> + Cast<T> + TypeCommon, <T as TypeCommon>::Vec: NormalOut<boolx16, Output = <T as TypeCommon>::Vec>,

Returns the lower triangular part of the matrix, with all elements above the kth diagonal set to zero.

This function generates a tensor where the elements above the specified diagonal are set to zero.

§Arguments
  • k - The diagonal offset (0 for main diagonal, positive for upper diagonals, negative for lower diagonals).
§Returns
  • A tensor with its upper triangular part set to zero.
§Panics
  • This function should not panic under normal conditions.
Source

fn triu(&self, k: i64) -> Result<Self::Output, TensorError>
where T: NormalOut<bool, Output = T> + Cast<T> + TypeCommon, <T as TypeCommon>::Vec: NormalOut<boolx16, Output = <T as TypeCommon>::Vec>,

Returns the upper triangular part of the matrix, with all elements below the kth diagonal set to zero.

This function generates a tensor where the elements below the specified diagonal are set to zero.

§Arguments
  • k - The diagonal offset (0 for main diagonal, positive for upper diagonals, negative for lower diagonals).
§Returns
  • A tensor with its lower triangular part set to zero.
§Panics
  • This function should not panic under normal conditions.
Source

fn identity(n: usize) -> Result<Self::Output, TensorError>
where u8: Cast<T>,

Creates a 2D identity matrix of size n by n.

This function generates a square matrix with ones on the main diagonal and zeros elsewhere.

§Arguments
  • n - The size of the matrix (both rows and columns).
§Returns
  • A 2D identity matrix of size n.
§Panics
  • This function will panic if n is zero.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: CommonBounds, const DEVICE: usize, Al> TensorCreator<T> for DiffTensor<T, Cpu, DEVICE, Al>

Source§

type Output = DiffTensor<T, Cpu, DEVICE, Al>

Source§

impl<T: CommonBounds, const DEVICE: usize, Al> TensorCreator<T> for Tensor<T, Cpu, DEVICE, Al>

Source§

type Output = Tensor<T, Cpu, DEVICE, Al>