Matrix_SVD_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 <iostream>
40 
41 #include "Matrix.hpp"
42 #include "Vector.hpp"
43 #include "TestUtil.hpp"
44 
45 using namespace std;
46 
47 template<typename T>
48 void SVDTest(size_t r, size_t c,
49  T xA[], T xB[], T xBSref[],
50  gnsstk::TestUtil& testFramework)
51 {
52  ostringstream oss;
53  oss << r << "x" << c;
54  testFramework.changeSourceMethod(oss.str());
55  T eps=100*std::numeric_limits<T>::epsilon();
56  gnsstk::Matrix<T> A(r,c);
57  A = xA;
58  gnsstk::SVD<T> svd;
59  svd(A);
60  gnsstk::Matrix<T> S(r, c, 0.0);
61  for (int i=0; i < min(r,c); i++)
62  S(i,i) = svd.S(i);
63  TUASSERTFEPS( A, svd.U * S * transpose(svd.V), eps);
64 
65  TUASSERTFEPS( gnsstk::ident<T>(r), svd.U * transpose(svd.U), eps);
66  TUASSERTFEPS( gnsstk::ident<T>(c), svd.V * transpose(svd.V), eps);
67 
68  if (r == c)
69  {
70  gnsstk::Vector<T> B(r), BSref(r);
71  B = xB;
72  BSref = xBSref;
73  svd.backSub(B);
74  TUASSERTFEPS( B, BSref, eps);
75  }
76 }
77 
78 
79 template<typename T>
80 unsigned multipass()
81 {
82  gnsstk::TestUtil testFramework("Matrix SVD<"+gnsstk::typeString<T>()+">", "--", __FILE__, __LINE__);
83  T a22[] = {2,1,1,2};
84  T b2[] = {1,2};
85  T bs2[] = {0,1};
86  SVDTest(2, 2, a22, b2, bs2, testFramework);
87 
88  T a23[] = {4, 11, 14, 8, 7, -2};
89  SVDTest<T>(3, 2, a23, NULL, NULL, testFramework);
90  SVDTest<T>(2, 3, a23, NULL, NULL, testFramework);
91 
92  T a33[] = {2,-1,0,-1,2,-1,0,-1,2};
93  T b3[] = {7,-3,2};
94  T bs3[] = {4.25,1.5,1.75};
95  SVDTest<T>(3, 3, a33, b3, bs3, testFramework);
96 
97  T a44[] = {2,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,-1,2};
98  T b4[] = {5,1,-2,6};
99  T bs4[] ={5,5,4,5};
100  SVDTest<T>(4, 4, a44, b4, bs4, testFramework);
101  SVDTest<T>(2, 8, a44, NULL, NULL, testFramework);
102  SVDTest<T>(8, 2, a44, NULL, NULL, testFramework);
103  return testFramework.countFails();
104 }
105 
106 
107 int main()
108 {
109  unsigned ec=0;
110 
111  ec += multipass<float>();
112  ec += multipass<double>();
113  ec += multipass<long double>();
114 
115  std::cout << "Total Failures for " << __FILE__ << ": " << ec << std::endl;
116 
117  return ec;
118 }
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
gnsstk::SVD::U
Matrix< T > U
Matrix U.
Definition: MatrixFunctors.hpp:410
main
int main()
Definition: Matrix_SVD_T.cpp:107
NULL
#define NULL
Definition: getopt1.c:64
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
TestUtil.hpp
gnsstk::transpose
SparseMatrix< T > transpose(const SparseMatrix< T > &M)
transpose
Definition: SparseMatrix.hpp:829
gnsstk::Matrix
Definition: Matrix.hpp:72
multipass
unsigned multipass()
Definition: Matrix_SVD_T.cpp:80
gnsstk::min
T min(const SparseMatrix< T > &SM)
Maximum element - return 0 if empty.
Definition: SparseMatrix.hpp:858
TUASSERTFEPS
#define TUASSERTFEPS(EXP, GOT, EPS)
Definition: TestUtil.hpp:126
gnsstk::SVD::backSub
void backSub(RefVectorBase< T, BaseClass > &b) const
Definition: MatrixFunctors.hpp:355
gnsstk::Vector
Definition: Vector.hpp:67
std
Definition: Angle.hpp:142
gnsstk::SVD::S
Vector< T > S
Vector of singular values.
Definition: MatrixFunctors.hpp:412
gnsstk::SVD::V
Matrix< T > V
Matrix V (not transpose(V))
Definition: MatrixFunctors.hpp:414
Matrix.hpp
SVDTest
void SVDTest(size_t r, size_t c, T xA[], T xB[], T xBSref[], gnsstk::TestUtil &testFramework)
Definition: Matrix_SVD_T.cpp:48
gnsstk::TestUtil
Definition: TestUtil.hpp:265
gnsstk::SVD
Definition: MatrixFunctors.hpp:99
Vector.hpp


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