fmatvec  0.0.0
Fast Matrix/Vector Library
var_row_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_row_vector_h
23 #define var_row_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 RowVector<Var,AT> : public Matrix<General,Fixed<1>,Var,AT> {
40  using Matrix<General,Fixed<1>,Var,AT>::N;
41  using Matrix<General,Fixed<1>,Var,AT>::ele;
42  friend class Vector<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 RowVector<Var,AT>& copy(const RowVector<Row,AT> &x);
57 
59 
60  public:
61 
66  explicit RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
67  }
68 
69  // move
70  RowVector(RowVector<Var,AT> &&src) : Matrix<General,Fixed<1>,Var,AT>(std::move(src)) {}
71  RowVector<Var,AT>& operator=(RowVector<Var,AT> &&src) {
72  FMATVEC_ASSERT(N == src.size(), AT);
73  src.N=0;
74  delete[]ele;
75  ele=src.ele;
76  src.ele=nullptr;
77  return *this;
78  }
79  RowVector(Transpose, Vector<Var, AT> &&src) : Matrix<General,Fixed<1>,Var,AT>() {
80  N=src.M;
81  src.M=0;
82  ele=src.ele;
83  src.ele=nullptr;
84  }
85 
86 // template<class Ini=All<AT>>
87 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
88 
89  explicit RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
90  explicit RowVector(int n, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
91 
98  }
99 
105  template<class Row>
107  }
108 
114  template<class Type, class Row, class Col>
115  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
116  }
117 
130  RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
131  }
132 
133  RowVector<Var,AT>& resize(int n, Noinit) {
134  Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
135  return *this;
136  }
137 
138  RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) {
139  Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
140  return *this;
141  }
142 
150  FMATVEC_ASSERT(N == x.size(), AT);
151  return copy(x);
152  }
153 
160  template <class Row>
162  FMATVEC_ASSERT(N == x.size(), AT);
163  return copy(x);
164  }
165 
172  template <class Row>
173  inline RowVector<Var,AT>& operator<<=(const RowVector<Row,AT> &x) {
174  if(N!=x.size()) resize(x.size(),NONINIT);
175  return copy(x);
176  }
177  // move
178  template <class Row>
179  inline RowVector<Var,AT>& operator<<=(RowVector<Row,AT> &&src) {
180  N=src.N;
181  src.N=0;
182  delete[]ele;
183  ele=src.ele;
184  src.ele=nullptr;
185  return *this;
186  }
187 
196  AT& operator()(int i) {
197 
198  FMATVEC_ASSERT(i>=0, AT);
199  FMATVEC_ASSERT(i<N, AT);
200 
201  return e(i);
202  }
203 
208  const AT& operator()(int i) const {
209 
210  FMATVEC_ASSERT(i>=0, AT);
211  FMATVEC_ASSERT(i<N, AT);
212 
213  return e(i);
214  }
215 
216  iterator begin() { return &ele[0]; }
217  iterator end() { return &ele[N]; }
218  const_iterator begin() const { return &ele[0]; }
219  const_iterator end() const { return &ele[N]; }
220  const_iterator cbegin() const noexcept { return &ele[0]; }
221  const_iterator cend() const noexcept { return &ele[N]; }
222 
223  AT& e(int i) {
224  return ele[i];
225  }
226 
231  const AT& e(int i) const {
232  return ele[i];
233  }
234 
242  inline RowVector<Var,AT>& init(const AT& val=AT());
243  inline RowVector<Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
244  inline RowVector<Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
245 
250  constexpr int size() const {return N;}
251 
258  int inc() const {return 1;}
259 
260  using Matrix<General,Fixed<1>,Var,AT>::operator();
261 
266  explicit inline operator std::vector<AT>() const;
267 
272  explicit inline RowVector(const std::vector<AT> &v);
273 
274 // /*! \brief Cast to AT.
275 // *
276 // * \return The AT representation of the vector
277 // * */
278 // explicit operator AT() const {
279 // FMATVEC_ASSERT(N==1, AT);
280 // return ele[0];
281 // }
282 //
283 // /*! \brief AT Constructor.
284 // * Constructs and initializes a vector with a AT object.
285 // * \param x The AT the vector will be initialized with.
286 // * */
287 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Var,AT>(x) { }
288 
289 #if defined(SWIG) && SWIG_VERSION < 0x040000
290  inline const Vector<Var,AT> T() const;
291  inline Vector<Var,AT> T();
292 #else
293  inline const Vector<Var,AT> T() const &;
294  inline Vector<Var,AT> T() &;
295 #endif
296  // move
297 #ifndef SWIG
298  inline Vector<Var,AT> T() &&;
299 #endif
300 
301  inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
302 
303  template <class Row>
304  inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
305 
306  template <class Row>
307  inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
308  };
309 
310  template <class AT>
312  for(int i=0; i<N; i++)
313  e(i) = val;
314  return *this;
315  }
316 
317  template <class AT>
318 #if defined(SWIG) && SWIG_VERSION < 0x040000
319  inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
320 #else
321  inline const Vector<Var,AT> RowVector<Var,AT>::T() const & {
322 #endif
323  Vector<Var,AT> x(N,NONINIT);
324  for(int i=0; i<N; i++)
325  x.e(i) = e(i);
326  return x;
327  }
328  template <class AT>
329 #if defined(SWIG) && SWIG_VERSION < 0x040000
331 #else
333 #endif
334  Vector<Var,AT> x(N,NONINIT);
335  for(int i=0; i<N; i++)
336  x.e(i) = e(i);
337  return x;
338  }
339  // move
340 #ifndef SWIG
341  template <class AT>
342  inline Vector<Var,AT> RowVector<Var,AT>::T() && {
343  return Vector<Var,AT>(Transpose(), std::move(*this));
344  }
345 #endif
346 
347  template <class AT>
348  inline RowVector<Var,AT>::operator std::vector<AT>() const {
349  std::vector<AT> ret(size());
350  for(int i=0; i<size(); ++i)
351  ret[i] = operator()(i);
352  return ret;
353  }
354 
355  template <class AT>
356  inline RowVector<Var,AT>::RowVector(const std::vector<AT> &v) : Matrix<General,Fixed<1>,Var,AT>(1, v.size()) {
357  for(int i=0; i<size(); ++i)
358  operator()(i) = v[i];
359  }
360 
361  template <class AT>
362  inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
363  FMATVEC_ASSERT(I.end()<N, AT);
364  RowVector<Var,AT> x(I.end()-I.start()+1,NONINIT);
365 
366  for(int i=0; i<x.size(); i++)
367  x.e(i) = e(I.start()+i);
368 
369  return x;
370  }
371 
372  template <class AT> template <class Row>
373  inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
374  FMATVEC_ASSERT(I.end()<size(), AT);
375  FMATVEC_ASSERT(I.size()==x.size(), AT);
376 
377  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
378  e(i) = x.e(ii);
379  }
380 
381  template <class AT> template <class Row>
382  inline void RowVector<Var,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
383  FMATVEC_ASSERT(I.end()<size(), AT);
384  FMATVEC_ASSERT(I.size()==x.size(), AT);
385 
386  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
387  e(i) += x.e(ii);
388  }
389 
391 
392  template <class AT> template <class Row>
393  inline RowVector<Var,AT>& RowVector<Var,AT>::copy(const RowVector<Row,AT> &x) {
394  for(int i=0; i<N; i++)
395  e(i) = x.e(i);
396  return *this;
397  }
398 
400 
401 }
402 
403 #endif
int end() const
Last element.
Definition: range.h:91
Definition: fmatvec.h:38
const AT & e(int i) const
Element operator.
Definition: var_row_vector.h:231
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:208
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:130
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
RowVector()
Standard constructor.
Definition: var_row_vector.h:66
AT & operator()(int i)
Element operator.
Definition: var_row_vector.h:196
Definition: types.h:89
Definition: types.h:101
const Vector< Var, AT > T() const &
Cast to AT.
Definition: var_row_vector.h:321
Definition: fmatvec.h:50
constexpr int size() const
Size.
Definition: var_row_vector.h:250
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:97
RowVector(const RowVector< Row, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:106
int inc() const
Increment.
Definition: var_row_vector.h:258
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
RowVector< Var, AT > & operator=(const RowVector< Var, AT > &x)
Assignment operator.
Definition: var_row_vector.h:149
Namespace fmatvec.
Definition: _memory.cc:28
Definition: types.h:88
Basic shape class for matrices.
Definition: types.h:136
RowVector< Var, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: var_row_vector.h:161
RowVector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:115
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