fmatvec  0.0.0
Fast Matrix/Vector Library
var_vector.h
1 /* Copyright (C) 2003-2012 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 var_vector_h
23 #define var_vector_h
24 
25 #include "var_general_matrix.h"
26 #include <vector>
27 #include <cstring>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Vector<fmatvec::Var,AT> : public Matrix<fmatvec::General,fmatvec::Var,fmatvec::Fixed<1>,AT> {
40  using Matrix<General,Var,Fixed<1>,AT>::M;
41  using Matrix<General,Var,Fixed<1>,AT>::ele;
42  friend class RowVector<Var,AT>;
43 
44  public:
45  static constexpr bool isVector {true};
46 
47  typedef AT* iterator;
48  typedef const AT* const_iterator;
49 
50  typedef AT value_type;
51 
53 
54  protected:
55 
56  template<class Row> inline Vector<Var,AT>& copy(const Vector<Row,AT> &x);
57 
59 
60  public:
61 
66  explicit Vector() : Matrix<General,Var,Fixed<1>,AT>() { }
67 
68  // move
69  Vector(Vector<Var,AT> &&src) : Matrix<General,Var,Fixed<1>,AT>(std::move(src)) {}
70  Vector<Var,AT>& operator=(Vector<Var,AT> &&src) {
71  FMATVEC_ASSERT(M == src.size(), AT);
72  src.M=0;
73  delete[]ele;
74  ele=src.ele;
75  src.ele=nullptr;
76  return *this;
77  }
78  Vector(Transpose, RowVector<Var, AT> &&src) : Matrix<General,Var,Fixed<1>,AT>() {
79  M=src.N;
80  src.N=0;
81  ele=src.ele;
82  src.ele=nullptr;
83  }
84 
85 // template<class Ini=All<AT>>
86 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
87 
88  explicit Vector(int m, Noinit ini) : Matrix<General,Var,Fixed<1>,AT>(m,ini) { }
89  explicit Vector(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Var,Fixed<1>,AT>(m,ini,a) { }
90 
96  Vector(const Vector<Var,AT> &x) : Matrix<General,Var,Fixed<1>,AT>(x) {
97  }
98 
104  template<class Row>
105  Vector(const Vector<Row,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
106  }
107 
113  template<class Type, class Row, class Col>
114  explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
115  }
116 
129  Vector(const char *str) : Matrix<General,Var,Fixed<1>,AT>(str) {
130  }
131 
132  Vector<Var,AT>& resize(int m, Noinit) {
133  Matrix<General,Var,Fixed<1>,AT>::resize(m,Noinit());
134  return *this;
135  }
136 
137  Vector<Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
138  Matrix<General,Var,Fixed<1>,AT>::resize(m,ini,a);
139  return *this;
140  }
141 
149  FMATVEC_ASSERT(M == x.size(), AT);
150  return copy(x);
151  }
152 
159  template <class Row>
161  FMATVEC_ASSERT(M == x.size(), AT);
162  return copy(x);
163  }
164 
171  template <class Row>
172  inline Vector<Var,AT>& operator<<=(const Vector<Row,AT> &x) {
173  if(M!=x.size()) resize(x.size(),NONINIT);
174  return copy(x);
175  }
176  // move
177  template <class Row>
178  inline Vector<Var,AT>& operator<<=(Vector<Row,AT> &&src) {
179  M=src.M;
180  src.M=0;
181  delete[]ele;
182  ele=src.ele;
183  src.ele=nullptr;
184  return *this;
185  }
186 
187  template <class AT2>
188  operator Vector<Var,AT2>() const {
189  Vector<Var,AT2> ret(size());
190  for(size_t i=0; i<size(); ++i)
191  ret(i) = (*this)(i);
192  return ret;
193  }
194 
203  AT& operator()(int i) {
204 
205  FMATVEC_ASSERT(i>=0, AT);
206  FMATVEC_ASSERT(i<M, AT);
207 
208  return e(i);
209  }
210 
215  const AT& operator()(int i) const {
216 
217  FMATVEC_ASSERT(i>=0, AT);
218  FMATVEC_ASSERT(i<M, AT);
219 
220  return e(i);
221  }
222 
223  iterator begin() { return &ele[0]; }
224  iterator end() { return &ele[M]; }
225  const_iterator begin() const { return &ele[0]; }
226  const_iterator end() const { return &ele[M]; }
227  const_iterator cbegin() const noexcept { return &ele[0]; }
228  const_iterator cend() const noexcept { return &ele[M]; }
229 
230  AT& e(int i) {
231  return ele[i];
232  }
233 
238  const AT& e(int i) const {
239  return ele[i];
240  }
241 
249  inline Vector<Var,AT>& init(const AT& val=AT());
250  inline Vector<Var,AT>& init(Init, const AT& a=AT()) { return init(a); }
251  inline Vector<Var,AT>& init(Noinit, const AT& a=AT()) { return *this; }
252 
257  constexpr int size() const {return M;}
258 
265  int inc() const {return 1;}
266 
267  using Matrix<General,Var,Fixed<1>,AT>::operator();
268 
273  explicit inline operator std::vector<AT>() const;
274 
279  explicit inline Vector(const std::vector<AT> &v);
280 
281 // /*! \brief Cast to AT.
282 // *
283 // * \return The AT representation of the vector
284 // * */
285 // explicit operator AT() const {
286 // FMATVEC_ASSERT(M==1, AT);
287 // return ele[0];
288 // }
289 //
290 // /*! \brief AT Constructor.
291 // * Constructs and initializes a vector with a AT object.
292 // * \param x The AT the vector will be initialized with.
293 // * */
294 // explicit Vector(const AT &x) : Matrix<General,Var,Fixed<1>,AT>(x) { }
295 
296 #if defined(SWIG) && SWIG_VERSION < 0x040000
297  inline const RowVector<Var,AT> T() const;
298  inline RowVector<Var,AT> T();
299 #else
300  inline const RowVector<Var,AT> T() const &;
301  inline RowVector<Var,AT> T() &;
302 #endif
303  // move
304 #ifndef SWIG
305  inline RowVector<Var,AT> T() &&;
306 #endif
307 
308  inline const Vector<Var,AT> operator()(const fmatvec::Range<Var,Var> &I) const;
309 
310  template <class Row>
311  inline void set(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x);
312 
313  template <class Row>
314  inline void add(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x);
315  };
316 
317  template <class AT>
318  inline Vector<Var,AT>& Vector<Var,AT>::init(const AT &val) {
319  for(int i=0; i<M; i++)
320  e(i) = val;
321  return *this;
322  }
323 
324  template <class AT>
325 #if defined(SWIG) && SWIG_VERSION < 0x040000
326  inline const RowVector<Var,AT> Vector<Var,AT>::T() const {
327 #else
328  inline const RowVector<Var,AT> Vector<Var,AT>::T() const & {
329 #endif
330  RowVector<Var,AT> x(M,NONINIT);
331  for(int i=0; i<M; i++)
332  x.e(i) = e(i);
333  return x;
334  }
335  template <class AT>
336 #if defined(SWIG) && SWIG_VERSION < 0x040000
337  inline RowVector<Var,AT> Vector<Var,AT>::T() {
338 #else
339  inline RowVector<Var,AT> Vector<Var,AT>::T() & {
340 #endif
341  RowVector<Var,AT> x(M,NONINIT);
342  for(int i=0; i<M; i++)
343  x.e(i) = e(i);
344  return x;
345  }
346  // move
347 #ifndef SWIG
348  template <class AT>
349  inline RowVector<Var,AT> Vector<Var,AT>::T() && {
350  return RowVector<Var,AT>(Transpose(), std::move(*this));
351  }
352 #endif
353 
354  template <class AT>
355  inline Vector<Var,AT>::operator std::vector<AT>() const {
356  std::vector<AT> ret(size());
357  for(int i=0; i<size(); ++i)
358  ret[i] = operator()(i);
359  return ret;
360  }
361 
362  template <class AT>
363  inline Vector<Var,AT>::Vector(const std::vector<AT> &v) : Matrix<General,Var,Fixed<1>,AT>(static_cast<int>(v.size()),1) {
364  for(int i=0; i<size(); ++i)
365  operator()(i) = v[i];
366  }
367 
368  template <class AT>
369  inline const Vector<Var,AT> Vector<Var,AT>::operator()(const fmatvec::Range<Var,Var> &I) const {
370  FMATVEC_ASSERT(I.end()<M, AT);
371  Vector<Var,AT> x(I.end()-I.start()+1,NONINIT);
372 
373  for(int i=0; i<x.size(); i++)
374  x.e(i) = e(I.start()+i);
375 
376  return x;
377  }
378 
379  template <class AT> template <class Row>
380  inline void Vector<Var,AT>::set(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
381  FMATVEC_ASSERT(I.end()<size(), AT);
382  FMATVEC_ASSERT(I.size()==x.size(), AT);
383 
384  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
385  e(i) = x.e(ii);
386  }
387 
388  template <class AT> template <class Row>
389  inline void Vector<Var,AT>::add(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
390  FMATVEC_ASSERT(I.end()<size(), AT);
391  FMATVEC_ASSERT(I.size()==x.size(), AT);
392 
393  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
394  e(i) += x.e(ii);
395  }
396 
398 
399  template <class AT> template <class Row>
400  inline Vector<Var,AT>& Vector<Var,AT>::copy(const Vector<Row,AT> &x) {
401  for(int i=0; i<M; i++)
402  e(i) = x.e(i);
403  return *this;
404  }
405 
407 
408 }
409 
410 #endif
int end() const
Last element.
Definition: range.h:91
Definition: fmatvec.h:38
Vector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_vector.h:114
Vector(const char *str)
String Constructor.
Definition: var_vector.h:129
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i)
Element operator.
Definition: var_vector.h:203
Definition: types.h:89
Definition: types.h:101
Definition: fmatvec.h:50
Vector(const Vector< Var, AT > &x)
Copy Constructor.
Definition: var_vector.h:96
Vector< Var, AT > & operator=(const Vector< Row, AT > &x)
Assignment operator.
Definition: var_vector.h:160
int inc() const
Increment.
Definition: var_vector.h:265
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
Vector()
Standard constructor.
Definition: var_vector.h:66
Namespace fmatvec.
Definition: _memory.cc:28
int size() const
Size.
Definition: range.h:97
const AT & operator()(int i) const
Element operator.
Definition: var_vector.h:215
constexpr int size() const
Size.
Definition: var_vector.h:257
const AT & e(int i) const
Element operator.
Definition: var_vector.h:238
Definition: types.h:88
Basic shape class for matrices.
Definition: types.h:136
Vector(const Vector< Row, AT > &A)
Copy Constructor.
Definition: var_vector.h:105
Vector< Var, AT > & operator=(const Vector< Var, AT > &x)
Assignment operator.
Definition: var_vector.h:148
This is an index class for creating submatrices.
Definition: range.h:44
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39