Skip to main content

StorageScalar

Trait StorageScalar 

Source
pub trait StorageScalar:
    TensorElement
    + Default
    + PartialEq
    + Num {
    type Complex: StorageScalar;

    const KIND: &'static str;
    const IS_COMPLEX: bool;
Show 14 methods // Required methods fn widened_parts(self) -> (f64, f64); fn from_widened_parts(re: f64, im: f64) -> Self; fn real_part(self) -> Self::Real; fn imag_part(self) -> Self::Real; fn into_complex(self) -> Self::Complex; fn conjugate(self) -> Self; fn wrap_payload(payload: StructuredStorage<Self>) -> Storage; fn to_backend_scalar(self) -> BackendScalar; fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>; fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>; fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>; // Provided methods fn build_dense_storage( data: Vec<Self>, logical_dims: &[usize], ) -> Result<Storage> { ... } fn build_diag_storage( diag_data: Vec<Self>, logical_rank: usize, ) -> Result<Storage> { ... } fn build_structured_storage( data: Vec<Self>, payload_dims: Vec<usize>, strides: Vec<isize>, axis_classes: Vec<usize>, ) -> Result<Storage> { ... }
}
Expand description

Trait for scalar types that can be stored in Storage.

This enables generic constructors such as Storage::from_dense_col_major and Storage::from_diag_col_major, and it is the single seam through which dtype-erased storage code reaches a concrete payload. Implemented for f64, Complex64, f32, and Complex32.

Compact storage never widens a payload: f32 stays f32 and Complex32 stays Complex32 in memory and on every accessor. The associated items describe the real/complex structure so that dtype-agnostic scans can be written once; explicit width promotion happens only in the storage algebra that documents it (Storage::axpby, Storage::scale).

§Examples

use tensor4all_tensorbackend::{Storage, StorageScalar};

// Using the generic constructor -- scalar type is inferred from data
let s = Storage::from_dense_col_major(vec![1.0_f64, 2.0, 3.0], &[3]).unwrap();
assert!(s.is_f64());

use num_complex::Complex64;
let c = Storage::from_dense_col_major(
    vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 1.0)],
    &[2],
).unwrap();
assert!(c.is_c64());

// Narrow payloads keep their dtype instead of widening.
let narrow = Storage::from_dense_col_major(vec![1.0_f32, 2.0], &[2]).unwrap();
assert!(narrow.is_f32());
assert_eq!(f32::KIND, "f32");
assert_eq!(Complex64::KIND, "Complex64");

Required Associated Constants§

Source

const KIND: &'static str

Display name of this scalar type in diagnostics ("f32", "Complex64", …).

Source

const IS_COMPLEX: bool

Whether values carry an imaginary component.

Required Associated Types§

Source

type Complex: StorageScalar

The complex counterpart of this scalar type (f32 -> Complex32).

Required Methods§

Source

fn widened_parts(self) -> (f64, f64)

Widen this value to (real, imaginary) parts for dtype-agnostic scans.

Source

fn from_widened_parts(re: f64, im: f64) -> Self

Narrow widened parts back to this scalar type.

Source

fn real_part(self) -> Self::Real

Real part as this scalar’s real counterpart.

Source

fn imag_part(self) -> Self::Real

Imaginary part as this scalar’s real counterpart (0 for real scalars).

Source

fn into_complex(self) -> Self::Complex

Embed this value, including its imaginary part, in Self::Complex.

Source

fn conjugate(self) -> Self

The complex conjugate (self for real scalars).

Source

fn wrap_payload(payload: StructuredStorage<Self>) -> Storage

Wrap a compact payload of this scalar type as a Storage.

Source

fn to_backend_scalar(self) -> BackendScalar

Copy this value into a dynamic BackendScalar.

Source

fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>

Copy the compact payload of this scalar type, rejecting other dtypes.

§Errors

Returns StorageError::ScalarKindMismatch when storage holds a different scalar type.

Source

fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>

Borrow the compact payload of this scalar type when it is contiguous.

§Errors

Returns StorageError::ScalarKindMismatch when storage holds a different scalar type.

Source

fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>

Materialize the logical dense values of this scalar type.

§Errors

Returns StorageError::ScalarKindMismatch when storage holds a different scalar type, and StorageError::InvalidStructuredStorage when logical_dims does not match the stored logical dimensions or the logical element count overflows usize.

Provided Methods§

Source

fn build_dense_storage( data: Vec<Self>, logical_dims: &[usize], ) -> Result<Storage>

Build a dense Storage from column-major data.

§Errors

Returns an error when the shape product would overflow usize or data.len() does not match the expected shape.

Source

fn build_diag_storage( diag_data: Vec<Self>, logical_rank: usize, ) -> Result<Storage>

Build a diagonal Storage from diagonal payload data.

§Errors

Returns an error when the diagonal payload length does not match the length required for logical_rank or a stride product would overflow usize.

Source

fn build_structured_storage( data: Vec<Self>, payload_dims: Vec<usize>, strides: Vec<isize>, axis_classes: Vec<usize>, ) -> Result<Storage>

Build a structured Storage from explicit payload metadata.

§Errors

Returns an error when axis_classes is not in canonical first-appearance order, when the payload_dims or strides ranks do not match axis_classes, when a stride is negative, when a size product would overflow usize, or when data.len() does not match the required payload length.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl StorageScalar for Complex32

Source§

const KIND: &'static str = "Complex32"

Source§

const IS_COMPLEX: bool = true

Source§

type Complex = Complex<f32>

Source§

fn widened_parts(self) -> (f64, f64)

Source§

fn from_widened_parts(re: f64, im: f64) -> Self

Source§

fn real_part(self) -> Self::Real

Source§

fn imag_part(self) -> Self::Real

Source§

fn into_complex(self) -> Self::Complex

Source§

fn conjugate(self) -> Self

Source§

fn wrap_payload(payload: StructuredStorage<Self>) -> Storage

Source§

fn to_backend_scalar(self) -> BackendScalar

Source§

fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>

Source§

fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>

Source§

fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>

Source§

impl StorageScalar for Complex64

Source§

const KIND: &'static str = "Complex64"

Source§

const IS_COMPLEX: bool = true

Source§

type Complex = Complex<f64>

Source§

fn widened_parts(self) -> (f64, f64)

Source§

fn from_widened_parts(re: f64, im: f64) -> Self

Source§

fn real_part(self) -> Self::Real

Source§

fn imag_part(self) -> Self::Real

Source§

fn into_complex(self) -> Self::Complex

Source§

fn conjugate(self) -> Self

Source§

fn wrap_payload(payload: StructuredStorage<Self>) -> Storage

Source§

fn to_backend_scalar(self) -> BackendScalar

Source§

fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>

Source§

fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>

Source§

fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>

Source§

impl StorageScalar for f32

Source§

const KIND: &'static str = "f32"

Source§

const IS_COMPLEX: bool = false

Source§

type Complex = Complex<f32>

Source§

fn widened_parts(self) -> (f64, f64)

Source§

fn from_widened_parts(re: f64, im: f64) -> Self

Source§

fn real_part(self) -> Self::Real

Source§

fn imag_part(self) -> Self::Real

Source§

fn into_complex(self) -> Self::Complex

Source§

fn conjugate(self) -> Self

Source§

fn wrap_payload(payload: StructuredStorage<Self>) -> Storage

Source§

fn to_backend_scalar(self) -> BackendScalar

Source§

fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>

Source§

fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>

Source§

fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>

Source§

impl StorageScalar for f64

Source§

const KIND: &'static str = "f64"

Source§

const IS_COMPLEX: bool = false

Source§

type Complex = Complex<f64>

Source§

fn widened_parts(self) -> (f64, f64)

Source§

fn from_widened_parts(re: f64, im: f64) -> Self

Source§

fn real_part(self) -> Self::Real

Source§

fn imag_part(self) -> Self::Real

Source§

fn into_complex(self) -> Self::Complex

Source§

fn conjugate(self) -> Self

Source§

fn wrap_payload(payload: StructuredStorage<Self>) -> Storage

Source§

fn to_backend_scalar(self) -> BackendScalar

Source§

fn payload_vec(storage: &Storage) -> StorageResult<Vec<Self>>

Source§

fn payload_view(storage: &Storage) -> StorageResult<Option<&[Self]>>

Source§

fn dense_values( storage: &Storage, logical_dims: &[usize], ) -> StorageResult<Vec<Self>>

Implementors§