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§
Required Methods§
Sourcefn empty<S>(shape: S) -> Result<Self::Output, TensorError>
fn empty<S>(shape: S) -> Result<Self::Output, TensorError>
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 typeSmust implementInto<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.
Sourcefn zeros<S>(shape: S) -> Result<Self::Output, TensorError>
fn zeros<S>(shape: S) -> Result<Self::Output, TensorError>
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 typeSmust implementInto<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.
Sourcefn ones<S>(shape: S) -> Result<Self::Output, TensorError>
fn ones<S>(shape: S) -> Result<Self::Output, TensorError>
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 typeSmust implementInto<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.
Sourcefn empty_like(&self) -> Result<Self::Output, TensorError>
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.
Sourcefn zeros_like(&self) -> Result<Self::Output, TensorError>
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.
Sourcefn ones_like(&self) -> Result<Self::Output, TensorError>
fn ones_like(&self) -> Result<Self::Output, TensorError>
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.
Sourcefn full<S>(val: T, shape: S) -> Result<Self::Output, TensorError>
fn full<S>(val: T, shape: S) -> Result<Self::Output, TensorError>
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 typeSmust implementInto<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.
Sourcefn full_like(&self, val: T) -> Result<Self::Output, TensorError>
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.
Sourcefn arange<U>(start: U, end: U) -> Result<Self::Output, TensorError>
fn arange<U>(start: U, end: U) -> Result<Self::Output, TensorError>
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
starttoend.
§Panics
- This function will panic if
startis greater than or equal toend, or if the range is too large for available memory.
Sourcefn arange_step(start: T, end: T, step: T) -> Result<Self::Output, TensorError>
fn arange_step(start: T, end: T, step: T) -> Result<Self::Output, TensorError>
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
starttoend, incremented bystep.
§Panics
- This function will panic if
stepis zero or if the range and step values are incompatible.
Sourcefn eye(n: usize, m: usize, k: usize) -> Result<Self::Output, TensorError>
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
normis zero, or if memory constraints are exceeded.
Sourcefn linspace<U>(
start: U,
end: U,
num: usize,
include_end: bool,
) -> Result<Self::Output, TensorError>
fn linspace<U>( start: U, end: U, num: usize, include_end: bool, ) -> Result<Self::Output, TensorError>
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 theendvalue in the generated tensor.
§Returns
- A 1D tensor with
numlinearly spaced values betweenstartandend.
§Panics
- This function will panic if
numis zero or ifnumis too large for available memory.
Sourcefn logspace(
start: T,
end: T,
num: usize,
include_end: bool,
base: T,
) -> Result<Self::Output, TensorError>
fn logspace( start: T, end: T, num: usize, include_end: bool, base: T, ) -> Result<Self::Output, TensorError>
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 (basebase).end- The ending exponent (basebase).num- The number of logarithmically spaced values to generate.include_end- Whether to include theendvalue in the generated tensor.base- The base of the logarithm.
§Returns
- A 1D tensor with
numlogarithmically spaced values betweenstartandend.
§Panics
- This function will panic if
numis zero or ifbaseis less than or equal to zero.
Sourcefn geomspace(
start: T,
end: T,
n: usize,
include_end: bool,
) -> Result<Self::Output, TensorError>
fn geomspace( start: T, end: T, n: usize, include_end: bool, ) -> Result<Self::Output, TensorError>
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 theendvalue in the generated tensor.
§Returns
- A 1D tensor with
ngeometrically spaced values betweenstartandend.
§Panics
- This function will panic if
nis zero, ifstartorendis negative, or if the values result in undefined behavior.
Sourcefn tri(
n: usize,
m: usize,
k: i64,
low_triangle: bool,
) -> Result<Self::Output, TensorError>
fn tri( n: usize, m: usize, k: i64, low_triangle: bool, ) -> Result<Self::Output, TensorError>
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- Iftrue, the matrix will be lower triangular; otherwise, upper triangular.
§Returns
- A 2D triangular matrix of ones and zeros.
§Panics
- This function will panic if
normis zero.
Sourcefn 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 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.
Sourcefn 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 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.
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.