Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestGaussJordan.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 
23 /** @file TestGaussJordan.cpp
24  * @ingroup g_tests
25  @brief Test GaussJordanAlgorithm computing the reduced row echelon form
26  @relates Matrix
27  @author woelk 05/2008 (c) www. vision-n.de */
28 
29 #include <Base/Math/Matrix.hh>
30 #include <Base/Math/Random.hh>
31 #include <Base/Common/CompareFloatingPoint.hh>
32 
33 using namespace BIAS;
34 using namespace std;
35 
36 Random myrand;
37 
38 void GenerateRandomMatrix(Matrix<double>& A);
39 
40 bool TestForReducedRowEchelonForm(Matrix<double>& A);
41 
42 
43 int main(int argc, char *argv[])
44 {
45  const bool verbose = false;
46  // check num_sizes different matric sizes
47  const int num_sizes = 100;
48  for (int t=0; t<num_sizes; t++){
49  const int numr=myrand.GetUniformDistributedInt(1,10),
50  numc=myrand.GetUniformDistributedInt(1,10);
51  Matrix<double> A(numr, numc), Aorig;
52 
53 
54  // check differently filled matrices
55  const int num_tries = 1000;
56  for (int i=0; i<num_tries; i++){
57  GenerateRandomMatrix(A);
58  Aorig = A;
59  //if (verbose) cout << "------------------------------------\nA: "<<A;
60  A.GaussJordan();
61  if (verbose) cout <<"\nrref(A): "<<A;
62  if (!TestForReducedRowEchelonForm(A)){
63  if (!verbose)
64  BIASERR("error: matrix is not in reduced row echelon form: "<<A);
65  //if (verbose)
66  cout << "\nA: "<<Aorig;
67  //if (verbose)
68  cout << " !!FAILED!!\n";
69  return -1;
70  }
71  if (verbose) cout << "passed!\n";
72  }
73  }
74 
75  return 0;
76 }
77 
78 void GenerateRandomMatrix(Matrix<double>& A)
79 {
80  static int num_calls = 0;
81  const double range=1.;
82  const int numr=A.num_rows(), numc=A.num_cols();
83  for (int r=0; r<numr; r++){
84  for (int c=0; c<numc; c++){
85  A[r][c] = myrand.GetUniformDistributed(-range, range);
86  // insert some zeroes in every second generation
87  //if (abs(A[r][c])<0.5) A[r][c] = 0.;
88  if ((num_calls%2)==0 && abs(A[r][c])<0.5) A[r][c] = 0.;
89  }
90  }
91  num_calls++;
92 }
93 
94 bool TestForReducedRowEchelonForm(Matrix<double>& A)
95 {
96  const int numr=A.num_rows(), numc=A.num_cols();
97  int lead=-1, r, c;
98  for (r=0; r<numr; r++){
99  // look for the first non-zero element in the row
100  for (c=0; c<numc && Equal(A[r][c], 0.0); c++){ }
101  // check if it is right of the leading elemnt from the previous row
102  if (c<=lead && lead<numc){ cout << "lead pos\n"; return false; }
103  // remember the leading element
104  lead = c;
105  if (lead==numc) continue;
106  // check if the leading element is one
107  if (!Equal(A[r][lead], 1.0)) { cout << "lead == 1.\n"; return false; }
108  // check if the leading element id the only nonzero element in its column
109  for (int rr=0; rr<numr; rr++){
110  if (rr==r) continue;
111  if (!Equal(A[rr][lead], 0.0)) { cout << "top == 0.\n"; return false; }
112  }
113  }
114  return true;
115 }
Subscript num_cols() const
Definition: cmat.h:320
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
void GaussJordan()
use the Gauss Jordan Algrithm to transform the matrix to reduced row echelon form.
Definition: Matrix.cpp:740
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
Subscript num_rows() const
Definition: cmat.h:319
class for producing random numbers from different distributions
Definition: Random.hh:51