summaryrefslogtreecommitdiff
path: root/lib/torus.h
blob: 2aead525c2ebd35afcba29b4bbb9757d2e4d917d (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

#pragma once

#include <cmath>
#include <array>
#include "types.h"

template <q_t n>
class torus_t : public std::array<double, n> {
  public:
    typedef std::array<double, n> M_t;
    typedef std::array<double, n> F_t;

    torus_t() {
      this->fill(0);
    }

    inline torus_t<n> operator*(v_t a) const {
      torus_t<n> x;
      for (q_t i = 0; i < n; i++) {
        x[i] = a * (*this)[i];
      }

      return x;
    }

    inline torus_t<n> operator*(double a) const {
      torus_t<n> x;
      for (q_t i = 0; i < n; i++) {
        x[i] = a * (*this)[i];
      }

      return x;
    }

    inline torus_t<n>& operator+=(const torus_t<n>& x) {
      for (q_t i = 0; i < n; i++) {
        (*this)[i] += x[i];
      }
    }

    inline torus_t<n>& operator-=(const torus_t<n>& x) {
      for (q_t i = 0; i < n; i++) {
        (*this)[i] -= x[i];
      }
    }
};

template <q_t n>
double norm_squared(const torus_t<n>& x) {
  double tmp = 0;
  for (const double& xi : x) {
    tmp += pow(xi, 2);
  }
  return tmp;
}

void write_magnetization(const torus_t<n>& x, FILE *outfile) {
  for (const double& xi : x) {
    float tmp_xi = (float)xi;
    fwrite(&tmp_xi, sizeof(float), 1, outfile);
  }
}