Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleLapack.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 /** @example ExampleLapack.cpp
26  @relates SVD, Lapack
27  @ingroup g_examples
28  @brief example for Lapack MahalanobisDistance usage
29  @author koeser 11/2007
30 */
31 
32 #include <MathAlgo/SVD.hh>
33 #include <Base/Math/Random.hh>
34 #include <Base/Debug/TimeMeasure.hh>
35 #include <MathAlgo/Lapack.hh>
36 
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 
42 int main()
43 {
44  Random rand;
45  unsigned int matdim = 10;
46  Matrix<double> Cov(matdim,matdim,MatrixIdentity),
47  factor(matdim,matdim,MatrixIdentity);
48  BIAS::Vector<double> dif(matdim);
49  for (unsigned int i=0; i<matdim; i++) {
50  for (unsigned int j=0; j<matdim; j++) {
51  factor[i][j] = rand.GetUniformDistributed(0.01,10);
52  }
53  dif[i] = rand.GetUniformDistributed(-10,10);
54  factor[i][i] += rand.GetUniformDistributed(10, 50);
55  }
56  Cov = factor.Transpose()*factor;
57  Cov.MakeSymmetric();
58  cout<<"Cov is "<< Cov<<endl;
59 
60  SVD mytestsvd;
61 
62 
63  double svddist=0,lapackdist=0;
64  TimeMeasure tsvd, tsvd3;
65  for (unsigned int i=0; i<1000; i++) {
66  tsvd.Start();
67  svddist = BIAS::Vector<double>(mytestsvd.Invert(Cov)*dif).ScalarProduct(dif);
68  tsvd.Stop();
69  tsvd3.Start();
70  lapackdist = MahalanobisDistance(Cov,dif);
71  tsvd3.Stop();
72  }
73 
74  cerr << "svd: ";
75  tsvd.Print();
76  cerr << "user: "<<(double)tsvd.GetUserTime() << " ms";
77  cerr << "real: "<<(double)tsvd.GetRealTime() << " us";
78  cerr << "lapack: ";
79  tsvd3.Print();
80  cerr << "user: "<<(double)tsvd3.GetUserTime() << " ms";
81  cerr << "real: "<<(double)tsvd3.GetRealTime() << " us"<<endl;
82 
83  cout<<"distances are "<<svddist<<" "<<lapackdist <<endl;
84  return 0;
85 
86 
87 }
88 
89 
void Print(std::ostream &os=std::cout) const
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
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
double MahalanobisDistance(const BIAS::Matrix< double > &Sigma, const BIAS::Vector< double > &V)
computes squared Mahalanobis distance V^T Sigma^-1 V efficiently using cholesky decomposition and exp...
Definition: Lapack.cpp:780
double GetUserTime() const
return user time (=system usage time) in msec JW For Win32: user-time is the sum over all processes o...
class for producing random numbers from different distributions
Definition: Random.hh:51
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111