C++ mpi module for stochmagnet_main Package
CORE_StdValField.h
1 #ifndef CORE_StdValField_H
2 #define CORE_StdValField_H
3 
4 //inherited header
5 #include "CORE_Field.h"
6 
7 //corresponding array header
8 #include "CORE_StdValArray.h"
9 
16 template <typename T,typename K,K D>
17 class CORE_StdValField : public CORE_Field<T,K,D,CORE_StdValArray<T>,CORE_StdValField<T,K,D>> {
18 
19 private:
20 
21  //This class type
23 
24 
25 
26  // CONSTRUCTORS
27 public:
31  }
32 
33 
34 
35  // DESTRUCTORS
38  virtual ~CORE_StdValField() {
39  }
40 
41 
42 private:
43 
44 
45 
46 
47  //MEMORY
48  //=====
49 public:
57  virtual tMemSize getMemorySize() const override {
58  return sizeof(*this)+this->getContentsMemorySize();
59  }
60 
61  //allocation methods
62  //===================
63 
64 public:
68  static inline CORE_UniquePointer<Self> New() {
69  return CORE_UniquePointer<Self>(new Self(),CORE_Object::Delete());
70  }
71 
72 
73 
74 
75  //accessor operators
76  //====================
77 
78 
79  //accessor iterator
80  //=================
81 
82 
83  //accessor methods
84  //=================
85 
86 
87  //assignment operators
88  //====================
92  inline Self& operator=(const T& v) {
93  initialize(v);
94  return *this;
95  }
99  inline Self& operator=(const std::initializer_list<T>& values) {
100  this->copy(values);
101  return *this;
102  }
106  inline Self& operator=(std::initializer_list<T>&& values) {
107  this->copy(values);
108  return *this;
109  }
110 
111 
117  template<size_t N,typename Q>
118  inline Self& operator=(const std::array<Q,N>& values) {
119  copy(values);
120  return *this;
121 
122  }
123 
128  template<typename Q>
129  inline Self& operator=(const std::valarray<Q>& values) {
130  copy(values);
131  return *this;
132  }
133 
138  template<typename Q>
139  inline Self& operator=(const std::vector<Q>& values) {
140  copy(values);
141  return *this;
142  }
143 
147  inline Self& operator=(const Self& values) {
148  copy(values);
149  return *this;
150  }
154  inline Self& operator=(Self&& values) {
155  copy(values);
156  return *this;
157  }
158 
165  template<typename Q,class S1,class I1>
166  inline Self& operator=(const CORE_Field<Q,K,D,S1,I1>& values) {
167  copy(values);
168  return *this;
169  }
176  template<typename Q,class S1,class I1>
178  copy(values);
179  return *this;
180  }
181 
182 
183  //initializers methods
184  //====================
185 
186 
191  template<typename Q>
192  inline void initialize(const std::array<Q,D>& a) {
193  auto iA=a.begin();
194  auto iAe=a.end();
195  //compute Vi:=Vi/norm
196  auto F=[&iA,&iAe](auto& Vi) {
197 
198  //compute Vi:=Vi/norm
199  auto Vid=&Vi;
200  std::for_each(iA,iAe,[&Vid](const auto& Aid){(*Vid)=Aid;Vid++;});
201 
202  //return the value
203  return Vi;
204  };
205 
206  //apply the function F
207  std::transform(this->sbegin(),this->send(),
208  this->sbegin(),F);
209  }
210 
211 
212 
213 
214 
215 
216  //compound assignement operators (+=,-=,*=,/=,^=...)
217  //===================================================
218  //arithmetic type compound operators
219  //====================================
223  inline Self& operator+=(const T& v) {
224  this->getStorage()+=v;
225  return (*this);
226  }
230  inline Self& operator-=(const T& v) {
231  this->getStorage()-=v;
232  return (*this);
233 
234  }
235 
239  inline Self& operator*=(const T& v) {
240  this->getStorage()*=v;
241  return (*this);
242 
243  }
247  inline Self& operator/=(const T& v) {
248  this->getStorage()/=v;
249  return (*this);
250  }
251 
252  //integer type compond operators
253  //===============================
257  inline Self& operator%=(const T& v) requires functions_type::isIntegerType<T>{
258  this->getStorage()%=v;
259  return (*this);
260  }
261 
265  inline Self& operator&=(const T& v) requires functions_type::isIntegerType<T>{
266  this->getStorage()&=v;
267  return (*this);
268  }
269 
273  inline Self& operator|=(const T& v) requires functions_type::isIntegerType<T>{
274  this->getStorage()|=v;
275  return (*this);
276  }
277 
281  inline Self& operator^=(const T& v) requires functions_type::isIntegerType<T> {
282  this->getStorage()^=v;
283  return (*this);
284  }
288  inline Self& operator<<=(const T& v) requires functions_type::isIntegerType<T> {
289  this->getStorage()<<=v;
290  return (*this);
291  }
292 
296  inline Self& operator>>=(const T& v) requires functions_type::isIntegerType<T> {
297  this->getStorage()>>=v;
298  return (*this);
299  }
300 
301  //class type compound operatos
302  //=============================
303 
310  template<class Q,class S,class I>
311  inline Self& operator+=(const CORE_Field<Q,K,D,S,I>& X) {
312  this->getStorage()+=X.getStorage();
313  return (*this);
314  }
321  template<class Q,class S,class I>
323  this->getStorage()-=X.getStorage();
324  return (*this);
325  }
332  template<class Q,class S,class I>
334  //T=F(T,V)
335  this->getStorage()*=X.getStorage();
336  return (*this);
337  }
344  template<class Q,class S,class I>
346  this->getStorage()/=X.getStorage();
347  return (*this);
348  }
349 
350 
351  //transform methods
352  //=================
353 
354 
355 
361  template<typename LambdaFct>
362  static inline void ElementsTransform(LambdaFct&& F,
363  Self& X) {
364 
365  std::transform(X.sbegin(),X.send(),
366  X.sbegin(),F);
367 
368  }
369 
370 
375  template<typename LambdaFct>
376  inline void elementsTransform(LambdaFct&& F) {
377  this->ElementsTransform(F,(*this));
378  }
379 
380 
381 
386  inline void normalize() {
387 
388  //error to detect null
389  T eps=std::numeric_limits<T>::epsilon();
390 
391  T norm=0;
392 
393 
394  //compute Vi:=Vi/norm
395  auto F=[&norm,&eps](auto& Vi) {
396  //compute Norm2
397  norm=0;
398  std::for_each_n(&Vi,D,[&norm](const auto& Vid){
399  norm+=Vid*Vid;
400  });
401 
402  //compute invers of norm
403  if (norm>eps) {
404  norm=1./sqrt(norm);
405  }
406 
407  //compute Vi:=Vi/norm
408  std::for_each_n(&Vi,D,[&norm](auto& Vid){Vid*=norm;});
409 
410  //return the value
411  return Vi;
412  };
413 
414  //apply the function F
415  // T* start=&(*(this->sbegin()));
416  // T* end=&(*(this->send()));
417  // std::cout<<"start:"<<start<<" end:"<<end<<" length="<<(end-start)<<"\n";
418  std::transform(this->sbegin(),this->send(),
419  this->sbegin(),F);
420 
421  }
422 
423 
424  //Data consistency
425  //=================
426 
427 
428  //transform methods
429  //==================
430 
431 
432  //algebric methods
433  //================
434 
435 
436 
441  template<class I>
442  inline void mod2(CORE_Array<T,I>& X) const {
443  X.setSize(this->getElementsNumber());
444  auto Vi=this->csbegin();
445  for(auto & Xi : X) {
446  Xi=0;
447  std::for_each_n(&(*Vi),D,[&Xi](const auto& Vid) {Xi+=Vid*Vid;});
448  Vi++;
449  }
450 
451  }
452 
453 
462  template<class Q,class S1,class I1>
463  inline T& scalarProduct(const CORE_Field<Q,K,D,S1,I1>& X,T& s) const {
464  return this->getStorage().scalarProduct(X.getStorage(),s);
465  }
466 
476  template<class Q,class S1,class I1>
477  inline T& scalarProduct(const std::valarray<Q>& weights,const CORE_Field<Q,K,D,S1,I1>& X,T& s) const {
478  //number of elements
479  tIndex n=this->getElementsNumber();
480 
481 
482 
483 
484  if (weights.size()==0) {
486  }
487 
488  //increment of W
489  tBoolean incW=(weights.size()>1);
490  //values of W
491  const tReal *iWs=&weights[0];
492 
493  //values of this
494  const T* iYs=this->getValues();
495  const T* iYe=iYs+n*D;
496  const T* iYd=null;
497 
498  //values of X
499  const Q* iXs=X.getValues();
500  tBoolean incX=(X.getElementsNumber()>1);
501  const Q* iXd=null;
502 
503  //s=sum_i Xi Yi
504  s=0;
505  while (iYs!=iYe) {//loop on elements
506  iYd=iYs+D;
507  iXd=iXs;
508  while(iYs!=iYd) {//loop on D
509  s+=(*iWs)*(*iXd)*(*iYs);
510  iYs++;
511  iXd++;
512  }//end loop on D
513  iWs+=incW;
514  iXs+=incX*D;
515  }//end loop on element
516 
517  return s;
518  }
519 
520 
521 };
522 
523 
524 #ifndef DIMENSION_TYPE
525 #define DIMENSION_TYPE
526 typedef tUCInt tDimension;//d in [0,256[
527 #endif
528 
530 
531 
532 
533 
534 #endif
this class describes an array of values T of dynamical size with algebrical operators and I is an imp...
Definition: CORE_Array.h:91
void setSize(const tIndex &n)
set the size of the container
Definition: CORE_Collection.h:104
this class describes an field. A field is composed by
Definition: CORE_Field.h:49
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_Field.h:102
void copy(const tIndex &n, const Q *vs)
initialize the field to the values of pointer of size n
Definition: CORE_Field.h:447
const T * getValues() const
get the values of the array for reading
Definition: CORE_Field.h:323
auto sbegin()
return begin stride iterator for writing
Definition: CORE_Field.h:276
auto send()
return end N-stride iterator for writing
Definition: CORE_Field.h:282
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:135
constexpr auto csbegin() const
return begin N-stride const iterator for writing
Definition: CORE_Field.h:289
const CORE_StdValArray< T > & getStorage() const
get the storage
Definition: CORE_Field.h:336
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
this class describes a standart arithmetic array type implemented with a std::valarray object of type...
Definition: CORE_StdValArray.h:14
this class describes a standart arithmetic array type implemented with a CORE_StdValArray object
Definition: CORE_StdValField.h:17
Self & operator=(Self &&values)
build an array by a copy of c
Definition: CORE_StdValField.h:154
void initialize(const std::array< Q, D > &a)
copy at element elemnt the array of size D
Definition: CORE_StdValField.h:192
T & scalarProduct(const CORE_Field< Q, K, D, S1, I1 > &X, T &s) const
return the weight scalar product
Definition: CORE_StdValField.h:463
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_StdValField.h:57
Self & operator=(std::initializer_list< T > &&values)
build an array by a copy of c
Definition: CORE_StdValField.h:106
virtual ~CORE_StdValField()
destroy an array of T*
Definition: CORE_StdValField.h:38
void elementsTransform(LambdaFct &&F)
apply the transform element with the lambda function Xid = F(Xid)
Definition: CORE_StdValField.h:376
Self & operator=(const std::vector< Q > &values)
build an array by a copy of c
Definition: CORE_StdValField.h:139
Self & operator=(const Self &values)
build an array by a copy of c
Definition: CORE_StdValField.h:147
Self & operator%=(const T &v) requires functions_type
modulo operator
Definition: CORE_StdValField.h:257
Self & operator/=(const CORE_Field< Q, K, D, S, I > &X)
array divisor operator
Definition: CORE_StdValField.h:345
Self & operator-=(const CORE_Field< Q, K, D, S, I > &X)
array sub operator
Definition: CORE_StdValField.h:322
void mod2(CORE_Array< T, I > &X) const
return the norm2 array per exch element
Definition: CORE_StdValField.h:442
static CORE_UniquePointer< Self > New()
return a new unique pointer of this
Definition: CORE_StdValField.h:68
void normalize()
normalize all the elements of the field return false if the method is not compatible with the floatin...
Definition: CORE_StdValField.h:386
Self & operator=(const std::initializer_list< T > &values)
build an array by a copy of c
Definition: CORE_StdValField.h:99
Self & operator=(const T &v)
fill the values of the array with v
Definition: CORE_StdValField.h:92
Self & operator=(const std::valarray< Q > &values)
build an array by a copy of c
Definition: CORE_StdValField.h:129
T & scalarProduct(const std::valarray< Q > &weights, const CORE_Field< Q, K, D, S1, I1 > &X, T &s) const
return the scalar product
Definition: CORE_StdValField.h:477
CORE_StdValField()
build an array of T*
Definition: CORE_StdValField.h:30
static void ElementsTransform(LambdaFct &&F, Self &X)
apply the transform element with the lambda function Ri = F(Xi)
Definition: CORE_StdValField.h:362
Self & operator*=(const T &v)
multiplicator operator
Definition: CORE_StdValField.h:239
Self & operator=(const std::array< Q, N > &values)
build an array by a copy of c
Definition: CORE_StdValField.h:118
Self & operator=(CORE_Field< Q, K, D, S1, I1 > &&values)
build an array by a copy of c
Definition: CORE_StdValField.h:177
Self & operator/=(const T &v)
divisor operator
Definition: CORE_StdValField.h:247
Self & operator+=(const T &v)
add operator
Definition: CORE_StdValField.h:223
Self & operator*=(const CORE_Field< Q, K, D, S, I > &X)
array multiply operator
Definition: CORE_StdValField.h:333
Self & operator=(const CORE_Field< Q, K, D, S1, I1 > &values)
build an array by a copy of c
Definition: CORE_StdValField.h:166
Self & operator-=(const T &v)
sub operator
Definition: CORE_StdValField.h:230