Introduction
Operator overloading shines when the operations already have familiar symbols, and matrices are the classic case. This project builds a Matrix class stored in one contiguous vector, implements +, -, * (matrix and scalar) the idiomatic way via compound operators, and adds Gaussian elimination for the determinant, rank and inverse.
Problem statement
let A 2 2 4 7 2 6) and evaluate operations: addition, subtraction, multiplication by a matrix or a number, determinant, inverse, transpose, rank and equality. Results are stored in ans. Shape mismatches, singular matrices and unknown names must produce clear errors.Objectives
- Overload arithmetic operators in terms of compound assignment
- Store a 2D structure in a 1D std::vector
- Implement Gaussian and Gauss-Jordan elimination with partial pivoting
- Use a custom exception type derived from std::invalid_argument
Features
- Named matrices and an
ansregister - Matrix and scalar multiplication
- Determinant, inverse, rank, transpose, equality with tolerance
- Shape-checked operations with readable errors
Concepts demonstrated
- Operator overloading
- Value semantics
- Custom exceptions
- Numerical algorithms
- Floating-point tolerance
System architecture
Matrix keeps rows_, cols_ and a row-major std::vector<double>; operator()(r, c) indexes it. operator+=/-=/*= do the work and the binary operators take their left operand by value and return the modified copy. A shared eliminate helper row-reduces a copy and returns the number of row swaps, which gives the determinant's sign; rank counts pivots; inverse runs Gauss-Jordan on an augmented identity. DimensionError signals shape mismatches; std::domain_error signals a singular matrix.
Folder structure
Complete source code
Makefile
What each file does
- src/matrix.h
- The
Matrixinterface, free operator declarations and theDimensionErrorexception type. - src/matrix.cpp
- Storage, arithmetic, elimination-based determinant/rank, Gauss-Jordan inverse and the
<<printer. - src/main.cpp
- The command language:
let, binary expressions,det,inv,T,rank,show,eq,vars. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
let A 2 2 4 7 2 6 let B 2 2 1 2 3 4 let C 2 3 1 2 3 4 5 6 let S 3 3 1 2 3 4 5 6 7 8 9 let D 2 2 1 2 3 A + B A * B A * C C * A A * 0.5 det A inv A A * ans let I 2 2 1 0 0 1 eq ans I det S rank S inv S T C det C inv X quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - det A = 4·6 − 7·2 = 10, and A × inv A equals the identity (
eq ans Iprints equal). - The 3×3 matrix 1..9 has determinant 0 and rank 2, so
inv Sfails. - 2×3 × 2×2 is rejected because the inner sizes differ.
Common errors
Inverse contains huge numbers or NaN
Use partial pivoting (choose the largest absolute value in the column) and treat values below a small epsilon as zero.
Prints -0.000
Floating-point results can be tiny negatives; snap values close to zero to 0.0 before printing.
Possible improvements
- Solve linear systems Ax = b
- Parse full expressions such as
A * B + C - Support exact fractions using the Fraction Calculator class
Additional challenges
- 1Add matrix powers
A ^ nusing repeated squaring. - 2Implement LU decomposition and reuse it for det and inverse.
- 3Template the class on the element type.
Built and tested it yourself? Mark it to update your dashboard.