1 #ifndef MSP_LINAL_DYNAMICVECTOR_H_
2 #define MSP_LINAL_DYNAMICVECTOR_H_
11 class size_mismatch: public std::logic_error
14 size_mismatch(const std::string &w): std::logic_error(w) { }
15 virtual ~size_mismatch() throw() { }
20 A general mathematical vector. Size is specified at runtime. May be slower
21 than a fixed-size vector. There are no compile-time diagnostics for mismatched
28 typedef T ElementType;
35 DynamicVector(unsigned);
36 DynamicVector(unsigned, const T *);
37 DynamicVector(const DynamicVector &);
38 DynamicVector &operator=(const DynamicVector &);
41 unsigned size() const { return size_; }
43 T &operator[](unsigned);
44 const T &operator[](unsigned) const;
46 DynamicVector &operator*=(T);
47 DynamicVector &operator/=(T);
48 DynamicVector &operator+=(const DynamicVector &);
49 DynamicVector &operator-=(const DynamicVector &);
52 DynamicVector &normalize();
56 inline DynamicVector<T>::DynamicVector(unsigned s):
60 std::fill(data, data+size_, T());
64 inline DynamicVector<T>::DynamicVector(unsigned s, const T *d):
68 std::copy(d, d+size_, data);
72 inline DynamicVector<T>::DynamicVector(const DynamicVector &other):
76 std::copy(other.data, other.data+size_, data);
80 inline DynamicVector<T> &DynamicVector<T>::operator=(const DynamicVector<T> &other)
82 if(size_!=other.size())
89 std::copy(other.data, other.data+size_, data);
95 inline DynamicVector<T>::~DynamicVector()
101 inline T &DynamicVector<T>::operator[](unsigned i)
104 throw std::out_of_range("DynamicVector::operator[]");
110 inline const T &DynamicVector<T>::operator[](unsigned i) const
113 throw std::out_of_range("DynamicVector::operator[]");
119 inline DynamicVector<T> &DynamicVector<T>::operator*=(T s)
121 for(unsigned i=0; i<size_; ++i)
127 inline DynamicVector<T> operator*(const DynamicVector<T> &v, T s)
129 DynamicVector<T> r(v);
134 inline DynamicVector<T> operator*(T s, const DynamicVector<T> &v)
140 inline DynamicVector<T> &DynamicVector<T>::operator/=(T s)
142 for(unsigned i=0; i<size_; ++i)
148 inline DynamicVector<T> operator/(const DynamicVector<T> &v, T s)
150 DynamicVector<T> r(v);
155 inline DynamicVector<T> &DynamicVector<T>::operator+=(const DynamicVector<T> &v)
158 throw size_mismatch("vector+vector");
160 for(unsigned i=0; i<size_; ++i)
161 data[i] += v.data[i];
167 inline DynamicVector<T> operator+(const DynamicVector<T> &v1, const DynamicVector<T> &v2)
169 DynamicVector<T> r(v1);
174 inline DynamicVector<T> &DynamicVector<T>::operator-=(const DynamicVector<T> &v)
177 throw size_mismatch("vector-vector");
179 for(unsigned i=0; i<size_; ++i)
180 data[i] -= v.data[i];
186 inline DynamicVector<T> operator-(const DynamicVector<T> &v1, const DynamicVector<T> &v2)
188 DynamicVector<T> r(v1);
193 inline DynamicVector<T> operator-(const DynamicVector<T> &v)
195 DynamicVector<T> r(v);
196 for(unsigned i=0; i<r.size(); ++i)
202 inline bool operator==(const DynamicVector<T> &v1, const DynamicVector<T> &v2)
204 if(v1.size()!=v2.size())
205 throw size_mismatch("vector==vector");
207 for(unsigned i=0; i<v1.size(); ++i)
215 inline T inner_product(const DynamicVector<T> &v1, const DynamicVector<T> &v2)
217 if(v1.size()!=v2.size())
218 throw size_mismatch("inner_product");
221 for(unsigned i=0; i<v1.size(); ++i)
227 inline T DynamicVector<T>::norm() const
230 return sqrt(inner_product(*this, *this));
234 inline DynamicVector<T> &DynamicVector<T>::normalize()
236 return *this /= norm();
240 inline DynamicVector<T> normalize(const DynamicVector<T> &v)
242 DynamicVector<T> r(v);
243 return r.normalize();