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);
26 const T &operator[](unsigned i) const;
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 T &Vector<T, N>::operator[](unsigned i)
63 template<typename T, unsigned N>
64 const T &Vector<T, N>::operator[](unsigned i) const
69 template<typename T, unsigned N>
70 inline Vector<T, N> &Vector<T, N>::operator*=(T s)
72 for(unsigned i=0; i<N; ++i)
77 template<typename T, unsigned N>
78 inline Vector<T, N> operator*(const Vector<T, N> &v, T s)
84 template<typename T, unsigned N>
85 inline Vector<T, N> operator*(T s, const Vector<T, N> &v)
90 template<typename T, unsigned N>
91 inline Vector<T, N> &Vector<T, N>::operator/=(T s)
93 for(unsigned i=0; i<N; ++i)
98 template<typename T, unsigned N>
99 inline Vector<T, N> operator/(const Vector<T, N> &v, T s)
105 template<typename T, unsigned N>
106 inline Vector<T, N> &Vector<T, N>::operator+=(const Vector<T, N> &v)
108 for(unsigned i=0; i<N; ++i)
113 template<typename T, unsigned N>
114 inline Vector<T, N> operator+(const Vector<T, N> &v1, const Vector<T, N> &v2)
120 template<typename T, unsigned N>
121 inline Vector<T, N> &Vector<T, N>::operator-=(const Vector<T, N> &v)
123 for(unsigned i=0; i<N; ++i)
128 template<typename T, unsigned N>
129 inline Vector<T, N> operator-(const Vector<T, N> &v1, const Vector<T, N> &v2)
135 template<typename T, unsigned N>
136 inline Vector<T, N> operator-(const Vector<T, N> &v)
139 for(unsigned i=0; i<N; ++i)
144 template<typename T, unsigned N>
145 inline T inner_product(const Vector<T, N> &v1, const Vector<T, N> &v2)
148 for(unsigned i=0; i<N; ++i)
153 template<typename T, unsigned N>
154 inline T Vector<T, N>::norm() const
156 return sqrt(inner_product(*this, *this));
159 template<typename T, unsigned N>
160 inline Vector<T, N> &Vector<T, N>::normalize()
162 return *this /= norm();
165 template<typename T, unsigned N>
166 inline Vector<T, N> normalize(const Vector<T, N> &v)
169 return r.normalize();