This repository provides C++17 implementations for matrix and vector calculations, covering both exact rational arithmetic and modular arithmetic over Galois Fields. No external libraries or extensions are required; all code runs with basic C++17.
- Exact value calculations using a custom
Ratiotype:- Structure: sign bit, unsigned long long denominator and numerator
- Operators:
<, <=, >, >=, ==, !=, +, -, *, / - Utility functions:
normalize(),Ratio_print(),Ratio_power()
- Limitations: Square roots and non-integer exponents (e.g.,
2^(1/2),3^(3.4)) are not supported. For these, uselong double.
- Operators:
==, !=, *, +, - - Functions:
matrix_transpose()RatioMat_to_LDMat()– converts Ratio matrix to long double matrixmatrix_power()matrix_print()matrix_inverse()matrix_determinant()QR_decomposition()– Q columns are orthogonal (not normalized if using Ratio)LU_decomposition()Eigen_Approx()– only for symmetric, non-singular matrices (long double)Null_Space()Ax=b()– finds particular solution if multiple existmatrix_full_row_rank()– reduces m×n matrix to r×n (or r×r if repeated with transpose)change_of_basis_P()– computes change-of-basis matrix between two bases
- Supports vector multiplication and matrix-vector multiplication.
- Modular value is user-configurable and must be a prime.
- Type:
long long(modular arithmetic) - Functions:
- Modular routines:
inverse(),power(),gcd(),decompose(),divisor(),Extended_Euclid() - Matrix operators:
==, !=, *, +, -, |(diagonal expansion) matrix_transpose()matrix_power()matrix_rank()matrix_inverse()matrix_determinant()Null_Space()Ax=b()– finds particular solution if more than one existsis_in()– checks if vector is in column space of matrix- Eigenvalue decomposition:
matrix_diagonalize_BF()– brute force all values 1 to p-1matrix_diagonalize_henry()– fastest i found (see 'O(n^3) Diagonalization over Fp via the A^p=A criterion.pdf')
- Modular routines:
- Modified version of
Linear_Algebra_finitefor parallel computing. - Uses
omp.hand threading to accelerate computations for large matrices (N > 200). - Advanced workload distribution: e.g., in
matrix_diagonalize_henry. - Warning: Parallelism may cause race conditions or scheduler-related errors. For 100% accuracy, use the serial (
Linear_Algebra_finite.cpp) version.
- All functionality is accessed via the
main()function in each file. - Compilation does not require any non-standard headers (except
omp.hfor parallel).
Example compilation:
g++ -std=c++17 Linear_Algebra_Matrix.cpp -o linear_algebra_matrix
g++ -std=c++17 Linear_Algebra_finite.cpp -o linear_algebra_finite
g++ -std=c++17 -fopenmp Linear_Algebra_finite_parallel.cpp -o linear_algebra_finite_parallel
- Use the
Ratiotype for exact arithmetic when integer/fraction results are required. - For operations requiring square roots, non-integer powers, or normalization, use
long double. - Use the Galois field module for all modular arithmetic, ensuring your modulus is prime.
- Employ the parallel version for large matrices, but prefer the serial implementation for smaller sizes or if strict correctness is essential.
| Module | Numeric Type | Key Features |
|---|---|---|
| Linear_Algebra_Matrix | Ratio / long double | Exact matrix, vector ops; QR/LU/Eigen |
| Linear_Algebra_finite | long long (mod prime) | Modular matrix ops; null space; eigenvalues |
| Linear_Algebra_finite_parallel | long long (mod prime) | Parallel matrix ops for large N |
- Every function is documented with comments.
- For deeper mathematical and algorithmic explanations, see the inline documentation and (where referenced) supplementary documents in the repository.
Good luck with your computations and mathematical research!