Skip to content

BLA&DSP Subprogram Specification #38

@Mitchelldscott

Description

@Mitchelldscott

This issue tracks the foundational task of defining the generic traits that will form the core API of our library. The goal is to create a set of abstractions that align with standard BLAS (Levels 1, 2, 3) and common DSP interfaces. These traits will allow downstream algorithms to be written generically, independent of the underlying data storage (e.g., static no_std arrays vs. dynamic ndarray-backed matrices).

This is the first critical step outlined in Towards a Zero-Cost, Trait-Based Math Abstraction Layer for Control and Signal Processing in Rust 🚀


Tasks & Acceptance Criteria

A pull request that resolves this issue should include the following defined traits in a new module (e.g., src/traits.rs):

  • 1. Generic Storage Traits:

    • Define base traits to abstract over vector and matrix-like containers. This is essential for writing generic operations.
    /// A trait for types that can be treated as a vector.
    pub trait Vector<T> {
        fn len(&self) -> usize;
        // Maybe an iterator or `as_slice` method?
    }
    
    /// A trait for types that can be treated as a matrix.
    pub trait Matrix<T> {
        fn shape(&self) -> (usize, usize); // (rows, cols)
        // Methods for row/col access?
    }
  • 2. BLAS Level 1 (Vector-Vector) Traits:

    • trait Axpy<T, V: Vector<T>>: For scaled vector addition, $y \leftarrow \alpha x + y$.
    • trait Dot<V: Vector<T>>: For the dot product, $s \leftarrow x^T y$.
    • trait Nrm2<T>: For the Euclidean ($L_2$) norm, $|x|_2$.
    • trait Asum<T>: For the sum of absolute values ($L_1$ norm), $|x|_1$.
    • trait Scal<T>: For in-place vector scaling, $x \leftarrow \alpha x$.
  • 3. BLAS Level 2 (Matrix-Vector) Traits:

    • trait Gemv<T, V: Vector<T>>: For general matrix-vector multiplication, $y \leftarrow \alpha A x + \beta y$.
  • 4. BLAS Level 3 (Matrix-Matrix) Traits:

    • trait Gemm<T, M: Matrix<T>>: For general matrix-matrix multiplication, $C \leftarrow \alpha A B + \beta C$.
  • 5. Core DSP Traits:

    • trait Conv<K: Vector<T>>: For 1D convolution.
    • trait Fft: For the Fast Fourier Transform.

Design Considerations & Open Questions

  • Naming Convention: Should we stick to the traditional, terse BLAS names (gemm, axpy) for recognizability, or adopt more verbose, Rust-style names (general_matrix_multiply)?
    • Proposal: Stick to BLAS names. They are a well-understood standard in the domain.
  • Scalar Generics: The traits should be generic over the scalar type T (e.g., f32, f64). What bounds should we use? T: num_traits::Float seems like a reasonable starting point.
  • Mutability and Ownership: How should inputs and outputs be handled? Many BLAS operations are mutable. The method signatures should reflect this clearly. For example, axpy modifies its y vector, so it should probably be a mutable receiver or argument.
    // Example signature for AXPY
    fn axpy(&self, alpha: T, x: &impl Vector<T>, y: &mut impl Vector<T>);
  • Error Handling: How should dimension mismatches be handled?
    • Proposal: For performance-critical code, mismatched dimensions are a logic error. We should panic in debug builds and potentially have unchecked versions for release builds. This avoids the overhead of returning a Result. Let's discuss this.
  • Documentation: Each trait and method must include clear rustdoc comments explaining the operation it represents, including the mathematical formula in LaTeX.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNew feature or requestResearchThis issues requires more than code developmentapi-designProposals for the crate architecture

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions