]> git.tdb.fi Git - libs/math.git/blob - source/linal/vector.h
Another big batch of stuff
[libs/math.git] / source / linal / vector.h
1 #ifndef MSP_LINAL_VECTOR_H_
2 #define MSP_LINAL_VECTOR_H_
3
4 #include <algorithm>
5 #include <cmath>
6
7 namespace Msp {
8 namespace LinAl {
9
10 /**
11 A general mathematical vector.
12 */
13 template<typename T, unsigned N>
14 class Vector
15 {
16 protected:
17         T data[N];
18
19 public:
20         Vector();
21         Vector(const T *d);
22         template<typename U>
23         Vector(const Vector<U, N> &v);
24
25         T &operator[](unsigned i);
26         const T &operator[](unsigned i) const;
27
28         Vector &operator*=(T);
29         Vector &operator/=(T);
30         Vector &operator+=(const Vector &);
31         Vector &operator-=(const Vector &);
32
33         T norm() const;
34         Vector &normalize();
35 };
36
37
38 template<typename T, unsigned N>
39 inline Vector<T, N>::Vector()
40 {
41         std::fill(data, data+N, T());
42 }
43
44 template<typename T, unsigned N>
45 inline Vector<T, N>::Vector(const T *d)
46 {
47         std::copy(d, d+N, data);
48 }
49
50 template<typename T, unsigned N>
51 template<typename U>
52 inline Vector<T, N>::Vector(const Vector<U, N> &v)
53 {
54         std::copy(v.data, v.data+N, data);
55 }
56
57 template<typename T, unsigned N>
58 T &Vector<T, N>::operator[](unsigned i)
59 {
60         return data[i];
61 }
62
63 template<typename T, unsigned N>
64 const T &Vector<T, N>::operator[](unsigned i) const
65 {
66         return data[i];
67 }
68
69 template<typename T, unsigned N>
70 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
71 {
72         for(unsigned i=0; i<N; ++i)     
73                 data[i] *= s;
74         return *this;
75 }
76
77 template<typename T, unsigned N>
78 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
79 {
80         Vector<T, N> r(v);
81         return r *= s;
82 }
83
84 template<typename T, unsigned N>
85 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
86 {
87         return v*s;
88 }
89
90 template<typename T, unsigned N>
91 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
92 {
93         for(unsigned i=0; i<N; ++i)     
94                 data[i] /= s;
95         return *this;
96 }
97
98 template<typename T, unsigned N>
99 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
100 {
101         Vector<T, N> r(v);
102         return r /= s;
103 }
104
105 template<typename T, unsigned N>
106 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
107 {
108         for(unsigned i=0; i<N; ++i)     
109                 data[i] += v[i];
110         return *this;
111 }
112
113 template<typename T, unsigned N>
114 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
115 {
116         Vector<T, N> r(v1);
117         return r += v2;
118 }
119
120 template<typename T, unsigned N>
121 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
122 {
123         for(unsigned i=0; i<N; ++i)     
124                 data[i] -= v[i];
125         return *this;
126 }
127
128 template<typename T, unsigned N>
129 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
130 {
131         Vector<T, N> r(v1);
132         return r -= v2;
133 }
134
135 template<typename T, unsigned N>
136 inline Vector<T, N> operator-(const Vector<T, N> &v)
137 {
138         Vector<T, N> r(v);
139         for(unsigned i=0; i<N; ++i)
140                 r[i] = -r[i];
141         return r;
142 }
143
144 template<typename T, unsigned N>
145 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
146 {
147         for(unsigned i=0; i<N; ++i)
148                 if(v[i]!=w[i])
149                         return false;
150         return true;
151 }
152
153 template<typename T, unsigned N>
154 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
155 {
156         T r = T();
157         for(unsigned i=0; i<N; ++i)
158                 r += v1[i]*v2[i];
159         return r;
160 }
161
162 template<typename T, unsigned N>
163 inline T Vector<T, N>::norm() const
164 {
165         return sqrt(inner_product(*this, *this));
166 }
167
168 template<typename T, unsigned N>
169 inline Vector<T, N> &Vector<T, N>::normalize()
170 {
171         return *this /= norm();
172 }
173
174 template<typename T, unsigned N>
175 inline Vector<T, N> normalize(const Vector<T, N> &v)
176 {
177         Vector<T, N> r(v);
178         return r.normalize();
179 }
180
181 } // namespace LinAl
182 } // namespace Msp
183
184 #endif