DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlMatrix.h
Go to the documentation of this file.
1 
10 #ifndef __utlMatrix_h
11 #define __utlMatrix_h
12 
13 #include "utlNDArray.h"
14 
15 namespace utl
16 {
17 
18 template < class T, unsigned int Dim >
19 class NDArray;
20 
21 template < class T, unsigned int Dim >
23 
24 
36 template < class T >
37 class NDArray<T,2> : public NDArrayBase<T,2>
38 {
39 public:
40  typedef NDArray Self;
42 
43  typedef typename Superclass::ValueType ValueType;
45 
46  typedef typename Superclass::SizeType SizeType;
47  typedef typename Superclass::ShapeType ShapeType;
48  typedef typename Superclass::Pointer Pointer;
50  typedef typename Superclass::Reference Reference;
52  typedef typename Superclass::Iterator Iterator;
54 
57 
58  using Superclass::SetData;
60  using Superclass::ReSize;
61  using Superclass::operator();
62  using Superclass::operator=;
63  using Superclass::operator+=;
64  using Superclass::operator-=;
65  using Superclass::operator%=;
66  using Superclass::operator/=;
67 
68  NDArray() : Superclass()
69  { }
70  explicit NDArray(const SizeType rows, const SizeType columns) : Superclass()
71  {
72  SizeType shape[2];
73  shape[0]=rows, shape[1]=columns;
75  }
76  NDArray(const NDArray<T,2>& mat) : Superclass(mat)
77  {
78  }
80  {
81  operator=(std::move(mat));
82  }
83 
84  template<typename EType>
85  NDArray(const Expr<EType, typename EType::ValueType>& expr) : Superclass(expr)
86  { }
87  NDArray(const T* vec, const SizeType rows, const SizeType columns) : Superclass()
88  {
89  SizeType shape[2];
90  shape[0]=rows, shape[1]=columns;
92  utl::cblas_copy<T>(this->Size(), vec, 1, this->m_Data, 1);
93  }
94  NDArray(const SizeType rows, const SizeType columns, const T r) : Superclass()
95  {
96  SizeType shape[2];
97  shape[0]=rows, shape[1]=columns;
99  this->Fill(r);
100  }
101 
102  template< typename TMatrixValueType >
103  NDArray(const NDArray< TMatrixValueType,2> & r) : Superclass(r)
104  { }
105 
106  explicit NDArray(const ShapeType& shape) : Superclass(shape) { }
107 
113  NDArray(const T* vec, const ShapeType& shape) : Superclass(vec,shape) { }
114 
118  NDArray(const ShapeType& shape, const T r) : Superclass(shape, r) { }
119 
121  {
123  return *this;
124  }
125 
127  {
128  if ( this != &r )
129  {
130  this->Clear();
131  this->Swap(r);
132  }
133  return *this;
134  }
135 
136  UTL_ALWAYS_INLINE SizeType Rows() const {return this->m_Shape[0];}
137  UTL_ALWAYS_INLINE SizeType Columns() const {return this->m_Shape[1];}
138  UTL_ALWAYS_INLINE SizeType Cols() const {return this->m_Shape[1];}
139 
140  UTL_ALWAYS_INLINE bool ReSize(const SizeType rows, const SizeType cols)
141  {
142  SizeType shape[2];
143  shape[0]=rows, shape[1]=cols;
144  return Superclass::ReSize(shape);
145  }
146 
147  UTL_ALWAYS_INLINE T & operator()(unsigned int row, unsigned int col)
148  { return this->m_Data[row*this->m_Shape[1]+col];}
149  UTL_ALWAYS_INLINE const T & operator()(unsigned int row, unsigned int col) const
150  { return this->m_Data[row*this->m_Shape[1]+col];}
151 
152 
153 
156  {
157  int min_MN = utl::min(this->m_Shape[0], this->m_Shape[1]);
158  for ( int i = 0; i < min_MN; ++i )
159  (*this)(i,i) = val;
160  return *this;
161  }
162 
165  {
166  int min_MN = utl::min(this->m_Shape[0], this->m_Shape[1]);
167  utlSAException(vec.Size()!=min_MN)(vec.Size())(min_MN).msg("wrong size of input vector");
168  for ( int i = 0; i < min_MN; ++i )
169  (*this)(i,i) = vec[i];
170  return *this;
171  }
172 
173  void GetDiagonal(NDArray<T,1>& vec) const
174  {
175  int min_MN = utl::min(this->m_Shape[0], this->m_Shape[1]);
176  vec.ReSize(min_MN);
177  for ( int i = 0; i < min_MN; ++i )
178  vec[i] = (*this)(i,i);
179  }
180 
182  {
183  for ( int i = 0; i < this->m_Shape[0]; ++i )
184  for ( int j = 0; j < this->m_Shape[1]; ++j )
185  (*this)(i,j) = (i==j) ? (T)1.0 : (T)0.0;
186  return *this;
187  }
188 
190  void SetData( T* const data, const unsigned rows, const unsigned cols )
191  {
192  SizeType shape[2];
193  shape[0]=rows, shape[1]=cols;
194  Superclass::SetData(data, shape);
195  }
196 
198  void CopyData(T* const data, const unsigned rows, const unsigned cols )
199  {
200  SizeType shape[2];
201  shape[0]=rows, shape[1]=cols;
202  Superclass::CopyData(data, shape);
203  }
204 
205  NDArray<T,2> GetTranspose(const T scale=1.0) const
206  {
207  NDArray<T,2> result(this->m_Shape[1], this->m_Shape[0]);
208 #ifdef UTL_USE_MKL
209  utl::mkl_omatcopy<T>('R', 'T', this->m_Shape[0], this->m_Shape[1], scale, this->m_Data, this->m_Shape[1], result.m_Data, result.m_Shape[1]);
210 #else
211  for ( int i = 0; i < this->m_Shape[0]; ++i )
212  for ( int j = 0; j < this->m_Shape[1]; ++j )
213  result[j*this->m_Shape[0]+i] = (*this)[i*this->m_Shape[1]+j];
214  result.Scale(scale);
215 #endif
216  return result;
217  }
218 
219  NDArray<T,2> GetConjugateTranspose(const T scale=1.0) const
220  {
221  NDArray<T, 2> result = GetTranspose(scale);
222  result = utl::Conj(result);
223  return result;
224  }
225 
226  NDArray<T,2>& TransposeInplace(const T scale=1.0)
227  {
228 #ifdef UTL_USE_MKL
229  utl::mkl_imatcopy<T>('R', 'T', this->m_Shape[0], this->m_Shape[1], scale, this->m_Data, this->m_Shape[1], this->m_Shape[0]);
230  std::swap(this->m_Shape[0], this->m_Shape[1]);
231  this->ComputeOffSetTable();
232 #else
233  // NOTE: force swap row and col, because when -O3 is used, it may not swap the shape.
234  SizeType row=this->m_Shape[0], col=this->m_Shape[1];
235  this->GetTranspose(scale).Swap(*this);
236  this->m_Shape[0]=col, this->m_Shape[1]=row;
237 #endif
238  return *this;
239  }
240 
241  NDArray<T,2>& SetRow(const int index, T const* vec)
242  {
243  utlSAException(index<0 || index>=this->m_Shape[0])(index)(this->m_Shape[0]).msg("wrong index");
244  utl::cblas_copy<T>(this->m_Shape[1],vec,1,this->m_Data+this->m_Shape[1]*index,1);
245  return *this;
246  }
247  NDArray<T,2>& SetRow(const int index, const NDArray<T,1>& vec)
248  {
249  utlSAException(vec.Size()!=this->m_Shape[1])(vec.Size())(this->m_Shape[1]).msg("wrong size of vec");
250  SetRow(index, vec.GetData());
251  return *this;
252  }
253  NDArray<T,2>& SetColumn(const int index, T const* vec)
254  {
255  utlSAException(index<0 || index>=this->m_Shape[1])(index)(this->m_Shape[1]).msg("wrong index");
256  utl::cblas_copy<T>(this->m_Shape[0],vec,1,this->m_Data+index,this->m_Shape[1]);
257  return *this;
258  }
259  NDArray<T,2>& SetColumn(const int index, const NDArray<T,1>& vec)
260  {
261  utlSAException(vec.Size()!=this->m_Shape[0])(vec.Size())(this->m_Shape[0]).msg("wrong size of vec");
262  SetColumn(index, vec.GetData());
263  return *this;
264  }
265 
266  void GetRow(const int index, T* vec) const
267  {
268  utlSAException(index<0 || index>=this->m_Shape[0])(index)(this->m_Shape[0]).msg("wrong index");
269  utl::cblas_copy<T>(this->m_Shape[1],this->m_Data+this->m_Shape[1]*index,1,vec,1);
270  }
271  NDArray<T,1> GetRow(const int index) const
272  {
273  NDArray<T,1> vec(this->m_Shape[1]);
274  GetRow(index, vec.GetData());
275  return vec;
276  }
277  void GetColumn(const int index, T* vec) const
278  {
279  utlSAException(index<0 || index>=this->m_Shape[1])(index)(this->m_Shape[1]).msg("wrong index");
280  utl::cblas_copy<T>(this->m_Shape[0],this->m_Data+index,this->m_Shape[1],vec,1);
281  }
282  NDArray<T,1> GetColumn(const int index) const
283  {
284  NDArray<T,1> vec(this->m_Shape[0]);
285  GetColumn(index, vec.GetData());
286  return vec;
287  }
288 
289  NDArray<T,2> GetRows(const std::vector<int>& indexVec) const
290  {
291  NDArray<T,2> mat(indexVec.size(), this->m_Shape[1]);
292  for ( int i = 0; i < indexVec.size(); ++i )
293  GetRow(indexVec[i], mat.m_Data+i*this->m_Shape[1]);
294  return mat;
295  }
296  NDArray<T,2> GetNRows(const int index0, const int N) const
297  {
298  std::vector<int> indexVec;
299  for ( int i = 0; i < N; ++i )
300  indexVec.push_back(index0+i);
301  return GetRows(indexVec);
302  }
303  NDArray<T,2> GetColumns(const std::vector<int>& indexVec) const
304  {
305  NDArray<T,2> mat(this->m_Shape[0], indexVec.size());
306  NDArray<T,1> vec;
307  for ( int i = 0; i < indexVec.size(); ++i )
308  {
309  vec = GetColumn(indexVec[i]);
310  mat.SetColumn(i, vec);
311  }
312  return mat;
313  }
314  NDArray<T,2> GetNColumns(const int index0, const int N) const
315  {
316  std::vector<int> indexVec;
317  for ( int i = 0; i < N; ++i )
318  indexVec.push_back(index0+i);
319  return GetColumns(indexVec);
320  }
321  NDArray<T,2>& SetRows(const std::vector<int>& indexVec, const NDArray<T,2>& mat)
322  {
323  NDArray<T,1> vec;
324  for ( int i = 0; i < indexVec.size(); ++i )
325  {
326  mat.GetRow(i,vec);
327  SetRow(indexVec[i], vec);
328  }
329  return *this;
330  }
331  NDArray<T,2>& SetColumns(const std::vector<int>& indexVec, const NDArray<T,2>& mat)
332  {
333  NDArray<T,1> vec;
334  for ( int i = 0; i < indexVec.size(); ++i )
335  {
336  mat.GetColumn(i,vec);
337  SetColumn(indexVec[i], vec);
338  }
339  return *this;
340  }
341 
348  NDArray<T,2> GetCrop(const int x0, const int x1, const int y0, const int y1) const
349  {
350  utlSAException(x0<0 || x0>=this->m_Shape[0] || x1<0 || x1>=this->m_Shape[0] || x0>x1)(x0)(x1)(this->m_Shape[0]).msg("wrong index");
351  utlSAException(y0<0 || y0>=this->m_Shape[0] || y1<0 || y1>=this->m_Shape[1] || y0>y1)(y0)(y1)(this->m_Shape[1]).msg("wrong index");
352  NDArray<T,2> mat(x1-x0+1, y1-y0+1);
353  for ( int i = 0; i < mat.Rows(); ++i )
354  for ( int j = 0; j < mat.Cols(); ++j )
355  mat(i,j) = (*this)(i+x0, j+x1);
356  return mat;
357  }
358 
359  NDArray<T,2>& SetCrop(const int x0, const int x1, const int y0, const int y1, const NDArray<T,2>& mat)
360  {
361  utlSAException(x0<0 || x0>=this->m_Shape[0] || x1<0 || x1>=this->m_Shape[0] || x0>x1)(x0)(x1)(this->m_Shape[0]).msg("wrong index");
362  utlSAException(y0<0 || y0>=this->m_Shape[0] || y1<0 || y1>=this->m_Shape[1] || y0>y1)(y0)(y1)(this->m_Shape[1]).msg("wrong index");
363  utlSAException(mat.Rows()!=x1-x0+1 || mat.Cols()!=y1-y0+1)(mat.Rows())(mat.Cols()).msg("wrong size");
364  for ( int i = 0; i < mat.Rows(); ++i )
365  for ( int j = 0; j < mat.Cols(); ++j )
366  (*this)(i+x0, j+x1) = mat(i,j);
367  return *this;
368  }
369 
378  void SVD(NDArray<T,2>& U, NDArray<ScalarValueType,1>& S, NDArray<T,2>& V, char format='S') const
379  {
380 #ifdef UTL_USE_FASTLAPACK
381  // NOTE: fast but may be wrong for big matrix and multi-thread if openblas is not correctly built
382  utl::gesdd_UtlMatrix(*this, U, S, V, format);
383 #else
384  // slow but robust
385  utl::gesvd_UtlMatrix<T>(*this, U, S, V, format);
386 #endif
387  }
388 
389 
396  void EigenDecompositionSymmetricMatrix (NDArray<T,1>& eigenValues, NDArray<T,2>& eigenVectors ) const
397  {
398  utlSAException(this->m_Shape[0]!=this->m_Shape[1])(this->m_Shape[0])(this->m_Shape[1]).msg("the matrix should be symmetric");
399 #ifdef UTL_USE_FASTLAPACK
400  // NOTE: fast but may be wrong for big matrix and multi-thread if openblas is not correctly built
401  syevd_UtlMatrix<T>(*this, eigenValues, eigenVectors);
402 #else
403  // slow but robust
404  syev_UtlMatrix<T>(*this, eigenValues, eigenVectors);
405 #endif
406  }
407 
409  {
410  utl::geev_UtlMatrix(*this, valReal, valImg);
411  }
412  void EigenDecompositionNonSymmetricMatrix (NDArray<T,1>& valReal, NDArray<T,1>& valImg, NDArray<T,2>& vecRealR, NDArray<T,2>& vecImgR) const
413  {
414  utl::geev_UtlMatrix(*this, valReal, valImg, vecRealR, vecImgR);
415  }
416  void EigenDecompositionNonSymmetricMatrix (NDArray<T,1>& valReal, NDArray<T,1>& valImg, NDArray<T,2>& vecRealR, NDArray<T,2>& vecImgR, NDArray<T,2>& vecRealL, NDArray<T,2>& vecImgL) const
417  {
418  utl::geev_UtlMatrix(*this, valReal, valImg, vecRealR, vecImgR, vecRealL, vecImgL);
419  }
420 
421  NDArray<T,2> InverseSymmericMatrix(const double eps=1e-10)
422  {
423  return utl::InverseSymmericMatrix(*this, eps);
424  }
425 
426  NDArray<T,2> PInverseSymmericMatrix(const double eps=1e-10)
427  {
428  return utl::PInverseSymmericMatrix(*this, eps);
429  }
430 
431  NDArray<T,2> PInverseMatrix(const double eps=1e-10)
432  {
433  return utl::PInverseMatrix(*this, eps);
434  }
435 
436  NDArray<T,2> InverseMatrix(const double eps=1e-10)
437  {
438  return utl::InverseMatrix(*this, eps);
439  }
440 
441  T Determinant() const
442  {
443  utlException(!IsSquareMatrix(), "should be a square matrix");
444  utlException(this->m_Shape[0]==0, "should not be empty");
445  const T* p = this->m_Data;
446  switch ( this->m_Shape[0] )
447  {
448  case 1 : return p[0];
449  case 2 : return p[0]* p[3] - p[1]*p[2];
450  case 3 :
451  {
452  const T
453  a = p[0], d = p[1], g = p[2],
454  b = p[3], e = p[4], h = p[5],
455  c = p[6], f = p[7], i = p[8];
456  return i*a*e-a*h*f-i*b*d+b*g*f+c*d*h-c*g*e;
457  }
458  case 4 :
459  return
460  + p[0]*p[5]*p[10]*p[15]
461  - p[0]*p[5]*p[14]*p[11]
462  - p[0]*p[9]*p[6]*p[15]
463  + p[0]*p[9]*p[14]*p[7]
464  + p[0]*p[13]*p[6]*p[11]
465  - p[0]*p[13]*p[10]*p[7]
466  - p[4]*p[1]*p[10]*p[15]
467  + p[4]*p[1]*p[14]*p[11]
468  + p[4]*p[9]*p[2]*p[15]
469  - p[4]*p[9]*p[14]*p[3]
470  - p[4]*p[13]*p[2]*p[11]
471  + p[4]*p[13]*p[10]*p[3]
472  + p[8]*p[1]*p[6]*p[15]
473  - p[8]*p[1]*p[14]*p[7]
474  - p[8]*p[5]*p[2]*p[15]
475  + p[8]*p[5]*p[14]*p[3]
476  + p[8]*p[13]*p[2]*p[7]
477  - p[8]*p[13]*p[6]*p[3]
478  - p[12]*p[1]*p[6]*p[11]
479  + p[12]*p[1]*p[10]*p[7]
480  + p[12]*p[5]*p[2]*p[11]
481  - p[12]*p[5]*p[10]*p[3]
482  - p[12]*p[9]*p[2]*p[7]
483  + p[12]*p[9]*p[6]*p[3];
484  default :
485  {
486  utlException(true, "TODO use LU factorization for matrix");
487  return 0;
488  }
489  }
490  }
491 
492  bool IsSquareMatrix() const
493  {
494  return this->m_Shape[0] == this->m_Shape[1];
495  }
496 
497  bool IsSymmetric(const double eps=1e-10) const
498  {
499  if (this->m_Shape[0]!=this->m_Shape[1])
500  return false;
501  for ( int i = 0; i < this->m_Shape[0]; ++i )
502  for ( int j = 0; j < i; ++j )
503  {
504  T v1 = (*this)(i,j), v2 = (*this)(j,i);
505  if (std::abs(v1-v2)>eps*std::abs(v1))
506  return false;
507  }
508  return true;
509  }
510 
511  void Symmetrize()
512  {
513  utlException(this->m_Shape[0]!=this->m_Shape[1], "rows!=cols");
514  for ( int i = 0; i < this->m_Shape[0]; ++i )
515  for ( int j = 0; j < i; ++j )
516  {
517  T v1 = (*this)(i,j), v2 = (*this)(j,i);
518  (*this)(i,j) = (*this)(j,i) = (v1+v2)*0.5;
519  }
520  }
521 
524  {
525  NDArray<T,1> eigenValues;
526  NDArray<T,2> eigenVectors, result;
527 
528  EigenDecompositionSymmetricMatrix(eigenValues, eigenVectors);
529  for ( int i = 0; i < eigenValues.Size(); ++i )
530  eigenValues[i] = std::exp(eigenValues[i]);
531 
532  result = eigenVectors.GetTranspose() * eigenValues.GetDiagonalMatrix() * eigenVectors;
533  return result;
534  }
535 
538  {
539  NDArray<T,1> eigenValues;
540  NDArray<T,2> eigenVectors, result;
541 
542  EigenDecompositionSymmetricMatrix(eigenValues, eigenVectors);
543  for ( int i = 0; i < eigenValues.Size(); ++i )
544  {
545  utlSAException(eigenValues[i]<=0)(i)(eigenValues[i]).msg("negative eigenValue");
546  eigenValues[i] = std::log(eigenValues[i]);
547  }
548 
549  result = eigenVectors.GetTranspose() * eigenValues.GetDiagonalMatrix() * eigenVectors;
550  return result;
551  }
552 
553  double GetArrayOneNorm() const { return Superclass::GetOneNorm(); }
554  double GetArrayTwoNorm() const { return Superclass::GetTwoNorm(); }
555  double GetArrayInfNorm() const { return Superclass::GetInfNorm(); }
556  int GetArrayZeroNorm() const { return Superclass::GetZeroNorm(); }
557 
558  double GetTwoNorm() const {return GetArrayTwoNorm();}
559  double GetOneNorm() const
560  { return utl::lange(LAPACK_ROW_MAJOR, 'O', this->m_Shape[0], this->m_Shape[1], this->m_Data, this->m_Shape[1]); }
561  double GetInfNorm() const
562  { return utl::lange(LAPACK_COL_MAJOR, 'O', this->m_Shape[1], this->m_Shape[0], this->m_Data, this->m_Shape[1]); }
563 
564  void Save(const std::string& file) const
565  {
566  utl::SaveMatrix<Self>(*this, this->m_Shape[0], this->m_Shape[1], file);
567  }
568 
569 protected:
570 
571 private:
572 
573  // not used for matrix
574  // Reference operator()(unsigned int index) ;
575  // ConstReference operator()(unsigned int index) const ;
576 
577 }; // ----- end of template class Matrix -----
578 
579 
582 }
583 
584 #endif
const ValueType * ConstIterator
Definition: utlNDArray.h:171
NDArray is a N-Dimensional array class (row-major, c version)
Definition: utlFunctors.h:131
NDArray< T, 2 > GetColumns(const std::vector< int > &indexVec) const
Definition: utlMatrix.h:303
NDArray<T,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
NDArray< T, 2 > & SetRow(const int index, const NDArray< T, 1 > &vec)
Definition: utlMatrix.h:247
NDArray< T, 2 > & operator=(NDArray< T, 2 > &r)
Definition: utlMatrix.h:120
T & operator()(unsigned int row, unsigned int col)
Definition: utlMatrix.h:147
auto Conj(const ExprT &expr) -> decltype(utl::F< utl::Functor::Conj< typename ExprT::ValueType > >(expr))
Definition: utlFunctors.h:380
void GetRow(const int index, T *vec) const
Definition: utlMatrix.h:266
base class for expression
Definition: utlExpression.h:36
double GetInfNorm() const
Definition: utlNDArray.h:1006
SizeType m_Shape[Dimension]
Definition: utlNDArray.h:1168
double GetTwoNorm() const
Definition: utlMatrix.h:558
void GetColumn(const vnl_matrix< T > &mat, const int index, vnl_vector< T > &v1)
Definition: utlVNLBlas.h:238
NDArray< T, 2 > SetIdentity()
Definition: utlMatrix.h:181
void gesdd_UtlMatrix(const NDArray< T, 2 > &mat, NDArray< T, 2 > &U, NDArray< utl::remove_complex_t< T >, 1 > &s, NDArray< T, 2 > &V, char format='S')
dgesdd_UtlMatrix dgesdd is faster than dgesvd. http://www.netlib.org/lapack/explore-html/db/db4/dgesd...
T Determinant() const
Definition: utlMatrix.h:441
NDArray(const Expr< EType, typename EType::ValueType > &expr)
Definition: utlMatrix.h:85
void SVD(NDArray< T, 2 > &U, NDArray< ScalarValueType, 1 > &S, NDArray< T, 2 > &V, char format='S') const
SVD.
Definition: utlMatrix.h:378
NDArray(const ShapeType &shape)
Definition: utlMatrix.h:106
NDArray< T, 2 > & SetColumn(const int index, T const *vec)
Definition: utlMatrix.h:253
NDArray(const NDArray< T, 2 > &mat)
Definition: utlMatrix.h:76
SizeType Columns() const
Definition: utlMatrix.h:137
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
NDArray< T, 2 > GetTranspose(const T scale=1.0) const
Definition: utlMatrix.h:205
SizeType Rows() const
Definition: utlMatrix.h:136
static constexpr SizeType SubDimension
Definition: utlNDArray.h:162
Superclass::ShapeType ShapeType
Definition: utlMatrix.h:47
NDArray< T, 2 > & SetDiagonal(const NDArray< T, 1 > &vec)
Definition: utlMatrix.h:164
NDArray< T, 2 > ExpM()
Definition: utlMatrix.h:523
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
bool ReSize(const ShapeType &shape)
Definition: utlNDArray.h:697
Superclass::ConstIterator ConstIterator
Definition: utlMatrix.h:53
const ValueType & ConstReference
Definition: utlNDArray.h:179
Superclass::Iterator Iterator
Definition: utlMatrix.h:52
Superclass::Reference Reference
Definition: utlMatrix.h:50
Superclass::ConstReference ConstReference
Definition: utlMatrix.h:51
double GetTwoNorm() const
Definition: utlNDArray.h:1013
NDArray< T, 2 > PInverseSymmericMatrix(const double eps=1e-10)
Definition: utlMatrix.h:426
void SetData(T *const data, const ShapeType &shape)
Definition: utlNDArray.h:677
NDArray< T, Dim > & operator=(const NDArray< T, Dim > &r)
Definition: utlNDArray.h:116
NDArray< T, 2 > & SetRow(const int index, T const *vec)
Definition: utlMatrix.h:241
const T & operator()(unsigned int row, unsigned int col) const
Definition: utlMatrix.h:149
NDArray< T, 2 > PInverseSymmericMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
double GetOneNorm() const
Definition: utlMatrix.h:559
NDArray(const SizeType rows, const SizeType columns)
Definition: utlMatrix.h:70
double GetArrayTwoNorm() const
Definition: utlMatrix.h:554
double GetOneNorm() const
Definition: utlNDArray.h:1028
NDArrayBase< T, 2 > Superclass
Definition: utlMatrix.h:41
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
void EigenDecompositionNonSymmetricMatrix(NDArray< T, 1 > &valReal, NDArray< T, 1 > &valImg, NDArray< T, 2 > &vecRealR, NDArray< T, 2 > &vecImgR) const
Definition: utlMatrix.h:412
#define UTL_ALWAYS_INLINE
Definition: utlCoreMacro.h:76
NDArray< T, 2 > InverseMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
void GetDiagonal(NDArray< T, 1 > &vec) const
Definition: utlMatrix.h:173
NDArray< T, 2 > & SetRows(const std::vector< int > &indexVec, const NDArray< T, 2 > &mat)
Definition: utlMatrix.h:321
T abs(const T x)
template version of the fabs function
NDArray(const T *vec, const ShapeType &shape)
Definition: utlMatrix.h:113
Base class for utl::NDArray.
Definition: utlMatrix.h:22
NDArray< T, 2 > & FillDiagonal(const T val)
Definition: utlMatrix.h:155
Definition: utl.h:90
Superclass::ValueType ValueType
Definition: utlMatrix.h:43
NDArray< T, 2 > & SetColumn(const int index, const NDArray< T, 1 > &vec)
Definition: utlMatrix.h:259
Superclass::SizeType SizeType
Definition: utlNDArray.h:154
void ComputeOffSetTable()
Definition: utlNDArray.h:1190
void Fill(const T &value)
Definition: utlNDArray.h:922
NDArray< T, 2 > PInverseMatrix(const double eps=1e-10)
Definition: utlMatrix.h:431
NDArray< T, 2 > GetConjugateTranspose(const T scale=1.0) const
Definition: utlMatrix.h:219
SizeType Cols() const
Definition: utlMatrix.h:138
void CopyData(T *const data, const unsigned rows, const unsigned cols)
Definition: utlMatrix.h:198
Superclass::ConstPointer ConstPointer
Definition: utlMatrix.h:49
NDArray< T, 2 > PInverseMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
#define utlSAException(expr)
Definition: utlCoreMacro.h:543
NDArray(const ShapeType &shape, const T r)
Definition: utlMatrix.h:118
void Swap(NDArrayBase< T, Dim > &vec)
Definition: utlNDArray.h:1062
void EigenDecompositionSymmetricMatrix(NDArray< T, 1 > &eigenValues, NDArray< T, 2 > &eigenVectors) const
Eigen-decomposition for symmetric matrix.
Definition: utlMatrix.h:396
void GetDiagonalMatrix(NDArray< T, 2 > &mat) const
Definition: utlVector.h:264
double GetArrayInfNorm() const
Definition: utlMatrix.h:555
Superclass::ScalarValueType ScalarValueType
Definition: utlMatrix.h:44
NDArray< T, 2 > GetNColumns(const int index0, const int N) const
Definition: utlMatrix.h:314
int GetArrayZeroNorm() const
Definition: utlMatrix.h:556
Superclass::SizeType SizeType
Definition: utlMatrix.h:46
bool IsSquareMatrix() const
Definition: utlMatrix.h:492
NDArray< T, 2 > InverseSymmericMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
void GetColumn(const int index, T *vec) const
Definition: utlMatrix.h:277
NDArray< T, 2 > GetCrop(const int x0, const int x1, const int y0, const int y1) const
Definition: utlMatrix.h:348
const ValueType * ConstPointer
Definition: utlNDArray.h:175
NDArray< T, 2 > & SetColumns(const std::vector< int > &indexVec, const NDArray< T, 2 > &mat)
Definition: utlMatrix.h:331
bool IsSymmetric(const double eps=1e-10) const
Definition: utlMatrix.h:497
T lange(int matrix_order, char norm, int m, int n, const T *A, int LDA)
NDArray(const T *vec, const SizeType rows, const SizeType columns)
Definition: utlMatrix.h:87
NDArray< T, 2 > LogM()
Definition: utlMatrix.h:537
void EigenDecompositionNonSymmetricMatrix(NDArray< T, 1 > &valReal, NDArray< T, 1 > &valImg) const
Definition: utlMatrix.h:408
Superclass::Pointer Pointer
Definition: utlMatrix.h:48
NDArray< T, 1 > GetColumn(const int index) const
Definition: utlMatrix.h:282
void EigenDecompositionNonSymmetricMatrix(NDArray< T, 1 > &valReal, NDArray< T, 1 > &valImg, NDArray< T, 2 > &vecRealR, NDArray< T, 2 > &vecImgR, NDArray< T, 2 > &vecRealL, NDArray< T, 2 > &vecImgL) const
Definition: utlMatrix.h:416
NDArray< T, 1 > GetRow(const int index) const
Definition: utlMatrix.h:271
double GetInfNorm() const
Definition: utlMatrix.h:561
NDArrayBase< T, Dim > & Scale(const T a)
Definition: utlNDArray.h:873
void Save(const std::string &file) const
Definition: utlMatrix.h:564
NDArray(const SizeType rows, const SizeType columns, const T r)
Definition: utlMatrix.h:94
NDArray< T, 2 > GetNRows(const int index0, const int N) const
Definition: utlMatrix.h:296
NDArray< T, 2 > & SetCrop(const int x0, const int x1, const int y0, const int y1, const NDArray< T, 2 > &mat)
Definition: utlMatrix.h:359
double GetArrayOneNorm() const
Definition: utlMatrix.h:553
void geev_UtlMatrix(const utl::NDArray< T, 2 > &mat, utl::NDArray< T, 1 > &valReal, utl::NDArray< T, 1 > &valImg, utl::NDArray< T, 2 > &vecRealR, utl::NDArray< T, 2 > &vecImgR, utl::NDArray< T, 2 > &vecRealL, utl::NDArray< T, 2 > &vecImgL)
geev_UtlMatrix calculate non-symmetric eigen-decomposition.
SizeType Size() const
Definition: utlNDArray.h:321
bool ReSize(const SizeType rows, const SizeType cols)
Definition: utlMatrix.h:140
int GetZeroNorm(const double eps=1e-10) const
Definition: utlNDArray.h:1034
NDArrayBase< T, Dim > & operator=(const Expr< EType, typename EType::ValueType > &src)
Definition: utlNDArray.h:385
utl::remove_complex_t< T > ScalarValueType
Definition: utlNDArray.h:165
bool ReSize(const SizeType size)
Definition: utlVector.h:167
NDArray< T, 2 > InverseMatrix(const double eps=1e-10)
Definition: utlMatrix.h:436
void SetData(T *const data, const unsigned rows, const unsigned cols)
Definition: utlMatrix.h:190
NDArray< T, 2 > InverseSymmericMatrix(const double eps=1e-10)
Definition: utlMatrix.h:421
NDArray(const NDArray< TMatrixValueType, 2 > &r)
Definition: utlMatrix.h:103
Superclass::ShapeType ShapeType
Definition: utlNDArray.h:155
NDArray< T, 2 > & operator=(NDArray< T, 2 > &&r)
Definition: utlMatrix.h:126
NDArray< T, 2 > GetRows(const std::vector< int > &indexVec) const
Definition: utlMatrix.h:289
#define __utl_ndarray_alloc_blah(shape)
Definition: utlNDArray.h:30
NDArray< T, 2 > & TransposeInplace(const T scale=1.0)
Definition: utlMatrix.h:226
void CopyData(T *const data, const ShapeType &shape)
Definition: utlNDArray.h:689
void GetRow(const vnl_matrix< T > &mat, const int index, vnl_vector< T > &v1)
Definition: utlVNLBlas.h:229
NDArray(NDArray< T, 2 > &&mat)
Definition: utlMatrix.h:79