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

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

Addresses #2. Continued adding GSL_error exceptions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1// $Id: SVD.cc 751 2007-02-17 12:25:36Z 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#include <sstream>
26
27namespace theplu {
28namespace yat {
29namespace utility {
30
31
32  SVD::SVD(const utility::matrix& Ain)
33    : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns())
34  {
35  }
36
37
38  SVD::~SVD(void)
39  {
40  }
41
42
43  void SVD::decompose(SVDalgorithm algo)
44  {
45    int status=0;
46    switch (algo) {
47    case GolubReinsch:
48      status=golub_reinsch();
49      break;
50    case ModifiedGolubReinsch:
51      status=modified_golub_reinsch();
52      break;
53    case Jacobi:
54      status=jacobi();
55      break;
56    default:
57      std::stringstream ss("SVD::decompose ");
58      ss << algo << " is not a valid SVDalgorithm";
59      throw GSL_error(ss.str());
60    }
61    if (status)
62      throw utility::GSL_error(std::string("SVD::decompose",status));
63  }
64
65
66  int SVD::golub_reinsch(void)
67  {
68    utility::vector w(U_.columns());
69    return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
70                                s_.gsl_vector_p(), w.gsl_vector_p());
71  }
72
73
74  int SVD::jacobi(void)
75  {
76    return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(),
77                                       s_.gsl_vector_p());
78  }
79
80
81  int SVD::modified_golub_reinsch(void)
82  {
83    utility::vector w(U_.columns());
84    utility::matrix X(U_.columns(),U_.columns());
85    return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(), 
86                                    V_.gsl_matrix_p(), s_.gsl_vector_p(),
87                                    w.gsl_vector_p());
88  }
89
90
91  const utility::vector& SVD::s(void) const
92  {
93    return s_;
94  }
95
96
97  void SVD::solve(const utility::vector& b, utility::vector& x)
98  {
99    int status=gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 
100                                   s_.gsl_vector_p(), b.gsl_vector_p(),
101                                   x.gsl_vector_p());
102    if (status)
103      throw utility::GSL_error(std::string("SVD::solve",status));
104  }
105
106
107  const utility::matrix& SVD::U(void) const
108  {
109    return U_;
110  }
111
112
113  const utility::matrix& SVD::V(void) const
114  {
115    return V_;
116  }
117
118}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.