Skip to main content

TensorConstructionLike

Trait TensorConstructionLike 

Source
pub trait TensorConstructionLike: TensorContractionLike {
Show 13 methods // Required methods fn diagonal( input_index: &<Self as TensorIndex>::Index, output_index: &<Self as TensorIndex>::Index, ) -> Result<Self, Self::Error>; fn scalar_one() -> Result<Self, Self::Error>; fn ones( indices: &[<Self as TensorIndex>::Index], ) -> Result<Self, Self::Error>; fn onehot( index_vals: &[(<Self as TensorIndex>::Index, usize)], ) -> Result<Self, Self::Error>; // Provided methods fn delta( input_indices: &[<Self as TensorIndex>::Index], output_indices: &[<Self as TensorIndex>::Index], ) -> Result<Self, Self::Error> { ... } fn ones_in( _context: &ExecutionContext, _indices: &[<Self as TensorIndex>::Index], ) -> Result<Self, Self::Error> { ... } fn validate_context( &self, _context: &ExecutionContext, ) -> Result<(), Self::Error> { ... } fn from_dense_any( indices: Vec<<Self as TensorIndex>::Index>, data: Vec<AnyScalar>, ) -> Result<Self, Self::Error> where Self: TensorVectorSpace { ... } fn from_dense<T>( indices: Vec<<Self as TensorIndex>::Index>, data: Vec<T>, ) -> Result<Self, Self::Error> where Self: TensorVectorSpace, T: TensorElement + Into<AnyScalar> { ... } fn from_dense_in<T>( _context: &ExecutionContext, _indices: Vec<<Self as TensorIndex>::Index>, _data: Vec<T>, ) -> Result<Self, Self::Error> where Self: TensorVectorSpace, T: TensorElement + Into<AnyScalar> { ... } fn stack_along_new_index( tensors: &[&Self], new_index: <Self as TensorIndex>::Index, axis: isize, ) -> Result<Self, Self::Error> where Self: TensorVectorSpace { ... } fn concatenate_along_new_index( tensors: &[&Self], source_indices: &[<Self as TensorIndex>::Index], new_index: <Self as TensorIndex>::Index, ) -> Result<Self, Self::Error> where Self: TensorVectorSpace { ... } fn select_indices( &self, selected_indices: &[<Self as TensorIndex>::Index], positions: &[usize], ) -> Result<Self, Self::Error> { ... }
}
Expand description

Constructors and selection helpers for index-labelled tensors.

Required Methods§

Source

fn diagonal( input_index: &<Self as TensorIndex>::Index, output_index: &<Self as TensorIndex>::Index, ) -> Result<Self, Self::Error>

Create a diagonal (Kronecker delta) tensor for a single index pair.

§Errors

Returns Self::Error when the input and output indices have unequal dimensions (a shape mismatch) or the underlying construction reports a failure.

Source

fn scalar_one() -> Result<Self, Self::Error>

Create a scalar tensor with value 1.0.

§Errors

Returns Self::Error when the scalar type does not support the required construction (an invalid scalar dtype or a backend construction failure).

Source

fn ones(indices: &[<Self as TensorIndex>::Index]) -> Result<Self, Self::Error>

Create a tensor filled with 1.0 for the given indices.

§Errors

Returns Self::Error when an index dimension product overflows (an overflow failure) or the underlying construction reports a failure.

Source

fn onehot( index_vals: &[(<Self as TensorIndex>::Index, usize)], ) -> Result<Self, Self::Error>

Create a one-hot tensor with value 1.0 at the specified index positions.

§Errors

Returns Self::Error when a position is out of range for its index (an out of bounds failure) or the underlying construction reports a failure.

Provided Methods§

Source

fn delta( input_indices: &[<Self as TensorIndex>::Index], output_indices: &[<Self as TensorIndex>::Index], ) -> Result<Self, Self::Error>

Create a delta (identity) tensor as outer product of diagonals.

§Errors

Returns Self::Error when the input and output index lists differ in length (a length mismatch) or when a constituent diagonal or outer product reports a failure; propagates failures from Self::diagonal, Self::scalar_one, and [Self::outer_product].

Source

fn ones_in( _context: &ExecutionContext, _indices: &[<Self as TensorIndex>::Index], ) -> Result<Self, Self::Error>

Create an all-ones tensor in a caller-owned execution context.

Context-scoped counterpart of Self::ones: the result belongs to context, with an explicit host-to-device transfer for CUDA contexts.

§Examples
use std::sync::Arc;
use tensor4all_core::{DynIndex, ExecutionContext, TensorConstructionLike};
use tensor4all_core::IdxTensor;
use tensor4all_tensorbackend::CpuExecutionContext;
use tenferro_cpu::CpuBackend;

let context = ExecutionContext::Cpu(Arc::new(
    CpuExecutionContext::from_backend(CpuBackend::new()),
));
let tensor = <IdxTensor as TensorConstructionLike>::ones_in(
    &context,
    &[DynIndex::new_dyn(2)],
)?;
assert_eq!(tensor.to_vec::<f64>()?, vec![1.0, 1.0]);
§Errors

Returns Self::Error with an UnsupportedStorage failure when construction is unsupported for this tensor type; other failures report an invalid payload or a backend transfer failure.

Source

fn validate_context( &self, _context: &ExecutionContext, ) -> Result<(), Self::Error>

Validate that this tensor belongs to the supplied execution context.

Generic SRC entries call this on every input tensor before RNG advancement or contraction, so mixed host/CUDA inputs and foreign CUDA contexts fail at the boundary with typed errors.

§Errors

Returns Self::Error with an UnsupportedStorage failure when validation is unsupported for this tensor type, or a backend failure when tensor placement does not belong to context.

Source

fn from_dense_any( indices: Vec<<Self as TensorIndex>::Index>, data: Vec<AnyScalar>, ) -> Result<Self, Self::Error>
where Self: TensorVectorSpace,

Construct a tensor from a column-major dense payload.

Implementations with a native dense storage path should override this method. The default preserves compatibility for tensor types that only expose one-hot construction, at the cost of constructing a sparse sum of one-hot tensors.

§Arguments
  • indices - External indices in the intended column-major axis order.
  • data - Dense values in column-major order; its length must equal the product of the index dimensions.
§Errors

Returns Self::Error when the input payload length does not match the product of the index dimensions, that index-dimension product would overflow usize, or an underlying tensor construction operation fails.

§Examples
use tensor4all_core::{AnyScalar, DynIndex, IdxTensor, TensorConstructionLike};

let index = DynIndex::new_dyn(2);
let tensor = <IdxTensor as TensorConstructionLike>::from_dense_any(
    vec![index],
    vec![AnyScalar::new_real(2.0), AnyScalar::new_real(3.0)],
)
.unwrap();
assert_eq!(tensor.to_vec::<f64>().unwrap(), vec![2.0, 3.0]);
Source

fn from_dense<T>( indices: Vec<<Self as TensorIndex>::Index>, data: Vec<T>, ) -> Result<Self, Self::Error>

Construct a tensor directly from a typed column-major dense payload.

Implementations with native typed storage should override this method to avoid converting every element through AnyScalar.

§Arguments
  • indices - External indices in the intended column-major axis order.
  • data - Typed dense values in column-major order; its length must equal the product of the index dimensions.
§Returns

A tensor whose dtype is selected from T by the implementation.

§Errors

Returns Self::Error when the index-dimension product overflows, the data length does not match that product, the scalar dtype is unsupported, or Self::from_dense_any otherwise rejects construction.

§Examples
use tensor4all_core::{DynIndex, IdxTensor, TensorConstructionLike};

let index = DynIndex::new_dyn(2);
let tensor = <IdxTensor as TensorConstructionLike>::from_dense(
    vec![index],
    vec![2.0_f64, 3.0],
)
.unwrap();
assert_eq!(tensor.to_vec::<f64>().unwrap(), vec![2.0, 3.0]);
Source

fn from_dense_in<T>( _context: &ExecutionContext, _indices: Vec<<Self as TensorIndex>::Index>, _data: Vec<T>, ) -> Result<Self, Self::Error>

Construct a tensor from a column-major dense payload in a caller-owned execution context.

Context-scoped counterpart of Self::from_dense: host-originated data takes one explicit construction transfer for CUDA contexts, and the result belongs to context.

§Examples
use std::sync::Arc;
use tensor4all_core::{DynIndex, ExecutionContext, TensorConstructionLike};
use tensor4all_core::IdxTensor;
use tensor4all_tensorbackend::CpuExecutionContext;
use tenferro_cpu::CpuBackend;

let context = ExecutionContext::Cpu(Arc::new(
    CpuExecutionContext::from_backend(CpuBackend::new()),
));
let tensor = <IdxTensor as TensorConstructionLike>::from_dense_in(
    &context,
    vec![DynIndex::new_dyn(2)],
    vec![2.0_f64, 3.0],
)?;
assert_eq!(tensor.to_vec::<f64>()?, vec![2.0, 3.0]);
§Errors

Returns Self::Error with an UnsupportedStorage failure when construction is unsupported for this tensor type; other failures report an invalid payload or a backend transfer failure.

Source

fn stack_along_new_index( tensors: &[&Self], new_index: <Self as TensorIndex>::Index, axis: isize, ) -> Result<Self, Self::Error>
where Self: TensorVectorSpace,

Stack tensors along a newly created batch index.

Implementations with a native batch stack should override this method. The default constructs the batch by outer products with one-hot batch vectors, which is correct but intended only as a compatibility path.

§Arguments
  • tensors - Non-empty tensors with identical external index order.
  • new_index - Fresh index whose dimension equals tensors.len().
  • axis - Insertion axis; negative axes count from the end, so -1 appends the batch axis.
§Errors

Returns Self::Error when tensors are empty, their index orders differ, the batch dimension is wrong, the axis is invalid, or construction fails.

§Examples
use tensor4all_core::{DynIndex, IdxTensor, TensorConstructionLike};

let index = DynIndex::new_dyn(2);
let batch = DynIndex::new_dyn(2);
let first = IdxTensor::from_dense(vec![index.clone()], vec![1.0, 2.0]).unwrap();
let second = IdxTensor::from_dense(vec![index.clone()], vec![3.0, 4.0]).unwrap();
let stacked = <IdxTensor as TensorConstructionLike>::stack_along_new_index(
    &[&first, &second],
    batch.clone(),
    -1,
)
.unwrap();
assert_eq!(stacked.indices(), &[index, batch]);
assert_eq!(stacked.to_vec::<f64>().unwrap(), vec![1.0, 2.0, 3.0, 4.0]);
Source

fn concatenate_along_new_index( tensors: &[&Self], source_indices: &[<Self as TensorIndex>::Index], new_index: <Self as TensorIndex>::Index, ) -> Result<Self, Self::Error>
where Self: TensorVectorSpace,

Concatenate tensors whose selected axes are replaced by one new index.

The tensors must have the same index order away from the selected axis. Each tensor may use a distinct source index at that axis; the source axes are copied in tensor order into new_index. This is the batched counterpart to appending column blocks without recomputing the old columns.

§Arguments
  • tensors - Non-empty tensors with matching non-concatenated axes.
  • source_indices - One axis to concatenate for each tensor.
  • new_index - Fresh output axis whose dimension is the sum of source dimensions.
§Errors

Returns Self::Error when the input list is empty; the tensor and source-index counts do not match; a source index is missing; source axes occupy incompatible positions; non-concatenated indices are incompatible; the source-dimension sum overflows; or the new index dimension does not match that sum.

§Examples
use tensor4all_core::{DynIndex, IdxTensor, TensorConstructionLike};

let row = DynIndex::new_dyn(2);
let first_batch = DynIndex::new_link(1).unwrap();
let second_batch = DynIndex::new_link(2).unwrap();
let combined = DynIndex::new_link(3).unwrap();
let first = IdxTensor::from_dense(
    vec![row.clone(), first_batch.clone()],
    vec![1.0_f64, 2.0],
).unwrap();
let second = IdxTensor::from_dense(
    vec![row.clone(), second_batch.clone()],
    vec![3.0, 4.0, 5.0, 6.0],
).unwrap();
let result = <IdxTensor as TensorConstructionLike>::concatenate_along_new_index(
    &[&first, &second],
    &[first_batch, second_batch],
    combined.clone(),
).unwrap();
assert_eq!(result.indices(), &[row, combined]);
assert_eq!(result.to_vec::<f64>().unwrap(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
Source

fn select_indices( &self, selected_indices: &[<Self as TensorIndex>::Index], positions: &[usize], ) -> Result<Self, Self::Error>

Select fixed coordinates for a subset of this tensor’s external indices.

§Errors

Returns Self::Error when selected_indices and positions differ in length (a length mismatch), when an index is selected more than once (a duplicate-index failure), when a coordinate is out of range (an out of bounds failure), or when the underlying one-hot construction or contraction reports a failure; propagates failures from Self::onehot and [Self::contract].

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§