source: trunk/yat/utility/SVD.cc @ 715

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

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1// $Id: SVD.cc 715 2006-12-22 08:42:39Z 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 "SVD.h"
25
26namespace theplu {
27namespace yat {
28namespace utility {
29
30
31  SVD::SVD(const utility::matrix& Ain)
32    : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns())
33  {
34  }
35
36
37  SVD::~SVD(void)
38  {
39  }
40
41
42  int SVD::decompose(SVDalgorithm algo)
43  {
44    switch (algo) {
45        case GolubReinsch:
46          return golub_reinsch();
47        case ModifiedGolubReinsch:
48          return modified_golub_reinsch();
49        case Jacobi:
50          return jacobi();
51    }
52    // Jari, the program should never end up here, return values should be
53    // something different from normal GSL return values, or maybe
54    // throw an exception.
55    return 0;
56  }
57
58
59  int SVD::golub_reinsch(void)
60  {
61    utility::vector w(U_.columns());
62    return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
63                                s_.gsl_vector_p(), w.gsl_vector_p());
64  }
65
66
67  int SVD::jacobi(void)
68  {
69    return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
70                                       s_.gsl_vector_p());
71  }
72
73
74  int SVD::modified_golub_reinsch(void)
75  {
76    utility::vector w(U_.columns());
77    utility::matrix X(U_.columns(),U_.columns());
78    return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(), 
79                                    V_.gsl_matrix_p(), s_.gsl_vector_p(),
80                                    w.gsl_vector_p());
81  }
82
83
84  const utility::vector& SVD::s(void) const
85  {
86    return s_;
87  }
88
89
90  int SVD::solve(const utility::vector& b, utility::vector x)
91  {
92    return gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 
93                               s_.gsl_vector_p(), b.gsl_vector_p(),
94                               x.gsl_vector_p());
95  }
96
97
98  const utility::matrix& SVD::U(void) const
99  {
100    return U_;
101  }
102
103
104  const utility::matrix& SVD::V(void) const
105  {
106    return V_;
107  }
108
109}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.