DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlVector.h
Go to the documentation of this file.
1 
10 #ifndef __utlVector_h
11 #define __utlVector_h
12 
13 #include "utlNDArray.h"
14 
15 namespace utl
16 {
21 template < class T, unsigned int Dim >
22 class NDArray;
23 
24 template < class T, unsigned int Dim >
25 class NDArrayBase;
26 
35 template < class T >
36 class NDArray<T, 1> : public NDArrayBase<T,1>
37 {
38 
39 public:
40 
41  typedef NDArray Self;
43 
44  typedef typename Superclass::ValueType ValueType;
46 
47  typedef typename Superclass::SizeType SizeType;
48  typedef typename Superclass::ShapeType ShapeType;
49 
50  typedef typename Superclass::Iterator Iterator;
52  typedef typename Superclass::Pointer Pointer;
54  typedef typename Superclass::Reference Reference;
56 
59 
60  // NOTE: "using" to avoid hiding member functions of superclass
61  using Superclass::SetData;
63  using Superclass::ReSize;
64  using Superclass::operator();
65  using Superclass::operator=;
66  using Superclass::operator+=;
67  using Superclass::operator-=;
68  using Superclass::operator%=;
69  using Superclass::operator/=;
70 
71 public:
76  NDArray() : Superclass()
77  {}
78 
79 #define __NDArray_vector_constructor(intType) \
80  explicit NDArray(const intType size) : Superclass() \
81  { \
82  SizeType shape[1]; \
83  shape[0]=size; \
84  __utl_ndarray_alloc_blah(shape); \
85  }
86 
91 
92 #undef __NDArray_vector_constructor
93 
94  NDArray(const NDArray<T,1>& vec) : Superclass(vec) { }
95 
97  {
98  operator=(std::move(vec));
99  }
100 
101  NDArray(const std::initializer_list<T>& r)
102  {
103  ReSize(r.size());
104  utl::cblas_copy(this->Size(), r.begin(), 1, this->m_Data, 1);
105  }
106 
107  template<typename EType>
108  NDArray(const Expr<EType, typename EType::ValueType>& expr) : Superclass(expr)
109  {
110  }
111 
117  NDArray(const T* vec, const SizeType size) : Superclass()
118  {
119  SizeType shape[1];
120  shape[0]=size;
122  utl::cblas_copy<T>(this->Size(), vec, 1, this->m_Data, 1);
123  }
124 
128  NDArray(const SizeType size, const T r) : Superclass()
129  {
130  SizeType shape[1];
131  shape[0]=size;
133  this->Fill(r);
134  }
135 
136  template< typename TVectorValueType >
137  NDArray(const NDArray< TVectorValueType, 1> & r) : Superclass(r)
138  {
139  }
140 
141  explicit NDArray(const ShapeType& shape) : Superclass(shape) { }
142 
143  NDArray(const T* vec, const ShapeType& shape) : Superclass(vec,shape) { }
144 
148  NDArray(const ShapeType& shape, const T r) : Superclass(shape, r) { }
149 
150 
152  void SetData( T* const data, const SizeType size )
153  {
154  SizeType shape[1];
155  shape[0]=size;
156  Superclass::SetData(data, shape);
157  }
158 
160  void CopyData(T* const data, const SizeType size)
161  {
162  SizeType shape[1];
163  shape[0]=size;
164  Superclass::CopyData(data, shape);
165  }
166 
167  inline bool ReSize(const SizeType size)
168  {
169  SizeType shape[1];
170  shape[0]=size;
171  return Superclass::ReSize(shape);
172  }
173 
174 // #define __Vector_Saver_Expr(saver) \
175 // template<typename EType> \
176 // inline NDArray<T,1>& operator saver (const Expr<EType>& src){ \
177 // Superclass::operator saver (src); \
178 // return *this; \
179 // }
180 
181 // // __Vector_Saver_Expr(=)
182 // __Vector_Saver_Expr(+=)
183 // __Vector_Saver_Expr(-=)
184 // __Vector_Saver_Expr(%=)
185 // __Vector_Saver_Expr(/=)
186 
187 // #define __Vector_Saver_Scalar(saver) \
188 // inline NDArray<T,1>& operator saver (const T val){ \
189 // Superclass::operator saver(val); \
190 // return *this; \
191 // }
192 
193 // // __Vector_Saver_Scalar(=)
194 // __Vector_Saver_Scalar(+=)
195 // __Vector_Saver_Scalar(-=)
196 // __Vector_Saver_Scalar(%=)
197 // __Vector_Saver_Scalar(/=)
198 
201  UTL_ALWAYS_INLINE Reference operator()(unsigned long index) { return this->m_Data[index]; }
202  UTL_ALWAYS_INLINE ConstReference operator()(unsigned long index) const { return this->m_Data[index]; }
203  UTL_ALWAYS_INLINE Reference operator()(long index) { return this->m_Data[index]; }
204  UTL_ALWAYS_INLINE ConstReference operator()(long index) const { return this->m_Data[index]; }
205  UTL_ALWAYS_INLINE Reference operator()(int index) { return this->m_Data[index]; }
206  UTL_ALWAYS_INLINE ConstReference operator()(int index) const { return this->m_Data[index]; }
207  UTL_ALWAYS_INLINE Reference operator()(unsigned int index) { return this->m_Data[index]; }
208  UTL_ALWAYS_INLINE ConstReference operator()(unsigned int index) const { return this->m_Data[index]; }
209 
210  // UTL_ALWAYS_INLINE Reference operator()(const ShapeType& shape) { return this->m_Data[shape[0]]; }
211  // UTL_ALWAYS_INLINE ConstReference operator()(const ShapeType& shape) const { return this->m_Data[shape[0]]; }
212 
213 
215  {
217  return *this;
218  }
219 
221  {
222  if ( this != &r )
223  {
224  this->Clear();
225  this->Swap(r);
226  }
227  return *this;
228  }
229 
230 
233  NDArray<T,1> GetRotateAxis(const double alpha, const NDArray<T, 1>& axis)
234  {
235  utlException(this->Size()!=3, "wrong size");
236  utlException(axis.Size()!=3, "wrong size");
237  double cosA = std::cos(alpha);
238  double sinA = std::sin(alpha);
239  NDArray<T,1> p1 = *this % cosA;
240  NDArray<T,1> p2(3);
241  CrossProduct(axis, *this, p2);
242  p2.Scale(sinA);
243  NDArray<T,1> p3 = axis % (DotProduct(axis, *this, 3)*(1-cosA));
244  return p1+p2+p3;
245  }
246 
247  void RotateAxis(const double alpha, const NDArray<T, 1>& axis)
248  {
249  *this = GetRotateAxis(alpha, axis);
250  }
251 
252 
254  void PreMultiply(const NDArray<T,2>& mat, NDArray<T,1>& vec)
255  {
256  utl::ProductUtlMv(mat, *this, vec);
257  }
258 
259  void PostMultiply(const NDArray<T,2>& mat, NDArray<T,1>& vec)
260  {
261  utl::ProductUtlvM(*this, mat, vec);
262  }
263 
265  {
266  mat.ReSize(this->Size(), this->Size());
267  mat.Fill(0.0);
268  mat.SetDiagonal(*this);
269  }
271  {
272  NDArray<T,2> result(this->Size(), this->Size());
273  this->GetDiagonalMatrix(result);
274  return result;
275  }
276 
277  void GetSubVector(const std::vector<int>& indexVec, NDArray<T,1>& vec) const
278  {
279  vec.ReSize(indexVec.size());
280  for ( int i = 0; i < indexVec.size(); ++i )
281  vec[i] = this->m_Data[ indexVec[i] ];
282  }
283  NDArray<T,1> GetSubVector(const std::vector<int>& indexVec) const
284  {
285  NDArray<T,1> vec;
286  GetSubVector(indexVec, vec);
287  return vec;
288  }
289 
290  NDArray<T,1>& SetSubVector(const std::vector<int> indexVec, const NDArray<T,1>& vec)
291  {
292  for ( int i = 0; i < indexVec.size(); ++i )
293  this->m_Data[ indexVec[i] ] = vec[i];
294  return *this;
295  }
296 
297 
298 protected:
299 
300 
301 private:
302 
303 
304 }; // ----- end of template class Vector -----
305 
306 
307 
308 
311 }
312 
313 
314 #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,1> is a vector class which uses blas mkl.
Definition: utlVector.h:36
Superclass::ScalarValueType ScalarValueType
Definition: utlVector.h:45
#define __NDArray_vector_constructor(intType)
Definition: utlVector.h:79
void cblas_copy(const INTT N, const T *X, const INTT incX, T *Y, const INTT incY)
base class for expression
Definition: utlExpression.h:36
NDArray(const T *vec, const SizeType size)
Definition: utlVector.h:117
NDArray(const Expr< EType, typename EType::ValueType > &expr)
Definition: utlVector.h:108
void PreMultiply(const NDArray< T, 2 > &mat, NDArray< T, 1 > &vec)
Definition: utlVector.h:254
NDArray< T, 1 > GetSubVector(const std::vector< int > &indexVec) const
Definition: utlVector.h:283
ConstReference operator()(long index) const
Definition: utlVector.h:204
vnl_matrix< T > GetDiagonalMatrix(const vnl_vector< T > &vec)
Definition: utlVNL.h:365
Superclass::ValueType ValueType
Definition: utlVector.h:44
NDArray< T, 1 > & operator=(NDArray< T, 1 > &r)
Definition: utlVector.h:214
Superclass::ConstPointer ConstPointer
Definition: utlVector.h:53
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
Superclass::SizeType SizeType
Definition: utlVector.h:47
STL namespace.
static constexpr SizeType SubDimension
Definition: utlNDArray.h:162
Reference operator()(long index)
Definition: utlVector.h:203
NDArray< T, 2 > & SetDiagonal(const NDArray< T, 1 > &vec)
Definition: utlMatrix.h:164
NDArray<T,2> is a row-major matrix.
Definition: utlMatrix.h:37
bool ReSize(const ShapeType &shape)
Definition: utlNDArray.h:697
const ValueType & ConstReference
Definition: utlNDArray.h:179
NDArray(const ShapeType &shape, const T r)
Definition: utlVector.h:148
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
void PostMultiply(const NDArray< T, 2 > &mat, NDArray< T, 1 > &vec)
Definition: utlVector.h:259
double CrossProduct(const TVector1 &v1, const TVector2 &v2, TVector3 &v3)
Definition: utlMath.h:1350
NDArray(const NDArray< T, 1 > &vec)
Definition: utlVector.h:94
void ProductUtlvM(const utl::NDArray< T, 1 > &b, const utl::NDArray< T, 2 > &A, utl::NDArray< T, 1 > &c, const double alpha=1.0, const double beta=0.0)
#define UTL_ALWAYS_INLINE
Definition: utlCoreMacro.h:76
Superclass::Reference Reference
Definition: utlVector.h:54
Reference operator()(int index)
Definition: utlVector.h:205
NDArray< T, 1 > & operator=(NDArray< T, 1 > &&r)
Definition: utlVector.h:220
Base class for utl::NDArray.
Definition: utlMatrix.h:22
double DotProduct(const TVector1 &v1, const TVector2 &v2, const int N1)
Definition: utlMath.h:1332
Superclass::ConstIterator ConstIterator
Definition: utlVector.h:51
Superclass::Iterator Iterator
Definition: utlVector.h:50
Definition: utl.h:90
Superclass::SizeType SizeType
Definition: utlNDArray.h:154
void Fill(const T &value)
Definition: utlNDArray.h:922
ConstReference operator()(unsigned long index) const
Definition: utlVector.h:202
NDArray< T, 1 > GetRotateAxis(const double alpha, const NDArray< T, 1 > &axis)
Definition: utlVector.h:233
NDArray< T, 1 > & SetSubVector(const std::vector< int > indexVec, const NDArray< T, 1 > &vec)
Definition: utlVector.h:290
Superclass::ConstReference ConstReference
Definition: utlVector.h:55
ConstReference operator()(int index) const
Definition: utlVector.h:206
Reference operator()(unsigned int index)
Definition: utlVector.h:207
Reference operator()(unsigned long index)
Definition: utlVector.h:201
NDArrayBase< T, 1 > Superclass
Definition: utlVector.h:42
ConstReference operator()(unsigned int index) const
Definition: utlVector.h:208
void Swap(NDArrayBase< T, Dim > &vec)
Definition: utlNDArray.h:1062
void GetDiagonalMatrix(NDArray< T, 2 > &mat) const
Definition: utlVector.h:264
NDArray(NDArray< T, 1 > &&vec)
Definition: utlVector.h:96
Superclass::ShapeType ShapeType
Definition: utlVector.h:48
NDArray(const NDArray< TVectorValueType, 1 > &r)
Definition: utlVector.h:137
Superclass::Pointer Pointer
Definition: utlVector.h:52
NDArray(const ShapeType &shape)
Definition: utlVector.h:141
NDArray(const SizeType size, const T r)
Definition: utlVector.h:128
const ValueType * ConstPointer
Definition: utlNDArray.h:175
NDArray< T, 2 > GetDiagonalMatrix() const
Definition: utlVector.h:270
NDArray(const T *vec, const ShapeType &shape)
Definition: utlVector.h:143
void CopyData(T *const data, const SizeType size)
Definition: utlVector.h:160
NDArrayBase< T, Dim > & Scale(const T a)
Definition: utlNDArray.h:873
void SetData(T *const data, const SizeType size)
Definition: utlVector.h:152
SizeType Size() const
Definition: utlNDArray.h:321
bool ReSize(const SizeType rows, const SizeType cols)
Definition: utlMatrix.h:140
NDArrayBase< T, Dim > & operator=(const Expr< EType, typename EType::ValueType > &src)
Definition: utlNDArray.h:385
NDArray(const std::initializer_list< T > &r)
Definition: utlVector.h:101
utl::remove_complex_t< T > ScalarValueType
Definition: utlNDArray.h:165
void GetSubVector(const std::vector< int > &indexVec, NDArray< T, 1 > &vec) const
Definition: utlVector.h:277
bool ReSize(const SizeType size)
Definition: utlVector.h:167
Superclass::ShapeType ShapeType
Definition: utlNDArray.h:155
#define __utl_ndarray_alloc_blah(shape)
Definition: utlNDArray.h:30
void ProductUtlMv(const utl::NDArray< T, 2 > &A, const utl::NDArray< T, 1 > &b, utl::NDArray< T, 1 > &c, const double alpha=1.0, const double beta=0.0)
void RotateAxis(const double alpha, const NDArray< T, 1 > &axis)
Definition: utlVector.h:247
void CopyData(T *const data, const ShapeType &shape)
Definition: utlNDArray.h:689