10 #ifndef __utlNDArray_h 11 #define __utlNDArray_h 30 #define __utl_ndarray_alloc_blah(shape) \ 32 for ( int i = 0; i < this->Dimension; ++i ) \ 33 this->m_Shape[i] = shape[i]; \ 34 this->ComputeOffSetTable(); \ 35 SizeType size = this->GetSize(); \ 36 this->m_Data = (size>0) ? (new T[size]) : NULL; \ 37 this->m_IsShared = false; \ 41 template <
class T,
unsigned int Dim>
class NDArray;
42 template <
class T,
unsigned int Dim>
class NDArrayBase;
60 template <
class T,
unsigned int Dim >
84 using Superclass::operator=;
89 explicit NDArray(
const ShapeType& shape) : Superclass(shape) { }
98 template<
typename EType>
106 NDArray(
const T* vec,
const ShapeType& shape) : Superclass(vec,shape) { }
111 NDArray(
const ShapeType& shape,
const T r) : Superclass(shape, r) { }
113 template<
typename TValue >
141 template <
class T,
unsigned int Dim >
144 static_assert(::std::is_scalar<T>::value
145 || ::std::is_same<T,std::complex<double>>::value
146 || ::std::is_same<T,std::complex<float>>::value,
"NDArrayBase<T,Dim>: T must be a scalar or std::complex<double> or std::complex<float>");
181 class ConstReverseIterator;
195 ValueType &
operator*()
const {
return *( m_Iterator - 1 ); }
216 ConstIterator
operator->()
const {
return ( m_Iterator - 1 ); }
217 const ValueType &
operator*()
const {
return *( m_Iterator - 1 ); }
254 template<
typename EType>
270 utl::cblas_copy<T>(this->
Size(), vec, 1,
m_Data, 1);
282 template<
typename TValue >
343 index += shapeIndex[Dimension-1];
350 for (
int i = 1; i < Dim; i++ )
355 index[Dimension-1] = nn;
369 #define __Array_Saver_Expr(saver, saverReal) \ 370 template<typename EType> \ 371 UTL_ALWAYS_INLINE NDArrayBase<T,Dim>& operator saver (const Expr<EType, typename EType::ValueType>& src){ \ 372 auto srcDim = Expr<EType, typename EType::ValueType>::GetDimension(); \ 373 utlSAGlobalException(srcDim>0 && srcDim!=Dimension) \ 374 (srcDim)(Dimension).msg("the expression has a difference size"); \ 375 const EType &r = src.ConstRef(); \ 376 const ShapeType rShape = r.GetShape(); \ 378 this->ReSize(rShape); \ 379 for( int i=0; i < this->Size(); ++i ) \ 380 this->m_Data[i] saverReal r.Eval(i); \ 392 #define __Array_Saver_Scalar(saver) \ 393 inline NDArrayBase<T,Dim>& operator saver (const T val){ \ 394 for (Iterator p = Begin(); p!= End(); ++p) \ 429 template<
typename TValueType >
435 while ( i != this->
End() )
436 *i++ =
static_cast< T
>( *input++ );
442 if (r.size()==
Size())
452 utlException(Dim!=1,
"should have only 1 dimension, or have the same size.");
466 if (r.size()==
Size())
476 utlException(Dim!=1,
"should have only 1 dimension, or have the same size.");
481 {
Fill(r);
return *
this; }
492 ConstIterator i = this->
Begin();
493 ConstIterator j = r.
Begin();
495 while ( i != this->
End() )
514 ConstIterator i = this->
Begin();
515 ConstIterator j = r.
Begin();
517 while ( i != this->
End() )
533 ConstIterator i = this->
Begin();
534 ConstIterator j = r.
Begin();
536 while ( i != this->
End() )
557 #if defined( __GNUC__ ) 558 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) 559 #pragma GCC diagnostic push 560 #pragma GCC diagnostic ignored "-Warray-bounds" 565 #if defined( __GNUC__ ) 566 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) 567 #pragma GCC diagnostic pop 596 ConstIterator
Begin()
const 600 ConstIterator
cBegin()
const 616 ConstIterator
End()
const 620 ConstIterator
cEnd()
const 636 ConstReverseIterator
rBegin()
const 640 ConstReverseIterator
crBegin()
const 648 ReverseIterator
rEnd()
656 ConstReverseIterator
rEnd()
const 658 return ConstReverseIterator(
m_Data);
667 {
m_Data[index] = value; }
668 ConstReference
GetElement(
unsigned short index)
const 683 void SetData( T*
const data,
const ShapeType& shape )
685 utlException(data==
m_Data,
"cannot set itself. Please use ReShape() for change the shape");
689 for (
int i = 0; i < Dim; ++i )
695 void CopyData(T*
const data,
const ShapeType& shape)
730 SizeType newSize = std::accumulate(shape, shape+Dim, 0);
732 for (
int i = 0; i < Dim; ++i )
762 for (
int jj = 0; jj < Dim; ++jj )
764 shape[0]= iend-istart+1;
769 shape[0]= iend-istart+1;
776 template<
typename EType>
785 const ShapeType rShape = r.GetShape();
809 return Size() == std::accumulate(shape, shape+Dim, 0);
942 void CopyOut(T * ptr,
const int size,
const int start=0) const
950 template<
typename FuncT>
954 for (SizeType i = 0; i <
Size(); ++i)
960 for (SizeType i = 0; i <
Size(); ++i)
966 for (SizeType i = 0; i <
Size(); ++i)
992 return utl::cblas_iamax<T>(
Size(),
m_Data, 1);
1063 const unsigned int N = vec.
Size();
1078 for (
unsigned i=0; i<
Size()/2; i++)
1086 for (
unsigned i = 0; i <
Size();++i)
1087 if ( !(
m_Data[i] == zero) )
1093 {
return Size()==0; }
1096 int NNZ(
const double eps=1e-10)
const 1099 for (
int i = 0; i<
Size(); ++i)
1109 for (
int i = 0; i<
Size(); ++i)
1111 if (
m_Data[i] > threshold)
1113 else if (
m_Data[i] < -threshold)
1122 for (
int i = 0; i<
Size(); ++i)
1124 if (!(
m_Data[i] > threshold ||
m_Data[i] < -threshold))
1133 PrintInfo(std::ostream & os,
const char* separate=
" ")
const 1141 Print(std::ostream & os,
const char* separate=
" ")
const 1148 PrintWithIndex(std::ostream & os,
const char* separate=
" ")
const 1154 for (
int i = 0; i <
Size(); ++i )
1157 std::ostringstream oss;
1166 os << oss.str() <<
"[ " <<
m_Data[i] <<
" ];"<< std::endl;
1193 m_Shape[i]=0, m_OffSetTable[i]=0;
1198 for (
int i = Dimension-1; i >= 0; i-- )
1201 m_OffSetTable[i]=m_Shape[i];
1203 m_OffSetTable[i]=m_Shape[i]*m_OffSetTable[i+1];
const ValueType * ConstIterator
NDArray is a N-Dimensional array class (row-major, c version)
ConstReverseIterator crBegin() const
NDArrayBase< T, Dim > & Flip()
NDArrayBase< T, Dim > GetElementAbsolute() const
NDArray<T,1> is a vector class which uses blas mkl.
NDArrayBase< T, Dim > & ElementCos(T *outVec=NULL)
void PrintContainer(IteratorType v1, IteratorType v2, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
typename remove_complex< T >::type remove_complex_t
Reference operator[](short index)
void vMul(int n, T *vecIn, T *vecIn2, T *vecOut)
interface to v*Mul
unsigned int argmin(Iterator i1, Iterator i2)
T AbsoluteMaxValue() const
T min_element(const std::vector< T > &v)
void vSub(int n, T *vecIn, T *vecIn2, T *vecOut)
interface to v*Sub
Reference operator[](unsigned long long index)
Superclass::SizeType SizeType
void cblas_copy(const INTT N, const T *X, const INTT incX, T *Y, const INTT incY)
base class for expression
NDArrayBase< T, Dim > & ElementSubstract(T *const vec, T *outVec=NULL)
#define PrintVar1(cond, var, os)
NDArrayBase< T, Dim > & ElementSquare(T *outVec=NULL)
void vSqr(int n, T *vecIn, T *vecOut)
interface to v*Sqr
double GetInfNorm() const
SizeType m_Shape[Dimension]
Iterator operator->() const
NDArrayBase< T, Dim > & ElementMultiply(T *const vec, T *outVec=NULL)
void Print(std::ostream &os, const char *separate=" ") const
bool IsSameShape(const EType &src) const
NDArrayBase< T, Dim > & CopyIn(T const *ptr, const int size, const int start=0)
NDArray(const Expr< EType, typename EType::ValueType > &expr)
const ValueType & operator*() const
void CopyOut(T *ptr, const int size, const int start=0) const
Superclass::ConstPointer ConstPointer
NDArrayBase< T, Dim > & ElementSin(T *outVec=NULL)
NDArrayBase< T, Dim > & ElementInverse(T *outVec=NULL)
void vAbs(int n, T *vecIn, T *vecOut)
interface to v*Abs
Reference operator[](long index)
#define __Array_Saver_Expr(saver, saverReal)
ValueType & operator*() const
#define utlException(cond, expout)
ConstReverseIterator(ConstIterator i)
NDArrayBase< T, Dim > & operator%=(const Expr< EType, typename EType::ValueType > &src)
static constexpr SizeType SubDimension
bool operator==(const ConstReverseIterator &rit) const
NDArray(NDArray< T, Dim > &&vec)
void vAdd(int n, T *vecIn, T *vecIn2, T *vecOut)
interface to v*Add
T AbsoluteMinValue() const
bool IsSameValues(const NDArrayBase< T, Dim > &r, const double eps) const
NDArray<T,2> is a row-major matrix.
void cblas_axpby(const INTT N, const T alpha, const T *X, const INTT incX, const T beta, T *Y, const INTT incY)
bool ReSize(const ShapeType &shape)
void vSin(int n, T *vecIn, T *vecOut)
interface to v*Sin
void HardThreshold(const double threshold)
bool IsSameSize(const ShapeType &shape) const
ConstIterator operator--(int)
NDArrayBase< T, Dim > & operator+=(const Expr< EType, typename EType::ValueType > &src)
NDArrayBase< T, Dim > & ElementExp(T *outVec=NULL)
const ValueType & ConstReference
NDArrayBase< T, Dim > GetElementSqrt() const
static SizeType GetDimension()
double GetTwoNorm() const
NDArray(const NDArray< TValue, Dim > &r)
NDArrayBase(const NDArrayBase< TValue, Dim > &r)
bool operator==(const NDArrayBase< T, Dim > &r) const
NDArrayBase< T, Dim > & ElementDivide(T *const vec, T *outVec=NULL)
void SetData(T *const data, const ShapeType &shape)
NDArray< T, Dim > & operator=(const NDArray< T, Dim > &r)
unsigned int argmax(Iterator i1, Iterator i2)
unsigned ArgAbsoluteMin() const
SizeType const * ShapeType
NDArray(const ShapeType &shape)
unsigned ArgAbsoluteMax() const
bool IsEqual(const NDArrayBase< T, Dim > &r, const double eps) const
NDArray< T, SubDimension > GetRefSubArray(const int i) const
void SetElement(unsigned short index, ConstReference value)
NDArray(const NDArray< T, Dim > &vec)
Superclass::ShapeType ShapeType
double GetOneNorm() const
NDArrayBase< T, Dim > GetElementSquare() const
#define UTL_ALWAYS_INLINE
NDArrayBase(const T *vec, const ShapeType &shape)
ConstIterator cBegin() const
SizeType m_OffSetTable[Dimension]
T abs(const T x)
template version of the fabs function
Base class for utl::NDArray.
void PrintInfo(std::ostream &os, const char *separate=" ") const
NDArrayBase< T, Dim > GetElementCos() const
ConstReference GetElement(unsigned short index) const
Superclass::Iterator Iterator
ConstReverseIterator crEnd() const
double GetSquaredTwoNorm() const
void SoftThreshold(const double threshold)
Superclass::SizeType SizeType
void ComputeOffSetTable()
void Fill(const T &value)
SizeType GetOffset(const ShapeType &shapeIndex) const
void Apply(T(*f)(T const &), NDArrayBase< T, Dim > &vec) const
A const reverse iterator through an array.
ReverseIterator(Iterator i)
void vSqrt(int n, T *vecIn, T *vecOut)
interface to v*Sqrt
bool operator==(const ReverseIterator &rit) const
bool operator!=(const NDArrayBase< T, Dim > &r) const
#define utlSAException(expr)
void Apply(T(*f)(T), NDArrayBase< T, Dim > &vec) const
Reference operator()(const ShapeType &shape)
NDArrayBase(const NDArrayBase< T, Dim > &vec)
void Swap(NDArrayBase< T, Dim > &vec)
NDArrayBase(const Expr< EType, typename EType::ValueType > &expr)
int NNZ(const double eps=1e-10) const
NDArrayBase< T, Dim > & ElementSqrt(T *outVec=NULL)
NDArrayBase< T, Dim > & operator-=(const Expr< EType, typename EType::ValueType > &src)
Superclass::ConstReference ConstReference
NDArrayBase< T, Dim > & ReShape(const ShapeType &shape)
T cblas_asum(const INTT N, const T *X, const INTT incX)
int cblas_iamin(const INTT N, const T *X, const INTT incX)
interface to cblas_i*amin
Expr< NDArrayBase< T, Dim >, T > Superclass
void vCos(int n, T *vecIn, T *vecOut)
interface to v*Cos
NDArrayBase< T, Dim > & ElementAdd(T *const vec, T *outVec=NULL)
void vExp(int n, T *vecIn, T *vecOut)
interface to v*Exp
const NDArrayBase< T, Dim > & ConstRef(void) const
const SizeType * GetOffSetTable() const
void PrintWithIndex(std::ostream &os, const char *separate=" ") const
utl::NDArray<T,2> class which uses blas mkl
NDArray(const T *vec, const ShapeType &shape)
void Apply(const FuncT &func, NDArrayBase< T, Dim > &vec) const
const ValueType * ConstPointer
NDArrayBase< T, Dim > & operator/=(const Expr< EType, typename EType::ValueType > &src)
ConstReverseIterator(const ReverseIterator &rit)
NDArrayBase< T, Dim > GetElementSin() const
NDArray< T, Dim > & operator=(NDArray< T, Dim > &&r)
ValueType InnerProduct(const NDArrayBase< T, Dim > &vec) const
ConstIterator operator--()
static constexpr SizeType GetDimension()
NDArrayBase< T, Dim > GetElementInverse() const
NDArrayBase< T, Dim > & ElementAbsolute(T *outVec=NULL)
void GetIndex(const SizeType offset, SizeType index[Dimension]) const
A reverse iterator through an array.
bool operator!=(const ReverseIterator &rit) const
NDArrayBase< T, Dim > & Scale(const T a)
ConstReference operator[](unsigned long long index) const
double InnerProduct(const TVector1 &v1, const TVector2 &v2, const int N1)
__Array_Saver_Scalar(+=) __Array_Saver_Scalar(-
NDArrayBase< T, Dim > Superclass
ConstIterator operator++()
utl::NDArray<T,1> class which uses blas mkl
const ShapeType GetShape() const
NDArrayBase(const ShapeType &shape)
NDArrayBase(NDArrayBase< T, Dim > &&vec)
void vDiv(int n, T *vecIn, T *vecIn2, T *vecOut)
interface to v*Div
#define utlOSPrintVar(cond, os,...)
double GetRootMeanSquares() const
Superclass::Pointer Pointer
int GetZeroNorm(const double eps=1e-10) const
NDArrayBase< T, Dim > & operator=(const Expr< EType, typename EType::ValueType > &src)
Superclass::ValueType ValueType
void vInv(int n, T *vecIn, T *vecOut)
interface to v*Inv
utl::remove_complex_t< T > ScalarValueType
ConstIterator operator++(int)
Superclass::ScalarValueType ScalarValueType
T cblas_nrm2(const INTT N, const T *X, const INTT incX)
ConstIterator cEnd() const
NDArray(const ShapeType &shape, const T r)
Superclass::ShapeType ShapeType
T max_element(const std::vector< T > &v)
NDArrayBase(const ShapeType &shape, const T r)
ConstIterator operator->() const
bool operator!=(const ConstReverseIterator &rit) const
Superclass::ConstIterator ConstIterator
NDArrayBase< T, Dim > & ElementAxpby(T *const vec, const T alpha, const T beta)
#define __utl_ndarray_alloc_blah(shape)
void CopyData(T *const data, const ShapeType &shape)
NDArrayBase< T, Dim > GetElementExp() const
Superclass::Reference Reference