summaryrefslogtreecommitdiff
path: root/lib/include/wolff/models/vector.hpp
blob: e4c4a1c0761f88a77abf18550b134f24be260a5e (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

#ifndef WOLFF_MODELS_VECTOR_H
#define WOLFF_MODELS_VECTOR_H

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

namespace wolff {

#include <wolff/types.h>

template <q_t 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 (q_t 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 (q_t 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 (q_t i = 0; i < q; i++) {
        (*this)[i] -= (T)v[i];
      }
      return *this;
    }

    inline vector_t<q, T> operator*(v_t x) const {
      vector_t<q, T> result;
      for (q_t 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 (q_t 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 (q_t 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 (q_t i = 0; i < q; i++) {
        result[i] = (*this)[i] / a;
      }

      return result;
    }
};

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

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

template<q_t 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