TsMatrixUtils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 // Unit-Test TerraLib
21 #include "TsMatrixUtils.h"
22 
24 
25 #include <cstdlib>
26 
28 
30 {
31 }
32 
34 {
35 }
36 
38 {
39  boost::numeric::ublas::matrix< double > inputMatrix( 3, 3 );
40  inputMatrix( 0 , 0 ) = 1;
41  inputMatrix( 0 , 1 ) = 2;
42  inputMatrix( 0 , 2 ) = 3;
43  inputMatrix( 1 , 0 ) = 4;
44  inputMatrix( 1 , 1 ) = 5;
45  inputMatrix( 1 , 2 ) = 6;
46  inputMatrix( 2 , 0 ) = 7;
47  inputMatrix( 2 , 1 ) = 8;
48  inputMatrix( 2 , 2 ) = 1;
49 
50  boost::numeric::ublas::matrix< double > outputMatrix( 3, 3 );
51  CPPUNIT_ASSERT( te::common::GetInverseMatrix( inputMatrix, outputMatrix ) );
52 
53  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 0 ) + 1.791667 ) < 0.000001 );
54  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 1 ) - 0.916667 ) < 0.000001 );
55  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 2 ) + 0.125000 ) < 0.000001 );
56  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 0 ) - 1.583333 ) < 0.000001 );
57  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 1 ) + 0.833333 ) < 0.000001 );
58  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 2 ) - 0.250000 ) < 0.000001 );
59  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 0 ) + 0.125000 ) < 0.000001 );
60  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 1 ) - 0.250000 ) < 0.000001 );
61  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 2 ) + 0.125000 ) < 0.000001 );
62 }
63 
65 {
66  boost::numeric::ublas::matrix< double > inputMatrix( 3, 3 );
67  inputMatrix( 0 , 0 ) = 1;
68  inputMatrix( 0 , 1 ) = 2;
69  inputMatrix( 0 , 2 ) = 3;
70  inputMatrix( 1 , 0 ) = 4;
71  inputMatrix( 1 , 1 ) = 5;
72  inputMatrix( 1 , 2 ) = 6;
73  inputMatrix( 2 , 0 ) = 7;
74  inputMatrix( 2 , 1 ) = 8;
75  inputMatrix( 2 , 2 ) = 1;
76 
77  boost::numeric::ublas::matrix< double > outputMatrix( 3, 3 );
78  CPPUNIT_ASSERT( te::common::GetPseudoInverseMatrix( inputMatrix, outputMatrix ) );
79 
80  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 0 ) + 1.791667 ) < 0.000001 );
81  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 1 ) - 0.916667 ) < 0.000001 );
82  CPPUNIT_ASSERT( std::abs( outputMatrix( 0 , 2 ) + 0.125000 ) < 0.000001 );
83  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 0 ) - 1.583333 ) < 0.000001 );
84  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 1 ) + 0.833333 ) < 0.000001 );
85  CPPUNIT_ASSERT( std::abs( outputMatrix( 1 , 2 ) - 0.250000 ) < 0.000001 );
86  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 0 ) + 0.125000 ) < 0.000001 );
87  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 1 ) - 0.250000 ) < 0.000001 );
88  CPPUNIT_ASSERT( std::abs( outputMatrix( 2 , 2 ) + 0.125000 ) < 0.000001 );
89 }
90 
92 {
93  {
94  boost::numeric::ublas::matrix< double > inputMatrix( 1, 1 );
95  inputMatrix( 0 , 0 ) = 1;
96  double determinant = 0;
97  CPPUNIT_ASSERT( te::common::GetDeterminant( inputMatrix, determinant ) );
98  CPPUNIT_ASSERT_DOUBLES_EQUAL( determinant, 1.0, 0.0000000001 );
99  }
100 
101  {
102  boost::numeric::ublas::matrix< double > inputMatrix( 2, 2 );
103  inputMatrix( 0 , 0 ) = 5;
104  inputMatrix( 0 , 1 ) = 3;
105  inputMatrix( 1 , 0 ) = 2;
106  inputMatrix( 1 , 1 ) = 4;
107  double determinant = 0;
108  CPPUNIT_ASSERT( te::common::GetDeterminant( inputMatrix, determinant ) );
109  CPPUNIT_ASSERT_DOUBLES_EQUAL( determinant, 14, 0.0000000001 );
110  }
111 
112  {
113  boost::numeric::ublas::matrix< double > inputMatrix( 3, 3 );
114  inputMatrix( 0 , 0 ) = 1;
115  inputMatrix( 0 , 1 ) = 2;
116  inputMatrix( 0 , 2 ) = 3;
117  inputMatrix( 1 , 0 ) = 4;
118  inputMatrix( 1 , 1 ) = 5;
119  inputMatrix( 1 , 2 ) = 6;
120  inputMatrix( 2 , 0 ) = 7;
121  inputMatrix( 2 , 1 ) = 8;
122  inputMatrix( 2 , 2 ) = 10;
123 
124  double determinant = 0;
125  CPPUNIT_ASSERT( te::common::GetDeterminant( inputMatrix, determinant ) );
126  CPPUNIT_ASSERT_DOUBLES_EQUAL( determinant, -3.0, 0.0000000001 );
127  }
128 
129 }
130 
void GetDeterminant()
Test Case: getInverseMatrix.
void getPseudoInverseMatrix()
Test Case: getInverseMatrix.
Matrix manipulation utils.
Test suite for Matrix utils.
CPPUNIT_TEST_SUITE_REGISTRATION(TsMatrixUtils)
bool GetPseudoInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Pseudo matrix inversion.
Definition: MatrixUtils.h:191
bool GetDeterminant(const boost::numeric::ublas::matrix< T > &inputMatrix, double &determinant)
Get the Matrix determinant value.
Definition: MatrixUtils.h:57
void getInverseMatrix()
Test Case: getInverseMatrix.
bool GetInverseMatrix(const boost::numeric::ublas::matrix< T > &inputMatrix, boost::numeric::ublas::matrix< T > &outputMatrix)
Matrix inversion.
Definition: MatrixUtils.h:143
Test suite for Matrix utils.
Definition: TsMatrixUtils.h:37