1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#include <getopt.h>
#include <iomanip>
#include <random>
#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"
#include "eigen/Eigen/Dense"
#include "eigen/unsupported/Eigen/CXX11/Tensor"
#include "eigen/unsupported/Eigen/CXX11/TensorSymmetry"
using Rng = randutils::random_generator<pcg32>;
using Real = double;
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
class Tensor : public Eigen::Tensor<Real, 3> {
using Eigen::Tensor<Real, 3>::Tensor;
public:
Matrix operator*(const Vector& x) const {
std::array<Eigen::IndexPair<int>, 1> ip20 = {Eigen::IndexPair<int>(2, 0)};
const Eigen::Tensor<Real, 1> xT = Eigen::TensorMap<const Eigen::Tensor<Real, 1>>(x.data(), x.size());
const Eigen::Tensor<Real, 2> JxT = contract(xT, ip20);
return Eigen::Map<const Matrix>(JxT.data(), dimension(0), dimension(1));
}
};
Vector normalize(const Vector& x) {
return x * sqrt(x.size() / x.squaredNorm());
}
class Spherical3Spin {
private:
Tensor J;
public:
unsigned N;
Spherical3Spin(unsigned N, Rng& r) : J(N, N, N), N(N) {
Eigen::StaticSGroup<Eigen::Symmetry<0,1>, Eigen::Symmetry<1,2>> sym123;
for (unsigned i = 0; i < N; i++) {
for (unsigned j = i; j < N; j++) {
for (unsigned k = j; k < N; k++) {
sym123(J, i, j, k) = r.variate<Real, std::normal_distribution>(0, sqrt(12) / N);
}
}
}
}
std::tuple<Real, Vector, Matrix> H_∂H_∂∂H(const Vector& x) const {
Matrix ∂∂H = J * x;
Vector ∂H = ∂∂H * x / 2;
Real H = ∂H.dot(x) / 6;
return {H, ∂H, ∂∂H};
}
};
class ConstrainedHeight {
private:
Vector x0;
Real E;
public:
Spherical3Spin S;
unsigned N;
ConstrainedHeight(unsigned N, Real E, Rng& r) : x0(N), E(E), S(N, r), N(N) {
for (Real& x0ᵢ : x0) {
x0ᵢ = r.variate<Real, std::normal_distribution>();
}
x0 = normalize(x0);
}
Real overlap(const Vector& v) const {
return v.head(N).dot(x0) / N;
}
std::tuple<Vector, Matrix> ∂L_∂∂L(const Vector& v) const {
Vector x = v.head(N);
Real ω₀ = v(N);
Real ω₁ = v(N + 1);
auto [H, ∂H∂x, ∂²H∂²x] = S.H_∂H_∂∂H(x);
Vector ∂L∂x = x0 + ω₀ * x + ω₁ * ∂H∂x;
Real ∂L∂ω₀ = 0.5 * (x.squaredNorm() - N);
Real ∂L∂ω₁ = H - N * E;
Vector ∂L(N + 2);
∂L << ∂L∂x, ∂L∂ω₀, ∂L∂ω₁;
Matrix ∂L²∂x² = ω₀ * Matrix::Identity(N, N) + ω₁ * ∂²H∂²x;
Vector ∂²L∂x∂ω₀ = x;
Vector ∂²L∂x∂ω₁ = ∂H∂x;
Matrix ∂∂L(N + 2, N + 2);
∂∂L <<
∂L²∂x², ∂²L∂x∂ω₀, ∂²L∂x∂ω₁,
∂²L∂x∂ω₀.transpose(), 0, 0,
∂²L∂x∂ω₁.transpose(), 0, 0;
return {∂L, ∂∂L};
}
Vector newtonMethod(const Vector& v0, Real γ = 1) {
Vector v = v0;
Vector ∂L;
Matrix ∂∂L;
while (std::tie(∂L, ∂∂L) = ∂L_∂∂L(v), ∂L.squaredNorm() > 1e-10) {
v -= γ * ∂∂L.partialPivLu().solve(∂L);
v.head(N) = normalize(v.head(N)); // might as well stay on the sphere
}
return v;
}
};
int main(int argc, char* argv[]) {
unsigned N = 10;
double E = 0;
double γ = 1;
unsigned samples = 10;
int opt;
while ((opt = getopt(argc, argv, "N:E:g:n:")) != -1) {
switch (opt) {
case 'N':
N = (unsigned)atof(optarg);
break;
case 'E':
E = atof(optarg);
break;
case 'g':
γ = atof(optarg);
break;
case 'n':
samples = atoi(optarg);
break;
default:
exit(1);
}
}
Rng r;
Vector x = Vector::Zero(N);
x(0) = sqrt(N);
std::cout << std::setprecision(15);
for (unsigned sample = 0; sample < samples; sample++) {
Vector v0(N + 2);
v0 << x,
r.variate<Real, std::normal_distribution>(),
r.variate<Real, std::normal_distribution>();
ConstrainedHeight M(N, E, r);
Vector v = M.newtonMethod(v0, γ);
std::cout << M.overlap(v) << std::endl;
}
return 0;
}
|