Skip to main content

trueno/matrix/
mod.rs

1//! Matrix operations for Trueno
2//!
3//! Provides 2D matrix operations with SIMD optimization for linear algebra,
4//! machine learning, and scientific computing.
5//!
6//! ## Module Structure (PMAT-018)
7//!
8//! Operations are organized into domain-specific submodules:
9//! - **ops/storage**: Constructors and element access
10//! - **ops/arithmetic**: Matrix multiplication
11//! - **ops/linear**: Transpose, matvec, vecmat
12//! - **ops/ml_ops**: Convolution, embedding, pooling
13//!
14//! # Example
15//!
16//! ```
17//! use trueno::Matrix;
18//!
19//! // Create a 2x3 matrix
20//! let m = Matrix::zeros(2, 3);
21//! assert_eq!(m.rows(), 2);
22//! assert_eq!(m.cols(), 3);
23//! ```
24
25use crate::Backend;
26
27mod ops;
28
29#[cfg(test)]
30mod tests;
31
32/// A 2D matrix with row-major storage
33///
34/// Data is stored in row-major format (C-style), where consecutive elements
35/// in memory belong to the same row. This is compatible with NumPy's default
36/// layout and optimal for cache locality when accessing rows.
37///
38/// # Storage Layout
39///
40/// For a 2x3 matrix:
41/// ```text
42/// [[a, b, c],
43///  [d, e, f]]
44/// ```
45/// Data is stored as: [a, b, c, d, e, f]
46///
47/// # Example
48///
49/// ```
50/// use trueno::Matrix;
51///
52/// let m = Matrix::from_vec(2, 2, vec![1.0, 2.0, 3.0, 4.0]).unwrap();
53/// assert_eq!(m.get(0, 0), Some(&1.0));
54/// assert_eq!(m.get(0, 1), Some(&2.0));
55/// assert_eq!(m.get(1, 0), Some(&3.0));
56/// assert_eq!(m.get(1, 1), Some(&4.0));
57/// ```
58#[derive(Debug, Clone, PartialEq)]
59pub struct Matrix<T> {
60    pub(crate) rows: usize,
61    pub(crate) cols: usize,
62    pub(crate) data: Vec<T>,
63    pub(crate) backend: Backend,
64}