All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 AtomicType;
46 
48 
49  protected:
50 
51  template<class Row> inline void deepCopy(const RowVector<Row,AT> &x);
52 
54 
55  public:
56 
61  RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
62  }
63 
64 // template<class Ini=All<AT> >
65 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
66 
67  RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
68  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
69 
82  RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
83  }
84 
93  }
94 
95  template<class Row>
96  RowVector(const RowVector<Row,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
97  }
98 
99  template<class Type, class Row, class Col>
100  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
101  }
102 
103  RowVector<Var,AT>& resize() {
104  Matrix<General,Fixed<1>,Var,AT>::resize();
105  return *this;
106  }
107 
108  RowVector<Var,AT>& resize(int n, Noinit) {
109  Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
110  return *this;
111  }
112 
113  RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
114  Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
115  return *this;
116  }
117 
124  inline RowVector<Var,AT>& operator=(const RowVector<Var,AT> &x);
125 
126  template <class Row>
127  inline RowVector<Var,AT>& operator=(const RowVector<Row,AT> &x);
128 
135  template <class Row>
136  inline RowVector<Var,AT>& operator<<(const RowVector<Row,AT> &x);
137 
148  AT& operator()(int i) {
149 
150 #ifndef FMATVEC_NO_BOUNDS_CHECK
151  assert(i>=0);
152  assert(i<N);
153 #endif
154 
155  return e(i);
156  };
157 
162  const AT& operator()(int i) const {
163 
164 #ifndef FMATVEC_NO_BOUNDS_CHECK
165  assert(i>=0);
166  assert(i<N);
167 #endif
168 
169  return e(i);
170  };
171 
172  AT& e(int i) {
173  return ele[i];
174  };
175 
180  const AT& e(int i) const {
181  return ele[i];
182  };
183 
191  inline RowVector<Var,AT>& init(const AT& a=0);
192  inline RowVector<Var,AT>& init(Init, const AT &a=0) { return init(a); }
193  inline RowVector<Var,AT>& init(Noinit, const AT &a=0) { return *this; }
194 
199  int size() const {return N;};
200 
207  int inc() const {return 1;};
208 
209  using Matrix<General,Fixed<1>,Var,AT>::operator();
210 
215  inline operator std::vector<AT>();
216 
221  inline RowVector(std::vector<AT> v);
222 
223  inline const Vector<Var,AT> T() const;
224 
225  inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
226 
227  template <class Row>
228  inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
229 
230  template <class Row>
231  inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
232  };
233 
234  template <class AT>
236  for(int i=0; i<N; i++)
237  e(i) = val;
238  return *this;
239  }
240 
241  template <class AT>
243 
244  if(!ele) {
245  delete[] ele;
246  N = x.size();
247  ele = new AT[N];
248  } else {
249 #ifndef FMATVEC_NO_SIZE_CHECK
250  assert(N == x.size());
251 #endif
252  }
253 
254  deepCopy(x);
255 
256  return *this;
257  }
258 
259  template <class AT> template<class Row>
261 
262  if(!ele) {
263  delete[] ele;
264  N = x.size();
265  ele = new AT[N];
266  } else {
267 #ifndef FMATVEC_NO_SIZE_CHECK
268  assert(N == x.size());
269 #endif
270  }
271 
272  deepCopy(x);
273 
274  return *this;
275  }
276 
277  template <class AT> template<class Row>
279 
280  if(N!=x.size()) {
281  delete[] ele;
282  N = x.size();
283  ele = new AT[N];
284  }
285 
286  deepCopy(x);
287 
288  return *this;
289  }
290 
291  template <class AT>
292  inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
293  Vector<Var,AT> x(N,NONINIT);
294  for(int i=0; i<N; i++)
295  x.e(i) = e(i);
296  return x;
297  }
298 
299  template <class AT>
300  inline RowVector<Var,AT>::operator std::vector<AT>() {
301  std::vector<AT> ret(size());
302  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
303  return ret;
304  }
305 
306  template <class AT>
307  inline RowVector<Var,AT>::RowVector(std::vector<AT> v) : Matrix<General,Fixed<1>,Var,AT>(v.size(),1) {
308  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
309  }
310 
311  template <class AT>
312  inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
313 #ifndef FMATVEC_NO_BOUNDS_CHECK
314  assert(I.end()<N);
315 #endif
316  RowVector<Var,AT> x(I.end()-I.start()+1,NONINIT);
317 
318  for(int i=0; i<x.size(); i++)
319  x.e(i) = e(I.start()+i);
320 
321  return x;
322  }
323 
324  template <class AT> template <class Row>
325  inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
326 #ifndef FMATVEC_NO_BOUNDS_CHECK
327  assert(I.end()<size());
328  assert(I.size()==x.size());
329 #endif
330 
331  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
332  e(i) = x.e(ii);
333  }
334 
335  template <class AT> template <class Row>
336  inline void RowVector<Var,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
337 #ifndef FMATVEC_NO_BOUNDS_CHECK
338  assert(I.end()<size());
339  assert(I.size()==x.size());
340 #endif
341 
342  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
343  e(i) += x.e(ii);
344  }
345 
347 
348  template <class AT> template <class Row>
349  inline void RowVector<Var,AT>::deepCopy(const RowVector<Row,AT> &x) {
350  for(int i=0; i<N; i++)
351  e(i) = x.e(i);
352  }
353 
355 
356 }
357 
358 #endif
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:162
Definition: fmatvec.h:38
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:82
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
RowVector()
Standard constructor.
Definition: var_row_vector.h:61
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
AT & operator()(int i)
Element operator.
Definition: var_row_vector.h:148
Definition: matrix.h:39
Definition: types.h:65
Definition: fmatvec.h:50
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:92
Definition: matrix.h:38
int size() const
Size.
Definition: var_row_vector.h:199
Basic shape class for matrices.
Definition: types.h:100
const AT & e(int i) const
Element operator.
Definition: var_row_vector.h:180
int end() const
Last element.
Definition: range.h:95
int inc() const
Increment.
Definition: var_row_vector.h:207
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
int start() const
First element.
Definition: range.h:89

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML