18 template <
class T,
unsigned int Dim >
21 template <
class T,
unsigned int Dim >
61 using Superclass::operator();
62 using Superclass::operator=;
63 using Superclass::operator+=;
64 using Superclass::operator-=;
65 using Superclass::operator%=;
66 using Superclass::operator/=;
70 explicit NDArray(
const SizeType rows,
const SizeType columns) : Superclass()
73 shape[0]=rows, shape[1]=columns;
84 template<
typename EType>
87 NDArray(
const T* vec,
const SizeType rows,
const SizeType columns) : Superclass()
90 shape[0]=rows, shape[1]=columns;
92 utl::cblas_copy<T>(this->
Size(), vec, 1, this->
m_Data, 1);
94 NDArray(
const SizeType rows,
const SizeType columns,
const T r) : Superclass()
97 shape[0]=rows, shape[1]=columns;
102 template<
typename TMatrixValueType >
106 explicit NDArray(
const ShapeType& shape) : Superclass(shape) { }
113 NDArray(
const T* vec,
const ShapeType& shape) : Superclass(vec,shape) { }
118 NDArray(
const ShapeType& shape,
const T r) : Superclass(shape, r) { }
143 shape[0]=rows, shape[1]=cols;
158 for (
int i = 0; i < min_MN; ++i )
168 for (
int i = 0; i < min_MN; ++i )
169 (*
this)(i,i) = vec[i];
177 for (
int i = 0; i < min_MN; ++i )
178 vec[i] = (*
this)(i,i);
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;
190 void SetData( T*
const data,
const unsigned rows,
const unsigned cols )
193 shape[0]=rows, shape[1]=cols;
198 void CopyData(T*
const data,
const unsigned rows,
const unsigned cols )
201 shape[0]=rows, shape[1]=cols;
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]);
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];
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]);
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;
244 utl::cblas_copy<T>(this->
m_Shape[1],vec,1,this->
m_Data+this->m_Shape[1]*index,1);
256 utl::cblas_copy<T>(this->
m_Shape[0],vec,1,this->
m_Data+index,this->m_Shape[1]);
262 SetColumn(index, vec.
GetData());
266 void GetRow(
const int index, T* vec)
const 269 utl::cblas_copy<T>(this->
m_Shape[1],this->
m_Data+this->m_Shape[1]*index,1,vec,1);
280 utl::cblas_copy<T>(this->
m_Shape[0],this->
m_Data+index,this->m_Shape[1],vec,1);
292 for (
int i = 0; i < indexVec.size(); ++i )
293 GetRow(indexVec[i], mat.m_Data+i*this->m_Shape[1]);
298 std::vector<int> indexVec;
299 for (
int i = 0; i < N; ++i )
300 indexVec.push_back(index0+i);
301 return GetRows(indexVec);
307 for (
int i = 0; i < indexVec.size(); ++i )
310 mat.SetColumn(i, vec);
316 std::vector<int> indexVec;
317 for (
int i = 0; i < N; ++i )
318 indexVec.push_back(index0+i);
319 return GetColumns(indexVec);
324 for (
int i = 0; i < indexVec.size(); ++i )
327 SetRow(indexVec[i], vec);
334 for (
int i = 0; i < indexVec.size(); ++i )
337 SetColumn(indexVec[i], vec);
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);
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);
380 #ifdef UTL_USE_FASTLAPACK 385 utl::gesvd_UtlMatrix<T>(*
this, U, S, V, format);
399 #ifdef UTL_USE_FASTLAPACK 401 syevd_UtlMatrix<T>(*
this, eigenValues, eigenVectors);
404 syev_UtlMatrix<T>(*
this, eigenValues, eigenVectors);
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 443 utlException(!IsSquareMatrix(),
"should be a square matrix");
445 const T* p = this->
m_Data;
448 case 1 :
return p[0];
449 case 2 :
return p[0]* p[3] - p[1]*p[2];
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;
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];
486 utlException(
true,
"TODO use LU factorization for matrix");
501 for (
int i = 0; i < this->
m_Shape[0]; ++i )
502 for (
int j = 0; j < i; ++j )
504 T v1 = (*this)(i,j), v2 = (*
this)(j,i);
514 for (
int i = 0; i < this->
m_Shape[0]; ++i )
515 for (
int j = 0; j < i; ++j )
517 T v1 = (*this)(i,j), v2 = (*
this)(j,i);
518 (*this)(i,j) = (*
this)(j,i) = (v1+v2)*0.5;
528 EigenDecompositionSymmetricMatrix(eigenValues, eigenVectors);
529 for (
int i = 0; i < eigenValues.
Size(); ++i )
530 eigenValues[i] = std::exp(eigenValues[i]);
542 EigenDecompositionSymmetricMatrix(eigenValues, eigenVectors);
543 for (
int i = 0; i < eigenValues.
Size(); ++i )
545 utlSAException(eigenValues[i]<=0)(i)(eigenValues[i]).msg(
"negative eigenValue");
546 eigenValues[i] = std::log(eigenValues[i]);
564 void Save(
const std::string& file)
const 566 utl::SaveMatrix<Self>(*
this, this->
m_Shape[0], this->m_Shape[1], file);
const ValueType * ConstIterator
NDArray is a N-Dimensional array class (row-major, c version)
NDArray< T, 2 > GetColumns(const std::vector< int > &indexVec) const
NDArray<T,1> is a vector class which uses blas mkl.
NDArray< T, 2 > & SetRow(const int index, const NDArray< T, 1 > &vec)
NDArray< T, 2 > & operator=(NDArray< T, 2 > &r)
T & operator()(unsigned int row, unsigned int col)
auto Conj(const ExprT &expr) -> decltype(utl::F< utl::Functor::Conj< typename ExprT::ValueType > >(expr))
void GetRow(const int index, T *vec) const
base class for expression
double GetInfNorm() const
SizeType m_Shape[Dimension]
double GetTwoNorm() const
void GetColumn(const vnl_matrix< T > &mat, const int index, vnl_vector< T > &v1)
NDArray< T, 2 > SetIdentity()
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...
NDArray(const Expr< EType, typename EType::ValueType > &expr)
void SVD(NDArray< T, 2 > &U, NDArray< ScalarValueType, 1 > &S, NDArray< T, 2 > &V, char format='S') const
SVD.
NDArray(const ShapeType &shape)
NDArray< T, 2 > & SetColumn(const int index, T const *vec)
NDArray(const NDArray< T, 2 > &mat)
#define utlException(cond, expout)
NDArray< T, 2 > GetTranspose(const T scale=1.0) const
static constexpr SizeType SubDimension
Superclass::ShapeType ShapeType
NDArray< T, 2 > & SetDiagonal(const NDArray< T, 1 > &vec)
NDArray<T,2> is a row-major matrix.
bool ReSize(const ShapeType &shape)
Superclass::ConstIterator ConstIterator
const ValueType & ConstReference
Superclass::Iterator Iterator
Superclass::Reference Reference
Superclass::ConstReference ConstReference
double GetTwoNorm() const
NDArray< T, 2 > PInverseSymmericMatrix(const double eps=1e-10)
void SetData(T *const data, const ShapeType &shape)
NDArray< T, Dim > & operator=(const NDArray< T, Dim > &r)
NDArray< T, 2 > & SetRow(const int index, T const *vec)
const T & operator()(unsigned int row, unsigned int col) const
NDArray< T, 2 > PInverseSymmericMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
double GetOneNorm() const
NDArray(const SizeType rows, const SizeType columns)
double GetArrayTwoNorm() const
double GetOneNorm() const
NDArrayBase< T, 2 > Superclass
const T & min(const T &a, const T &b)
Return the minimum between a and b.
void EigenDecompositionNonSymmetricMatrix(NDArray< T, 1 > &valReal, NDArray< T, 1 > &valImg, NDArray< T, 2 > &vecRealR, NDArray< T, 2 > &vecImgR) const
#define UTL_ALWAYS_INLINE
NDArray< T, 2 > InverseMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
void GetDiagonal(NDArray< T, 1 > &vec) const
NDArray< T, 2 > & SetRows(const std::vector< int > &indexVec, const NDArray< T, 2 > &mat)
T abs(const T x)
template version of the fabs function
NDArray(const T *vec, const ShapeType &shape)
Base class for utl::NDArray.
NDArray< T, 2 > & FillDiagonal(const T val)
Superclass::ValueType ValueType
NDArray< T, 2 > & SetColumn(const int index, const NDArray< T, 1 > &vec)
Superclass::SizeType SizeType
void ComputeOffSetTable()
void Fill(const T &value)
NDArray< T, 2 > PInverseMatrix(const double eps=1e-10)
NDArray< T, 2 > GetConjugateTranspose(const T scale=1.0) const
void CopyData(T *const data, const unsigned rows, const unsigned cols)
Superclass::ConstPointer ConstPointer
NDArray< T, 2 > PInverseMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
#define utlSAException(expr)
NDArray(const ShapeType &shape, const T r)
void Swap(NDArrayBase< T, Dim > &vec)
void EigenDecompositionSymmetricMatrix(NDArray< T, 1 > &eigenValues, NDArray< T, 2 > &eigenVectors) const
Eigen-decomposition for symmetric matrix.
void GetDiagonalMatrix(NDArray< T, 2 > &mat) const
double GetArrayInfNorm() const
Superclass::ScalarValueType ScalarValueType
NDArray< T, 2 > GetNColumns(const int index0, const int N) const
int GetArrayZeroNorm() const
Superclass::SizeType SizeType
bool IsSquareMatrix() const
NDArray< T, 2 > InverseSymmericMatrix(const NDArray< T, 2 > &mat, const double eps=1e-10)
void GetColumn(const int index, T *vec) const
NDArray< T, 2 > GetCrop(const int x0, const int x1, const int y0, const int y1) const
const ValueType * ConstPointer
NDArray< T, 2 > & SetColumns(const std::vector< int > &indexVec, const NDArray< T, 2 > &mat)
bool IsSymmetric(const double eps=1e-10) const
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)
void EigenDecompositionNonSymmetricMatrix(NDArray< T, 1 > &valReal, NDArray< T, 1 > &valImg) const
Superclass::Pointer Pointer
NDArray< T, 1 > GetColumn(const int index) const
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
NDArray< T, 1 > GetRow(const int index) const
double GetInfNorm() const
NDArrayBase< T, Dim > & Scale(const T a)
void Save(const std::string &file) const
NDArray(const SizeType rows, const SizeType columns, const T r)
NDArray< T, 2 > GetNRows(const int index0, const int N) const
NDArray< T, 2 > & SetCrop(const int x0, const int x1, const int y0, const int y1, const NDArray< T, 2 > &mat)
double GetArrayOneNorm() const
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.
bool ReSize(const SizeType rows, const SizeType cols)
int GetZeroNorm(const double eps=1e-10) const
NDArrayBase< T, Dim > & operator=(const Expr< EType, typename EType::ValueType > &src)
utl::remove_complex_t< T > ScalarValueType
bool ReSize(const SizeType size)
NDArray< T, 2 > InverseMatrix(const double eps=1e-10)
void SetData(T *const data, const unsigned rows, const unsigned cols)
NDArray< T, 2 > InverseSymmericMatrix(const double eps=1e-10)
NDArray(const NDArray< TMatrixValueType, 2 > &r)
Superclass::ShapeType ShapeType
NDArray< T, 2 > & operator=(NDArray< T, 2 > &&r)
NDArray< T, 2 > GetRows(const std::vector< int > &indexVec) const
#define __utl_ndarray_alloc_blah(shape)
NDArray< T, 2 > & TransposeInplace(const T scale=1.0)
void CopyData(T *const data, const ShapeType &shape)
void GetRow(const vnl_matrix< T > &mat, const int index, vnl_vector< T > &v1)
NDArray(NDArray< T, 2 > &&mat)