RML  1.0
Robotics Mathematical Library
Loading...
Searching...
No Matches
SVD.h
Go to the documentation of this file.
1
8#ifndef INCLUDE_RML_SVD_H_
9#define INCLUDE_RML_SVD_H_
10
11#include <eigen3/Eigen/Dense>
12#include <iostream>
13
14#include "MatrixOperations.h"
15
16namespace rml{
17
27void SVD_NumericalRecipes(double *a, int m, int n, double *w, double *v, double *rv1);
28
29
42template<class MatT>
43void SVD(const MatT& A, MatT& U, MatT& S, MatT& V) {
44
45 int m = A.rows(), n = A.cols();
46 int MaxMatDim = std::pow(std::max(m,n),2);
47 double SVD[3 * MaxMatDim];
48 double w[MaxMatDim];
49
50 int index[MaxMatDim];
51
52 Eigen::MatrixXd Utmp;
53 Eigen::MatrixXd Vtmp;
54
55 double U_temp[MaxMatDim], V_temp[MaxMatDim];
56
57 Eigen::Map<MatT>(U_temp, m, n) = A;
58
59 SVD_NumericalRecipes(U_temp, m, n, w, V_temp, SVD);
60
61 Utmp = Eigen::Map<MatT>(U_temp, m, m);
62 Vtmp = Eigen::Map<MatT>(V_temp, n, n);
63
64 for (int i = 0; i < MaxMatDim; i++)
65 index[i] = i;// + 1;
66
67 double max = 0;
68 int index_max;
69 for (int i = 0; i < n; i++) {
70 max = w[i];
71 index_max = i;
72 for (int j = i+1; j < n; j++) {
73 if (w[j] > max) {
74 index_max = j;
75 max = w[j];
76 }
77 }
78
79 double tmp = w[i];
80 int tmpindex = index[i];
81 w[i] = w[index_max];
82 index[i] = index[index_max];
83 w[index_max] = tmp;
84 index[index_max] = tmpindex;
85 }
86
87 Vtmp.conservativeResize(n, n);
88 Utmp.conservativeResize(m, std::max(m, n));
89
90 U = Utmp.block(0, index[0], m, 1);
91 V = Vtmp.block(0, index[0], n, 1);
92
93 for (int i = 1; i < n; i++) {
94 V = RightJuxtapose(V, Vtmp.block(0, index[i], n, 1));
95 }
96
97 for (int i = 1; i < m; i++) {
98 U = RightJuxtapose(U, Utmp.block(0, index[i], m, 1));
99 }
100
101 U = U.block(0, 0, m, m);
102
103 S.resize(m, n);
104 S.setZero();
105
107
108 S = S.block(0, 0, m, n);
109}
110
111}
112
113
114
115#endif /* INCLUDE_RML_SVD_H_ */
Types and algorithms for robotic mobile manipulation.
Definition ArmModel.h:19
void SVD_NumericalRecipes(double *a, int m, int n, double *w, double *v, double *rv1)
void SetDiagonalFromDouble(Eigen::MatrixXd &MatT, double diag[])
Definition MatrixOperations.h:39
Eigen::MatrixXd RightJuxtapose(const Eigen::MatrixXd &A, const Eigen::MatrixXd &B)
Definition MatrixOperations.h:45
void SVD(const MatT &A, MatT &U, MatT &S, MatT &V)
Singular Value Decomposition.
Definition SVD.h:43