Matrix_InverseTranspose_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 
42 {
43  gnsstk::TestUtil testFramework("Matrix", "Inverse", __FILE__, __LINE__);
44 
45  gnsstk::Matrix<double> A1inv(2,2),A2inv(3,3),A3inv(4,4),A4inv(3,4);
46  gnsstk::Matrix<double> CompareA1inv(2,2),CompareA2inv(3,3),CompareA3inv(4,4),CompareA4inv(3,4);
47  A1inv = gnsstk::inverse(A1);
48  A2inv = gnsstk::inverse(A2);
49  A3inv = gnsstk::inverse(A3);
50  try{A4inv = gnsstk::inverse(A4); testFramework.assert(false, failMesg, __LINE__);}
51  catch(gnsstk::Exception e) {testFramework.assert(true, failMesg, __LINE__);}
52 
53  double temp1[4] = {-7,-5,3,2};
54  double temp2[9] = {7./3,2./3,2./3,-17./3,-1./3,-4./3,2./3,1./3,1./3};
55  double temp3[16] = {18, -35, -28, 1, 9, -18, -14, 1, -2, 4, 3, 0, -12, 24, 19, -1};
56 
57  CompareA1inv = temp1;
58  CompareA2inv = temp2;
59  CompareA3inv = temp3;
60 
61  int badCount = 0;
62 
63  //testFramework.assert(Ainv == CompareAinv, testMesg, __LINE__);
64  for(int i = 0; i < A1inv.rows(); i++)
65  for(int j = 0; j < A1inv.cols(); j++)
66  if (std::abs(A1inv(i,j) - CompareA1inv(i,j)) > eps) {badCount++;}
67  failDescriptionStream << "Check if gnsstk::inverse(A1) returns the right matrix. " << badCount << " of the elements are incorrect.";
69  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
70  badCount = 0; // Reset error counter
71 
72  for(int i = 0; i < A2inv.rows(); i++)
73  for(int j = 0; j < A2inv.cols(); j++)
74  if (std::abs(A2inv(i,j) - CompareA2inv(i,j)) > eps) {badCount++;}
75  failDescriptionStream << "Check if gnsstk::inverse(A2) returns the right matrix. " << badCount << " of the elements are incorrect.";
77  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
78  badCount = 0; // Reset error counter
79 
80  for(int i = 0; i < A3inv.rows(); i++)
81  for(int j = 0; j < A3inv.cols(); j++)
82  if (std::abs(A3inv(i,j) - CompareA3inv(i,j)) > eps) {badCount++;}
83  failDescriptionStream << "Check if gnsstk::inverse(A3) returns the right matrix. " << badCount << " of the elements are incorrect.";
85  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
86  badCount = 0; // Reset error counter
87 
88  return testFramework.countFails();
89 }
90 
92 {
93  gnsstk::TestUtil testFramework("Matrix", "Transpose", __FILE__, __LINE__);
94 
95  gnsstk::Matrix<double> A1T(2,2),A2T(3,3),A3T(4,4),A4T(4,5);
96  gnsstk::Matrix<double> CompareA1T(2,2),CompareA2T(3,3),CompareA3T(4,4),CompareA4T(5,4);
97  A1T = gnsstk::transpose(A1);
98  A2T = gnsstk::transpose(A2);
99  A3T = gnsstk::transpose(A3);
100  A4T = gnsstk::transpose(A4);
101 
102  double temp4[4] = {2,-3,5,-7};
103  double temp5[9] = {1,3,-5,0,1,-1,-2,-2,9};
104  double temp6[16] = {2,1,0,0,3,0,2,2,1,3,-3,3,5,1,2,1};
105  double temp7[20] = {8,7,1,-78,5,-9,7,24,18,5,10,20,-2,0,11,-68,1.5,7,47,0};
106 
107  CompareA1T = temp4;
108  CompareA2T = temp5;
109  CompareA3T = temp6;
110  CompareA4T = temp7;
111 
112  int badCount = 0;
113 
114  //testFramework.assert(AT == CompareAT, testMesg, __LINE__);
115  for(int i = 0; i < A1T.rows(); i++)
116  for(int j = 0; j < A1T.cols(); j++)
117  if (A1T(i,j) != CompareA1T(i,j)) {badCount++;}
118  failDescriptionStream << "Check if gnsstk::transpose(A1) returns the right matrix. " << badCount << " of the elements are incorrect.";
120  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
121  badCount = 0; // Reset error counter
122 
123  for(int i = 0; i < A2T.rows(); i++)
124  for(int j = 0; j < A2T.cols(); j++)
125  if (A2T(i,j) != CompareA2T(i,j)) {badCount++;}
126  failDescriptionStream << "Check if gnsstk::transpose(A2) returns the right matrix. " << badCount << " of the elements are incorrect.";
128  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
129  badCount = 0; // Reset error counter
130 
131  for(int i = 0; i < A3T.rows(); i++)
132  for(int j = 0; j < A3T.cols(); j++)
133  if (A3T(i,j) != CompareA3T(i,j)) {badCount++;}
134  failDescriptionStream << "Check if gnsstk::transpose(A3) returns the right matrix. " << badCount << " of the elements are incorrect.";
136  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
137  badCount = 0; // Reset error counter
138 
139  for(int i = 0; i < A4T.rows(); i++)
140  for(int j = 0; j < A4T.cols(); j++)
141  if (A4T(i,j) != CompareA4T(i,j)) {badCount++;}
142  failDescriptionStream << "Check if gnsstk::transpose(A4) returns the right matrix. " << badCount << " of the elements are incorrect.";
144  testFramework.assert(badCount==0, failDescriptionString, __LINE__);
145  badCount = 0; // Reset error counter
146 
147  return testFramework.countFails();
148  }
149 
150 
152 {
153  gnsstk::TestUtil testFramework("Matrix", "Solution", __FILE__, __LINE__);
154 
155 //Solution via Ainv*B, DEPENDENT ON INVERSE FUNCTION
156 //Is there another way to solve the system? Need to find A4sol still
157  gnsstk::Vector<double> A1sol(2), A2sol(3), A3sol(4);
158 
159  gnsstk::Vector<double> CompareA1sol(2), CompareA2sol(3), CompareA3sol(4);
160 
161  A1sol = gnsstk::inverse(A1) * B1;
162  A2sol = gnsstk::inverse(A2) * B2;
163  A3sol = gnsstk::inverse(A3) * B3;
164  double temp8[2]= {-45,19};
165  double temp9[3]= {17./3,-31./3,7./3};
166  double temp10[4]= {-132,-65,15,89};
167  CompareA1sol = temp8;
168  CompareA2sol = temp9;
169  CompareA3sol = temp10;
170 
171  int badCount = 0;
172 
173  //testFramework.assert(Asol == CompareAsol, failDescriptionString, __LINE__);
174  for(int i = 0; i < A1sol.size(); i++)
175  {
176  failMesg = "The solution calculated from A1inverse * b is incorrect";
177  if (std::abs(CompareA1sol[i] - A1sol[i]) > eps) {badCount++;} //sizes mismatch, check till v1 ends
178  }
179  testFramework.assert(badCount==0, failMesg, __LINE__);
180  badCount = 0; // Reset error counter
181 
182  for(int i = 0; i < A2sol.size(); i++)
183  {
184  failMesg = "The solution calculated from A2inverse * b is incorrect";
185  if (std::abs(CompareA2sol[i] - A2sol[i]) > eps) {badCount++;} //sizes mismatch, check till v1 ends
186  }
187  testFramework.assert(badCount==0, failMesg, __LINE__);
188  badCount = 0; // Reset error counter
189 
190  for(int i = 0; i < A3sol.size(); i++)
191  {
192  failMesg = "The solution calculated from A3inverse * b is incorrect";
193  if (std::abs(CompareA3sol[i] - A3sol[i]) > eps) {badCount++; std::cout<<(CompareA3sol[i]-A3sol[i])<<std::endl;} //sizes mismatch, check till v1 ends
194  }
195  testFramework.assert(badCount==0, failMesg, __LINE__);
196  badCount = 0; // Reset error counter
197 
198  return testFramework.countFails();
199 }
200 
201 
203 {
204  gnsstk::TestUtil testFramework("Matrix", "Determinant", __FILE__, __LINE__);
205 
206  double CompareDetA1 = 1.0;
207  double CompareDetA2 = 3.0;
208  double CompareDetA3 = 1.0;
209 
210  failMesg = "The calculated determinant is incorrect";
211  testFramework.assert(std::abs(gnsstk::det(A1) - CompareDetA1) < eps, failMesg, __LINE__);
212  testFramework.assert(std::abs(gnsstk::det(A2) - CompareDetA2) < eps, failMesg, __LINE__);
213  testFramework.assert(std::abs(gnsstk::det(A3) - CompareDetA3) < eps, failMesg, __LINE__);
214 
215  return testFramework.countFails();
216 }
217 
218 int main() //Main function to initialize and run all tests above
219 {
220 
221  int check, errorCounter = 0;
222  Matrix_T testClass;
223 
224  check = testClass.inverseTest();
225  errorCounter += check;
226 
227  check = testClass.transposeTest();
228  errorCounter += check;
229 
230  check = testClass.solutionTest();
231  errorCounter += check;
232 
233  check = testClass.determinantTest();
234  errorCounter += check;
235 
236  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
237 
238  return errorCounter; //Return the total number of errors
239 }
Matrix_T::eps
double eps
Definition: Matrix_T.hpp:109
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
Matrix_T.hpp
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
Matrix_T::failDescriptionStream
std::stringstream failDescriptionStream
Definition: Matrix_T.hpp:143
Matrix_T
Definition: Matrix_T.hpp:49
Matrix_T::inverseTest
int inverseTest(void)
Definition: Matrix_InverseTranspose_T.cpp:41
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::failDescriptionString
std::string failDescriptionString
Definition: Matrix_T.hpp:144
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
main
int main()
Definition: Matrix_InverseTranspose_T.cpp:218
Matrix_T::transposeTest
int transposeTest(void)
Definition: Matrix_InverseTranspose_T.cpp:91
Matrix_T::solutionTest
int solutionTest(void)
Definition: Matrix_InverseTranspose_T.cpp:151
Matrix_T::B3
gnsstk::Vector< double > B3
Definition: Matrix_T.hpp:111
gnsstk::transpose
SparseMatrix< T > transpose(const SparseMatrix< T > &M)
transpose
Definition: SparseMatrix.hpp:829
gnsstk::Matrix< double >
Matrix_T::A3
gnsstk::Matrix< double > A3
Definition: Matrix_T.hpp:110
gnsstk::Vector< double >
gnsstk::Vector::size
size_t size() const
STL size.
Definition: Vector.hpp:207
Matrix_T::determinantTest
int determinantTest(void)
Definition: Matrix_InverseTranspose_T.cpp:202
Matrix_T::A2
gnsstk::Matrix< double > A2
Definition: Matrix_T.hpp:110
Matrix_T::B1
gnsstk::Vector< double > B1
Definition: Matrix_T.hpp:111
gnsstk::inverse
SparseMatrix< T > inverse(const SparseMatrix< T > &A)
Definition: SparseMatrix.hpp:1890
gnsstk::TestUtil
Definition: TestUtil.hpp:265
Matrix_T::A1
gnsstk::Matrix< double > A1
Definition: Matrix_T.hpp:110
Matrix_T::A4
gnsstk::Matrix< double > A4
Definition: Matrix_T.hpp:110


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