v2.0.0
Loading...
Searching...
No Matches
linalg.h
Go to the documentation of this file.
1//=============================================================================================================
28
29#ifndef LINALG_H
30#define LINALG_H
31
32//=============================================================================================================
33// INCLUDES
34//=============================================================================================================
35
36#include "math_global.h"
37
38#include <utility>
39#include <vector>
40
41//=============================================================================================================
42// EIGEN INCLUDES
43//=============================================================================================================
44
45#include <Eigen/Core>
46#include <Eigen/SparseCore>
47#include <Eigen/SVD>
48
49//=============================================================================================================
50// QT INCLUDES
51//=============================================================================================================
52
53#include <QString>
54
55//=============================================================================================================
56// DEFINE NAMESPACE UTILSLIB
57//=============================================================================================================
58
59namespace UTILSLIB
60{
61
62//=============================================================================================================
71{
72public:
73 typedef std::pair<int,int> IdxIntValue;
74
75 //=========================================================================================================
79 ~Linalg() = default;
80
81 //=========================================================================================================
89 static Eigen::VectorXd combine_xyz(const Eigen::VectorXd& vec);
90
91 //=========================================================================================================
100 static double getConditionNumber(const Eigen::MatrixXd& A,
101 Eigen::VectorXd &s);
102
103 //=========================================================================================================
112 static double getConditionSlope(const Eigen::MatrixXd& A,
113 Eigen::VectorXd &s);
114
115 //=========================================================================================================
125 static void get_whitener(Eigen::MatrixXd& A,
126 bool pca,
127 QString ch_type,
128 Eigen::VectorXd& eig,
129 Eigen::MatrixXd& eigvec);
130
131 //=========================================================================================================
141 static void get_whitener(Eigen::MatrixXd& A,
142 bool pca,
143 const std::string& ch_type,
144 Eigen::VectorXd& eig,
145 Eigen::MatrixXd& eigvec);
146
147 //=========================================================================================================
157 static Eigen::VectorXi intersect(const Eigen::VectorXi &v1,
158 const Eigen::VectorXi &v2,
159 Eigen::VectorXi &idx_sel);
160
161 //=========================================================================================================
174 static Eigen::SparseMatrix<double> make_block_diag(const Eigen::MatrixXd &A,
175 qint32 n);
176
177 //=========================================================================================================
185 template<typename T>
186 static Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> pinv(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& a);
187
188 //=========================================================================================================
197 static qint32 rank(const Eigen::MatrixXd& A,
198 double tol = 1e-8);
199
200 //=========================================================================================================
209 template<typename T>
210 static Eigen::VectorXi sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v,
211 bool desc = true);
212
213 //=========================================================================================================
224 template<typename T>
225 static Eigen::VectorXi sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v_prime,
226 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat,
227 bool desc = true);
228
229 //=========================================================================================================
238 template<typename T>
239 static std::vector<Eigen::Triplet<T> > sortrows(const std::vector<Eigen::Triplet<T> > &A,
240 qint32 column = 0);
241
242 //=========================================================================================================
246 template<typename T>
247 static inline bool compareIdxValuePairBiggerThan(const std::pair<int,T>& lhs,
248 const std::pair<int,T>& rhs);
249
250 //=========================================================================================================
254 template<typename T>
255 static inline bool compareIdxValuePairSmallerThan(const std::pair<int,T>& lhs,
256 const std::pair<int,T>& rhs);
257
258 //=========================================================================================================
262 template<typename T>
263 static inline bool compareTripletFirstEntry(const Eigen::Triplet<T>& lhs,
264 const Eigen::Triplet<T> & rhs);
265
266 //=========================================================================================================
270 template<typename T>
271 static inline bool compareTripletSecondEntry(const Eigen::Triplet<T>& lhs,
272 const Eigen::Triplet<T> & rhs);
273};
274
275//=============================================================================================================
276// INLINE & TEMPLATE DEFINITIONS
277//=============================================================================================================
278
279template<typename T>
280Eigen::VectorXi Linalg::sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v,
281 bool desc)
282{
283 std::vector< std::pair<int,T> > t_vecIdxValue;
284 Eigen::VectorXi idx(v.size());
285
286 if(v.size() > 0)
287 {
288 for(qint32 i = 0; i < v.size(); ++i)
289 t_vecIdxValue.push_back(std::pair<int,T>(i, v[i]));
290
291 if(desc)
292 std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), Linalg::compareIdxValuePairBiggerThan<T>);
293 else
294 std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), Linalg::compareIdxValuePairSmallerThan<T>);
295
296 for(qint32 i = 0; i < v.size(); ++i)
297 {
298 idx[i] = t_vecIdxValue[i].first;
299 v[i] = t_vecIdxValue[i].second;
300 }
301 }
302
303 return idx;
304}
305
306//=============================================================================================================
307
308template<typename T>
309Eigen::VectorXi Linalg::sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v_prime,
310 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat,
311 bool desc)
312{
313 Eigen::VectorXi idx = Linalg::sort<T>(v_prime, desc);
314
315 if(v_prime.size() > 0)
316 {
317 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> newMat(mat.rows(), mat.cols());
318 for(qint32 i = 0; i < idx.size(); ++i)
319 newMat.col(i) = mat.col(idx[i]);
320 mat = newMat;
321 }
322
323 return idx;
324}
325
326//=============================================================================================================
327
328template<typename T>
329std::vector<Eigen::Triplet<T> > Linalg::sortrows(const std::vector<Eigen::Triplet<T> > &A,
330 qint32 column)
331{
332 std::vector<Eigen::Triplet<T> > p_ASorted;
333
334 for(quint32 i = 0; i < A.size(); ++i)
335 p_ASorted.push_back(A[i]);
336
337 if(column == 0)
338 std::sort(p_ASorted.begin(), p_ASorted.end(), Linalg::compareTripletFirstEntry<T>);
339 if(column == 1)
340 std::sort(p_ASorted.begin(), p_ASorted.end(), Linalg::compareTripletSecondEntry<T>);
341
342 return p_ASorted;
343}
344
345//=============================================================================================================
346
347template<typename T>
348inline bool Linalg::compareIdxValuePairBiggerThan(const std::pair<int,T>& lhs,
349 const std::pair<int,T>& rhs)
350{
351 return lhs.second > rhs.second;
352}
353
354//=============================================================================================================
355
356template<typename T>
357inline bool Linalg::compareIdxValuePairSmallerThan(const std::pair<int,T>& lhs,
358 const std::pair<int,T>& rhs)
359{
360 return lhs.second < rhs.second;
361}
362
363//=============================================================================================================
364
365template<typename T>
366inline bool Linalg::compareTripletFirstEntry(const Eigen::Triplet<T>& lhs,
367 const Eigen::Triplet<T> & rhs)
368{
369 return lhs.row() < rhs.row();
370}
371
372//=============================================================================================================
373
374template<typename T>
375inline bool Linalg::compareTripletSecondEntry(const Eigen::Triplet<T>& lhs,
376 const Eigen::Triplet<T> & rhs)
377{
378 return lhs.col() < rhs.col();
379}
380
381//=============================================================================================================
382
383template<typename T>
384Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> Linalg::pinv(const Eigen::Matrix<T,
385 Eigen::Dynamic,
386 Eigen::Dynamic>& a)
387{
388 double epsilon = std::numeric_limits<double>::epsilon();
389 Eigen::JacobiSVD< Eigen::MatrixXd > svd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
390 double tolerance = epsilon * std::max(a.cols(), a.rows()) * svd.singularValues().array().abs()(0);
391 return svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
392}
393
394//=============================================================================================================
395} // NAMESPACE
396
397#endif // LINALG_H
Eigen::JacobiSVD< Eigen::Matrix3f > svd(S, Eigen::ComputeFullU|Eigen::ComputeFullV)
Export/import macros and build-stamp accessors for MATHLIB.
#define MATHSHARED_EXPORT
Definition math_global.h:51
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Static Eigen-based linear-algebra helpers used across MATHLIB and the inverse solvers.
Definition linalg.h:71
static bool compareIdxValuePairSmallerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition linalg.h:357
static Eigen::VectorXd combine_xyz(const Eigen::VectorXd &vec)
Definition linalg.cpp:57
static bool compareTripletFirstEntry(const Eigen::Triplet< T > &lhs, const Eigen::Triplet< T > &rhs)
Definition linalg.h:366
static Eigen::VectorXi sort(Eigen::Matrix< T, Eigen::Dynamic, 1 > &v, bool desc=true)
Definition linalg.h:280
static qint32 rank(const Eigen::MatrixXd &A, double tol=1e-8)
Definition linalg.cpp:233
static void get_whitener(Eigen::MatrixXd &A, bool pca, const std::string &ch_type, Eigen::VectorXd &eig, Eigen::MatrixXd &eigvec)
~Linalg()=default
std::pair< int, int > IdxIntValue
Definition linalg.h:73
static void get_whitener(Eigen::MatrixXd &A, bool pca, QString ch_type, Eigen::VectorXd &eig, Eigen::MatrixXd &eigvec)
static Eigen::VectorXi intersect(const Eigen::VectorXi &v1, const Eigen::VectorXi &v2, Eigen::VectorXi &idx_sel)
Definition linalg.cpp:163
static bool compareIdxValuePairBiggerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition linalg.h:348
static double getConditionNumber(const Eigen::MatrixXd &A, Eigen::VectorXd &s)
Definition linalg.cpp:79
static std::vector< Eigen::Triplet< T > > sortrows(const std::vector< Eigen::Triplet< T > > &A, qint32 column=0)
Definition linalg.h:329
static double getConditionSlope(const Eigen::MatrixXd &A, Eigen::VectorXd &s)
Definition linalg.cpp:92
static bool compareTripletSecondEntry(const Eigen::Triplet< T > &lhs, const Eigen::Triplet< T > &rhs)
Definition linalg.h:375
static Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > pinv(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &a)
Definition linalg.h:384
static Eigen::SparseMatrix< double > make_block_diag(const Eigen::MatrixXd &A, qint32 n)
Definition linalg.cpp:198