Memosa-FVM  0.2
Array.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _ARRAY_H_
6 #define _ARRAY_H_
7 
8 
9 #include "NumType.h"
10 #include "ArrayBase.h"
11 
12 
13 template <class T>
14 class Array : public ArrayBase
15 {
16 
17 public:
18 
21 
22  explicit Array(const int length) :
23  ArrayBase(),
24  _length(length),
25  _data(new T[length]),
26  _ownData(true)
27  {
28  logCtorVerbose("of length %d" , _length);
29  }
30 
31 
32 
33  Array(Array& parent, const int offset, const int length) :
34  ArrayBase(),
35  _length(length),
36  _data(parent._data+offset),
37  _ownData(false)
38  {
39  logCtorVerbose("with offset %d and length %d" , offset, _length);
40  }
41 
42 
43  virtual ~Array()
44  {
45  if (_ownData)
46  {
47  logDtorVerbose("of length %d with own data" , _length);
48  delete [] _data;
49  }
50  else
51  {
52  logDtorVerbose("of length %d with offset data" , _length);
53  }
54  }
55 
56  void resize(const int newLength)
57  {
58  if (_ownData)
59  {
60  T* newData = new T[newLength];
61  if (_data)
62  {
63  int len;
64  if(newLength<_length)
65  len=newLength;
66  else
67  len=_length;
68 
69  for(int i=0; i<len; i++)
70  newData[i] = _data[i];
71  delete [] _data;
72  }
73  _data = newData;
74  _length = newLength;
75  }
76  else
77  throw CException("cannot resize offset array");
78  }
79 
81 
82  virtual int getDimension() const
83  {
84  return NumTypeTraits<T>::getDimension() + 1;
85  };
86 
87  int getLength() const {return _length;}
88 
89  virtual PrimType getPrimType() const
90  {
92  }
93 
94  virtual void getShape(int* shp) const
95  {
96  *shp = _length;
98  }
99 
100 
101  T& operator[](int n) {return _data[n];}
102  const T& operator[](int n) const {return _data[n];}
103 
104  void operator=(const T& x)
105  {
106  for(int i=0;i<_length;i++)
107  _data[i] = x;
108  }
109 
110  void operator=(const Array& o)
111  {
112  for(int i=0;i<_length;i++)
113  _data[i] = o._data[i];
114  }
115 
116 
117  virtual ArrayBase& operator+=(const ArrayBase& obase)
118  {
119  const Array& o = dynamic_cast<const Array& >(obase);
120  if (o._length == _length)
121  {
122  for(int i=0;i<_length;i++)
123  _data[i] += o._data[i];
124  }
125  else
126  throw CException("invalid array for operator +=");
127  return *this;
128  }
129 
130  virtual ArrayBase& operator-=(const ArrayBase& obase)
131  {
132  const Array& o = dynamic_cast<const Array& >(obase);
133  if (o._length == _length)
134  {
135  for(int i=0;i<_length;i++)
136  _data[i] -= o._data[i];
137  }
138  else
139  throw CException("invalid array for operator -=");
140  return *this;
141  }
142 
143  virtual ArrayBase& operator/=(const ArrayBase& obase)
144  {
145  const Array& o = dynamic_cast<const Array& >(obase);
146  if (o._length == 1)
147  {
148  for(int i=0;i<_length;i++)
149  _data[i] /= o._data[0];
150  }
151  else if (o._length == _length)
152  {
153  for(int i=0;i<_length;i++)
154  _data[i] /= o._data[i];
155  }
156  else
157  throw CException("invalid array for operator /=");
158  return *this;
159  }
160 
161  virtual void setMax(const ArrayBase& obase)
162  {
163  const Array& o = dynamic_cast<const Array& >(obase);
164  if (_length == 1 && o._length==1)
165  {
166  for(int i=0;i<_length;i++)
168  }
169  else
170  throw CException("invalid array for setMax");
171  }
172 
173  virtual ArrayBase& safeDivide(const ArrayBase& obase)
174  {
175  const Array& o = dynamic_cast<const Array& >(obase);
176  if (_length == 1 && o._length==1)
177  {
178  for(int i=0;i<_length;i++)
180  }
181  else
182  throw CException("invalid array for safeDivide");
183  return *this;
184  }
185 
186  virtual ArrayBase& normalize(const ArrayBase& obase)
187  {
188  const Array& o = dynamic_cast<const Array& >(obase);
189  if (_length == 1 && o._length==1)
190  {
191  for(int i=0;i<_length;i++)
193  }
194  else
195  throw CException("invalid array for normalize");
196  return *this;
197  }
198 
199 
200  virtual ArrayBase& operator*=(const ArrayBase& obase)
201  {
202  const Array& o = dynamic_cast<const Array& >(obase);
203  if (o._length == 1)
204  {
205  for(int i=0;i<_length;i++)
206  _data[i] *= o._data[0];
207  }
208  else if (o._length == _length)
209  {
210  for(int i=0;i<_length;i++)
211  _data[i] *= o._data[i];
212  }
213  else
214  throw CException("invalid array for operator *=");
215  return *this;
216  }
217 
218  virtual bool operator<(const double tolerance) const
219  {
220  if (_length == 1)
221  {
222  return (_data[0] <tolerance);
223  }
224  else
225  throw CException("invalid array for operator<");
226  }
227 
228  virtual void print(ostream& os) const
229  {
230  if (_length > 0)
231  {
232  for ( int i = 0; i < _length; i++ )
233  if ( i < _length-1)
234  os <<_data[i] << endl;
235  else
236  os <<_data[i];
237  }
238  else
239  throw CException("invalid array for print");
240  }
241 
242  // this += alpha*x
243  virtual ArrayBase& saxpy(const ArrayBase& alphabase, const ArrayBase& xbase)
244  {
245  const Array& alpha = dynamic_cast<const Array& >(alphabase);
246  const Array& x = dynamic_cast<const Array& >(xbase);
247 
248  if (alpha._length == 1 && x._length == _length)
249  {
250  for(int i=0;i<_length;i++)
251  _data[i] += alpha._data[0]*x._data[i];
252  }
253  else
254  throw CException("invalid arrays for saxpy");
255  return *this;
256  }
257 
258 
259  // this -= alpha*x; defined to avoid having to create a negative of array
260  virtual ArrayBase& msaxpy(const ArrayBase& alphabase, const ArrayBase& xbase)
261  {
262  const Array& alpha = dynamic_cast<const Array& >(alphabase);
263  const Array& x = dynamic_cast<const Array& >(xbase);
264 
265  if (alpha._length == 1 && x._length == _length)
266  {
267  for(int i=0;i<_length;i++)
268  _data[i] -= alpha._data[0]*x._data[i];
269  }
270  else
271  throw CException("invalid arrays for msaxpy");
272  return *this;
273  }
274 
275  virtual void* getData() const {return _data;}
276  virtual int getDataSize() const
277  {
279  }
280 
281  virtual void zero()
282  {
283  memset(_data,0,getDataSize());
284  }
285 
286  void disownData() const {_ownData = false;}
287 
288 
289 
290  virtual shared_ptr<ArrayBase>
291  dotWith(const ArrayBase& abase, const int lengthToUse) const
292  {
293  const Array& a = dynamic_cast<const Array&>(abase);
294  T sum(NumTypeTraits<T>::getZero());
295  for(int i=0; i<lengthToUse; i++)
297  shared_ptr<Array> nPtr(new Array(1));
298  (*nPtr)[0] = sum;
299  return nPtr;
300  }
301 
302  virtual shared_ptr<ArrayBase>
303  getOneNorm(const int lengthToUse) const
304  {
305  T sum(NumTypeTraits<T>::getZero());
306  for(int i=0; i<lengthToUse; i++)
308  shared_ptr<Array> nPtr(new Array(1));
309  (*nPtr)[0] = sum;
310  return nPtr;
311  }
312 
313  virtual shared_ptr<ArrayBase>
314  reduceSum() const
315  {
317  for(int i=0; i<_length; i++)
319  shared_ptr<Array<T_Scalar> > nPtr(new Array<T_Scalar>(1));
320  (*nPtr)[0] = sum;
321  return nPtr;
322  }
323 
324  virtual void
325  setSum(const ArrayBase& sumBase)
326  {
327  const Array<T_Scalar>& sum =
328  dynamic_cast<const Array<T_Scalar>& >(sumBase);
329  if (sum.getLength() == 1)
330  {
331  const T_Scalar& s = sum[0];
332  for(int i=0; i<_length; i++)
333  _data[i]=s;
334  }
335  }
336 
337  virtual void
338  scatter(ArrayBase& other_, const ArrayBase& indices_, const int offset=0) const
339  {
340  const Array<int>& indices = dynamic_cast<const Array<int>& >(indices_);
341  Array& other = dynamic_cast<Array& >(other_);
342  for(int ii=0; ii<indices.getLength(); ii++)
343  other[offset+ii] = _data[indices[ii]];
344  }
345 
346  virtual void
347  gather(const ArrayBase& other_, const ArrayBase& indices_, const int offset=0)
348  {
349  const Array<int>& indices = dynamic_cast<const Array<int>& >(indices_);
350  const Array& other = dynamic_cast<const Array& >(other_);
351  for(int ii=0; ii<indices.getLength(); ii++)
352  _data[indices[ii]] = other[offset+ii];
353  }
354 
355  virtual shared_ptr<Array>
356  getSubset(const Array<int>& indices)
357  {
358  shared_ptr<Array> subPtr(new Array(indices.getLength()));
359  Array& sub = *subPtr;
360 
361  for(int ii=0; ii<indices.getLength(); ii++)
362  {
363  sub[ii] = _data[indices[ii]];
364  }
365 
366  return subPtr;
367  }
368 
369  virtual void
370  setSubsetFromSubset(const ArrayBase& other_, const ArrayBase& fromIndices_,
371  const ArrayBase& toIndices_)
372  {
373  const Array<int>& toIndices = dynamic_cast<const Array<int>& >(toIndices_);
374  const Array<int>& fromIndices = dynamic_cast<const Array<int>& >(fromIndices_);
375  const Array& other = dynamic_cast<const Array& >(other_);
376  for(int ii=0; ii<fromIndices.getLength(); ii++)
377  {
378  _data[toIndices[ii]] = other[fromIndices[ii]];
379  }
380  }
381 
382  virtual void
383  copyFrom(const IContainer& oc)
384  {
385  const Array& other = dynamic_cast<const Array&>(oc);
386  operator=(other);
387  }
388 
389  virtual void
390  copyPartial(const IContainer& oc, const int iBeg, const int iEnd)
391  {
392  const Array& other = dynamic_cast<const Array&>(oc);
393  for(int i=iBeg;i<iEnd;i++)
394  _data[i] = other._data[i];
395  }
396 
397  virtual void
398  zeroPartial(const int iBeg, const int iEnd)
399  {
400  for(int i=iBeg;i<iEnd;i++)
402  }
403 
404  virtual void limit(const double min, const double max)
405  {
406  if (_length == 1)
407  {
408  ArrayScalarTraits<T>::limit(_data[0],min, max);
409  }
410  else
411  throw CException("invalid array for limit");
412  }
413 
414  virtual shared_ptr<ArrayBase>
415  operator-() const
416  {
417  if (_length == 1)
418  {
419  Array* nPtr(new Array(1));
420  (*nPtr)[0] = -_data[0];
421  return shared_ptr<ArrayBase>(nPtr);
422  }
423  throw CException("invalid array for operator-");
424  }
425 
426 
427  virtual void
428  inject(IContainer& coarseI, const IContainer& coarseIndexI, const int length) const
429  {
430  Array& coarse = dynamic_cast<Array&>(coarseI);
431  const Array<int>& coarseIndex = dynamic_cast<const Array<int>& >(coarseIndexI);
432 
433  for(int i=0; i<length; i++)
434  if (coarseIndex[i]>=0)
435  coarse[coarseIndex[i]] += _data[i];
436  }
437 
438  virtual void
439  correct(const IContainer& coarseI, const IContainer& coarseIndexI,
440  const IContainer* scaleI, const int length)
441  {
442  const Array& coarse = dynamic_cast<const Array&>(coarseI);
443  const Array<int>& coarseIndex = dynamic_cast<const Array<int>& >(coarseIndexI);
444 
445  if (scaleI)
446  {
447  const Array& scaleArray =
448  dynamic_cast<const Array&>(*scaleI);
449  if (scaleArray.getLength() == 1)
450  {
451  const T& scale = scaleArray[0];
452  //cout << "correction scale" << scale << endl;
453 
454  for(int i=0; i<length; i++)
455  _data[i] += coarse[coarseIndex[i]]*scale;
456 
457  }
458  else
459  throw CException("invalid scale array");
460  }
461  else
462  {
463  for(int i=0; i<length; i++)
464  if (coarseIndex[i]>=0)
465  _data[i] += coarse[coarseIndex[i]];
466  }
467  }
468 
469  virtual shared_ptr<IContainer>
470  newClone() const
471  {
472  return shared_ptr<Array>(new Array(_length));
473  }
474 
475  virtual shared_ptr<ArrayBase>
476  newSizedClone(const int size) const
477  {
478  return shared_ptr<Array>(new Array(size));
479  }
480 
481 
482  virtual shared_ptr<IContainer>
483  newCopy() const
484  {
485  shared_ptr<Array> c(new Array(_length));
486  *c = *this;
487  return c;
488  }
489 
490  virtual shared_ptr<ArrayBase>
491  createOffsetArray(const int offset, const int length)
492  {
493  return shared_ptr<Array>(new Array(*this,offset,length));
494  }
495 
496 private:
497  Array(const Array&);
498 
499  int _length;
500  T* _data;
501  mutable bool _ownData;
502 };
503 
504 
505 
506 template<class T>
508 {
509 public:
510  ConstAsArray(const T& c) :
511  _c(c)
512  {}
513 
514  const T& operator[](int n) const {return _c;}
515 
516 private:
517  const T& _c;
518 };
519 
520 
521 template<class T>
522 shared_ptr<Array<T> >
523 arrayFromVector(const vector<T>& v)
524 {
525  const int count = v.size();
526  Array<T>* a = new Array<T>(count);
527  for(int i=0; i<count; i++)
528  (*a)[i] = v[i];
529  return shared_ptr<Array<T> >(a);
530 }
531 
532 #endif
virtual void zero()
Definition: Array.h:281
T & operator[](int n)
Definition: Array.h:101
Array(const int length)
Definition: Array.h:22
#define logCtorVerbose(str,...)
Definition: RLogInterface.h:29
const T & operator[](int n) const
Definition: Array.h:102
static void limit(T &val, const double min, const double max)
Definition: NumType.h:150
virtual ArrayBase & msaxpy(const ArrayBase &alphabase, const ArrayBase &xbase)
Definition: Array.h:260
bool _ownData
Definition: Array.h:501
virtual ArrayBase & operator-=(const ArrayBase &obase)
Definition: Array.h:130
virtual shared_ptr< Array > getSubset(const Array< int > &indices)
Definition: Array.h:356
virtual ArrayBase & operator+=(const ArrayBase &obase)
Definition: Array.h:117
virtual void setSubsetFromSubset(const ArrayBase &other_, const ArrayBase &fromIndices_, const ArrayBase &toIndices_)
Definition: Array.h:370
const T & operator[](int n) const
Definition: Array.h:514
NumTypeTraits< T >::T_BuiltIn T_BuiltIn
Definition: Array.h:20
virtual ArrayBase & saxpy(const ArrayBase &alphabase, const ArrayBase &xbase)
Definition: Array.h:243
virtual void correct(const IContainer &coarseI, const IContainer &coarseIndexI, const IContainer *scaleI, const int length)
Definition: Array.h:439
virtual ArrayBase & safeDivide(const ArrayBase &obase)
Definition: Array.h:173
double max(double x, double y)
Definition: Octree.cpp:18
virtual void print(ostream &os) const
Definition: Array.h:228
DEFINE_TYPENAME("Array<"+NumTypeTraits< T >::getTypeName()+">")
virtual void copyPartial(const IContainer &oc, const int iBeg, const int iEnd)
Definition: Array.h:390
virtual void gather(const ArrayBase &other_, const ArrayBase &indices_, const int offset=0)
Definition: Array.h:347
virtual int getDataSize() const
Definition: Array.h:276
virtual void zeroPartial(const int iBeg, const int iEnd)
Definition: Array.h:398
virtual ArrayBase & operator/=(const ArrayBase &obase)
Definition: Array.h:143
virtual void inject(IContainer &coarseI, const IContainer &coarseIndexI, const int length) const
Definition: Array.h:428
virtual void * getData() const
Definition: Array.h:275
virtual shared_ptr< ArrayBase > getOneNorm(const int lengthToUse) const
Definition: Array.h:303
virtual void setMax(const ArrayBase &obase)
Definition: Array.h:161
void resize(const int newLength)
Definition: Array.h:56
Array(Array &parent, const int offset, const int length)
Definition: Array.h:33
void operator=(const Array &o)
Definition: Array.h:110
void operator=(const T &x)
Definition: Array.h:104
virtual int getDimension() const
Definition: Array.h:82
virtual ~Array()
Definition: Array.h:43
void disownData() const
Definition: Array.h:286
virtual shared_ptr< ArrayBase > createOffsetArray(const int offset, const int length)
Definition: Array.h:491
virtual shared_ptr< IContainer > newClone() const
Definition: Array.h:470
virtual ArrayBase & normalize(const ArrayBase &obase)
Definition: Array.h:186
ConstAsArray(const T &c)
Definition: Array.h:510
Definition: Array.h:14
virtual void limit(const double min, const double max)
Definition: Array.h:404
virtual void setSum(const ArrayBase &sumBase)
Definition: Array.h:325
T * _data
Definition: Array.h:500
int _length
Definition: Array.h:499
virtual void scatter(ArrayBase &other_, const ArrayBase &indices_, const int offset=0) const
Definition: Array.h:338
virtual shared_ptr< IContainer > newCopy() const
Definition: Array.h:483
NumTypeTraits< T >::T_Scalar T_Scalar
Definition: Array.h:19
virtual bool operator<(const double tolerance) const
Definition: Array.h:218
virtual ArrayBase & operator*=(const ArrayBase &obase)
Definition: Array.h:200
double min(double x, double y)
Definition: Octree.cpp:23
virtual shared_ptr< ArrayBase > reduceSum() const
Definition: Array.h:314
const T & _c
Definition: Array.h:517
virtual shared_ptr< ArrayBase > operator-() const
Definition: Array.h:415
virtual void copyFrom(const IContainer &oc)
Definition: Array.h:383
virtual shared_ptr< ArrayBase > dotWith(const ArrayBase &abase, const int lengthToUse) const
Definition: Array.h:291
static PrimType getPrimType()
Definition: NumType.h:27
#define logDtorVerbose(str,...)
Definition: RLogInterface.h:36
virtual shared_ptr< ArrayBase > newSizedClone(const int size) const
Definition: Array.h:476
virtual PrimType getPrimType() const
Definition: Array.h:89
virtual void getShape(int *shp) const
Definition: Array.h:94
shared_ptr< Array< T > > arrayFromVector(const vector< T > &v)
Definition: Array.h:523
int getLength() const
Definition: Array.h:87
PrimType
Definition: NumType.h:15