QMCPACK
TinyVector.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #ifndef OHMMS_TINYVECTOR_H
15 #define OHMMS_TINYVECTOR_H
16 
17 /***************************************************************************
18  *
19  * \class TinyVector
20  * \brief Pooma/AppyTypes/Vecktor is modified to work with PETE.
21  *
22  * The POOMA Framework
23  *
24  * This program was prepared by the Regents of the University of
25  * California at Los Alamos National Laboratory (the University) under
26  * Contract No. W-7405-ENG-36 with the U.S. Department of Energy (DOE).
27  * The University has certain rights in the program pursuant to the
28  * contract and the program should not be copied or distributed outside
29  * your organization. All rights in the program are reserved by the DOE
30  * and the University. Neither the U.S. Government nor the University
31  * makes any warranty, express or implied, or assumes any liability or
32  * responsibility for the use of this software
33  *
34  * Visit http://www.acl.lanl.gov/POOMA for more details
35  *
36  ***************************************************************************/
37 
38 
39 // include files
40 #include <iomanip>
41 #include <cmath>
42 #include "PETE/PETE.h"
45 
46 namespace qmcplusplus
47 {
48 /** Fixed-size array. candidate for array<T,D>
49  */
50 template<class T, unsigned D>
51 struct TinyVector
52 {
53  using Type_t = T;
54  enum
55  {
56  Size = D
57  };
58  T X[Size];
59 
60  // Default Constructor initializes to zero.
61  inline TinyVector()
62  {
63  for (size_t d = 0; d < D; ++d)
64  X[d] = T(0);
65  }
66 
67  // Copy Constructor
68  inline TinyVector(const TinyVector& rhs) = default;
69 
70  // Move Constructor
71  inline TinyVector(TinyVector&& rhs) = default;
72 
73  // Templated TinyVector constructor.
74  template<class T1>
75  inline TinyVector(const TinyVector<T1, D>& rhs)
76  {
77  for (size_t d = 0; d < D; ++d)
78  X[d] = rhs[d];
79  }
80 
81  // Constructor from a single T
82  inline TinyVector(const T& x00)
83  {
84  for (size_t d = 0; d < D; ++d)
85  X[d] = x00;
86  }
87 
88  // Constructors for fixed dimension
89  inline TinyVector(const T& x00, const T& x01)
90  {
91  X[0] = x00;
92  X[1] = x01;
93  }
94  inline TinyVector(const T& x00, const T& x01, const T& x02)
95  {
96  X[0] = x00;
97  X[1] = x01;
98  X[2] = x02;
99  }
100  inline TinyVector(const T& x00, const T& x01, const T& x02, const T& x03)
101  {
102  X[0] = x00;
103  X[1] = x01;
104  X[2] = x02;
105  X[3] = x03;
106  }
107 
108  inline TinyVector(const T* restrict base, int offset)
109  {
110 #pragma unroll
111  for (int i = 0; i < D; ++i)
112  X[i] = base[i * offset];
113  }
114 
115  inline int size() const { return D; }
116 
117  inline TinyVector& operator=(const TinyVector& rhs) = default;
118  inline TinyVector& operator=(TinyVector&& rhs) = default;
119 
120  template<class T1>
122  {
123  for (size_t d = 0; d < D; ++d)
124  X[d] = rhs[d];
125  return *this;
126  }
127 
128  inline TinyVector<T, D>& operator=(const T& rhs)
129  {
130  for (size_t d = 0; d < D; ++d)
131  X[d] = rhs;
132  return *this;
133  }
134 
135  // Get and Set Operations
136  inline Type_t& operator[](unsigned int i) { return X[i]; }
137  inline const Type_t& operator[](unsigned int i) const { return X[i]; }
138  inline Type_t* data() { return X; }
139  inline const Type_t* data() const { return X; }
140  inline Type_t* begin() { return X; }
141  inline const Type_t* begin() const { return X; }
142  inline Type_t* end() { return X + D; }
143  inline const Type_t* end() const { return X + D; }
144 
146  {
148  for (size_t d = 0; d < D; ++d)
149  inverse[d] = -X[d];
150  return inverse;
151  }
152 
153  // Elementwise comparison
154  bool operator==(const TinyVector<T, D>& that) const
155  {
156  for (int i = 0; i < D; ++i)
157  {
158  if ((*this)[i] != that[i])
159  return false;
160  }
161  return true;
162  }
163 
164  bool operator!=(const TinyVector<T, D>& that) const
165  {
166  for (int i = 0; i < D; ++i)
167  {
168  if ((*this)[i] == that[i])
169  return false;
170  }
171  return true;
172  }
173 };
174 
175 // Adding binary operators using macro defined in OhmmsTinyMeta.h
176 OHMMS_META_ACCUM_OPERATORS(TinyVector, operator+=, OpAddAssign)
177 OHMMS_META_ACCUM_OPERATORS(TinyVector, operator-=, OpSubtractAssign)
178 OHMMS_META_ACCUM_OPERATORS(TinyVector, operator*=, OpMultiplyAssign)
179 OHMMS_META_ACCUM_OPERATORS(TinyVector, operator/=, OpDivideAssign)
180 
181 OHMMS_META_BINARY_OPERATORS(TinyVector, operator+, OpAdd)
182 OHMMS_META_BINARY_OPERATORS(TinyVector, operator-, OpSubtract)
183 OHMMS_META_BINARY_OPERATORS(TinyVector, operator*, OpMultiply)
184 OHMMS_META_BINARY_OPERATORS(TinyVector, operator/, OpDivide)
185 
186 //----------------------------------------------------------------------
187 // dot product
188 //----------------------------------------------------------------------
189 template<class T1, class T2, unsigned D>
191 {
192  return OTDot<TinyVector<T1, D>, TinyVector<T2, D>>::apply(lhs, rhs);
193 }
194 
195 //----------------------------------------------------------------------
196 // cross product
197 //----------------------------------------------------------------------
198 
199 template<class T1, class T2, unsigned D>
201  const TinyVector<T2, D>& rhs)
202 {
203  return OTCross<TinyVector<T1, D>, TinyVector<T2, D>>::apply(lhs, rhs);
204 }
205 
206 //----------------------------------------------------------------------
207 // cross product
208 //----------------------------------------------------------------------
209 
210 template<class T1, class T2, unsigned D>
212  const TinyVector<T2, D>& rhs)
213 {
214  return OuterProduct<TinyVector<T1, D>, TinyVector<T2, D>>::apply(lhs, rhs);
215 }
216 
217 //----------------------------------------------------------------------
218 // I/O
219 template<class T>
221 {};
222 
223 /** template functor for Vector<TinyVector<T,D> streamn output
224  * 0 equivalent values are output as 0.
225  * In the case of complex or integer type T's this can be added if needed.
226  * But now you don't pay for this if unless T is actually real.
227  */
228 template<class T, unsigned D>
230 {
231  inline static void print(std::ostream& os, const TinyVector<T, D>& r)
232  {
233  for (int d = 0; d < D; d++)
234  if constexpr(IsComplex_t<T>::value)
235  os << std::setw(18) << std::setprecision(10) << r[d];
236  else if constexpr(IsReal_t<T>::value)
237  if (FP_ZERO == std::fpclassify(r[d]))
238  os << std::setw(18) << std::setprecision(10) << 0;
239  else
240  os << std::setw(18) << std::setprecision(10) << r[d];
241  else
242  os << r[d];
243  }
244 };
245 
246 template<class T, unsigned D>
247 std::ostream& operator<<(std::ostream& out, const TinyVector<T, D>& rhs)
248 {
250  return out;
251 }
252 
253 template<class T, unsigned D>
254 std::istream& operator>>(std::istream& is, TinyVector<T, D>& rhs)
255 {
256  //printTinyVector<TinyVector<T,D> >::print(out,rhs);
257  for (int i = 0; i < D; i++)
258  is >> rhs[i];
259  return is;
260 }
261 } // namespace qmcplusplus
262 
263 #endif // OHMMS_TINYVECTOR_H
TinyVector & operator=(const TinyVector &rhs)=default
typename Promote< T1, T2 >::Type_t Type_t
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void print(OptimizableFunctorBase &func, std::ostream &os, double extent)
evaluates a functor (value and derivative) and dumps the quantities to output
bool operator!=(const TinyVector< T, D > &that) const
Definition: TinyVector.h:164
TinyVector(const T *restrict base, int offset)
Definition: TinyVector.h:108
const Type_t * end() const
Definition: TinyVector.h:143
TinyVector(const T &x00)
Definition: TinyVector.h:82
TinyVector(const T &x00, const T &x01, const T &x02)
Definition: TinyVector.h:94
const Type_t & operator[](unsigned int i) const
Definition: TinyVector.h:137
Tensor<T,D> class for D by D tensor.
Definition: OhmmsTinyMeta.h:32
TinyVector< T, D > & operator=(const TinyVector< T1, D > &rhs)
Definition: TinyVector.h:121
TinyVector(const T &x00, const T &x01)
Definition: TinyVector.h:89
Tensor< typename BinaryReturn< T1, T2, OpMultiply >::Type_t, D > outerProduct(const TinyVector< T1, D > &lhs, const TinyVector< T2, D > &rhs)
Definition: TinyVector.h:211
TinyVector< T, D > & operator=(const T &rhs)
Definition: TinyVector.h:128
const Type_t * data() const
Definition: TinyVector.h:139
TinyVector operator-() const
Definition: TinyVector.h:145
bool operator==(const TinyVector< T, D > &that) const
Definition: TinyVector.h:154
#define OHMMS_META_BINARY_OPERATORS(TENT, FUNC, TAG)
Definition: OhmmsTinyMeta.h:75
Type_t & operator[](unsigned int i)
Definition: TinyVector.h:136
TinyVector< typename BinaryReturn< T1, T2, OpMultiply >::Type_t, D > cross(const TinyVector< T1, D > &lhs, const TinyVector< T2, D > &rhs)
Definition: TinyVector.h:200
const Type_t * begin() const
Definition: TinyVector.h:141
static void print(std::ostream &os, const TinyVector< T, D > &r)
Definition: TinyVector.h:231
Tensor< T, D > inverse(const Tensor< T, D > &a)
Definition: TensorOps.h:879
TinyVector(const TinyVector< T1, D > &rhs)
Definition: TinyVector.h:75
Tensor< typename BinaryReturn< T1, T2, OpMultiply >::Type_t, D > dot(const AntiSymTensor< T1, D > &lhs, const AntiSymTensor< T2, D > &rhs)
#define OHMMS_META_ACCUM_OPERATORS(TENT, FUNC, TAG)
std::is_floating_point< T > IsReal_t
std::istream & operator>>(std::istream &is, Matrix< T, Alloc > &rhs)
Definition: OhmmsMatrix.h:427
TinyVector(const T &x00, const T &x01, const T &x02, const T &x03)
Definition: TinyVector.h:100