1 #ifndef MSP_LINAL_VECTOR_H_
2 #define MSP_LINAL_VECTOR_H_
11 A general mathematical vector.
13 template<typename T, unsigned N>
23 Vector(const Vector<U, N> &v);
25 T &operator[](unsigned i) { return data[i]; }
26 const T &operator[](unsigned i) const { return data[i]; }
28 Vector &operator*=(T);
29 Vector &operator/=(T);
30 Vector &operator+=(const Vector &);
31 Vector &operator-=(const Vector &);
38 template<typename T, unsigned N>
39 inline Vector<T, N>::Vector()
41 std::fill(data, data+N, T());
44 template<typename T, unsigned N>
45 inline Vector<T, N>::Vector(const T *d)
47 std::copy(d, d+N, data);
50 template<typename T, unsigned N>
52 inline Vector<T, N>::Vector(const Vector<U, N> &v)
54 std::copy(v.data, v.data+N, data);
57 template<typename T, unsigned N>
58 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
60 for(unsigned i=0; i<N; ++i)
65 template<typename T, unsigned N>
66 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
72 template<typename T, unsigned N>
73 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
78 template<typename T, unsigned N>
79 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
81 for(unsigned i=0; i<N; ++i)
86 template<typename T, unsigned N>
87 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
93 template<typename T, unsigned N>
94 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
96 for(unsigned i=0; i<N; ++i)
101 template<typename T, unsigned N>
102 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
108 template<typename T, unsigned N>
109 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
111 for(unsigned i=0; i<N; ++i)
116 template<typename T, unsigned N>
117 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
123 template<typename T, unsigned N>
124 inline Vector<T, N> operator-(const Vector<T, N> &v)
127 for(unsigned i=0; i<N; ++i)
132 template<typename T, unsigned N>
133 inline bool operator==(const Vector<T, N> &v, const Vector<T, N> &w)
135 for(unsigned i=0; i<N; ++i)
141 template<typename T, unsigned N>
142 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
145 for(unsigned i=0; i<N; ++i)
150 template<typename T, unsigned N>
151 inline T Vector<T, N>::norm() const
153 return sqrt(inner_product(*this, *this));
156 template<typename T, unsigned N>
157 inline Vector<T, N> &Vector<T, N>::normalize()
159 return *this /= norm();
162 template<typename T, unsigned N>
163 inline Vector<T, N> normalize(const Vector<T, N> &v)
166 return r.normalize();