-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathqbLinSolve.h
More file actions
165 lines (126 loc) · 5.67 KB
/
qbLinSolve.h
File metadata and controls
165 lines (126 loc) · 5.67 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This file is part of the qbLinAlg linear algebra library.
/*
MIT License
Copyright (c) 2021 Michael Bennett
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef QBLINSOLVE_H
#define QBLINESOLVE_H
/* *************************************************************************************************
qbLinSolve
Function to solve a system of linear equations in the form of y = X*beta, where we
want to solve for beta.
*** INPUTS ***
aMatrix qbMatrix2<T> The matrix of independent variables (X in the above equation).
bVector qbVector<T> The vector of dependent variables (y in the above equation).
resultVec qbVector<T> The vector of unknown parameters (beta in the above equation).
The final solution is returned in this vector.
*** OUTPUTS ***
INT Flag indicating success or failure of the process.
1 Indicates success.
-1 indicates failure due to there being no unique solution (infinite solutions).
-2 indicates failure due to there being no solution.
Uses Gaussian elimination on the augmented matrix, followed by back substitution.
Created as part of the qbLinAlg linear algebra library, which is intended to be primarily for
educational purposes. For more details, see the corresponding videos on the QuantitativeBytes
YouTube channel at:
www.youtube.com/c/QuantitativeBytes
************************************************************************************************* */
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <vector>
#include "qbMatrix.h"
#include "qbVector.h"
// Define error codes.
constexpr int QBLINSOLVE_NOUNIQUESOLUTION = -1;
constexpr int QBLINSOLVE_NOSOLUTIONS = -2;
// The qbLinSolve function.
template <typename T>
int qbLinSolve(const qbMatrix2<T> &aMatrix, const qbVector<T> &bVector, qbVector<T> &resultVec)
{
// Make a copy of the input matrix, aMatrix.
// We will use this to create the augmented matrix, so we have
// to make a copy.
qbMatrix2<T> inputMatrix = aMatrix;
// Compute the rank of the original matrix.
int originalRank = inputMatrix.Rank();
/* Combine inputMatrix and bVector together into a single matrix,
ready for using Gaussian elimination to reduce to
row-echelon form. */
// Extract data from bVector.
int numDims = bVector.GetNumDims();
std::vector<T> bVecData;
for (int i=0; i<numDims; ++i)
bVecData.push_back(bVector.GetElement(i));
// Use this to create a qbMatrix2 object with the same data (nx1).
qbMatrix2<T> bMatrix(numDims, 1, bVecData);
// Combine the two matrices together.
inputMatrix.Join(bMatrix);
/* Use Gaussian elmination to convert to row-echelon form. */
qbMatrix2<T> rowEchelonMatrix = inputMatrix.RowEchelon();
/* Comute the rank of the augmented matrix.
Note that we do this after performing Gaussian elimination to
reduce the matrix to row echelon form so that if this was
successful, there is no need to repeat this operation twice. */
int augmentedRank = rowEchelonMatrix.Rank();
/* *********************************************************************
Test the two ranks to determine the nature of the system we
are dealing with. The conditions are as follows:
n = number of rows.
1) originalRank = augmentedRank = n => A unique solution exists.
2) originalRank = augmentedRank < n => An infinite number of solutions exist.
3) originalRank < augmentedRank => No solutions exist.
********************************************************************* */
if ((originalRank == augmentedRank) && (originalRank < inputMatrix.GetNumRows()))
{
return QBLINSOLVE_NOUNIQUESOLUTION;
}
else if (originalRank < augmentedRank)
{
return QBLINSOLVE_NOSOLUTIONS;
}
else
{
/* Create a qbVector object to store the output. Initially we will
populate this with the data from bVecData, but we are going to modify
the elements as we compute them. */
qbVector<T> output(bVecData);
// Now use back-substitution to compute the result.
int numRows = rowEchelonMatrix.GetNumRows();
int numCols = rowEchelonMatrix.GetNumCols();
int startRow = numRows-1;
// Loop over the rows, in reverse order.
for (int i=startRow; i>=0; --i)
{
// Extract the currentResult for this row.
T currentResult = rowEchelonMatrix.GetElement(i, numCols-1);
// Compute the cumulative sum.
T cumulativeSum = static_cast<T>(0.0);
for (int j=i+1; j<numRows; ++j)
{
cumulativeSum += (rowEchelonMatrix.GetElement(i,j) * output.GetElement(j));
}
// Compute the answer.
T finalAnswer = (currentResult - cumulativeSum) / rowEchelonMatrix.GetElement(i,i);
// And store.
output.SetElement(i, finalAnswer);
}
// Return the output.
resultVec = output;
}
return 1;
}
#endif