Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSparseMatrix2.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 
24 
25 /**
26  @example ExampleSparseMatrix2.cpp
27  @relates SparseMatrix
28  @brief Example for sparse matrix with umfpack
29 
30 
31  This examples demonstrates how the umfpack library can be used to solve
32  sparse linear systems of equations.
33 
34 
35  @see SparseMatrix.hh and umfpack userguide.pdf for more details on
36  the sparse matrix methods
37 
38  @ingroup g_examples
39  @author sedlazeck
40  @date 06/2009
41  */
42 
43 
44 #include <iostream>
45 #include <Base/Math/SparseMatrix.hh>
46 #include <MathAlgo/SVD.hh>
47 #include <Base/Math/Random.hh>
48 
49 #ifdef BIAS_HAVE_UMFPACK
50 # include <umfpack.h>
51 #endif
52 
53 using namespace std;
54 using namespace BIAS;
55 
56 
57 
58 int main()
59 {
60 
61  cout << "-- Testing umfpack - conversion and usage -- " << endl;
62 
63  cout << "does this get me anywhere " << endl << flush;
64 
65  // matrix example from umfpack userguide
66  BIAS::Matrix<double> A(5, 5, MatrixIdentity);
67  A[0][0] = 2.0;
68  A[0][1] = 3.0;
69  A[1][0] = 3.0;
70  A[1][1] = 0.0;
71  A[1][2] = 4.0;
72  A[1][4] = 6.0;
73  A[2][1] = -1.0;
74  A[2][2] = -3.0;
75  A[2][3] = 2.0;
76  A[3][2] = 1.0;
77  A[3][3] = 0.0;
78  A[4][1] = 4.0;
79  A[4][2] = 2.0;
80  A[4][4] = 1.0;
81  BIAS::SparseMatrix S(A);
82 
83  cout << "sparse matrix S " << S << endl;
84 
85  Vector<double> b(5);
86  b[0] = 8.0;
87  b[1] = 45.0;
88  b[2] = -3.0;
89  b[3] = 3.0;
90  b[4] = 19.0;
91 
92  Vector<double> x(5);
93 
94  S.Solve(b, x);
95 
96  cout << "solved with sparse matrix (should be [1 2 3 4 5]) : " << x << endl << flush;
97 
98  SparseMatrix ST(S.GetColsNum(), S.GetRowsNum());
99  S.Transpose(ST);
100  cout << "ST " << ST << endl;
101 
102  // testing invert method on larger sparse matrix
103  int m = 100;
104  cout << "testing invert for larger matrix " << m << "x" << m << endl << flush;
105  cout << "haaae? "<< endl;
106  Random random;
107  Matrix<double> MLarge(m, m);
108  double randElem;
109  for(int i=0; i < m; i++){
110  for(int j=0; j < m; j++){
111  randElem = random.GetUniformDistributed(0.0, 1.0);
112  if(randElem > 0.9){
113  MLarge[i][j] = random.GetUniformDistributed(-100.0, 100.0);
114  cout << "setting non-zero element " << endl;
115  } else {
116  MLarge[i][j] = 0.0;
117  }
118  }
119  }
120  // in case umfpack does not run, matrix needs to have last row and colum elem set
121  MLarge[m-1][m-1] = random.GetUniformDistributed(-100.0, 100.0);
122 
123  cout << "new matrix is " << MLarge << endl << flush;
124 
125  cout << "building sparse matrix now... " << endl << flush;
126 
127  SparseMatrix SLarge(MLarge);
128  SparseMatrix SLargeInv(SLarge.GetRowsNum(), SLarge.GetColsNum());
129 
130  cout << "maxCols " << SLarge.GetCols() << " actualCols " << SLarge.GetColsNum()
131  << " maxRows " << SLarge.GetRows() << " actual rows " << SLarge.GetRowsNum() << endl << flush;
132 
133  SLarge.Invert(SLargeInv);
134 
135  // reinit SLarge in case umfpack is not used
136  SLarge = SparseMatrix(MLarge);
137 
138  Matrix<double> MLargeInv;
139  SLarge.Invert(MLargeInv);
140 
141 
142  cout << "Done with inverting, checking for id now... " << endl;
143 
144  SparseMatrix IdSparse(SLarge.GetRowsNum(), SLargeInv.GetColsNum());
145  Matrix<double> IdSparseAsDense;
146  SLarge.Multiply(SLargeInv, IdSparse);
147  IdSparse.GetAsDense(IdSparseAsDense);
148 
149  cout << "Done getting sparse id as dense... " << endl;
150 
151  Matrix<double> IdDense;
152  MLarge.Mult(MLargeInv, IdDense);
153 
154 
155  double epsilon = 1e-8;
156  bool res1 = IdSparseAsDense.IsIdentity(epsilon);
157  bool res2 = IdDense.IsIdentity(epsilon);
158  cout << "testing for identity - sparse : " << res1 << endl;
159  cout << "testing for identity - dense : " << res2 << endl;
160 
161  Matrix<double> ID(m, m, MatrixIdentity);
162  cout << "checking norm sparse case " << Matrix<double>(IdSparseAsDense - ID).NormFrobenius() << endl;
163  cout << "checking norm dense case " << Matrix<double>(IdDense - ID).NormFrobenius() << endl;
164 
165 
166  SVD svd(MLarge);
167  Matrix<double> MLargeInvSvd = svd.Invert(MLarge);
168  Matrix<double> idMLarge;
169  MLarge.Mult(MLargeInvSvd, idMLarge);
170  cout << "checking norm in svd case svd res " << Matrix<double>(ID - idMLarge).NormFrobenius() << endl;
171 
172  if(!res1) exit(-1);
173  if(!res2) exit(-1);
174 
175  exit(0);
176 }
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
Implementation of sparse matrix operations.
Definition: SparseMatrix.hh:51
unsigned int GetCols() const
Returns the maximum number of columns (max non-zero entry).
Definition: SparseMatrix.hh:74
void Mult(const Matrix< T > &arg, Matrix< T > &result) const
matrix multiplication, result is not allocated
Definition: Matrix.hh:913
void Multiply(const T &scalar, Matrix< T > &dest) const
multiplication function, storing data destination matrix
Definition: Matrix.hh:779
bool IsIdentity(double eps=0.0) const
Checks if the matrix a an identity.
Definition: Matrix.cpp:669
class for producing random numbers from different distributions
Definition: Random.hh:51
void Transpose(SparseMatrix &result)
Returns transposed matrix in result (as sparse matrix).