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 
43  public:
44  static constexpr bool isVector {true};
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 RowVector<Var,AT>& copy(const RowVector<Row,AT> &x);
56 
58 
59  public:
60 
65  explicit RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
66  }
67 
68 // template<class Ini=All<AT>>
69 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
70 
71  explicit RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
72  explicit RowVector(int n, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
73 
80  }
81 
87  template<class Row>
89  }
90 
96  template<class Type, class Row, class Col>
97  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
98  }
99 
112  RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
113  }
114 
115  RowVector<Var,AT>& resize(int n, Noinit) {
116  Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
117  return *this;
118  }
119 
120  RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) {
121  Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
122  return *this;
123  }
124 
132  FMATVEC_ASSERT(N == x.size(), AT);
133  return copy(x);
134  }
135 
142  template <class Row>
144  FMATVEC_ASSERT(N == x.size(), AT);
145  return copy(x);
146  }
147 
154  template <class Row>
155  inline RowVector<Var,AT>& operator<<=(const RowVector<Row,AT> &x) {
156  if(N!=x.size()) resize(x.size(),NONINIT);
157  return copy(x);
158  }
159 
168  AT& operator()(int i) {
169 
170  FMATVEC_ASSERT(i>=0, AT);
171  FMATVEC_ASSERT(i<N, AT);
172 
173  return e(i);
174  }
175 
180  const AT& operator()(int i) const {
181 
182  FMATVEC_ASSERT(i>=0, AT);
183  FMATVEC_ASSERT(i<N, AT);
184 
185  return e(i);
186  }
187 
188  iterator begin() { return &ele[0]; }
189  iterator end() { return &ele[N]; }
190  const_iterator begin() const { return &ele[0]; }
191  const_iterator end() const { return &ele[N]; }
192  const_iterator cbegin() const noexcept { return &ele[0]; }
193  const_iterator cend() const noexcept { return &ele[N]; }
194 
195  AT& e(int i) {
196  return ele[i];
197  }
198 
203  const AT& e(int i) const {
204  return ele[i];
205  }
206 
214  inline RowVector<Var,AT>& init(const AT& val=AT());
215  inline RowVector<Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
216  inline RowVector<Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
217 
222  constexpr int size() const {return N;}
223 
230  int inc() const {return 1;}
231 
232  using Matrix<General,Fixed<1>,Var,AT>::operator();
233 
238  explicit inline operator std::vector<AT>() const;
239 
244  explicit inline RowVector(const std::vector<AT> &v);
245 
246 // /*! \brief Cast to AT.
247 // *
248 // * \return The AT representation of the vector
249 // * */
250 // explicit operator AT() const {
251 // FMATVEC_ASSERT(N==1, AT);
252 // return ele[0];
253 // }
254 //
255 // /*! \brief AT Constructor.
256 // * Constructs and initializes a vector with a AT object.
257 // * \param x The AT the vector will be initialized with.
258 // * */
259 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Var,AT>(x) { }
260 
261  inline const Vector<Var,AT> T() const;
262 
263  inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
264 
265  template <class Row>
266  inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
267 
268  template <class Row>
269  inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
270  };
271 
272  template <class AT>
274  for(int i=0; i<N; i++)
275  e(i) = val;
276  return *this;
277  }
278 
279  template <class AT>
280  inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
281  Vector<Var,AT> x(N,NONINIT);
282  for(int i=0; i<N; i++)
283  x.e(i) = e(i);
284  return x;
285  }
286 
287  template <class AT>
288  inline RowVector<Var,AT>::operator std::vector<AT>() const {
289  std::vector<AT> ret(size());
290  for(int i=0; i<size(); ++i)
291  ret[i] = operator()(i);
292  return ret;
293  }
294 
295  template <class AT>
296  inline RowVector<Var,AT>::RowVector(const std::vector<AT> &v) : Matrix<General,Fixed<1>,Var,AT>(1, v.size()) {
297  for(int i=0; i<size(); ++i)
298  operator()(i) = v[i];
299  }
300 
301  template <class AT>
302  inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
303  FMATVEC_ASSERT(I.end()<N, AT);
304  RowVector<Var,AT> x(I.end()-I.start()+1,NONINIT);
305 
306  for(int i=0; i<x.size(); i++)
307  x.e(i) = e(I.start()+i);
308 
309  return x;
310  }
311 
312  template <class AT> template <class Row>
313  inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
314  FMATVEC_ASSERT(I.end()<size(), AT);
315  FMATVEC_ASSERT(I.size()==x.size(), AT);
316 
317  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
318  e(i) = x.e(ii);
319  }
320 
321  template <class AT> template <class Row>
322  inline void RowVector<Var,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
323  FMATVEC_ASSERT(I.end()<size(), AT);
324  FMATVEC_ASSERT(I.size()==x.size(), AT);
325 
326  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
327  e(i) += x.e(ii);
328  }
329 
331 
332  template <class AT> template <class Row>
333  inline RowVector<Var,AT>& RowVector<Var,AT>::copy(const RowVector<Row,AT> &x) {
334  for(int i=0; i<N; i++)
335  e(i) = x.e(i);
336  return *this;
337  }
338 
340 
341 }
342 
343 #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:203
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:180
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:112
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
RowVector()
Standard constructor.
Definition: var_row_vector.h:65
AT & operator()(int i)
Element operator.
Definition: var_row_vector.h:168
Definition: types.h:85
Definition: types.h:97
Definition: fmatvec.h:50
constexpr int size() const
Size.
Definition: var_row_vector.h:222
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:79
RowVector(const RowVector< Row, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:88
int inc() const
Increment.
Definition: var_row_vector.h:230
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:131
Namespace fmatvec.
Definition: _memory.cc:28
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
RowVector< Var, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: var_row_vector.h:143
RowVector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:97
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