openGPMP
Open Source Mathematics Package
svd.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 #ifndef SVD_HPP
35 #define SVD_HPP
36 
37 #include <iostream>
38 #include <vector>
39 
40 namespace gpmp {
41 
42 namespace linalg {
43 
44 class SVD {
45  public:
51  explicit SVD(std::vector<std::vector<double>> &matrix);
52 
57  std::vector<double> getSingularValues() const;
58 
63  std::vector<std::vector<double>> getLeftSingularVectors() const;
64 
69  std::vector<std::vector<double>> getRightSingularVectors() const;
70 
71  private:
72  std::vector<double> singularValues_;
73  std::vector<std::vector<double>> leftSingularVectors_;
74  std::vector<std::vector<double>> rightSingularVectors_;
75 
76  // Helper functions for SVD computation
77  void computeSVD(std::vector<std::vector<double>> &matrix);
78  void bidiagonalize(std::vector<std::vector<double>> &matrix,
79  std::vector<double> &diagonal,
80  std::vector<double> &superdiagonal);
81 
82  void computeHouseholderReflection(const std::vector<double> &x,
83  std::vector<double> &v,
84  double &beta);
85  void applyHouseholder(std::vector<std::vector<double>> &matrix,
86  const std::vector<double> &v,
87  double beta,
88  size_t fromRow,
89  size_t fromCol);
90 };
91 } // namespace linalg
92 
93 } // namespace gpmp
94 
95 #endif
std::vector< std::vector< double > > rightSingularVectors_
Definition: svd.hpp:74
void computeHouseholderReflection(const std::vector< double > &x, std::vector< double > &v, double &beta)
Definition: svd.cpp:116
std::vector< double > getSingularValues() const
Get the singular values of the matrix.
Definition: svd.cpp:46
void bidiagonalize(std::vector< std::vector< double >> &matrix, std::vector< double > &diagonal, std::vector< double > &superdiagonal)
Definition: svd.cpp:86
SVD(std::vector< std::vector< double >> &matrix)
Constructor to compute SVD for a given matrix.
Definition: svd.cpp:38
std::vector< std::vector< double > > getLeftSingularVectors() const
Get the left singular vectors of the matrix.
Definition: svd.cpp:51
void applyHouseholder(std::vector< std::vector< double >> &matrix, const std::vector< double > &v, double beta, size_t fromRow, size_t fromCol)
Definition: svd.cpp:136
std::vector< std::vector< double > > leftSingularVectors_
Definition: svd.hpp:73
std::vector< std::vector< double > > getRightSingularVectors() const
Get the right singular vectors of the matrix.
Definition: svd.cpp:56
std::vector< double > singularValues_
Definition: svd.hpp:72
void computeSVD(std::vector< std::vector< double >> &matrix)
Definition: svd.cpp:60
The source C++ openGPMP namespace.