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