summaryrefslogtreecommitdiff
path: root/lib/wolff_models/vector.hpp
blob: 4106e62ebccc914f6c1e4b79acd9972e7d0a2201 (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

#ifndef WOLFF_MODELS_VECTOR_H
#define WOLFF_MODELS_VECTOR_H

#include <cmath>
#include <array>
#include <iostream>

namespace wolff {

  template <unsigned q, class T>
  class vector_t : public std::array<T, q> {
    public: 

      vector_t() {
        this->fill((T)0);
        (*this)[0] = (T)1;
      }

      vector_t(const T *x) {
        for (unsigned i = 0; i < q; i++) {
          (*this)[i] = x[i];
        }
      }

      typedef vector_t <q, T> M_t;
      typedef vector_t <q, double> F_t;

      template <class U>
      inline vector_t<q, T>& operator+=(const vector_t<q, U> &v) {
        for (unsigned i = 0; i < q; i++) {
          (*this)[i] += (T)v[i];
        }
        return *this;
      }

      template <class U>
      inline vector_t<q, T>& operator-=(const vector_t<q, U> &v) {
        for (unsigned i = 0; i < q; i++) {
          (*this)[i] -= (T)v[i];
        }
        return *this;
      }

      inline vector_t<q, T> operator*(unsigned x) const {
        vector_t<q, T> result;
        for (unsigned i = 0; i < q; i++) {
          result[i] = x * (*this)[i];
        }

        return result;
      }

      inline vector_t<q, double> operator*(double x) const {
        vector_t<q, double> result;
        for (unsigned i = 0; i < q; i++) {
          result[i] = x * (*this)[i];
        }

        return result;
      }

      inline vector_t<q, T> operator-(const vector_t<q, T>& v) const {
        vector_t<q, T> diff = *this;
        diff -= v;
        return diff;
      }

      inline T operator*(const vector_t<q, T>& v) const {
        double prod = 0;

        for (unsigned i = 0; i < q; i++) {
          prod += v[i] * (*this)[i];
        }

        return prod;
      }

      template <class U>
      inline vector_t<q, T> operator/(U a) const {
        vector_t<q, T> result;
        for (unsigned i = 0; i < q; i++) {
          result[i] = (*this)[i] / a;
        }

        return result;
      }
  };

  template<unsigned q, class T>
  inline vector_t<q, T> operator*(unsigned a, const vector_t<q, T>&v) {
    return v * a;
  }

  template<unsigned q, class T>
  inline vector_t<q, double> operator*(double a, const vector_t<q, T>&v) {
    return v * a;
  }

  template<unsigned q, class T>
  std::ostream& operator<<(std::ostream& os, const vector_t<q, T>&v) {
    os << "( ";
    for (T vi : v) {
      os << vi << " ";
    }
    os << ")";
    return os;
  }

}

#endif