LatticeYangMills
random.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (c) 2018 Giovanni Pederiva
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 ******************************************************************************/
25 
34 #include "Math/su3.h"
35 #include "Math/random.h"
36 #include "Math/complex.h"
37 #include <random>
38 #include <boost/random.hpp>
39 
40 std::random_device Random::rd;
41 boost::random::mt19937 Random::randomGen(Random::rd());
42 boost::random::uniform_real_distribution<double> Random::randomUniformInterval(0.0, 1.0);
43 
48  return randomUniformInterval(randomGen);
49 }
50 
55  SU3 result;
56  complex v1[3], v2[3];
57  complex u1[3], u2[3], u3[3];
58 
59  // Create 2 random vectors
60  for(int i = 0; i < 3; i++){
61  v1[i].real = randUniform();
62  v1[i].imag = randUniform();
63  v2[i].real = randUniform();
64  v2[i].imag = randUniform();
65  }
66 
67  // Normalize vector v1 into u1
68  double w1 = 0;
69  for(int i = 0; i < 3; i++)
70  w1 += v1[i].real*v1[i].real + v1[i].imag*v1[i].imag;
71  w1 = sqrt(w1);
72  for(int i = 0; i < 3; i++)
73  u1[i] = v1[i]/w1;
74 
75  // Orthogonalize vector v2 to vector u1
76  complex v2u1;
77  for(int i = 0; i < 3; i++){
78  v2u1 += v2[i]*(~u1[i]);
79  }
80  for(int i = 0; i < 3; i++){
81  v2[i] = v2[i] - v2u1*u1[i];
82  }
83 
84  // Normalize vector v2 into u2
85  double w2 = 0;
86  for(int i = 0; i < 3; i++)
87  w2 += v2[i].real*v2[i].real + v2[i].imag*v2[i].imag;
88  w2 = sqrt(w2);
89  for(int i = 0; i < 3; i++)
90  u2[i] = v2[i]/w2;
91 
92  // Create vector u3 as u1xu2
93  u3[0] = ~(u1[1]*u2[2] - u1[2]*u2[1]);
94  u3[1] = ~(u1[2]*u2[0] - u1[0]*u2[2]);
95  u3[2] = ~(u1[0]*u2[1] - u1[1]*u2[0]);
96 
97  for(int i = 0; i < 3; i++){
98  result.mat[2*i] = u1[i].real;
99  result.mat[2*i+1] = u1[i].imag;
100  result.mat[6+2*i] = u2[i].real;
101  result.mat[6+2*i+1] = u2[i].imag;
102  result.mat[12+2*i] = u3[i].real;
103  result.mat[12+2*i+1] = u3[i].imag;
104  }
105  return std::move(result);
106 }
107 
118 SU3 Random::randSU3Transf(double epsilon){
119  SU3 R, S, T;
120  double x[4];
121  double norm;
122  // Create 3 SU2 matrices and map them on SU3 elements
123  R.setSU3Identity();
124  S.setSU3Identity();
125  T.setSU3Identity();
126 
127  norm = 0;
128  x[0] = randUniform() - 0.5;
129  for(int i = 1; i < 4; i++){
130  x[i] = randUniform() - 0.5;
131  norm += x[i]*x[i];
132  }
133  norm = sqrt(norm);
134 
135  if(x[0] < 0)
136  x[0] = -sqrt(1-epsilon*epsilon);
137  else
138  x[0] = sqrt(1-epsilon*epsilon);
139  for(int i = 1; i < 4; i++)
140  x[i] = x[i] * epsilon / norm;
141  R.mat[0] = x[0];
142  R.mat[8] = x[0];
143  R.mat[1] = x[3];
144  R.mat[9] = -x[3];
145 
146  R.mat[2] = x[2];
147  R.mat[6] = -x[2];
148  R.mat[3] = x[1];
149  R.mat[7] = x[1];
150 
151 
152  norm = 0;
153  x[0] = randUniform() - 0.5;
154  for(int i = 1; i < 4; i++){
155  x[i] = randUniform() - 0.5;
156  norm += x[i]*x[i];
157  }
158  norm = sqrt(norm);
159 
160  if(x[0] < 0)
161  x[0] = -sqrt(1-epsilon*epsilon);
162  else
163  x[0] = sqrt(1-epsilon*epsilon);
164  for(int i = 1; i < 4; i++)
165  x[i] = x[i] * epsilon / norm;
166  S.mat[0] = x[0];
167  S.mat[16] = x[0];
168  S.mat[1] = x[3];
169  S.mat[17] = -x[3];
170 
171  S.mat[4] = x[2];
172  S.mat[12] = -x[2];
173  S.mat[5] = x[1];
174  S.mat[13] = x[1];
175 
176 
177 
178  norm = 0;
179  x[0] = randUniform() - 0.5;
180  for(int i = 1; i < 4; i++){
181  x[i] = randUniform() - 0.5;
182  norm += x[i]*x[i];
183  }
184  norm = sqrt(norm);
185 
186  if(x[0] < 0)
187  x[0] = -sqrt(1-epsilon*epsilon);
188  else
189  x[0] = sqrt(1-epsilon*epsilon);
190  for(int i = 1; i < 4; i++)
191  x[i] = x[i] * epsilon / norm;
192  T.mat[8] = x[0];
193  T.mat[16] = x[0];
194  T.mat[9] = x[3];
195  T.mat[17] = -x[3];
196 
197  T.mat[10] = x[2];
198  T.mat[14] = -x[2];
199  T.mat[11] = x[1];
200  T.mat[15] = x[1];
201 
202  return std::move(R*S*T);
203 }
static SU3 randSU3()
returns random SU3 matrix, by choosing 2 random complex vectors as the first two columns, orthogonalizing them and taking the outer product as a third column
Definition: random.cpp:54
Implementation of a class to perform arithmetics between links.
Definition: su3.h:53
static double randUniform()
returns a number from a uniform distribution between 0 and 1
Definition: random.cpp:47
static SU3 randSU3Transf(double epsilon)
returns a random SU3 element with controlled spread around the unity matrix.
Definition: random.cpp:118
Basic library to implement SU3 matrix arithmetics and functions.
Contains the definition of the Random class.