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

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

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1// $Id: SVD.cc 1000 2007-12-23 20:09:15Z jari $
2
3/*
4  Copyright (C) 2003 Daniel Dalevi
5  Copyright (C) 2004 Jari Häkkinen
6  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
7  Copyright (C) 2006, 2007 Jari Häkkinen
8
9  This file is part of the yat library, http://trac.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "SVD.h"
28#include <sstream>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34
35  SVD::SVD(const utility::matrix& Ain)
36    : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns())
37  {
38  }
39
40
41  SVD::~SVD(void)
42  {
43  }
44
45
46  void SVD::decompose(SVDalgorithm algo)
47  {
48    int status=0;
49    switch (algo) {
50    case GolubReinsch:
51      status=golub_reinsch();
52      break;
53    case ModifiedGolubReinsch:
54      status=modified_golub_reinsch();
55      break;
56    case Jacobi:
57      status=jacobi();
58      break;
59    default:
60      std::stringstream ss("SVD::decompose ");
61      ss << algo << " is not a valid SVDalgorithm";
62      throw GSL_error(ss.str());
63    }
64    if (status)
65      throw utility::GSL_error(std::string("SVD::decompose",status));
66  }
67
68
69  int SVD::golub_reinsch(void)
70  {
71    utility::vector w(U_.columns());
72    return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
73                                s_.gsl_vector_p(), w.gsl_vector_p());
74  }
75
76
77  int SVD::jacobi(void)
78  {
79    return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
80                                       s_.gsl_vector_p());
81  }
82
83
84  int SVD::modified_golub_reinsch(void)
85  {
86    utility::vector w(U_.columns());
87    utility::matrix X(U_.columns(),U_.columns());
88    return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(), 
89                                    V_.gsl_matrix_p(), s_.gsl_vector_p(),
90                                    w.gsl_vector_p());
91  }
92
93
94  const utility::vector& SVD::s(void) const
95  {
96    return s_;
97  }
98
99
100  void SVD::solve(const utility::vector& b, utility::vector& x)
101  {
102    int status=gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 
103                                   s_.gsl_vector_p(), b.gsl_vector_p(),
104                                   x.gsl_vector_p());
105    if (status)
106      throw utility::GSL_error(std::string("SVD::solve",status));
107  }
108
109
110  const utility::matrix& SVD::U(void) const
111  {
112    return U_;
113  }
114
115
116  const utility::matrix& SVD::V(void) const
117  {
118    return V_;
119  }
120
121}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.