pub trait TensorElement:
TensorScalar
+ Copy
+ Send
+ Sync
+ 'static {
// Required methods
fn dense_native_tensor_from_col_major(
data: &[Self],
dims: &[usize],
) -> Result<Tensor, Error>;
fn dense_native_tensor_from_col_major_owned(
data: Vec<Self>,
dims: &[usize],
) -> Result<Tensor, Error>;
fn diag_native_tensor_from_col_major(
data: &[Self],
logical_rank: usize,
) -> Result<Tensor, Error>;
fn scalar_native_tensor(value: Self) -> Result<Tensor, Error>;
fn dense_values_from_native_col_major(
tensor: &Tensor,
) -> Result<Vec<Self>, Error>;
fn into_dense_values_from_native_col_major(
tensor: Tensor,
) -> Result<Vec<Self>, Error>;
fn diag_values_from_native_temp(tensor: &Tensor) -> Result<Vec<Self>, Error>;
}Expand description
Public scalar element types supported by tensor4all dense/diag constructors.
Implemented for f32, f64, Complex32, and Complex64.
§Examples
use tensor4all_tensorbackend::TensorElement;
let t = f64::dense_native_tensor_from_col_major(&[1.0, 2.0], &[2]).unwrap();
assert_eq!(t.shape(), &[2]);
let vals = f64::dense_values_from_native_col_major(&t).unwrap();
assert_eq!(vals, vec![1.0, 2.0]);Required Methods§
Sourcefn dense_native_tensor_from_col_major(
data: &[Self],
dims: &[usize],
) -> Result<Tensor, Error>
fn dense_native_tensor_from_col_major( data: &[Self], dims: &[usize], ) -> Result<Tensor, Error>
Build a dense native tensor from column-major data.
§Errors
Returns an error when the shape product would overflow usize, when
data.len() does not match the expected shape, or when the backend
rejects the tensor.
Sourcefn dense_native_tensor_from_col_major_owned(
data: Vec<Self>,
dims: &[usize],
) -> Result<Tensor, Error>
fn dense_native_tensor_from_col_major_owned( data: Vec<Self>, dims: &[usize], ) -> Result<Tensor, Error>
Build a dense native tensor by moving an owned column-major payload.
This is the allocation-preserving counterpart of
Self::dense_native_tensor_from_col_major. The payload must contain
exactly the product of dims elements.
§Errors
Returns an error for a shape mismatch when the data length does not equal the dimension product, or for a backend conversion failure.
§Arguments
data- Owned column-major values whose length equals the product ofdims.dims- Logical tensor dimensions in column-major axis order.
§Returns
A native dense tensor that owns the supplied payload without cloning it.
§Examples
use tensor4all_tensorbackend::TensorElement;
let tensor = f64::dense_native_tensor_from_col_major_owned(
vec![1.0, 2.0, 3.0, 4.0],
&[2, 2],
)
.unwrap();
assert_eq!(tensor.shape(), &[2, 2]);
assert_eq!(tensor.as_slice::<f64>().unwrap(), &[1.0, 2.0, 3.0, 4.0]);Sourcefn diag_native_tensor_from_col_major(
data: &[Self],
logical_rank: usize,
) -> Result<Tensor, Error>
fn diag_native_tensor_from_col_major( data: &[Self], logical_rank: usize, ) -> Result<Tensor, Error>
Build a diagonal native tensor from column-major diagonal payload data.
§Errors
Returns an error when logical_rank is zero, when expanding the
diagonal would overflow usize, or when the backend rejects the
resulting dense tensor.
Sourcefn scalar_native_tensor(value: Self) -> Result<Tensor, Error>
fn scalar_native_tensor(value: Self) -> Result<Tensor, Error>
Build a rank-0 native tensor.
§Errors
Returns an error when the backend rejects the rank-0 tensor as invalid input.
Sourcefn dense_values_from_native_col_major(
tensor: &Tensor,
) -> Result<Vec<Self>, Error>
fn dense_values_from_native_col_major( tensor: &Tensor, ) -> Result<Vec<Self>, Error>
Materialize dense column-major values from a native tensor.
§Errors
Returns an error when tensor’s dtype does not match Self.
Sourcefn into_dense_values_from_native_col_major(
tensor: Tensor,
) -> Result<Vec<Self>, Error>
fn into_dense_values_from_native_col_major( tensor: Tensor, ) -> Result<Vec<Self>, Error>
Materialize dense column-major values while consuming the native tensor.
This is the ownership-preserving counterpart of
Self::dense_values_from_native_col_major: a compact column-major host
result hands its buffer over instead of being copied, so a caller that
built the tensor only to read it back pays no extra full-size
allocation.
§Arguments
tensor- Consumed native tensor whose logical values are returned.
§Returns
The tensor’s values in column-major order, in Self’s scalar type.
§Errors
Returns an error on a dtype mismatch between tensor and Self, and when
tensor’s storage cannot hand over a compact column-major host buffer
because it is backend-resident or its layout is not compact, instead of
copying or silently reordering it.
§Examples
use tensor4all_tensorbackend::TensorElement;
let tensor =
f64::dense_native_tensor_from_col_major(&[1.0, 3.0, 2.0, 4.0], &[2, 2]).unwrap();
let values = f64::into_dense_values_from_native_col_major(tensor).unwrap();
assert_eq!(values, vec![1.0, 3.0, 2.0, 4.0]);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".