#include <svd.hpp>
|
void | computeSVD (std::vector< std::vector< double >> &matrix) |
|
void | bidiagonalize (std::vector< std::vector< double >> &matrix, std::vector< double > &diagonal, std::vector< double > &superdiagonal) |
|
void | computeHouseholderReflection (const std::vector< double > &x, std::vector< double > &v, double &beta) |
|
void | applyHouseholder (std::vector< std::vector< double >> &matrix, const std::vector< double > &v, double beta, size_t fromRow, size_t fromCol) |
|
Definition at line 44 of file svd.hpp.
◆ SVD()
gpmp::linalg::SVD::SVD |
( |
std::vector< std::vector< double >> & |
matrix | ) |
|
|
explicit |
Constructor to compute SVD for a given matrix.
- Parameters
-
matrix | The input matrix for SVD |
- Exceptions
-
std::invalid_argument | if the matrix is empty |
Definition at line 38 of file svd.cpp.
39 if (matrix.empty() || matrix[0].empty()) {
40 throw std::invalid_argument(
"Error: Matrix for SVD is empty.");
void computeSVD(std::vector< std::vector< double >> &matrix)
References computeSVD().
◆ applyHouseholder()
void gpmp::linalg::SVD::applyHouseholder |
( |
std::vector< std::vector< double >> & |
matrix, |
|
|
const std::vector< double > & |
v, |
|
|
double |
beta, |
|
|
size_t |
fromRow, |
|
|
size_t |
fromCol |
|
) |
| |
|
private |
Definition at line 136 of file svd.cpp.
142 size_t m = matrix.size();
143 size_t n = matrix[0].size();
145 for (
size_t i = fromRow; i < m; ++i) {
146 double dotProduct = 0.0;
147 for (
size_t j = fromCol; j < n; ++j) {
148 dotProduct += matrix[i][j] * v[j - fromCol];
153 for (
size_t j = fromCol; j < n; ++j) {
154 matrix[i][j] -= dotProduct * v[j - fromCol];
◆ bidiagonalize()
void gpmp::linalg::SVD::bidiagonalize |
( |
std::vector< std::vector< double >> & |
matrix, |
|
|
std::vector< double > & |
diagonal, |
|
|
std::vector< double > & |
superdiagonal |
|
) |
| |
|
private |
Definition at line 86 of file svd.cpp.
89 size_t m = matrix.size();
90 size_t n = matrix[0].size();
92 diagonal.resize(std::min(m, n));
93 superdiagonal.resize(std::min(m, n) - 1);
95 for (
size_t k = 0; k < std::min(m, n); ++k) {
97 for (
size_t i = k; i < m; ++i) {
98 alpha += matrix[i][k] * matrix[i][k];
100 alpha = (matrix[k][k] > 0) ? -std::sqrt(alpha) : std::sqrt(alpha);
102 double beta = alpha * (alpha - matrix[k][k]);
103 diagonal[k] = -alpha;
106 std::vector<double> columnCopy(m - k);
107 for (
size_t i = k; i < m; ++i) {
108 columnCopy[i - k] = matrix[i][k];
void computeHouseholderReflection(const std::vector< double > &x, std::vector< double > &v, double &beta)
void applyHouseholder(std::vector< std::vector< double >> &matrix, const std::vector< double > &v, double beta, size_t fromRow, size_t fromCol)
◆ computeHouseholderReflection()
void gpmp::linalg::SVD::computeHouseholderReflection |
( |
const std::vector< double > & |
x, |
|
|
std::vector< double > & |
v, |
|
|
double & |
beta |
|
) |
| |
|
private |
Definition at line 116 of file svd.cpp.
123 for (
size_t i = 1; i < n; ++i) {
124 sigma += x[i] * x[i];
129 for (
size_t i = 1; i < n; ++i) {
133 beta = 2.0 / (sigma + 1.0);
◆ computeSVD()
void gpmp::linalg::SVD::computeSVD |
( |
std::vector< std::vector< double >> & |
matrix | ) |
|
|
private |
Definition at line 60 of file svd.cpp.
61 size_t m = matrix.size();
62 size_t n = matrix[0].size();
66 std::vector<std::vector<double>>(m, std::vector<double>(m, 0.0));
68 std::vector<std::vector<double>>(n, std::vector<double>(n, 0.0));
70 std::vector<double> superdiagonal(n - 1, 0.0);
72 bidiagonalize(
const_cast<std::vector<std::vector<double>
> &>(matrix),
77 for (
size_t i = 0; i < m; ++i) {
81 for (
size_t i = 0; i < n; ++i) {
std::vector< std::vector< double > > rightSingularVectors_
void bidiagonalize(std::vector< std::vector< double >> &matrix, std::vector< double > &diagonal, std::vector< double > &superdiagonal)
std::vector< std::vector< double > > leftSingularVectors_
std::vector< double > singularValues_
Referenced by SVD().
◆ getLeftSingularVectors()
std::vector< std::vector< double > > gpmp::linalg::SVD::getLeftSingularVectors |
( |
| ) |
const |
Get the left singular vectors of the matrix.
- Returns
- Matrix containing left singular vectors as columns
Definition at line 51 of file svd.cpp.
◆ getRightSingularVectors()
std::vector< std::vector< double > > gpmp::linalg::SVD::getRightSingularVectors |
( |
| ) |
const |
Get the right singular vectors of the matrix.
- Returns
- Matrix containing right singular vectors as columns
Definition at line 56 of file svd.cpp.
◆ getSingularValues()
std::vector< double > gpmp::linalg::SVD::getSingularValues |
( |
| ) |
const |
Get the singular values of the matrix.
- Returns
- Vector containing singular values in descending order
Definition at line 46 of file svd.cpp.
◆ leftSingularVectors_
std::vector<std::vector<double> > gpmp::linalg::SVD::leftSingularVectors_ |
|
private |
◆ rightSingularVectors_
std::vector<std::vector<double> > gpmp::linalg::SVD::rightSingularVectors_ |
|
private |
◆ singularValues_
std::vector<double> gpmp::linalg::SVD::singularValues_ |
|
private |
The documentation for this class was generated from the following files: