Matrix_LUDecomp_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "Matrix_T.hpp"
40 
41 
42 //LU
43  //Testing determinant of LU compared to A
44  //Testing P * (L * U) = A
45 
50 
51 //-------------------------------------------------------------------------------
52 //Helper functions, don't actually run any tests
54 {
55  int zeroCount = Lno.cols() - 1;
56  for (int i = 0; i < Lno.rows(); i++) {
57  for (int j = 0; j < Lno.cols(); j++)
58  //Where a 0 should be in L of LU
59  if (j > Lno.cols() - zeroCount - 1)
60  Lno(i,j) = 0;
61  //Where a 1 should be in L of LU
62  else if (j == Lno.cols() - zeroCount - 1)
63  Lno(i,j) = 1;
64  //Value in L of LU
65  else if(j < Lno.cols() - zeroCount - 1)
66  Lno(i,j) = LUno.LU(i,j);
67  zeroCount -= 1;}
68 }
69 
71 {
72  int zeroCount = 0;
73  for (int i = 0; i< Uno.rows(); i++) {
74  for (int j = 0; j < Uno.cols(); j++)
75  //Value in U of LU
76  if (j >= zeroCount)
77  Uno(i,j) = LUno.LU(i,j);
78  //Where a 0 should be in U of LU
79  else if (j < zeroCount)
80  Uno(i,j) = 0;
81  zeroCount += 1;}
82 }
83 
85 {
86  //Identity Matrix Construction
87  int iCount = 0;
88  for (int i = 0; i < Pno.rows(); i++)
89  for (int j = 0; j < Pno.cols(); j++)
90  if ((j == iCount) && (i == iCount)) {
91  Pno(i,j) = 1;
92  iCount += 1;}
93  else
94  Pno(i,j) = 0;
95 
96  //Make Identity Martix the Permuation Matrix
97  for (int i = 0; i < LUno.Pivot.size(); i++)
98  Pno.swapRows(i, LUno.Pivot[i]);
99 }
100 
102 {
103  gnsstk::Matrix<double> L1Temp(2,2), U1Temp(2,2), L2Temp(3,3), U2Temp(3,3), L3Temp(4,4), U3Temp(4,4);
104  gnsstk::Matrix<double> CompareLUA1Temp(2,2), CompareLUA2Temp(3,3), CompareLUA3Temp(4,4);
105  gnsstk::Matrix<double> P1Temp(2,2), P2Temp(3,3), P3Temp(4,4);
106 
107  L1 = L1Temp; U1 = U1Temp; L2 = L2Temp; U2 = U2Temp; L3 = L3Temp; U3 = U3Temp;
108  CompareLUA1 = CompareLUA1Temp; CompareLUA2 = CompareLUA2Temp; CompareLUA3 = CompareLUA3Temp;
109  P1 = P1Temp; P2 = P2Temp; P3 = P3Temp;
110 
111  //L matrix generation
115 
116  //U matrix generation
120 
121  //P matrix generation
125 
126  CompareLUA1 = P1 * (L1 * U1);
127  CompareLUA2 = P2 * (L2 * U2);
128  CompareLUA3 = P3 * (L3 * U3);
129 }
130 
131 //-------------------------------------------------------------------------------
132 //Now the tests start
133 
135 {
136  gnsstk::TestUtil testFramework("Matrix LU","LU initialize", __FILE__, __LINE__);
137 
138  LUA1(A1); LUA2(A2); LUA3(A3);
139  failMesg = "Able to perform LU decomposition on non-square matrix";
140  try{LUA4(A4); testFramework.assert(false, failMesg, __LINE__);}
141  catch(gnsstk::Exception e) {testFramework.assert(true, failMesg, __LINE__);}\
142 
143  return testFramework.countFails();
144 }
145 
147 {
148  gnsstk::TestUtil testFramework("Matrix LU","LU Determinant", __FILE__, __LINE__);
149 
150  failMesg = "The LU decomposition's determinant is not equivalent to the determinant of the data matrix";
151  testFramework.assert(std::abs(LUA1.det() - gnsstk::det(A1)) < eps, failMesg, __LINE__);
152  testFramework.assert(std::abs(LUA2.det() - gnsstk::det(A2)) < eps, failMesg, __LINE__);
153  testFramework.assert(std::abs(LUA3.det() - gnsstk::det(A3)) < eps, failMesg, __LINE__);
154 
155  return testFramework.countFails();
156 }
157 
159 {
160  gnsstk::TestUtil testFramework("Matrix LU","P * (L * U) = A", __FILE__, __LINE__);
161 
162  std::cout<<U1<<"\n\n"<<A1<<std::endl;
163 
164  //testFramework.assert( P1 * (L1 * U1) == A1)
165  int badCount = 0;
166  for(int i = 0; i < A1.rows(); i++)
167  for(int j = 0; j < A1.cols(); j++)
168  if (A1(i,j) != CompareLUA1(i,j)) {badCount++;}
169  failDescriptionStream << "Check if LU decomposition of A1 returns the right matrix. " << badCount << " of the elements are incorrect.";
171  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
172  badCount = 0; // Reset error counter
173 
174  for(int i = 0; i < A2.rows(); i++)
175  for(int j = 0; j < A2.cols(); j++)
176  if (A2(i,j) != CompareLUA2(i,j)) {badCount++;}
177  failDescriptionStream << "Check if LU decomposition of A2 returns the right matrix. " << badCount << " of the elements are incorrect.";
179  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
180  badCount = 0; // Reset error counter
181 
182  for(int i = 0; i < A3.rows(); i++)
183  for(int j = 0; j < A3.cols(); j++)
184  if (A3(i,j) != CompareLUA3(i,j)) {badCount++;}
185  failDescriptionStream << "Check if LU decomposition of A3 returns the right matrix. " << badCount << " of the elements are incorrect.";
187  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
188  badCount = 0; // Reset error counter
189 
190  return testFramework.countFails();
191 }
192 
194 {
195  gnsstk::TestUtil testFramework("Matrix LU","LU Determinant", __FILE__, __LINE__);
196 
197  //backSub overwrites input vectors
198  LUA1.backSub(B1);
199  LUA2.backSub(B2);
200  LUA3.backSub(B3);
201 
202  gnsstk::Vector<double> A1sol(2), A2sol(3), A3sol(4);
203  gnsstk::Vector<double> CompareA1sol(2), CompareA2sol(3), CompareA3sol(4);
204 
205  A1sol = B1;
206  A2sol = B2;
207  A3sol = B3;
208  double temp1[2]= {-45,19};
209  double temp2[3]= {17./3,-31./3,7./3};
210  double temp3[4]= {-132,-65,15,89};
211  CompareA1sol = temp1;
212  CompareA2sol = temp2;
213  CompareA3sol = temp3;
214 
215  //testFramework.assert(Asol == CompareAsol, failDescriptionString, __LINE__);
216  int badCount = 0;
217  for(int i = 0; i < A1sol.size(); i++)
218  {
219  failMesg = "The solution calculated from back subsitution of LU decomposition is incorrect";
220  if (std::abs(CompareA1sol[i] - A1sol[i]) > eps) {badCount++;} //sizes mismatch, check till v1 ends
221  }
222  testFramework.assert(badCount==0, failMesg, __LINE__);
223  badCount = 0; // Reset error counter
224 
225  for(int i = 0; i < A2sol.size(); i++)
226  {
227  failMesg = "The solution calculated from back subsitution of LU decomposition is incorrect";
228  if (std::abs(CompareA2sol[i] - A2sol[i]) > eps) {badCount++;} //sizes mismatch, check till v1 ends
229  }
230  testFramework.assert(badCount==0, failMesg, __LINE__);
231  badCount = 0; // Reset error counter
232 
233  for(int i = 0; i < A3sol.size(); i++)
234  {
235  failMesg = "The solution calculated from back subsitution of LU decomposition is incorrect";
236  if (std::abs(CompareA3sol[i] - A3sol[i]) > eps) {badCount++; std::cout<<(CompareA3sol[i]-A3sol[i])<<std::endl;} //sizes mismatch, check till v1 ends
237  }
238  testFramework.assert(badCount==0, failMesg, __LINE__);
239  badCount = 0; // Reset error counter
240 
241  return testFramework.countFails();
242 }
243 
244 int main() //Main function to initialize and run all tests above
245 {
246  int check, errorCounter = 0;
247  Matrix_T testClass;
248 
249  check = testClass.LUinitializationTest(); //runs the gnsstk::LUDecomp
250  errorCounter += check;
251 
252  LUDecompInitializer(); //seperates gnsstk::LUDecomps into P L and U matrices
253 
254  check = testClass.LUdeterminantTest();
255  errorCounter += check;
256 
257  check = testClass.LUATest();
258  errorCounter += check;
259 
260  check = testClass.LUbackSubTest();
261  errorCounter += check;
262 
263  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
264 
265  return errorCounter; //Return the total number of errors
266 }
CompareLUA1
gnsstk::Matrix< double > CompareLUA1
Definition: Matrix_LUDecomp_T.cpp:47
Matrix_T::eps
double eps
Definition: Matrix_T.hpp:109
gnsstk::LUDecomp::Pivot
Vector< int > Pivot
The pivot array.
Definition: MatrixFunctors.hpp:559
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
Matrix_T.hpp
LUA1
gnsstk::LUDecomp< double > LUA1
Definition: Matrix_LUDecomp_T.cpp:48
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
L1
gnsstk::Matrix< double > L1
Definition: Matrix_LUDecomp_T.cpp:46
LUA2
gnsstk::LUDecomp< double > LUA2
Definition: Matrix_LUDecomp_T.cpp:48
Matrix_T::LUATest
int LUATest(void)
Definition: Matrix_LUDecomp_T.cpp:158
Matrix_T::failDescriptionStream
std::stringstream failDescriptionStream
Definition: Matrix_T.hpp:143
Matrix_T
Definition: Matrix_T.hpp:49
UMatrixGeneration
void UMatrixGeneration(gnsstk::Matrix< double > &Uno, gnsstk::LUDecomp< double > LUno)
Definition: Matrix_LUDecomp_T.cpp:70
CompareLUA2
gnsstk::Matrix< double > CompareLUA2
Definition: Matrix_LUDecomp_T.cpp:47
gnsstk::RefMatrixBase::swapRows
BaseClass & swapRows(size_t row1, size_t row2)
Definition: MatrixBase.hpp:579
LMatrixGeneration
void LMatrixGeneration(gnsstk::Matrix< double > &Lno, gnsstk::LUDecomp< double > LUno)
Definition: Matrix_LUDecomp_T.cpp:53
Matrix_T::B2
gnsstk::Vector< double > B2
Definition: Matrix_T.hpp:111
gnsstk::Matrix::cols
size_t cols() const
The number of columns in the matrix.
Definition: Matrix.hpp:167
gnsstk::Matrix::rows
size_t rows() const
The number of rows in the matrix.
Definition: Matrix.hpp:165
Matrix_T::LUbackSubTest
int LUbackSubTest(void)
Definition: Matrix_LUDecomp_T.cpp:193
Matrix_T::failDescriptionString
std::string failDescriptionString
Definition: Matrix_T.hpp:144
P1
gnsstk::Matrix< double > P1
Definition: Matrix_LUDecomp_T.cpp:49
Matrix_T::failMesg
std::string failMesg
Definition: Matrix_T.hpp:145
gnsstk::det
T det(const ConstMatrixBase< T, BaseClass > &m)
Definition: MatrixOperators.hpp:345
gnsstk::Exception
Definition: Exception.hpp:151
CompareLUA3
gnsstk::Matrix< double > CompareLUA3
Definition: Matrix_LUDecomp_T.cpp:47
LUA3
gnsstk::LUDecomp< double > LUA3
Definition: Matrix_LUDecomp_T.cpp:48
Matrix_T::B3
gnsstk::Vector< double > B3
Definition: Matrix_T.hpp:111
P2
gnsstk::Matrix< double > P2
Definition: Matrix_LUDecomp_T.cpp:49
gnsstk::LUDecomp
Definition: MatrixFunctors.hpp:434
LUDecompInitializer
void LUDecompInitializer(void)
Definition: Matrix_LUDecomp_T.cpp:101
gnsstk::Matrix< double >
U1
gnsstk::Matrix< double > U1
Definition: Matrix_LUDecomp_T.cpp:46
PermuationMatrixGeneration
void PermuationMatrixGeneration(gnsstk::Matrix< double > &Pno, gnsstk::LUDecomp< double > &LUno)
Definition: Matrix_LUDecomp_T.cpp:84
Matrix_T::LUinitializationTest
int LUinitializationTest(void)
Definition: Matrix_LUDecomp_T.cpp:134
Matrix_T::A3
gnsstk::Matrix< double > A3
Definition: Matrix_T.hpp:110
L3
gnsstk::Matrix< double > L3
Definition: Matrix_LUDecomp_T.cpp:46
gnsstk::LUDecomp::LU
Matrix< T > LU
Definition: MatrixFunctors.hpp:557
LUA4
gnsstk::LUDecomp< double > LUA4
Definition: Matrix_LUDecomp_T.cpp:48
U3
gnsstk::Matrix< double > U3
Definition: Matrix_LUDecomp_T.cpp:46
gnsstk::Vector< double >
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
Matrix_T::LUdeterminantTest
int LUdeterminantTest(void)
Definition: Matrix_LUDecomp_T.cpp:146
Matrix_T::A2
gnsstk::Matrix< double > A2
Definition: Matrix_T.hpp:110
main
int main()
Definition: Matrix_LUDecomp_T.cpp:244
U2
gnsstk::Matrix< double > U2
Definition: Matrix_LUDecomp_T.cpp:46
Matrix_T::B1
gnsstk::Vector< double > B1
Definition: Matrix_T.hpp:111
L2
gnsstk::Matrix< double > L2
Definition: Matrix_LUDecomp_T.cpp:46
gnsstk::TestUtil
Definition: TestUtil.hpp:265
Matrix_T::A1
gnsstk::Matrix< double > A1
Definition: Matrix_T.hpp:110
P3
gnsstk::Matrix< double > P3
Definition: Matrix_LUDecomp_T.cpp:49
Matrix_T::A4
gnsstk::Matrix< double > A4
Definition: Matrix_T.hpp:110


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39