-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinSolve.h
63 lines (44 loc) · 1.58 KB
/
LinSolve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef LINSOLVE_H
#define LINSOLVE_H
#include <stdexcept>
#include <vector>
#include <math.h>
#include <iostream>
#include "Matrix2.h"
#include "Vector.h"
constexpr int LINSOLVE_NO_UNIQUE_SOLUTION = -1;
constexpr int LINSOLVE_NO_SOLUTION = -2;
template <typename T>
int LinSolve(const Matrix2<T> &aMatrix, const abVector<T> &bVector, abVector<T> &output) {
Matrix2<T> inputMatrix = aMatrix;
int numDims = bVector.GetNumDims();
std::vector<T> bData;
for(int i = 0; i < numDims; i++) {
bData.push_back(bVector.GetElement(i));
}
Matrix2<T> rowEchelonBeforeJoin = inputMatrix.RowEchelon();
int rankBefore = rowEchelonBeforeJoin.Rank();
Matrix2<T> bMatrix(numDims, 1, bData);
inputMatrix.JoinMatrix(bMatrix);
Matrix2<T> rowEchelon = inputMatrix.RowEchelon();
int rankAfter = rowEchelon.Rank();
if(rankBefore == rankAfter && rankBefore < inputMatrix.GetNumRows()) {
return LINSOLVE_NO_UNIQUE_SOLUTION;
} else if(rankBefore < rankAfter) {
return LINSOLVE_NO_SOLUTION;
} else {
int nRows = rowEchelon.GetNumRows();
int nCols = rowEchelon.GetNumCols();
for(int i = nRows - 1; i >= 0; i--) {
T currRes = rowEchelon.GetElement(i, nCols - 1);
T cumSum = static_cast<T>(0.0);
for(int j = i + 1; j < nRows; j++) {
cumSum += rowEchelon.GetElement(i, j) * output.GetElement(j);
}
T finalAns = (currRes - cumSum) / rowEchelon.GetElement(i, i);
output.SetElement(i, finalAns);
}
return 1;
}
}
#endif