A learning-focused Rust tensor library for exploring linear algebra operations. Built from scratch as a hands-on approach to understanding matrix mathematics and numerical computing.
This library serves as a practical companion to learning linear algebra. Each operation is implemented manually to understand the underlying mechanics of tensor computations.
- Element-wise operations: Addition, subtraction, Hadamard product (element-wise multiplication)
- Scalar multiplication: Multiply tensors by scalar values
- Matrix multiplication: Standard matrix product (
matmul) - Operator overloading: Use
+,-,*operators naturally
- Row-major tensor storage with shape validation
- Element access via coordinates
- Comprehensive error handling
use tensor_zero::Tensor;
// Create tensors
let a = Tensor::new(&vec![1.0, 2.0, 3.0, 4.0], &vec![2, 2]).unwrap();
let b = Tensor::new(&vec![5.0, 6.0, 7.0, 8.0], &vec![2, 2]).unwrap();
// Element-wise operations
let sum = (&a + &b).unwrap(); // [6.0, 8.0, 10.0, 12.0]
let diff = (&a - &b).unwrap(); // [-4.0, -4.0, -4.0, -4.0]
let prod = (&a * &b).unwrap(); // [5.0, 12.0, 21.0, 32.0]
// Scalar multiplication
let scaled = a.mul_scalar(2.0); // [2.0, 4.0, 6.0, 8.0]
// Matrix multiplication
let x = Tensor::new(&vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &vec![2, 3]).unwrap();
let y = Tensor::new(&vec![7.0, 8.0, 9.0, 1.0, 2.0, 3.0], &vec![3, 2]).unwrap();
let result = x.matmul(&y).unwrap(); // Shape: [2, 2]tensorlab/
tensorlab/
├── src/
│ ├── lib.rs # Public API and module declarations
│ ├── error.rs # Error types and Display implementation
│ ├── tensor.rs # Tensor struct and core operations
│ ├── operations.rs # Operator trait implementations
│ ├── lib.rs # Public API and module declarations
│ ├── error.rs # Error types and Display implementation
│ ├── tensor.rs # Tensor struct and core operations
│ ├── operations.rs # Operator trait implementations
│ └── main.rs # Example usage
├── tests/
│ └── integration_test.rs # Comprehensive test suite
├── Cargo.toml # Project manifest
└── README.md # This file
error.rs: Defines theErrorenum for shape mismatches and invalid operationstensor.rs: CoreTensorstruct with methods for:- Construction and element access
- Element-wise operations (add, sub, mul)
- Scalar multiplication
- Matrix multiplication
operations.rs: ImplementsAdd,Sub, andMultraits for ergonomic operator usagelib.rs: Consolidates and re-exports public API
error.rs: Defines theErrorenum for shape mismatches and invalid operationstensor.rs: CoreTensorstruct with methods for:- Construction and element access
- Element-wise operations (add, sub, mul)
- Scalar multiplication
- Matrix multiplication
operations.rs: ImplementsAdd,Sub, andMultraits for ergonomic operator usagelib.rs: Consolidates and re-exports public API
cargo testAll operations include integration tests covering both valid operations and error cases.
Next Steps:
- Matrix transpose
- Optimize matmul with transpose
- Parallel operations with Rayon
Future Goals:
- Broadcasting support
- N-dimensional operations
- Reshape and view operations
- Advanced linear algebra (inverse, determinant, SVD)
- Storage: Flat
Vec<f32>with row-major ordering - Operations: Iterator-based functional style
- Errors: Typed errors for shape mismatches and invalid operations
- Memory: Zero-copy references where possible
This is primarily a learning project, but feel free to suggest improvements or share your own learning journey!
Note: Active development follows my linear algebra learning progress. Expect frequent updates as new concepts are explored.