philsupertramp/game-math
Loading...
Searching...
No Matches
gaussJordan.h
Go to the documentation of this file.
1
12#pragma once
13#include "../../Matrix.h"
14#include "../../matrix_utils.h"
15#include "LU.h"
16#include "backwardSub.h"
17#include "forwardSub.h"
18#include <vector>
19
28 size_t i, j, k;
29 size_t n = A.columns();
30 double d;
31 Matrix<double> mat = HorizontalConcat(A, eye(A.rows(), n));
32
33 // Partial pivoting
34 for(i = n - 1; i > 1; i--) {
35 if(mat(i - 1, 1) < mat(i, 1)) {
36 for(j = 0; j < (2 * n); ++j) {
37 d = mat(i, j);
38 mat(i, j) = mat(i - 1, j);
39 mat(i - 1, j) = d;
40 }
41 }
42 }
43
44 // Reducing To Diagonal Matrix
45 for(i = 0; i < n; i++) {
46 for(j = 0; j < n; j++) {
47 if(j != i) {
48 d = mat(j, i) / mat(i, i);
49 for(k = 0; k < n * 2; ++k) { mat(j, k) -= mat(i, k) * d; }
50 }
51 }
52 d = mat(i, i);
53 for(j = 0; j < (2 * n); j++) { mat(i, j) = mat(i, j) / d; }
54 }
55
56 return mat.GetSlice(0, mat.rows() - 1, n, mat.columns() - 1);
57}
58
Definition: Matrix.h:42
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
Matrix GetSlice(size_t rowStart) const
Definition: Matrix.h:609
Matrix< double > gaussJordan(const Matrix< double > &A)
Definition: gaussJordan.h:27
Matrix< T > HorizontalConcat(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:81
Matrix< double > eye(size_t rows, size_t columns=0)