source: trunk/test/svd_test.cc @ 680

Last change on this file since 680 was 680, checked in by Jari Häkkinen, 17 years ago

Addresses #153. Introduced yat namespace. Removed alignment namespace. Clean up of code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1// $Id: svd_test.cc 680 2006-10-11 17:49:03Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/random/random.h"
25#include "yat/utility/matrix.h"
26#include "yat/utility/SVD.h"
27#include "yat/utility/vector.h"
28
29using namespace theplu::yat;
30
31double this_norm(const utility::matrix& A)
32{
33  double sum=0.0;
34  for (size_t i=0; i<A.rows(); ++i)
35    for (size_t j=0; j<A.columns(); ++j)
36      sum += A(i,j)*A(i,j);
37  return sum;
38}
39
40
41
42bool test(size_t m, size_t n, utility::SVD::SVDalgorithm algo)
43{
44  // accepted error, Jari: should be picked up from GSL
45  double MAXTOL=1e-13;
46
47  // initialise a random test-matrix
48  theplu::yat::random::ContinuousUniform rnd;
49  utility::matrix A(m,n);
50  for (size_t i=0; i<m; ++i)
51    for(size_t j=0; j<n; ++j)
52      A(i,j)=1000*rnd();
53
54  utility::SVD svd(A);
55  svd.decompose(algo);
56  theplu::yat::utility::vector s(svd.s());
57  utility::matrix S(s.size(),s.size());
58  for (size_t i=0; i<s.size(); ++i)
59    S(i,i)=s[i];
60  utility::matrix Vtranspose=svd.V();
61  Vtranspose.transpose();
62  // Reconstructing A = U*S*Vtranspose
63  utility::matrix Areconstruct=svd.U();
64  Areconstruct*=S;
65  Areconstruct*=Vtranspose;
66  Areconstruct-=A;  // Expect null matrix
67  double error = this_norm(Areconstruct);
68  bool testerror=false;
69  if (error>MAXTOL) {
70    std::cerr << "test_svd: FAILED, algorithm " << algo
71              << " recontruction error ("
72              << error << ") > tolerance (" << MAXTOL << "), matrix dimension ("
73              << m << ',' << n << ')' << std::endl;
74    testerror=true;
75  }
76
77  Vtranspose*=svd.V();  // Expect unity matrix
78  error=this_norm(Vtranspose)-n;
79  if (error>MAXTOL) {
80    std::cerr << "test_svd: FAILED, algorithm " << algo
81              << " V orthogonality error ("
82              << error << ") > tolerance (" << MAXTOL << ')' << std::endl;
83    testerror=true;
84  }
85
86  utility::matrix Utranspose=svd.U();
87  Utranspose.transpose();
88  Utranspose*=svd.U();  // Expect unity matrix
89  error=this_norm(Utranspose)-n;
90  if (error>MAXTOL) {
91    std::cerr << "test_svd: FAILED, algorithm " << algo
92              << " U orthogonality error ("
93              << error << ") > tolerance (" << MAXTOL << ')' << std::endl;
94    testerror=true;
95  }
96  return testerror;
97}
98
99
100
101int main(const int argc,const char* argv[])
102{
103  bool testfail=false;
104
105  // The GSL Jacobi, Golub-Reinsch, and modified Golub-Reinsch
106  // implementations supports rows>=columns matrix dimensions only
107  testfail|=test(12,12,utility::SVD::GolubReinsch);
108  testfail|=test(12,4,utility::SVD::GolubReinsch);
109  testfail|=test(12,12,utility::SVD::ModifiedGolubReinsch);
110  testfail|=test(12,4,utility::SVD::ModifiedGolubReinsch);
111  testfail|=test(12,12,utility::SVD::Jacobi);
112  testfail|=test(12,4,utility::SVD::Jacobi);
113
114  return (testfail ? -1 : 0);
115}
Note: See TracBrowser for help on using the repository browser.