MNE-CPP  beta 1.0
mnemath.h
Go to the documentation of this file.
1 //=============================================================================================================
36 #ifndef MNEMATH_H
37 #define MNEMATH_H
38 
39 //ToDo move this to the new MNE math library
40 
41 //*************************************************************************************************************
42 //=============================================================================================================
43 // MNE INCLUDES
44 //=============================================================================================================
45 
46 #include "utils_global.h"
47 
48 #include <iostream>
49 
50 
51 //*************************************************************************************************************
52 //=============================================================================================================
53 // Eigen INCLUDES
54 //=============================================================================================================
55 
56 #include <Eigen/Core>
57 #include <Eigen/SparseCore>
58 #include <Eigen/Eigen>
59 #include <Eigen/SVD>
60 
61 
62 //*************************************************************************************************************
63 //=============================================================================================================
64 // QT INCLUDES
65 //=============================================================================================================
66 
67 #include <QStringList>
68 #include <QVariant>
69 #include <QDebug>
70 
71 
72 //*************************************************************************************************************
73 //=============================================================================================================
74 // DEFINE NAMESPACE UTILSLIB
75 //=============================================================================================================
76 
77 namespace UTILSLIB
78 {
79 
80 //*************************************************************************************************************
81 //=============================================================================================================
82 // USED NAMESPACES
83 //=============================================================================================================
84 
85 using namespace Eigen;
86 
87 
88 //*************************************************************************************************************
89 //=============================================================================================================
90 // FORWARD DECLARATIONS
91 //=============================================================================================================
92 
93 
94 //=============================================================================================================
102 {
103 public:
104  typedef std::pair<int,int> IdxIntValue;
106  //=========================================================================================================
110  virtual ~MNEMath()
111  { }
112 
113  //=========================================================================================================
127  static VectorXd* combine_xyz(const VectorXd& vec);
128 
129 // //=========================================================================================================
130 // /**
131 // * ### MNE toolbox root function ###: Implementation of the mne_block_diag function - decoding part
132 // */
133 // static inline MatrixXd extract_block_diag(MatrixXd& A, qint32 n);
134 
135  //=========================================================================================================
143  static double getConditionNumber(const MatrixXd& A, VectorXd &s);
144 
145  //=========================================================================================================
153  static double getConditionSlope(const MatrixXd& A, VectorXd &s);
154 
155  //=========================================================================================================
164  static void get_whitener(MatrixXd& A, bool pca, QString ch_type, VectorXd& eig, MatrixXd& eigvec);
165 
166 
167  //=========================================================================================================
177  static VectorXi intersect(const VectorXi &v1, const VectorXi &v2, VectorXi &idx_sel);
178 
179  //=========================================================================================================
188  static bool issparse(VectorXd &v);
189 
190  //=========================================================================================================
201  static MatrixXd legendre(qint32 n, const VectorXd &X, QString normalize = QString("unnorm"));
202 
203  //=========================================================================================================
220  static SparseMatrix<double>* make_block_diag(const MatrixXd &A, qint32 n);
221 
222  //=========================================================================================================
229  static int nchoose2(int n);
230 
231  //=========================================================================================================
242  static qint32 rank(const MatrixXd& A, double tol = 1e-8);
243 
244  //=========================================================================================================
262  static MatrixXd rescale(const MatrixXd &data, const RowVectorXf &times, QPair<QVariant,QVariant> baseline, QString mode);
263 
264  //=========================================================================================================
273  template<typename T>
274  static VectorXi sort(Matrix<T, Dynamic, 1> &v, bool desc = true);
275 
276  //=========================================================================================================
287  template<typename T>
288  static VectorXi sort(Matrix<T, Dynamic, 1> &v_prime, Matrix<T, Dynamic, Dynamic> &mat, bool desc = true);
289 
290  //=========================================================================================================
299  template<typename T>
300  static std::vector<Triplet<T> > sortrows(const std::vector<Triplet<T> > &A, qint32 column = 0);
301 
302  //=========================================================================================================
311  template<typename T>
312  static inline bool compareIdxValuePairBiggerThan( const std::pair<int,T>& lhs, const std::pair<int,T>& rhs);
313 
314  //=========================================================================================================
323  template<typename T>
324  static inline bool compareIdxValuePairSmallerThan( const std::pair<int,T>& lhs, const std::pair<int,T>& rhs);
325 
326  //=========================================================================================================
335  template<typename T>
336  static inline bool compareTripletFirstEntry( const Triplet<T>& lhs, const Triplet<T> & rhs);
337 
338  //=========================================================================================================
347  template<typename T>
348  static inline bool compareTripletSecondEntry( const Triplet<T>& lhs, const Triplet<T> & rhs);
349 
350  //=========================================================================================================
358  template<typename T>
359  static inline double log2( const T d);
360 };
361 
362 //*************************************************************************************************************
363 //=============================================================================================================
364 // INLINE & TEMPLATE DEFINITIONS
365 //=============================================================================================================
366 
367 template< typename T>
368 VectorXi MNEMath::sort(Matrix<T, Dynamic, 1> &v, bool desc)
369 {
370  std::vector< std::pair<int,T> > t_vecIdxValue;
371  VectorXi idx(v.size());
372 
373  if(v.size() > 0)
374  {
375  //fill temporal vector
376  for(qint32 i = 0; i < v.size(); ++i)
377  t_vecIdxValue.push_back(std::pair<int,T>(i, v[i]));
378 
379  //sort temporal vector
380  if(desc)
381  std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), MNEMath::compareIdxValuePairBiggerThan<T>);
382  else
383  std::sort(t_vecIdxValue.begin(), t_vecIdxValue.end(), MNEMath::compareIdxValuePairSmallerThan<T>);
384 
385  //store results
386  for(qint32 i = 0; i < v.size(); ++i)
387  {
388  idx[i] = t_vecIdxValue[i].first;
389  v[i] = t_vecIdxValue[i].second;
390  }
391  }
392 
393  return idx;
394 }
395 
396 
397 //*************************************************************************************************************
398 
399 template<typename T>
400 VectorXi MNEMath::sort(Matrix<T, Dynamic, 1> &v_prime, Matrix<T, Dynamic, Dynamic> &mat, bool desc)
401 {
402  VectorXi idx = MNEMath::sort<T>(v_prime, desc);
403 
404  if(v_prime.size() > 0)
405  {
406  //sort Matrix
407  Matrix<T, Dynamic, Dynamic> newMat(mat.rows(), mat.cols());
408  for(qint32 i = 0; i < idx.size(); ++i)
409  newMat.col(i) = mat.col(idx[i]);
410  mat = newMat;
411  }
412 
413  return idx;
414 }
415 
416 
417 //*************************************************************************************************************
418 
419 template<typename T>
420 std::vector<Triplet<T> > MNEMath::sortrows(const std::vector<Triplet<T> > &A, qint32 column)
421 {
422  std::vector<Triplet<T> > p_ASorted;
423 
424  for(quint32 i = 0; i < A.size(); ++i)
425  p_ASorted.push_back(A[i]);
426 
427  if(column == 0)
428  std::sort(p_ASorted.begin(), p_ASorted.end(), MNEMath::compareTripletFirstEntry<T>);
429  if(column == 1)
430  std::sort(p_ASorted.begin(), p_ASorted.end(), MNEMath::compareTripletSecondEntry<T>);
431 
432  return p_ASorted;
433 }
434 
435 
436 //*************************************************************************************************************
437 
438 template<typename T>
439 inline bool MNEMath::compareIdxValuePairBiggerThan( const std::pair<int,T>& lhs, const std::pair<int,T>& rhs)
440 {
441  return lhs.second > rhs.second;
442 }
443 
444 
445 //*************************************************************************************************************
446 
447 template<typename T>
448 inline bool MNEMath::compareIdxValuePairSmallerThan( const std::pair<int,T>& lhs, const std::pair<int,T>& rhs)
449 {
450  return lhs.second < rhs.second;
451 }
452 
453 
454 //*************************************************************************************************************
455 
456 template<typename T>
457 inline bool MNEMath::compareTripletFirstEntry( const Triplet<T>& lhs, const Triplet<T> & rhs)
458 {
459  return lhs.row() < rhs.row();
460 }
461 
462 
463 //*************************************************************************************************************
464 
465 template<typename T>
466 inline bool MNEMath::compareTripletSecondEntry( const Triplet<T>& lhs, const Triplet<T> & rhs)
467 {
468  return lhs.col() < rhs.col();
469 }
470 
471 
472 //*************************************************************************************************************
473 
474 template<typename T>
475 inline double MNEMath::log2( const T d)
476 {
477  return log(d)/log(2);
478 }
479 
480 } // NAMESPACE
481 
482 #endif // MNEMATH_H
Math methods.
Definition: mnemath.h:101
static bool compareTripletSecondEntry(const Triplet< T > &lhs, const Triplet< T > &rhs)
Definition: mnemath.h:466
#define UTILSSHARED_EXPORT
Definition: utils_global.h:57
static bool compareIdxValuePairBiggerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition: mnemath.h:439
utils library export/import macros.
virtual ~MNEMath()
Definition: mnemath.h:110
static VectorXi sort(Matrix< T, Dynamic, 1 > &v, bool desc=true)
Definition: mnemath.h:368
static bool compareTripletFirstEntry(const Triplet< T > &lhs, const Triplet< T > &rhs)
Definition: mnemath.h:457
static std::vector< Triplet< T > > sortrows(const std::vector< Triplet< T > > &A, qint32 column=0)
Definition: mnemath.h:420
static bool compareIdxValuePairSmallerThan(const std::pair< int, T > &lhs, const std::pair< int, T > &rhs)
Definition: mnemath.h:448
std::pair< int, int > IdxIntValue
Definition: mnemath.h:104
static double log2(const T d)
Definition: mnemath.h:475