PeTar
N-body code for collisional gravitational systems
pikg_vector.hpp
Go to the documentation of this file.
1 #ifndef HPP_PIKG_VECTOR
2 #define HPP_PIKG_VECTOR
3 
4 #ifdef PIKG_USE_FDPS_VECTOR
5 #include <particle_simulator.hpp>
6 namespace PIKG = ParticleSimulator;
7 
8 namespace ParticleSimulator{
9 
10 #else
11 
12 #include <iostream>
13 #include <iomanip>
14 #include <cassert>
15 
16 namespace PIKG{
17  using F64 = double;
18  using F32 = float;
19  //using F16 = float16_t;
20  using U64 = uint64_t;
21  using U32 = uint32_t;
22  //using U16 = uint16_t;
23  using S64 = int64_t;
24  using S32 = int32_t;
25  //using S16 = int16_t;
26 
27  template<class T>
28  class Vector3{
29  public:
30  T x, y, z;
31  Vector3() : x(T(0)), y(T(0)), z(T(0)) {}
32  Vector3(const T _x, const T _y, const T _z) : x(_x), y(_y), z(_z) {}
33  Vector3(const T s) : x(s), y(s), z(s) {}
34  Vector3(const Vector3 & src) : x(src.x), y(src.y), z(src.z) {}
35 
36  typedef T DataType;
37  static const int DIM = 3;
38 
39  const Vector3 & operator = (const Vector3 & rhs){
40  x = rhs.x;
41  y = rhs.y;
42  z = rhs.z;
43  return (*this);
44  }
45 
46  const Vector3 & operator = (const T s){
47  x = y = z = s;
48  return (*this);
49  }
50 
51  Vector3 operator + (const Vector3 & rhs) const{
52  return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
53  }
54  const Vector3 & operator += (const Vector3 & rhs){
55  (*this) = (*this) + rhs;
56  return (*this);
57  }
58  Vector3 operator - (const Vector3 & rhs) const{
59  return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
60  }
61 
62  const Vector3 & operator -= (const Vector3 & rhs){
63  (*this) = (*this) - rhs;
64  return (*this);
65  }
66 
67  Vector3 operator * (const T s) const{
68  return Vector3(x * s, y * s, z * s);
69  }
70  const Vector3 & operator *= (const T s){
71  (*this) = (*this) * s;
72  return (*this);
73  }
74  friend Vector3 operator * (const T s, const Vector3 & v){
75  return (v * s);
76  }
77  Vector3 operator / (const T s) const{
78  return Vector3(x / s, y / s, z / s);
79  }
80  const Vector3 & operator /= (const T s){
81  (*this) = (*this) / s;
82  return (*this);
83  }
84 
85  const Vector3 & operator + () const {
86  return (* this);
87  }
88 
89  const Vector3 operator - () const {
90  return Vector3(-x, -y, -z);
91  }
92 
93  T operator * (const Vector3 & rhs) const{
94  return (x * rhs.x) + (y * rhs.y) + (z * rhs.z);
95  }
96 
97  Vector3 operator ^ (const Vector3 & rhs) const{
98  return Vector3( (y * rhs.z - z * rhs.y),
99  (z * rhs.x - x * rhs.z),
100  (x * rhs.y - y * rhs.x) );
101  }
102 
103  template <typename U>
104  operator Vector3<U> () const {
105  return Vector3<U> (static_cast<U>(x),
106  static_cast<U>(y),
107  static_cast<U>(z));
108  }
109 
110  friend std::ostream & operator <<(std::ostream & c, const Vector3 & u){
111  c<<u.x<<" "<<u.y<<" "<<u.z;
112  return c;
113  }
114 
115  friend std::istream & operator >>(std::istream & c, Vector3 & u){
116  c>>u.x; c>>u.y; c>>u.z;
117  return c;
118  }
119 
120  const T & operator[](const int i) const {
121  assert(i>=0 && i<3);
122  if(0==i) return x;
123  if(1==i) return y;
124  return z;
125  }
126 
127  T & operator[](const int i){
128  assert(i>=0 && i<3);
129  if(0==i) return x;
130  if(1==i) return y;
131  return z;
132  }
133 
134  bool operator == (const Vector3 & u) const {
135  return ( (x==u.x) && (y==u.y) && (z==u.z) );
136  }
137  bool operator != (const Vector3 & u) const {
138  return ( (x!=u.x) || (y!=u.y) || (z!=u.z) );
139  }
140  };
141 
142  template<typename T>
143  inline T max(const Vector3<T>& v){
144  T max_val = (v.x > v.y) ? v.x : v.y;
145  max_val = (max_val > v.z ) ? max_val : v.z;
146  return max_val;
147  }
148 
149  template<typename T>
150  inline T min(const Vector3<T>& v){
151  T min_val = (v.x < v.y) ? v.x : v.y;
152  min_val = (min_val < v.z ) ? min_val : v.z;
153  return min_val;
154  }
155 
156  template <>
157  inline Vector3<float> Vector3<float>::operator / (const float s) const {
158  const float inv_s = 1.0f/s;
159  return Vector3(x * inv_s, y * inv_s, z * inv_s);
160  }
161  template <>
162  inline Vector3<double> Vector3<double>::operator / (const double s) const {
163  const double inv_s = 1.0/s;
164  return Vector3(x * inv_s, y * inv_s, z * inv_s);
165  }
166 
171  //using F16vec = Vector3<F16>;
172 
173  template <typename T>
174  class Vector2{
175  public:
176  T x, y;
177  Vector2() : x(T(0)), y(T(0)) {}
178  Vector2(const T _x, const T _y) : x(_x), y(_y) {}
179  Vector2(const T s) : x(s), y(s) {}
180  Vector2(const Vector2 & src) : x(src.x), y(src.y) {}
181 
182  static const int DIM = 2;
183 
184  const Vector2 & operator = (const Vector2 & rhs){
185  x = rhs.x;
186  y = rhs.y;
187  return (*this);
188  }
189 
190  const Vector2 & operator = (const T s){
191  x = y = s;
192  return (*this);
193  }
194 
195  Vector2 operator + (const Vector2 & rhs) const{
196  return Vector2(x + rhs.x, y + rhs.y);
197  }
198  const Vector2 & operator += (const Vector2 & rhs){
199  (*this) = (*this) + rhs;
200  return (*this);
201  }
202  Vector2 operator - (const Vector2 & rhs) const{
203  return Vector2(x - rhs.x, y - rhs.y);
204  }
205  const Vector2 & operator -= (const Vector2 & rhs){
206  (*this) = (*this) - rhs;
207  return (*this);
208  }
209 
210  // vector scholar products
211  Vector2 operator * (const T s) const{
212  return Vector2(x * s, y * s);
213  }
214  const Vector2 & operator *= (const T s){
215  (*this) = (*this) * s;
216  return (*this);
217  }
218  friend Vector2 operator * (const T s, const Vector2 & v){
219  return (v * s);
220  }
221  Vector2 operator / (const T s) const{
222  return Vector2(x / s, y / s);
223  }
224  const Vector2 & operator /= (const T s){
225  (*this) = (*this) / s;
226  return (*this);
227  }
228 
229  const Vector2 & operator + () const {
230  return (* this);
231  }
232 
233  const Vector2 operator - () const {
234  return Vector2(-x, -y);
235  }
236 
237  // inner product
238  T operator * (const Vector2 & rhs) const{
239  return (x * rhs.x) + (y * rhs.y);
240  }
241 
242  // outer product (retruned value is scholar)
243  T operator ^ (const Vector2 & rhs) const{
244  const T z = (x * rhs.y) - (y * rhs.x);
245  return z;
246  }
247 
248  //cast to Vector2<U>
249  template <typename U>
250  operator Vector2<U> () const {
251  return Vector2<U> (static_cast<U>(x),
252  static_cast<U>(y));
253  }
254 
255  T getMax() const {
256  return x > y ? x : y;
257  }
258 
259  T getMin() const {
260  return x < y ? x : y;
261  }
262 
263  template <class F>
264  Vector2 applyEach(F f) const {
265  return Vector2(f(x), f(y));
266  }
267  template <class F>
268  friend Vector2 ApplyEach(F f, const Vector2 & arg1, const Vector2 & arg2){
269  return Vector2( f(arg1.x, arg2.x), f(arg1.y, arg2.y) );
270  }
271 
272  friend std::ostream & operator <<(std::ostream & c, const Vector2 & u){
273  c<<u.x<<" "<<u.y;
274  return c;
275  }
276 
277  friend std::istream & operator >>(std::istream & c, Vector2 & u){
278  c>>u.x; c>>u.y;
279  return c;
280  }
281 
282  T& operator[](const int i){
283  assert(i>=0 && i<2);
284  if(0==i) return x;
285  return y;
286  }
287  const T & operator[](const int i) const {
288  assert(i>=0 && i<2);
289  if(0==i) return x;
290  return y;
291  }
292 
293 
294  bool operator == (const Vector2 & u) const {
295  return ( (x==u.x) && (y==u.y) );
296  }
297  bool operator != (const Vector2 & u) const {
298  return ( (x!=u.x) || (y!=u.y) );
299  }
300 
301  };
302 
303  template <>
304  inline Vector2<float> Vector2<float>::operator / (const float s) const {
305  const float inv_s = 1.0f/s;
306  return Vector2(x * inv_s, y * inv_s);
307  }
308  template <>
309  inline Vector2<double> Vector2<double>::operator / (const double s) const {
310  const double inv_s = 1.0/s;
311  return Vector2(x * inv_s, y * inv_s);
312  }
313 
316  //using F16vec2 = Vector2<float16_t>;
317 
318 #endif
319 
320  template<class T>
321  class Vector4{
322  public:
323  T x, y, z, w;
324  Vector4() : x(T(0)), y(T(0)), z(T(0)), w(T(0)) {}
325  Vector4(const T _x, const T _y, const T _z, const T _w) : x(_x), y(_y), z(_z), w(_w) {}
326  Vector4(const T s) : x(s), y(s), z(s), w(s) {}
327  Vector4(const Vector4 & src) : x(src.x), y(src.y), z(src.z), w(src.w) {}
328 
329  typedef T DataType;
330  static const int DIM = 4;
331 
332  const Vector4 & operator = (const Vector4 & rhs){
333  x = rhs.x;
334  y = rhs.y;
335  z = rhs.z;
336  w = rhs.w;
337  return (*this);
338  }
339 
340  const Vector4 & operator = (const T s){
341  x = y = z = w = s;
342  return (*this);
343  }
344 
345  Vector4 operator + (const Vector4 & rhs) const{
346  return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.z);
347  }
348  const Vector4 & operator += (const Vector4 & rhs){
349  (*this) = (*this) + rhs;
350  return (*this);
351  }
352  Vector4 operator - (const Vector4 & rhs) const{
353  return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
354  }
355 
356  const Vector4 & operator -= (const Vector4 & rhs){
357  (*this) = (*this) - rhs;
358  return (*this);
359  }
360 
361  Vector4 operator * (const T s) const{
362  return Vector4(x * s, y * s, z * s, w * s);
363  }
364  const Vector4 & operator *= (const T s){
365  (*this) = (*this) * s;
366  return (*this);
367  }
368  friend Vector4 operator * (const T s, const Vector4 & v){
369  return (v * s);
370  }
371  Vector4 operator / (const T s) const{
372  return Vector4(x / s, y / s, z / s, w / s);
373  }
374  const Vector4 & operator /= (const T s){
375  (*this) = (*this) / s;
376  return (*this);
377  }
378 
379  const Vector4 & operator + () const {
380  return (* this);
381  }
382 
383  const Vector4 operator - () const {
384  return Vector4(-x, -y, -z, -w);
385  }
386 
387  T operator * (const Vector4 & rhs) const{
388  return (x * rhs.x) + (y * rhs.y) + (z * rhs.z) + (w * rhs.w);
389  }
390 
391 
392  template <typename U>
393  operator Vector4<U> () const {
394  return Vector4<U> (static_cast<U>(x),
395  static_cast<U>(y),
396  static_cast<U>(z),
397  static_cast<U>(w));
398  }
399 
400  friend std::ostream & operator <<(std::ostream & c, const Vector4 & u){
401  c<<u.x<<" "<<u.y<<" "<<u.z<<" "<<u.w;
402  return c;
403  }
404 
405  friend std::istream & operator >>(std::istream & c, Vector4 & u){
406  c>>u.x; c>>u.y; c>>u.z; c>>u.w;
407  return c;
408  }
409 
410  const T & operator[](const int i) const {
411  assert(i>=0 && i<3);
412  if(0==i) return x;
413  if(1==i) return y;
414  if(2==i) return z;
415  return w;
416  }
417 
418  T & operator[](const int i){
419  assert(i>=0 && i<3);
420  if(0==i) return x;
421  if(1==i) return y;
422  if(2==i) return z;
423  return w;
424  }
425 
426  bool operator == (const Vector4 & u) const {
427  return ( (x==u.x) && (y==u.y) && (z==u.z) && (w==u.w));
428  }
429  bool operator != (const Vector4 & u) const {
430  return ( (x!=u.x) || (y!=u.y) || (z!=u.z) || (w!=u.w));
431  }
432  };
433 
434  template<typename T>
435  inline T max(const Vector4<T>& v){
436  T max_val = (v.x > v.y) ? v.x : v.y;
437  max_val = (max_val > v.z ) ? max_val : v.z;
438  max_val = (max_val > v.w ) ? max_val : v.w;
439  return max_val;
440  }
441 
442  template<typename T>
443  inline T min(const Vector4<T>& v){
444  T min_val = (v.x < v.y) ? v.x : v.y;
445  min_val = (min_val < v.z ) ? min_val : v.z;
446  min_val = (min_val < v.w ) ? min_val : v.w;
447  return min_val;
448  }
449 
450  template <>
451  inline Vector4<float> Vector4<float>::operator / (const float s) const {
452  const float inv_s = 1.0f/s;
453  return Vector4(x * inv_s, y * inv_s, z * inv_s, w * inv_s);
454  }
455  template <>
456  inline Vector4<double> Vector4<double>::operator / (const double s) const {
457  const double inv_s = 1.0/s;
458  return Vector4(x * inv_s, y * inv_s, z * inv_s, w * inv_s);
459  }
460 
463  //using F16vec = Vector4<F16>;
464 };
465 
466 #endif
PIKG::Vector4
Definition: pikg_vector.hpp:321
PIKG::Vector4::operator[]
T & operator[](const int i)
Definition: pikg_vector.hpp:418
PIKG::Vector4::w
T w
Definition: pikg_vector.hpp:323
PIKG::Vector4::y
T y
Definition: pikg_vector.hpp:323
PIKG::Vector4::operator[]
const T & operator[](const int i) const
Definition: pikg_vector.hpp:410
PIKG::Vector3::z
T z
Definition: pikg_vector.hpp:30
PIKG::U32
uint32_t U32
Definition: pikg_vector.hpp:21
PIKG::Vector4::x
T x
Definition: pikg_vector.hpp:323
PIKG::Vector3::Vector3
Vector3(const T s)
Definition: pikg_vector.hpp:33
PIKG::S32
int32_t S32
Definition: pikg_vector.hpp:24
PIKG::Vector3::operator[]
T & operator[](const int i)
Definition: pikg_vector.hpp:127
PIKG::Vector3::DataType
T DataType
Definition: pikg_vector.hpp:36
PIKG::Vector4::Vector4
Vector4(const Vector4 &src)
Definition: pikg_vector.hpp:327
operator<<
std::ostream & operator<<(std::ostream &os, const IOParams< Type > &par)
Definition: io.hpp:75
PIKG::Vector3::y
T y
Definition: pikg_vector.hpp:30
PIKG::F64
double F64
Definition: pikg_vector.hpp:17
PIKG::Vector2::Vector2
Vector2(const T s)
Definition: pikg_vector.hpp:179
PIKG::Vector2::getMax
T getMax() const
Definition: pikg_vector.hpp:255
PIKG::Vector2::x
T x
Definition: pikg_vector.hpp:176
PIKG::Vector4::Vector4
Vector4(const T s)
Definition: pikg_vector.hpp:326
PIKG::Vector2::operator[]
T & operator[](const int i)
Definition: pikg_vector.hpp:282
PIKG::S64
int64_t S64
Definition: pikg_vector.hpp:23
PIKG::Vector2::operator[]
const T & operator[](const int i) const
Definition: pikg_vector.hpp:287
PIKG::Vector2
Definition: pikg_vector.hpp:174
PIKG::Vector2::applyEach
Vector2 applyEach(F f) const
Definition: pikg_vector.hpp:264
PIKG::Vector3::Vector3
Vector3(const T _x, const T _y, const T _z)
Definition: pikg_vector.hpp:32
PIKG::Vector3::operator[]
const T & operator[](const int i) const
Definition: pikg_vector.hpp:120
PIKG::Vector3::Vector3
Vector3(const Vector3 &src)
Definition: pikg_vector.hpp:34
PIKG::Vector4::Vector4
Vector4()
Definition: pikg_vector.hpp:324
PIKG::Vector3::Vector3
Vector3()
Definition: pikg_vector.hpp:31
PIKG::Vector4::Vector4
Vector4(const T _x, const T _y, const T _z, const T _w)
Definition: pikg_vector.hpp:325
PIKG::Vector2::getMin
T getMin() const
Definition: pikg_vector.hpp:259
PIKG::Vector4::DataType
T DataType
Definition: pikg_vector.hpp:329
PIKG::Vector3
Definition: pikg_vector.hpp:28
PIKG::min
T min(const Vector4< T > &v)
Definition: pikg_vector.hpp:443
PIKG::Vector3::x
T x
Definition: pikg_vector.hpp:30
PIKG::Vector2::Vector2
Vector2(const T _x, const T _y)
Definition: pikg_vector.hpp:178
PIKG
Definition: pikg_vector.hpp:16
PIKG::max
T max(const Vector4< T > &v)
Definition: pikg_vector.hpp:435
PIKG::Vector4::z
T z
Definition: pikg_vector.hpp:323
PIKG::Vector2::y
T y
Definition: pikg_vector.hpp:176
PIKG::Vector2::Vector2
Vector2()
Definition: pikg_vector.hpp:177
PIKG::Vector2::Vector2
Vector2(const Vector2 &src)
Definition: pikg_vector.hpp:180
PIKG::Vector2::ApplyEach
friend Vector2 ApplyEach(F f, const Vector2 &arg1, const Vector2 &arg2)
Definition: pikg_vector.hpp:268
PIKG::U64
uint64_t U64
Definition: pikg_vector.hpp:20
PIKG::F32
float F32
Definition: pikg_vector.hpp:18