Designing a C++ Matrix Class for Quantitative Computing
Summary
This article outlines the interface and storage choices for a reusable templated matrix class intended for quantitative finance calculations. It compares `std::vector` with `std::valarray` and favors a vector of row vectors for straightforward element access, while noting that encapsulation lets client code remain independent of the storage implementation.
The proposed interface includes matrix addition, subtraction, multiplication and transpose; elementwise scalar operations; matrix-vector multiplication; diagonal extraction; indexed element access; and dimension queries. It distinguishes operations that return a new matrix from compound assignments that update the existing one, and provides const and mutable accessors. Matrix division, determinants and inverses are omitted, with matrix division described as ill-defined and the latter operations deferred partly for performance reasons. The article also explains that template declarations and implementations must both be visible to client code, illustrating this by including the implementation from the header. It specifies a design rather than a complete implementation, so numerical behavior and performance are not evaluated.
Key ideas
- The article favors nested standard vectors as a flexible storage choice for matrix elements.
- The proposed API covers core matrix, scalar and matrix-vector operations alongside element access.
- Compound assignment operators update the current matrix, while ordinary binary operators produce a result matrix.
- Const and mutable element accessors support both read-only and writable use.
- Template implementation code must be visible alongside its declaration when client code instantiates the class.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.