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

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

Addresses #436.

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