tensor-operations

Crates.iotensor-operations
lib.rstensor-operations
version0.1.0
created_at2026-01-18 15:33:10.07808+00
updated_at2026-01-18 15:33:10.07808+00
descriptionWeek 13: Matrix and Tensor Operations in Rust - Manual vs Optimized
homepage
repository
max_upload_size
id2052502
size368,013
Maxim Vialyx (vialyx)

documentation

README

tensor-operations

โค๏ธ Support This Project

If you find this project helpful, please consider supporting it:

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
cargo run --example basic_operations
cargo run --example performance_comparison --release
cargo run --example advanced_operations

# Run tests
cargo test

# Run benchmarks
cargo bench

# View documentation
cargo doc --no-deps --open

๐Ÿ“š Documentation

  1. RUST_GUIDE.md - Complete learning guide covering:

    • Manual vs optimized implementations
    • Algorithm explanations
    • Rust language fundamentals
    • Performance characteristics
    • Testing and benchmarking
  2. 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)

cargo run --example basic_operations

Shows side-by-side comparison of both implementations performing the same operations.

2. Performance Comparison

cargo run --example performance_comparison --release

Measures execution time and calculates speedup of optimized vs manual implementation.

3. Advanced Operations

cargo run --example advanced_operations

Demonstrates QR decomposition, SVD, eigenvalues, and linear system solving using optimized nalgebra backend.

๐Ÿงช Testing

# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_matrix_creation

# Run specific module tests
cargo test manual::
cargo test optimized::

Test Coverage:

  • Manual implementation: 17 tests covering all operations
  • Optimized implementation: 11 tests
  • Total: 28 unit tests

๐Ÿ“Š Benchmarking

# Run criterion benchmarks
cargo bench

# Benchmark specific operation
cargo bench benchmark_addition

Benchmarks test performance across multiple matrix sizes and generate HTML reports in target/criterion/.

๐Ÿ’ก Key Code Examples

Creating a Matrix:

use tensor_operations::manual::Matrix;

let m = Matrix::new(vec![
    vec![1.0, 2.0],
    vec![3.0, 4.0],
])?;

Matrix Operations:

let sum = a.add(&b)?;
let product = a.mul(&b)?;
let det = a.determinant()?;
let inverse = a.inverse()?;

Advanced Operations:

use tensor_operations::optimized::OptimizedMatrix;

let (q, r) = m.qr_decomposition()?;
let (u, singular_vals, vt) = m.svd()?;
let evals = m.eigenvalues()?;
let x = a.solve(&b)?;

โšก 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

  1. Days 1-2: Learn Rust fundamentals
  2. Days 3-4: Study manual implementation (src/manual.rs)
  3. Days 5-6: Study optimized implementation (src/optimized.rs)
  4. Day 7: Run benchmarks and analyze results

See RUST_GUIDE.md for detailed learning plan.

๐Ÿ”— Further Resources

๐Ÿ“‹ 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 materials
  • manual_matrix.py - Manual implementation
  • numpy_matrix.py - NumPy implementation
  • Week13_ndarray_tensor_operations.ipynb - Jupyter notebook

๐Ÿ“ License

MIT License

  • Speedup increases with matrix size
  • BLAS/LAPACK optimizations provide major advantages

๐Ÿš€ Quick Start

# Manual implementation
from manual_matrix import Matrix
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[5, 6], [7, 8]])
C = A @ B  # Matrix multiplication

# NumPy implementation
from numpy_matrix import NumpyMatrix
A = NumpyMatrix([[1, 2], [3, 4]])
B = NumpyMatrix([[5, 6], [7, 8]])
C = A @ B  # 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:

  1. โœ… Understand ndarray structure and capabilities
  2. โœ… Master tensor operations and broadcasting
  3. โœ… Implement matrix operations from scratch
  4. โœ… Optimize code using NumPy
  5. โœ… Compare performance of different approaches
  6. โœ… 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
Commit count: 0

cargo fmt