v2.0.0
Loading...
Searching...
No Matches
linalg.h
Go to the documentation of this file.
1//=============================================================================================================
34
35#ifndef LINALG_H
36#define LINALG_H
37
38//=============================================================================================================
39// INCLUDES
40//=============================================================================================================
41
42#include "math_global.h"
43
44#include <utility>
45#include <vector>
46
47//=============================================================================================================
48// EIGEN INCLUDES
49//=============================================================================================================
50
51#include <Eigen/Core>
52#include <Eigen/SparseCore>
53#include <Eigen/SVD>
54
55//=============================================================================================================
56// QT INCLUDES
57//=============================================================================================================
58
59#include <QString>
60
61//=============================================================================================================
62// DEFINE NAMESPACE UTILSLIB
63//=============================================================================================================
64
65namespace UTILSLIB
66{
67
68//=============================================================================================================
75{
76public:
77 typedef std::pair<int,int> IdxIntValue;
78
79 //=========================================================================================================
83 ~Linalg() = default;
84
85 //=========================================================================================================
93 static Eigen::VectorXd combine_xyz(const Eigen::VectorXd& vec);
94
95 //=========================================================================================================
104 static double getConditionNumber(const Eigen::MatrixXd& A,
105 Eigen::VectorXd &s);
106
107 //=========================================================================================================
116 static double getConditionSlope(const Eigen::MatrixXd& A,
117 Eigen::VectorXd &s);
118
119 //=========================================================================================================
129 static void get_whitener(Eigen::MatrixXd& A,
130 bool pca,
131 QString ch_type,
132 Eigen::VectorXd& eig,
133 Eigen::MatrixXd& eigvec);
134
135 //=========================================================================================================
145 static void get_whitener(Eigen::MatrixXd& A,
146 bool pca,
147 const std::string& ch_type,
148 Eigen::VectorXd& eig,
149 Eigen::MatrixXd& eigvec);
150
151 //=========================================================================================================
161 static Eigen::VectorXi intersect(const Eigen::VectorXi &v1,
162 const Eigen::VectorXi &v2,
163 Eigen::VectorXi &idx_sel);
164
165 //=========================================================================================================
178 static Eigen::SparseMatrix<double> make_block_diag(const Eigen::MatrixXd &A,
179 qint32 n);
180
181 //=========================================================================================================
189 template<typename T>
190 static Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> pinv(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& a);
191
192 //=========================================================================================================
201 static qint32 rank(const Eigen::MatrixXd& A,
202 double tol = 1e-8);
203
204 //=========================================================================================================
213 template<typename T>
214 static Eigen::VectorXi sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v,
215 bool desc = true);
216
217 //=========================================================================================================
228 template<typename T>
229 static Eigen::VectorXi sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v_prime,
230 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat,
231 bool desc = true);
232
233 //=========================================================================================================
242 template<typename T>
243 static std::vector<Eigen::Triplet<T> > sortrows(const std::vector<Eigen::Triplet<T> > &A,
244 qint32 column = 0);
245
246 //=========================================================================================================
250 template<typename T>
251 static inline bool compareIdxValuePairBiggerThan(const std::pair<int,T>& lhs,
252 const std::pair<int,T>& rhs);
253
254 //=========================================================================================================
258 template<typename T>
259 static inline bool compareIdxValuePairSmallerThan(const std::pair<int,T>& lhs,
260 const std::pair<int,T>& rhs);
261
262 //=========================================================================================================
266 template<typename T>
267 static inline bool compareTripletFirstEntry(const Eigen::Triplet<T>& lhs,
268 const Eigen::Triplet<T> & rhs);
269
270 //=========================================================================================================
274 template<typename T>
275 static inline bool compareTripletSecondEntry(const Eigen::Triplet<T>& lhs,
276 const Eigen::Triplet<T> & rhs);
277};
278
279//=============================================================================================================
280// INLINE & TEMPLATE DEFINITIONS
281//=============================================================================================================
282
283template<typename T>
284Eigen::VectorXi Linalg::sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v,
285 bool desc)
286{
287 std::vector< std::pair<int,T> > t_vecIdxValue;
288 Eigen::VectorXi idx(v.size());
289
290 if(v.size() > 0)
291 {
292 for(qint32 i = 0; i < v.size(); ++i)
293 t_vecIdxValue.push_back(std::pair<int,T>(i, v[i]));
294
295 if(desc)
296 std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), Linalg::compareIdxValuePairBiggerThan<T>);
297 else
298 std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), Linalg::compareIdxValuePairSmallerThan<T>);
299
300 for(qint32 i = 0; i < v.size(); ++i)
301 {
302 idx[i] = t_vecIdxValue[i].first;
303 v[i] = t_vecIdxValue[i].second;
304 }
305 }
306
307 return idx;
308}
309
310//=============================================================================================================
311
312template<typename T>
313Eigen::VectorXi Linalg::sort(Eigen::Matrix<T, Eigen::Dynamic, 1> &v_prime,
314 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat,
315 bool desc)
316{
317 Eigen::VectorXi idx = Linalg::sort<T>(v_prime, desc);
318
319 if(v_prime.size() > 0)
320 {
321 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> newMat(mat.rows(), mat.cols());
322 for(qint32 i = 0; i < idx.size(); ++i)
323 newMat.col(i) = mat.col(idx[i]);
324 mat = newMat;
325 }
326
327 return idx;
328}
329
330//=============================================================================================================
331
332template<typename T>
333std::vector<Eigen::Triplet<T> > Linalg::sortrows(const std::vector<Eigen::Triplet<T> > &A,
334 qint32 column)
335{
336 std::vector<Eigen::Triplet<T> > p_ASorted;
337
338 for(quint32 i = 0; i < A.size(); ++i)
339 p_ASorted.push_back(A[i]);
340
341 if(column == 0)
342 std::sort(p_ASorted.begin(), p_ASorted.end(), Linalg::compareTripletFirstEntry<T>);
343 if(column == 1)
344 std::sort(p_ASorted.begin(), p_ASorted.end(), Linalg::compareTripletSecondEntry<T>);
345
346 return p_ASorted;
347}
348
349//=============================================================================================================
350
351template<typename T>
352inline bool Linalg::compareIdxValuePairBiggerThan(const std::pair<int,T>& lhs,
353 const std::pair<int,T>& rhs)
354{
355 return lhs.second > rhs.second;
356}
357
358//=============================================================================================================
359
360template<typename T>
361inline bool Linalg::compareIdxValuePairSmallerThan(const std::pair<int,T>& lhs,
362 const std::pair<int,T>& rhs)
363{
364 return lhs.second < rhs.second;
365}
366
367//=============================================================================================================
368
369template<typename T>
370inline bool Linalg::compareTripletFirstEntry(const Eigen::Triplet<T>& lhs,
371 const Eigen::Triplet<T> & rhs)
372{
373 return lhs.row() < rhs.row();
374}
375
376//=============================================================================================================
377
378template<typename T>
379inline bool Linalg::compareTripletSecondEntry(const Eigen::Triplet<T>& lhs,
380 const Eigen::Triplet<T> & rhs)
381{
382 return lhs.col() < rhs.col();
383}
384
385//=============================================================================================================
386
387template<typename T>
388Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> Linalg::pinv(const Eigen::Matrix<T,
389 Eigen::Dynamic,
390 Eigen::Dynamic>& a)
391{
392 double epsilon = std::numeric_limits<double>::epsilon();
393 Eigen::JacobiSVD< Eigen::MatrixXd > svd(a, Eigen::ComputeThinU | Eigen::ComputeThinV);
394 double tolerance = epsilon * std::max(a.cols(), a.rows()) * svd.singularValues().array().abs()(0);
395 return svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
396}
397
398//=============================================================================================================
399} // NAMESPACE
400
401#endif // LINALG_H
math library export/import macros.
#define MATHSHARED_EXPORT
Definition math_global.h:55
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Static linear algebra utility functions.
Definition linalg.h:75
static bool compareIdxValuePairSmallerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition linalg.h:361
static Eigen::VectorXd combine_xyz(const Eigen::VectorXd &vec)
Definition linalg.cpp:64
static bool compareTripletFirstEntry(const Eigen::Triplet< T > &lhs, const Eigen::Triplet< T > &rhs)
Definition linalg.h:370
static Eigen::VectorXi sort(Eigen::Matrix< T, Eigen::Dynamic, 1 > &v, bool desc=true)
Definition linalg.h:284
static qint32 rank(const Eigen::MatrixXd &A, double tol=1e-8)
Definition linalg.cpp:240
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:77
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:170
static bool compareIdxValuePairBiggerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition linalg.h:352
static double getConditionNumber(const Eigen::MatrixXd &A, Eigen::VectorXd &s)
Definition linalg.cpp:86
static std::vector< Eigen::Triplet< T > > sortrows(const std::vector< Eigen::Triplet< T > > &A, qint32 column=0)
Definition linalg.h:333
static double getConditionSlope(const Eigen::MatrixXd &A, Eigen::VectorXd &s)
Definition linalg.cpp:99
static bool compareTripletSecondEntry(const Eigen::Triplet< T > &lhs, const Eigen::Triplet< T > &rhs)
Definition linalg.h:379
static Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > pinv(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &a)
Definition linalg.h:388
static Eigen::SparseMatrix< double > make_block_diag(const Eigen::MatrixXd &A, qint32 n)
Definition linalg.cpp:205