Matrix_HouseHolder_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.hpp"
40 #include "Vector.hpp"
41 #include "TestUtil.hpp"
42 
43 void hhtest(size_t r, size_t c, double a[], double hh_ref[], gnsstk::TestUtil& testFramework)
44 {
45  gnsstk::Matrix<double> A(r,c), HH_ref(r,c);
46  A = a;
47  HH_ref = hh_ref;
49  HH(A);
50 
51  testFramework.assert(
52  HH.A.rows() == HH.A.cols(),
53  "Householder Transformation is square.",
54  __LINE__);
55 
56  int fail = 0;
57  for (int i = 0; i < HH.A.rows(); i++)
58  {
59  for (int j = 0; j < HH.A.cols(); j++)
60  {
61  if (j == i && HH.A(i,j) == 0)
62  fail++;
63  if (j < i && HH.A(i,j) != 0)
64  fail++;
65  }
66  }
67  testFramework.assert(
68  fail==0,
69  "Householder Transformation is upper triangular",
70  __LINE__);
71 
72  int zeroCount=0, badCount = 0;
73  int iRowNegCount=0, oRowNegCount=0;
74  double eps = 1e-10;
75  for(int i = 0; i < A.rows(); i++)
76  {
77  for(int j = 0; j < A.cols(); j++)
78  {
79  //if values is WRONG, determine if it's negative or just plain wrong
80  if ((std::abs(HH.A(i,j) - HH_ref(i,j)) > eps))
81  {
82  if (std::abs(-1 * HH.A(i,j) - HH_ref(i,j)) < eps)
83  iRowNegCount++;
84  else
85  badCount++;
86  }
87  if (HH.A(i,j) == 0)
88  zeroCount++;
89  }
90  //If all non-zero values in a row are negative, mark row as negative
91  if (iRowNegCount == A.cols() - zeroCount)
92  oRowNegCount++;
93  //Otherwise, all negative values become plain wrong
94  else if (iRowNegCount != 0)
95  badCount += iRowNegCount;
96  zeroCount = 0;
97  iRowNegCount = 0;
98  }
99  testFramework.assert(
100  badCount==0,
101  "Householder Transformation matches expected values",
102  __LINE__);
103 }
104 
105 int main()
106 {
107  int errorCounter = 0;
108  gnsstk::TestUtil testFramework("Matrix Householder", "--", __FILE__, __LINE__);
109 
110  // all of the reference values were computed using WolframAlpha to compute
111  // the R value from a QR factorization
112  // for A8 https://www.wolframalpha.com/input/?i=QR+factorization++{{6,5},{-5,7}}
113 
114  double a5[] = {
115  1,1,0,
116  1,0,1,
117  0,1,1};
118  double ref5[] = {
119  pow(2,.5), pow(2,-.5), pow(2,-.5),
120  0, pow(1.5,.5), pow(6,-.5),
121  0,0,2./pow(3,.5)};
122  testFramework.changeSourceMethod("A5");
123  hhtest(3, 3, a5, ref5, testFramework);
124 
125  double a6[9] = {
126  12,-51,4,
127  6,167,-68,
128  -4,24,41};
129  double ref6[9] = {
130  14, 21, -262./7,
131  0, 175, -1958./35,
132  0, 0, 1481./35};
133  testFramework.changeSourceMethod("A6");
134  hhtest(3, 3, a6, ref6, testFramework);
135 
136  double a7[9] = {
137  1,2,3,
138  -1,0,-3,
139  0,-2,3};
140  double ref7[9] = {
141  -1*pow(2,.5),-1*pow(2,.5),-1*pow(18,.5),
142  0,-1*pow(6,.5),pow(6,.5),
143  0,0,pow(3,.5)};
144  testFramework.changeSourceMethod("A7");
145  hhtest(3, 3, a7, ref7, testFramework);
146 
147  double a8[4] = {
148  6,5,
149  -5,7};
150  double ref8[4] = {
151  pow(61, .5), -5./pow(61, .5),
152  0, 67./pow(61, .5)};
153  testFramework.changeSourceMethod("A8");
154  hhtest(2, 2, a8, ref8, testFramework);
155 
156  double a9[16] = {
157  2,1,0,0,
158  1,2,1,0,
159  0,1,2,1,
160  0,0,1,2};
161  double ref9[16] = {
162  pow(5, .5), 4./pow(5, .5), 1./pow(5, .5), 0,
163  0, pow(14./5, .5), 3*pow(2./35, .5)+pow(10./7, .5), pow(5./14, .5),
164  0, 0, pow(15./7, .5), 2*pow(3./35, .5)+2*pow(7./15, .5),
165  0, 0, 0, pow(5./6, .5)};
166  testFramework.changeSourceMethod("A9");
167  hhtest(4, 4, a9, ref9, testFramework);
168 
169  std::cout << "Total Failures for " << __FILE__ << ": " << testFramework.countFails() << std::endl;
170 
171  return testFramework.countFails();
172 }
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
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
hhtest
void hhtest(size_t r, size_t c, double a[], double hh_ref[], gnsstk::TestUtil &testFramework)
Definition: Matrix_HouseHolder_T.cpp:43
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
TestUtil.hpp
gnsstk::Matrix< double >
gnsstk::Householder::A
Matrix< T > A
The upper triangular transformed matrix.
Definition: MatrixFunctors.hpp:811
gnsstk::Householder
Definition: MatrixFunctors.hpp:745
Matrix.hpp
main
int main()
Definition: Matrix_HouseHolder_T.cpp:105
gnsstk::TestUtil
Definition: TestUtil.hpp:265
Vector.hpp


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