NDDEM
Tools.h
Go to the documentation of this file.
1 #ifndef TOOLS
2 #define TOOLS
3 #include <cstdlib>
4 #include <cmath>
5 #include <cstdio>
6 #include <vector>
7 #include <cmath>
8 #include "../Dem/Typedefs.h"
9 #include <boost/math/special_functions/factorials.hpp>
10 #include <boost/random.hpp>
11 
12 template <int d>
13 class Tools
14 {
15 public:
16  static void initialise ();
17  static void clear() ;
18  static void matvecmult (v1d & res, cv1d &A, cv1d &v);
19  static double hyperspherical_xtophi (cv1d &x, v1d &phi);
20  static void hyperspherical_phitox (double r, cv1d &phi, v1d &x) ;
21 
22  static double normsq (const vector <double> & a) {double res=0 ; for (int i=0 ; i<d ; i++) res+=a[i]*a[i] ; return (res) ; }
23  static int sgn (uint8_t a) {return a & (128) ? -1:1 ; }
24  static v1d transpose (cv1d & a) {v1d b (d*d,0) ; for (int i=0 ; i<d*d ; i++) b[(i/d)*d+i%d] = a[(i%d)*d+(i/d)] ; return b ; }
25  static void transpose_inplace (v1d & a) { for (int i=0 ; i<d ; i++) for (int j=i+1 ; j<d ; j++) std::swap(a[i*d+j], a[j*d+i]) ; }
26 
27  static v1d Eye ;
28  static boost::random::mt19937 rng ;
29  static boost::random::uniform_01<boost::mt19937> rand ;
30 
31 private:
32  static vector < vector <int> > MSigns ;
33  static vector < vector <int> > MIndexAS ;
34  static vector < pair <int,int> > MASIndex ;
35  static vector <FILE *> outs ;
36 };
37 
38 // Static member definitions ---------------------------------------------------
39 template <int d> vector < vector <int> > Tools<d>::MSigns ;
40 template <int d> vector < vector <int> > Tools<d>::MIndexAS ;
41 template <int d> vector < pair <int,int> > Tools<d>::MASIndex;
42 template <int d> vector < double > Tools<d>::Eye ;
43 template <int d> vector <FILE *> Tools<d>::outs ;
44 template <int d> boost::random::mt19937 Tools<d>::rng ;
45 template <int d> boost::random::uniform_01<boost::mt19937> Tools<d>::rand(rng) ;
46 
47 //--------------------------------------------------------------------------------
48 // All vector operators
49 inline v1d operator* (v1d a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b ; return a ; }
50 inline v1f operator* (v1f a, float b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b ; return a ; }
51 inline v1d operator* (v1d a, cv1d &b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b[i] ; return a ; }
52 inline v1f operator* (v1f a, cv1f &b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b[i] ; return a ; }
53 inline v1d operator+ (v1d a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]+=b ; return a ; }
54 inline v1d operator+ (v1d a, cv1d &b) {for (uint i=0 ; i<a.size() ; i++) a[i]+=b[i] ; return a ; }
55 inline v1f operator+ (v1f a, cv1f &b) {for (uint i=0 ; i<a.size() ; i++) a[i]+=b[i] ; return a ; }
56 inline v1d operator- (v1d a, double &b) {for (uint i=0 ; i<a.size() ; i++) a[i]-=b ; return a ; }
57 inline v1d operator- (v1d a, cv1d &b) {for (uint i=0 ; i<a.size() ; i++) a[i]-=b[i] ; return a ; }
58 inline v1d operator- (v1d a) {for (uint i=0 ; i<a.size() ; i++) a[i]=-a[i] ; return a ; }
59 inline v1f operator- (v1f a, cv1f &b) {for (uint i=0 ; i<a.size() ; i++) a[i]-=b[i] ; return a ; }
60 inline v1d operator/ (v1d a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]/=b ; return a ; }
61 inline v1d & operator-= (v1d & a, cv1d &b) {for (uint i=0 ; i<a.size() ; i++) a[i]-=b[i] ; return a; }
62 inline v1d & operator*= (v1d & a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b ; return a; }
63 inline v1f & operator*= (v1f & a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]*=b ; return a; }
64 inline v1d & operator+= (v1d & a, cv1d &b) {for (uint i=0 ; i<a.size() ; i++) a[i]+=b[i] ; return a; }
65 inline v1f & operator+= (v1f & a, cv1f &b) {for (uint i=0 ; i<a.size() ; i++) a[i]+=b[i] ; return a; }
66 inline v1f & operator/= (v1f & a, cv1f &b) {for (uint i=0 ; i<a.size() ; i++) a[i]/=b[i] ; return a; }
67 inline v1d & operator/= (v1d & a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]/=b ; return a; }
68 inline v1f & operator/= (v1f & a, double b) {for (uint i=0 ; i<a.size() ; i++) a[i]/=b ; return a; }
69 
70 /*****************************************************************************************************
71  * *
72  * *
73  * *
74  * IMPLEMENTATIONS *
75  * *
76  * *
77  * *
78  * ***************************************************************************************************/
79 template <int d>
81 {
82  MSigns.resize(d, vector <int> (d, 0)) ;
83  for (int i=0 ; i<d ; i++) for (int j=0 ; j<d ; j++) MSigns[i][j]=(i<j)*(1)+(i>j)*(-1) ;
84 
85  MIndexAS.resize(d, vector < int > (d,0)) ;
86  MASIndex.resize(d*(d-1)/2, make_pair(0,0)) ;
87  int n=0 ;
88  for (int i=0 ; i<d ; i++)
89  for (int j=i+1 ; j<d ; j++,n++)
90  {
91  MIndexAS[i][j]=n ; MIndexAS[j][i]=n ;
92  MASIndex[n]=make_pair(i,j) ;
93  //MASIndex.push_back(make_pair(i, j)) ;
94  }
95 
96  Eye.clear() ; Eye.resize(d*d,0) ;
97  for (int de=0 ; de<d ; de++) Eye[de*d+de]=1 ; //initial orientation matrix
98 }
99 //===================================
100 template <int d>
101 void Tools<d>::clear()
102 {
103  MSigns.clear() ;
104  MIndexAS.clear() ;
105  MASIndex.clear() ;
106  Eye.clear() ;
107 }
108 //-------------------------------
109 template <int d>
110 void Tools<d>::matvecmult (v1d & res, cv1d &A, cv1d &v)
111 {
112  for (int i=0 ; i<d ; i++) res[i] = 0.0;
113  for (int i=0 ; i<d; i++)
114  for (int k=0 ; k<d ; k++)
115  res[i]+=A[i*d+k]*v[k] ;
116 }
117 //--------------------------------
118 template <int d>
119 double Tools<d>::hyperspherical_xtophi (cv1d &x, v1d &phi) // WARNING NOT EXTENSIVELY TESTED
120 {
121  double rsqr = normsq(x) ; // norm of x_rotated
122  double r= sqrt(rsqr) ;
123  int j;
124  phi=vector<double>(d-1, 0) ;
125  for (j=d-1 ; j>=0 && fabs(x[j])<1e-6 ; j--) ;
126  int lastnonzero=j ;
127  for (j=0 ; j<d-1 ; j++)
128  {
129  if (j==lastnonzero)
130  {
131  if (x[j]<0) phi[j]=M_PI ;
132  else phi[j]=0 ;
133  return r ;
134  }
135  phi[j] = acos(x[j]/sqrt(rsqr)) ;
136  //printf("[%g %g]", x[j], sqrt(rsqr)) ;
137  if (isnan(phi[j])) {phi[j]=acos(sgn(x[j])*x[j]) ;} //TODO Check that ................
138  rsqr -= x[j]*x[j] ;
139  }
140  //printf("%g %g %g | %g | %g %g \n ", x[0], x[1], x[2], normsq(x), phi[0], phi[1]) ;
141  if (x[d-1]<0) phi[d-2] = 2*M_PI - phi[d-2] ;
142  return r ;
143 }
144 
145 template <int d>
146 void Tools<d>::hyperspherical_phitox (double r, cv1d &phi, v1d &x) // WARNING NOT EXTENSIVELY TESTED
147 {
148  x = v1d (d,r) ;
149  for (int i=0 ; i<d-1 ; i++)
150  {
151  x[i] *= cos(phi[i]) ;
152  for (int j=i+1 ; j<d ; j++)
153  x[j] *= sin(phi[i]) ;
154  }
155  x[d-1] *= sin(phi[d-2]) ;
156 }
157 #endif
boost::random::mt19937 rng(static_cast< unsigned int >(std::time(nullptr)))
v1d & operator+=(v1d &a, cv1d &b)
Definition: Tools.h:64
v1d & operator-=(v1d &a, cv1d &b)
Definition: Tools.h:61
v1f & operator/=(v1f &a, cv1f &b)
Definition: Tools.h:66
v1d operator/(v1d a, double b)
Definition: Tools.h:60
v1d & operator*=(v1d &a, double b)
Definition: Tools.h:62
v1d operator*(v1d a, double b)
Definition: Tools.h:49
v1d operator+(v1d a, double b)
Definition: Tools.h:53
Static class to handle multi-dimensional mathematics, and more. It gets specialised for speed with te...
Definition: Tools.h:101
static double hyperspherical_xtophi(cv1d &x, v1d &phi)
static void clear()
static void hyperspherical_phitox(double r, cv1d &phi, v1d &x)
static v1d transpose(cv1d &a)
Definition: Tools.h:24
static double normsq(const vector< double > &a)
Definition: Tools.h:22
static void transpose_inplace(v1d &a)
Definition: Tools.h:25
static void initialise()
static void matvecmult(v1d &res, cv1d &A, cv1d &v)
static int sgn(uint8_t a)
Definition: Tools.h:23
const vector< float > cv1f
Definition: Typedefs.h:16
static vector< pair< int, int > > MASIndex
For skew symmetric matrix, make the correspondance between linear index and (row,column) index.
Definition: Tools.h:287
static v1d Eye
The identity matrix in dimension <d>
Definition: Tools.h:281
static vector< FILE * > outs
Store the output file descriptors.
Definition: Tools.h:292
static vector< vector< int > > MSigns
For skew symetric matrix. -1 below the diagonal, 0 on the diagonal, +1 above the diagnal.
Definition: Tools.h:290
vector< float > v1f
Definition: Typedefs.h:12
static boost::random::uniform_01< boost::mt19937 > rand
Returns a random number between 0 and 1.
Definition: Tools.h:283
unsigned int uint
Definition: Typedefs.h:8
static void matvecmult(v1d &res, cv1d &A, cv1d &B)
Multiply a matrix with a vector, in place.
Definition: Tools.h:701
static double hyperspherical_xtophi(cv1d &x, v1d &phi)
Convert from cartesian to hyperspherical coordinates.
Definition: Tools.h:889
const vector< double > cv1d
Definition: Typedefs.h:13
static void hyperspherical_phitox(double r, cv1d &phi, v1d &x)
Convert from hyperspherical to cartesian coordinates.
Definition: Tools.h:916
static void clear()
Get the class ready for a different dimension.
Definition: Tools.h:339
vector< double > v1d
Definition: Typedefs.h:9
static vector< vector< int > > MIndexAS
For skew symmetric matrix, make the correspondance between linear index of a full matrix with the lin...
Definition: Tools.h:291
static void initialise()
Initialise the member variables, in particular the variables to handle skew-symmetric flattened matri...
Definition: Tools.h:318
static boost::random::mt19937 rng
Random number generator.
Definition: Tools.h:282
v1d operator-(v1d a, double b)
uint d
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:25399
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
unsigned char uint8_t
Definition: stdint.h:124