tensor-operations
โค๏ธ Support This Project
If you find this project helpful, please consider supporting it:
- Donate via PayPal - Help keep this project maintained and improved
Week 13: Matrix and Tensor Operations in Rust
A comprehensive learning project implementing matrix and linear algebra operations in Rust, with two complementary approaches:
- Manual Implementation: Pure Rust for understanding algorithms
- Optimized Implementation: nalgebra library for production-ready code
Perfect for learning both Rust fundamentals and linear algebra concepts through practical implementation.
๐ฏ Quick Start
Prerequisites: Rust 1.56+ installed (install.rust-lang.org)
# Run examples
# Run tests
# Run benchmarks
# View documentation
๐ Documentation
-
RUST_GUIDE.md - Complete learning guide covering:
- Manual vs optimized implementations
- Algorithm explanations
- Rust language fundamentals
- Performance characteristics
- Testing and benchmarking
-
WEEK13_STUDY_GUIDE.md - Original Python study guide with:
- Matrix operation fundamentals
- Linear algebra concepts
- Performance analysis methodology
๐๏ธ Project Structure
โโโ src/
โ โโโ lib.rs # Library root (module exports)
โ โโโ manual.rs # Pure Rust matrix implementation (~450 lines)
โ โโโ optimized.rs # nalgebra-based implementation (~380 lines)
โ
โโโ examples/
โ โโโ basic_operations.rs # Side-by-side comparison demo
โ โโโ performance_comparison.rs # Timing measurements
โ โโโ advanced_operations.rs # QR, SVD, eigenvalues, solving
โ
โโโ benches/
โ โโโ matrix_benchmarks.rs # Criterion benchmarks
โ
โโโ Cargo.toml # Project configuration
โโโ README.md # This file
๐ What You'll Learn
Rust Concepts:
- Ownership and borrowing
- Error handling with Result types
- Trait implementation (Display, Add, Sub, Mul, Index)
- Module system
- Testing framework
- Benchmarking
Linear Algebra:
- Matrix creation and manipulation
- Matrix arithmetic (addition, multiplication)
- Determinant and matrix inverse
- QR decomposition
- Singular Value Decomposition (SVD)
- Eigenvalues and eigenvectors
- Linear system solving
Performance Engineering:
- Algorithm complexity analysis
- Benchmarking techniques
- BLAS-optimized operations
- Memory layout impact on performance
๐ Running Examples
1. Basic Operations (Manual vs Optimized)
Shows side-by-side comparison of both implementations performing the same operations.
2. Performance Comparison
Measures execution time and calculates speedup of optimized vs manual implementation.
3. Advanced Operations
Demonstrates QR decomposition, SVD, eigenvalues, and linear system solving using optimized nalgebra backend.
๐งช Testing
# Run all tests
# Run with output
# Run specific test
# Run specific module tests
Test Coverage:
- Manual implementation: 17 tests covering all operations
- Optimized implementation: 11 tests
- Total: 28 unit tests
๐ Benchmarking
# Run criterion benchmarks
# Benchmark specific operation
Benchmarks test performance across multiple matrix sizes and generate HTML reports in target/criterion/.
๐ก Key Code Examples
Creating a Matrix:
use Matrix;
let m = new?;
Matrix Operations:
let sum = a.add?;
let product = a.mul?;
let det = a.determinant?;
let inverse = a.inverse?;
Advanced Operations:
use OptimizedMatrix;
let = m.qr_decomposition?;
let = m.svd?;
let evals = m.eigenvalues?;
let x = a.solve?;
โก Performance Overview
Typical speedups (optimized vs manual) on modern hardware:
| Operation | Size | Manual | Optimized | Speedup |
|---|---|---|---|---|
| Addition | 10ร10 | ~5 ยตs | ~0.7 ยตs | 7x |
| Multiplication | 20ร20 | ~16 ms | ~150 ยตs | 100x |
| Determinant | 5ร5 | ~50 ยตs | ~5 ยตs | 10x |
| Transpose | 50ร50 | ~10 ยตs | ~2 ยตs | 5x |
See RUST_GUIDE.md for detailed performance analysis.
๐ฆ Dependencies
- nalgebra 0.32: High-performance linear algebra (BLAS-optimized)
- ndarray 0.15: N-dimensional arrays
- serde 1.0: Serialization framework
- criterion 0.5: Benchmarking (dev)
- rand 0.8: Random numbers (dev)
๐ Educational Path
- Days 1-2: Learn Rust fundamentals
- Days 3-4: Study manual implementation (
src/manual.rs) - Days 5-6: Study optimized implementation (
src/optimized.rs) - Day 7: Run benchmarks and analyze results
See RUST_GUIDE.md for detailed learning plan.
๐ Further Resources
- Rust Book
- nalgebra Documentation
- 3Blue1Brown - Essence of Linear Algebra
- Linear Algebra - MIT OpenCourseWare
๐ File-by-File Guide
| File | Lines | Purpose |
|---|---|---|
src/manual.rs |
450+ | Pure Rust implementation, 17 tests |
src/optimized.rs |
380+ | nalgebra wrapper, 11 tests |
src/lib.rs |
44 | Library root with exports |
examples/basic_operations.rs |
180+ | Basic usage demonstration |
examples/performance_comparison.rs |
160+ | Timing measurements |
examples/advanced_operations.rs |
210+ | QR, SVD, eigenvalues |
benches/matrix_benchmarks.rs |
120+ | Criterion benchmarks |
๐ Learning Outcomes
Upon completing this project, you will understand:
โ
How matrix operations work at a fundamental level
โ
Rust ownership, borrowing, and error handling
โ
Algorithm complexity and performance optimization
โ
When to use optimized libraries vs. manual implementation
โ
How to benchmark and measure performance
โ
Best practices for numerical computing in Rust
๐ Python Version
This project also includes the original Python implementation for comparison:
WEEK13_STUDY_GUIDE.md- Study materialsmanual_matrix.py- Manual implementationnumpy_matrix.py- NumPy implementationWeek13_ndarray_tensor_operations.ipynb- Jupyter notebook
๐ License
MIT License
- Speedup increases with matrix size
- BLAS/LAPACK optimizations provide major advantages
๐ Quick Start
# Manual implementation
=
=
= @ # Matrix multiplication
# NumPy implementation
=
=
= @ # Same operation, much faster
๐ Project Structure
tensor-operations/
โโโ README.md # This file
โโโ WEEK13_STUDY_GUIDE.md # Comprehensive study guide
โโโ manual_matrix.py # Manual implementations
โโโ numpy_matrix.py # NumPy implementations
โโโ Week13_ndarray_tensor_operations.ipynb # Interactive notebook
โโโ performance_comparison.png # Benchmark visualization
๐ก Learning Outcomes
By completing this week's materials, you will:
- โ Understand ndarray structure and capabilities
- โ Master tensor operations and broadcasting
- โ Implement matrix operations from scratch
- โ Optimize code using NumPy
- โ Compare performance of different approaches
- โ Know when to use manual vs library implementations
๐ Topics Covered
- ndarray creation and initialization
- Tensor operations and linear algebra
- Broadcasting and vectorization
- Matrix operations: addition, subtraction, multiplication, transpose
- Advanced operations: determinant, inverse, eigenvalues, SVD
- Performance benchmarking and analysis
- Memory efficiency considerations
- Best practices for numerical computing