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

Last change on this file since 1016 was 1016, checked in by Peter, 14 years ago

passing VectorBase? in SVD - refs #256

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