summaryrefslogtreecommitdiff
path: root/lib/include/wolff/models/orthogonal.hpp
blob: 514c88a117895860c2fda5553956379f7c832655 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

#ifndef WOLFF_MODELS_ORTHOGONAL_H
#define WOLFF_MODELS_ORTHOGONAL_H

#include <random>
#include <cmath>

#include "../system.hpp"
#include "vector.hpp"

namespace wolff {

#include "../types.h"

template <q_t q, class T>
class orthogonal_t : public std::array<std::array<T, q>, q> {
  public :
  bool is_reflection;

  orthogonal_t() : is_reflection(false) {
    for (q_t i = 0; i < q; i++) {
      (*this)[i].fill(0);
      (*this)[i][i] = (T)1;
    }
  }

  vector_t<q, T> act(const vector_t <q, T>& v) const {
    vector_t <q, T> v_rot;
    v_rot.fill(0);

    if (is_reflection) {
      double prod = 0;
      for (q_t i = 0; i < q; i++) {
        prod += v[i] * (*this)[0][i];
      }
      for (q_t i = 0; i < q; i++) {
        v_rot[i] = v[i] - 2 * prod * (*this)[0][i];
      }
    } else {
      for (q_t i = 0; i < q; i++) {
        for (q_t j = 0; j < q; j++) {
          v_rot[i] += (*this)[i][j] * v[j];
        }
      }
    }

    return v_rot;
  }

  orthogonal_t<q, T> act(const orthogonal_t <q, T>& m) const {
    orthogonal_t <q, T> m_rot;

    m_rot.is_reflection = false;

    if (is_reflection) {
      for (q_t i = 0; i < q; i++) {
        double akOki = 0;

        for (q_t k = 0; k < q; k++) {
          akOki += (*this)[0][k] * m[k][i];
        }

        for (q_t j = 0; j < q; j++) {
          m_rot[j][i] = m[j][i] - 2 * akOki * (*this)[0][j];
        }
      }
    } else {
      for (q_t i = 0; i < q; i++) {
        m_rot[i].fill(0);
        for (q_t j = 0; j < q; j++) {
          for (q_t k = 0; k < q; k++) {
            m_rot[i][j] += (*this)[i][j] * m[j][k];
          }
        }
      }
    }

    return m_rot;
  }

  vector_t <q, T> act_inverse(const vector_t <q, T>& v) const {
    if (is_reflection) {
      return this->act(v); // reflections are their own inverse
    } else {
      vector_t <q, T> v_rot;
      v_rot.fill(0);

      for (q_t i = 0; i < q; i++) {
        for (q_t j = 0; j < q; j++) {
          v_rot[i] += (*this)[j][i] * v[j];
        }
      }

      return v_rot;
    }
  }

  vector_t <q, T> act_inverse(const orthogonal_t <q, T>& m) const {
    if (is_reflection) {
      return this->act(m); // reflections are their own inverse
    } else {
      orthogonal_t <q, T> m_rot;
      m_rot.is_reflection = false;

      for (q_t i = 0; i < q; i++) {
        m_rot[i].fill(0);
        for (q_t j = 0; j < q; j++) {
          for (q_t k = 0; k < q; k++) {
            m_rot[i][j] += (*this)[j][i] * m[j][k];
          }
        }
      }

      return m_rot;
    }
  }

};

template <q_t q>
orthogonal_t <q, double> generate_rotation_uniform (std::mt19937& r, const system<orthogonal_t<q, double>, vector_t<q, double>>&, v_t) {
  std::normal_distribution<double> dist(0.0,1.0);
  orthogonal_t <q, double> ptr;
  ptr.is_reflection = true;

  double v2 = 0;

  for (q_t i = 0; i < q; i++) {
    ptr[0][i] = dist(r);
    v2 += ptr[0][i] * ptr[0][i];
  }

  double mag_v = sqrt(v2);

  for (q_t i = 0; i < q; i++) {
    ptr[0][i] /= mag_v;
  }

  return ptr;
}

template <q_t q>
orthogonal_t <q, double> generate_rotation_perturbation (std::mt19937& r, const system<orthogonal_t<q, double>, vector_t<q, double>>& S, v_t i0, double epsilon, unsigned int n) {
  std::normal_distribution<double> dist(0.0,1.0);
  orthogonal_t <q, double> m;
  m.is_reflection = true;

  vector_t <q, double> v;

  if (n > 1) {
    std::uniform_int_distribution<unsigned int> udist(0, n);
    unsigned int rotation = udist(r);

    double cosr = cos(2 * M_PI * rotation / (double)n / 2.0);
    double sinr = sin(2 * M_PI * rotation / (double)n / 2.0);

    v[0] = S.s[i0][0] * cosr - S.s[i0][1] * sinr;
    v[1] = S.s[i0][1] * cosr + S.s[i0][0] * sinr;

    for (q_t i = 2; i < q; i++) {
      v[i] = S.s[i0][i];
    }
  } else {
    v = S.s[i0];
  }

  double m_dot_v = 0;

  for (q_t i = 0; i < q; i++) {
    m[0][i] = dist(r); // create a random vector
    m_dot_v += m[0][i] * v[i];
  }

  double v2 = 0;

  for (q_t i = 0; i < q; i++) {
    m[0][i] = m[0][i] - m_dot_v * v[i]; // find the component orthogonal to v
    v2 += pow(m[0][i], 2);
  }

  double mag_v = sqrt(v2);

  for (q_t i = 0; i < q; i++) {
    m[0][i] /= mag_v; // normalize
  }

  v2 = 0;

  double factor = epsilon * dist(r);

  for (q_t i = 0; i < q; i++) {
    m[0][i] += factor * v[i]; // perturb orthogonal vector in original direction
    v2 += pow(m[0][i], 2);
  }

  mag_v = sqrt(v2);

  for (q_t i = 0; i < q; i++) {
    m[0][i] /= mag_v; // normalize
  }

  return m;
}

}

#endif