Skip to content

Refactor: Introduce MatrixRef / SquareMatrix abstraction for flat-Vec 2D indexing #244

@ocdbytes

Description

@ocdbytes

Multiple files use flat Vec with manual i * n + j indexing for 2D matrix access. This is error-prone (off-by-one on row/col, wrong stride) and obscures intent. A thin shared abstraction would make the 2D access self-documenting.

MatrixMut already exists in src/algebra/ntt/matrix.rs with matrix[(row, col)] indexing, used in 26+ places in ntt/transpose.rs. However, it is mutable and NTT-specific (strided). The sites below are mostly read-only row slicing and diagonal access, so a lighter-weight immutable MatrixRef (or a SquareMatrix newtype for the square case) would be more appropriate.

Example suggested API

  /// Immutable view over a flat slice interpreted as a row-major matrix.
  pub struct MatrixRef<'a, T> {
      data: &'a [T],
      cols: usize,
  }

  impl<'a, T> MatrixRef<'a, T> {
      pub fn new(data: &'a [T], cols: usize) -> Self { ... }
      pub fn row(&self, i: usize) -> &[T] { ... }
      pub fn get(&self, row: usize, col: usize) -> &T { ... }
      pub fn diagonal(&self, i: usize) -> &T { ... } // square matrices
      pub fn rows(&self) -> usize { ... }
      pub fn cols(&self) -> usize { ... }
  }

  impl<T> Index<(usize, usize)> for MatrixRef<'_, T> { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions