Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
class_vec_3d.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include <array>
11 
17 template <typename T>
18 class Vec_3D : public std::array<T, 3>
19 {
20 public:
21  // CONSTRUCTORS
22  Vec_3D(){};
23 
24  template<typename ...U> Vec_3D(U...init):
25  std::array<T, 3>{init...} {}
26 
27  template<typename U> Vec_3D(const Vec_3D<U>& vec):
28  std::array<T, 3>({T(std::get<0>(vec)), T(std::get<1>(vec)), T(std::get<2>(vec))}) {}
29 
30  // METHODS
31  T norm2() const
32  {
33  T tmp(0);
34  for (T val : *this) tmp += val*val;
35  return tmp;
36  }
37 
38  auto norm() const -> decltype(sqrt(norm2())) { return sqrt(norm2()); }
39 
40  // OPERATORS
41  template<typename U>
42  Vec_3D<T>& operator+=(const Vec_3D<U>& rhs)
43  {
44  for(size_t i = 0; i < 3; ++i) (*this)[i] += rhs[i];
45  return *this;
46  }
47 
48  template<typename U>
49  Vec_3D<T>& operator-=(const Vec_3D<U>& rhs)
50  {
51  for(size_t i = 0; i < 3; ++i) (*this)[i] -= rhs[i];
52  return *this;
53  }
54 
55  template<typename U>
56  Vec_3D<T>& operator*=(U rhs)
57  {
58  for(T& val : *this) val *= rhs;
59  return *this;
60  }
61 
62  template<typename U>
63  Vec_3D<T>& operator/=(U rhs)
64  {
65  for(T& val : *this) val /= rhs;
66  return *this;
67  }
68 };
69 
70 // NON-MEMBER FUNCTIONS
71 template <typename T, typename U>
73 {
74  lhs += rhs;
75  return lhs;
76 }
77 
78 template <typename T, typename U>
80 {
81  lhs -= rhs;
82  return lhs;
83 }
84 
85 template <typename T, typename U>
87 {
88  lhs *= rhs;
89  return lhs;
90 }
91 
92 template <typename T, typename U>
93 Vec_3D<T> operator*(T lhs, const Vec_3D<U>& rhs)
94 {
95  Vec_3D<T> tmp;
96  for(size_t i = 0; i < 3; ++i) tmp[i] = lhs*rhs[i];
97  return tmp;
98 }
99 
100 template <typename T, typename U>
102 {
103  for(T& val : lhs) val /= rhs;
104  return lhs;
105 }
Vec_3D< T > operator*(Vec_3D< T > lhs, U rhs)
Definition: json.hpp:20160
Vec_3D(U...init)
Vec_3D< T > operator-(Vec_3D< T > lhs, const Vec_3D< U > &rhs)
Vec_3D< T > operator+(Vec_3D< T > lhs, const Vec_3D< U > &rhs)
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
Vec_3D< T > operator/(Vec_3D< T > lhs, U rhs)
auto operator+=(std::string &lhs, StringRef const &sr) -> std::string &
: class handling basic 3D-vector functions, definitions