tenrso-core
Core tensor types, axis metadata, views, and basic operations for TenRSo.
Overview
tenrso-core provides the foundational tensor abstraction for the TenRSo ecosystem. It defines:
- Dense N-dimensional tensors with efficient memory layout
- Axis metadata with symbolic names and size tracking
- Zero-copy views and slicing operations
- Unfold/fold operations for mode-n matricization
- Reshape and permute transformations
- Unified tensor handle supporting multiple representations
Features
- Dense tensor storage backed by
scirs2_core::ndarray_ext - Flexible axis metadata with symbolic naming
- Memory-efficient views without data copying
- Safe indexing with bounds checking
- Generic over scalar types (f32, f64, complex, etc.)
- Optional serde support for serialization
Usage
Add to your Cargo.toml:
[]
= "0.1"
Creating Tensors
use ;
use Array;
// Create a 3D tensor
let data = zeros;
let axes = vec!;
let tensor = from_dense;
println!;
println!;
Axis Metadata
use AxisMeta;
let axis = new;
assert_eq!;
assert_eq!;
Tensor Views (TODO: M1)
use DenseND;
let tensor = zeros;
let view = tensor.slice;
// Zero-copy view into subset of data
Unfold/Fold Operations (TODO: M1)
use DenseND;
// Unfold along mode 1 (matricize)
let tensor = zeros;
let matrix = tensor.unfold?; // Shape: (20, 10*30)
// Fold back to original shape
let reconstructed = matrix.fold?;
Architecture
Type Hierarchy
TensorHandle<T> // Unified handle for all representations
├── TensorRepr<T> // Enum of representations
│ ├── Dense(DenseND<T>) // Dense storage
│ ├── Sparse(SparseND<T>) // Sparse storage (from tenrso-sparse)
│ └── LowRank(LowRank<T>) // Low-rank (CP/Tucker/TT)
└── Vec<AxisMeta> // Axis metadata
Dense Tensor Structure (TODO: M1)
DenseND<T>
├── data: Array<T, IxDyn> // N-D array from scirs2_core
├── strides: Vec<usize> // Memory strides
└── offset: usize // Base offset for views
API Reference
Types
Axis- Type alias forusize(axis index)Rank- Type alias forusize(number of dimensions)Shape- SmallVec for tensor dimensions (optimized for ≤6D)AxisMeta- Metadata for a single axis (name + size)TensorRepr<T>- Enum of tensor representationsTensorHandle<T>- Main tensor handle
Key Methods
TensorHandle:
rank() -> Rank- Get number of dimensionsshape() -> Shape- Get tensor shapefrom_dense(data, axes) -> Self(TODO)from_sparse(data, axes) -> Self(TODO)from_lowrank(factors, axes) -> Self(TODO)
DenseND (TODO: M1):
zeros(shape) -> Self- Create zero-initialized tensorones(shape) -> Self- Create one-initialized tensorfrom_array(array) -> Self- Create from ndarrayview() -> DenseView<T>- Get immutable viewview_mut() -> DenseMut<T>- Get mutable viewslice(&[SliceInfo]) -> DenseView<T>- Slice tensorunfold(mode) -> Array2<T>- Mode-n matricizationreshape(new_shape) -> Result<Self>- Change shapepermute(axes) -> Self- Permute dimensions
Feature Flags
default- Standard featuresserde- Enable serialization/deserialization
Dependencies
- scirs2-core (mandatory) - Array operations and random generation
- smallvec - Stack-optimized shape storage
- num-traits - Generic numeric traits
- anyhow - Error handling
- thiserror - Error type definitions
Performance Considerations
- Views are zero-copy: Slicing creates views, not copies
- SmallVec optimization: Shapes up to 6D avoid heap allocation
- Contiguous memory: Default layout is C-contiguous (row-major)
- SIMD operations: Element-wise ops use scirs2_core SIMD paths
Examples
See examples/ directory:
basic_tensor.rs- Creating and manipulating tensors (TODO)views.rs- Zero-copy views and slicing (TODO)unfold_fold.rs- Mode-n matricization (TODO)
Run examples:
Testing
# Run unit tests
# Run with all features
# Run property tests
Contributing
See ../../CONTRIBUTING.md for development guidelines.
License
Apache-2.0