fmatvec  2.0.0
Fast Matrix/Vector Library
fixed_vector.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef fixed_vector_h
23 #define fixed_vector_h
24 
25 #include "fixed_general_matrix.h"
26 #include <vector>
27 #include <cstring>
28 #include <stdexcept>
29 
30 namespace fmatvec {
31 
40  template <int M, class AT>
41  class Vector<Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<1>,AT> {
42  using Matrix<General,Fixed<M>,Fixed<1>,AT>::ele;
43 
44  public:
45 
46  typedef AT* iterator;
47  typedef const AT* const_iterator;
48 
49  typedef AT value_type;
50 
52 
53  protected:
54 
55  template<class Row> inline Vector<Fixed<M>,AT>& copy(const Vector<Row,AT> &x);
56 
58 
59  public:
60 
61  explicit Vector(Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
62  explicit Vector(Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
63  explicit Vector(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { FMATVEC_ASSERT(m==M, AT); }
64  explicit Vector(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { FMATVEC_ASSERT(m==M, AT); }
65 
73  Vector(const Vector<Fixed<M>,AT> &x) = default;
74 
79  template<class Row>
80  Vector(const Vector<Row,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
81  }
82 
87  template<class Type, class Row, class Col>
88  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
89  }
90 
103  Vector(const char *str) : Matrix<General,Fixed<M>,Fixed<1>,AT>(str) {
104  }
105 
112  inline Vector<Fixed<M>,AT>& operator=(const Vector<Fixed<M>,AT> &x) = default;
113 
120  template <class Row>
121  inline Vector<Fixed<M>,AT>& operator=(const Vector<Row,AT> &x) {
122  FMATVEC_ASSERT(x.size() == M, AT);
123  return copy(x);
124  }
125 
132  template <class Row>
133  inline Vector<Fixed<M>,AT>& operator<<=(const Vector<Row,AT> &x) { return operator=(x); }
134 
135  template <class AT2>
136  operator Vector<Fixed<M>,AT2>() const {
137  Vector<Fixed<M>,AT2> ret;
138  for(size_t i=0; i<M; ++i)
139  ret(i) = (*this)(i);
140  return ret;
141  }
142 
151  AT& operator()(int i) {
152 
153  FMATVEC_ASSERT(i>=0, AT);
154  FMATVEC_ASSERT(i<M, AT);
155 
156  return e(i);
157  }
158 
163  const AT& operator()(int i) const {
164 
165  FMATVEC_ASSERT(i>=0, AT);
166  FMATVEC_ASSERT(i<M, AT);
167 
168  return e(i);
169  }
170 
171  iterator begin() { return &ele[0][0]; }
172  iterator end() { return &ele[M][0]; }
173  const_iterator begin() const { return &ele[0][0]; }
174  const_iterator end() const { return &ele[M][0]; }
175  const_iterator cbegin() const noexcept { return &ele[0][0]; }
176  const_iterator cend() const noexcept { return &ele[M][0]; }
177 
178  AT& e(int i) {
179  return ele[i][0];
180  }
181 
186  const AT& e(int i) const {
187  return ele[i][0];
188  }
189 
197  inline Vector<Fixed<M>,AT>& init(const AT& val=AT());
198  inline Vector<Fixed<M>,AT>& init(Init, const AT& a=AT()) { return init(a); }
199  inline Vector<Fixed<M>,AT>& init(Noinit, const AT& a=AT()) { return *this; }
200 
205  int size() const {return M;}
206 
209  void resize(int m) {
210  if(m!=M)
211  throw std::runtime_error("A fixed vector cannot be resized.");
212  }
213 
220  int inc() const {return 1;}
221 
222  template <int M1, int M2>
223  inline const Vector<Fixed<M2-M1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I) const;
224 
225  using Matrix<General,Fixed<M>,Fixed<1>,AT>::operator();
226 
231  explicit inline operator std::vector<AT>() const;
232 
237  explicit inline Vector(const std::vector<AT> &v);
238 
239 // /*! \brief Cast to AT.
240 // *
241 // * \return The AT representation of the vector
242 // * */
243 // explicit operator AT() const {
244 // FMATVEC_ASSERT(M==1, AT);
245 // return ele[0][0];
246 // }
247 //
248 // /*! \brief AT Constructor.
249 // * Constructs and initializes a vector with a AT object.
250 // * \param x The AT the vector will be initialized with.
251 // * */
252 // explicit Vector(const AT &x) : Matrix<General,Fixed<M>,Fixed<1>,AT>(x) { }
253 
257  inline const RowVector<Fixed<M>,AT> T() const;
258 
262  template<class Type, class Row, class Col>
263  inline void set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A);
264  };
265 
266  template <int M, class AT>
267  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::init(const AT &val) {
268  for(int i=0; i<M; i++)
269  e(i) = val;
270  return *this;
271  }
272 
273  template <int M, class AT> template <int M1, int M2>
274  inline const Vector<Fixed<M2-M1+1>,AT> Vector<Fixed<M>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2>> &I) const {
275  FMATVEC_ASSERT(M2<M, AT);
276  Vector<Fixed<M2-M1+1>,AT> x(NONINIT);
277 
278  for(int i=0; i<x.size(); i++)
279  x.e(i) = e(M1+i);
280 
281  return x;
282  }
283 
284  template <int M, class AT>
285  inline const RowVector<Fixed<M>,AT> Vector<Fixed<M>,AT>::T() const {
286  RowVector<Fixed<M>,AT> x(NONINIT);
287  for(int i=0; i<M; i++)
288  x.e(i) = e(i);
289  return x;
290  }
291 
292  template <int M, class AT>
293  inline Vector<Fixed<M>,AT>::operator std::vector<AT>() const {
294  std::vector<AT> ret(size());
295  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
296  return ret;
297  }
298 
299  template <int M, class AT>
300  inline Vector<Fixed<M>,AT>::Vector(const std::vector<AT> &v) : Matrix<General,Fixed<M>,Fixed<1>,AT>() {
301  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
302  }
303 
305 
306  template <int M, class AT> template<class Row>
307  inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::copy(const Vector<Row,AT> &x) {
308  for(int i=0; i<M; i++)
309  e(i) = x.e(i);
310  return *this;
311  }
312 
314 
315  template<int M, class AT> template<class Type, class Row, class Col>
316  inline void Vector<Fixed<M>,AT>::set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A) {
317  Matrix<General,Fixed<M>,Fixed<1>,AT>::set(I, Range<Var,Var>(0,0), A);
318  }
319 
320 }
321 
322 #endif
Definition: fmatvec.h:38
Vector(const Vector< Row, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:80
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Definition: types.h:85
void resize(int m)
Definition: fixed_vector.h:209
Definition: fmatvec.h:50
int inc() const
Increment.
Definition: fixed_vector.h:220
Vector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:88
Vector< Fixed< M >, AT > & operator=(const Vector< Row, AT > &x)
Assignment operator.
Definition: fixed_vector.h:121
Definition: fmatvec.h:47
const AT & e(int i) const
Element operator.
Definition: fixed_vector.h:186
int size() const
Size.
Definition: fixed_vector.h:205
This is an index class for creating submatrices.
Definition: range.h:35
Namespace fmatvec.
Definition: _memory.cc:28
Definition: types.h:84
const AT & operator()(int i) const
Element operator.
Definition: fixed_vector.h:163
Basic shape class for matrices.
Definition: types.h:132
AT & operator()(int i)
Element operator.
Definition: fixed_vector.h:151
Vector(const char *str)
String Constructor.
Definition: fixed_vector.h:103
This is an index class for creating submatrices.
Definition: range.h:44