vectormatrix/lib.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2/*!
3Idiomatic Matrix and Vector types for Rust.
4
5
6
7This crate implements small stack-allocated Vector and Matrix types with the obvious semantics and operations.
8`no_std` support and zero dependencies.
9
10# Core Types
11
12* [`Vector<T, N>`] - An N-dimensional vector with elements of type T
13* [`NormalizedVector<T, N>`] - A unit vector (length = 1.0) with compile-time guarantee
14* [`Matrix<T, R, C>`] - An R×C matrix with elements of type T
15
16# Operations
17
18Most obvious matrix and vector operations including:
19* scalar addition, subtraction, multiplication, and division with operator overloading
20* elementwise addition, subtraction, multiplication, and division with operator overloading
21* map operations for transforming elements
22* approximate equality for floating point types
23
24## Vector operations
25* dot product
26* cross product (3D vectors only)
27* length, square length
28* normalization to unit vector
29* euclidean distance
30* convert to/from row and column matrix
31* element access via x(), y(), z(), w() methods or indexing
32* min/max element finding
33* clamping and linear interpolation (mix)
34
35## Matrix operations
36* matrix multiplication
37* transpose
38* determinant (for 2×2, 3×3, and 4×4)
39* inverse (for 2×2, 3×3, and 4×4)
40* common affine transformations for 3×3 and 4×4 matrices:
41 - translation
42 - rotation (2D for 3×3, 3D axis-aligned for 4×4)
43 - scaling
44 - shearing
45* column and row access
46* element access via element_at() or direct column indexing
47
48# Type design
49
50* Use generics to support any element type with appropriate numeric traits
51* Use const generics to encode dimensions in the type system and enable stack allocation
52* Column-major storage for cache efficiency and graphics API compatibility
53* Where practical, use generics for optimized code at any matrix size
54* Where mathematically appropriate, implement particular algorithms for particular size
55* All operations are inlined for maximum performance
56* Use `repr(Rust)` so that the compiler may optimize memory layout for SIMD
57 (Convert to your own `repr(C)` type if you need specific memory layout)
58* API design supports future SIMD or hardware accelerated operations
59
60# Supported numeric types
61
62The library provides trait implementations for standard numeric types:
63* Floating point: `f32`, `f64` (with additional operations like sqrt, sin, cos)
64* Unsigned integers: `u8`, `u16`, `u32`, `u64`
65* Signed integers: `i8`, `i16`, `i32`, `i64`
66
67# Features
68
69* `std` (default): Enables standard library support for floating point functions like sqrt, sin, cos
70* Without `std`: Core functionality works in `no_std` environments
71
72*/
73#![no_std] // Always applies to the crate
74
75#[cfg(feature = "std")]
76extern crate std; // Explicitly import std when enabled
77
78extern crate alloc;
79
80pub mod matrix;
81mod types;
82pub mod vector;
83
84// Re-export the main types at the crate root for convenience
85pub use matrix::Matrix;
86pub use vector::{NormalizedVector, Vector};