All Classes Namespaces Functions 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 
46 
47  protected:
48 
49  template<class Row> inline void deepCopy(const RowVector<Row,AT> &x);
50 
52 
53  public:
54 
59  RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
60  }
61 
62 // template<class Ini=All<AT> >
63 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
64 
65  RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
66  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
67 
80  RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
81  }
82 
91  }
92 
93  template<class Row>
94  RowVector(const RowVector<Row,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
95  }
96 
97  template<class Type, class Row, class Col>
98  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
99  }
100 
101  RowVector<Var,AT>& resize() {
102  Matrix<General,Fixed<1>,Var,AT>::resize();
103  return *this;
104  }
105 
106  RowVector<Var,AT>& resize(int n, Noinit) {
107  Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
108  return *this;
109  }
110 
111  RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
112  Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
113  return *this;
114  }
115 
122  inline RowVector<Var,AT>& operator=(const RowVector<Var,AT> &x);
123 
124  template <class Row>
125  inline RowVector<Var,AT>& operator=(const RowVector<Row,AT> &x);
126 
133  template <class Row>
134  inline RowVector<Var,AT>& operator<<(const RowVector<Row,AT> &x);
135 
146  AT& operator()(int i) {
147 
148 #ifndef FMATVEC_NO_BOUNDS_CHECK
149  assert(i>=0);
150  assert(i<N);
151 #endif
152 
153  return e(i);
154  };
155 
160  const AT& operator()(int i) const {
161 
162 #ifndef FMATVEC_NO_BOUNDS_CHECK
163  assert(i>=0);
164  assert(i<N);
165 #endif
166 
167  return e(i);
168  };
169 
170  AT& e(int i) {
171  return ele[i];
172  };
173 
178  const AT& e(int i) const {
179  return ele[i];
180  };
181 
189  inline RowVector<Var,AT>& init(const AT& a=0);
190  inline RowVector<Var,AT>& init(Init, const AT &a=0) { return init(a); }
191  inline RowVector<Var,AT>& init(Noinit, const AT &a=0) { return *this; }
192 
197  int size() const {return N;};
198 
205  int inc() const {return 1;};
206 
207  using Matrix<General,Fixed<1>,Var,AT>::operator();
208 
213  inline operator std::vector<AT>();
214 
219  inline RowVector(std::vector<AT> v);
220 
221  inline const Vector<Var,AT> T() const;
222 
223  inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
224 
225  template <class Row>
226  inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
227  };
228 
229  template <class AT>
231  for(int i=0; i<N; i++)
232  e(i) = val;
233  return *this;
234  }
235 
236  template <class AT>
238 
239  if(!ele) {
240  delete[] ele;
241  N = x.size();
242  ele = new AT[N];
243  } else {
244 #ifndef FMATVEC_NO_SIZE_CHECK
245  assert(N == x.size());
246 #endif
247  }
248 
249  deepCopy(x);
250 
251  return *this;
252  }
253 
254  template <class AT> template<class Row>
256 
257  if(!ele) {
258  delete[] ele;
259  N = x.size();
260  ele = new AT[N];
261  } else {
262 #ifndef FMATVEC_NO_SIZE_CHECK
263  assert(N == x.size());
264 #endif
265  }
266 
267  deepCopy(x);
268 
269  return *this;
270  }
271 
272  template <class AT> template<class Row>
274 
275  if(N!=x.size()) {
276  delete[] ele;
277  N = x.size();
278  ele = new AT[N];
279  }
280 
281  deepCopy(x);
282 
283  return *this;
284  }
285 
286  template <class AT>
287  inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
288  Vector<Var,AT> x(N,NONINIT);
289  for(int i=0; i<N; i++)
290  x.e(i) = e(i);
291  return x;
292  }
293 
294  template <class AT>
295  inline RowVector<Var,AT>::operator std::vector<AT>() {
296  std::vector<AT> ret(size());
297  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
298  return ret;
299  }
300 
301  template <class AT>
302  inline RowVector<Var,AT>::RowVector(std::vector<AT> v) : Matrix<General,Fixed<1>,Var,AT>(v.size(),1) {
303  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
304  }
305 
306  template <class AT>
307  inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
308 #ifndef FMATVEC_NO_BOUNDS_CHECK
309  assert(I.end()<N);
310 #endif
311  RowVector<Var,AT> x(I.end()-I.start()+1,NONINIT);
312 
313  for(int i=0; i<x.size(); i++)
314  x.e(i) = e(I.start()+i);
315 
316  return x;
317  }
318 
319  template <class AT> template <class Row>
320  inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
321 #ifndef FMATVEC_NO_BOUNDS_CHECK
322  assert(I.end()<size());
323  assert(I.size()==x.size());
324 #endif
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 void RowVector<Var,AT>::deepCopy(const RowVector<Row,AT> &x) {
334  for(int i=0; i<N; i++)
335  e(i) = x.e(i);
336  }
337 
339 
340 }
341 
342 #endif
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:160
Definition: types.h:68
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:80
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
RowVector()
Standard constructor.
Definition: var_row_vector.h:59
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:146
Definition: matrix.h:39
Definition: types.h:65
Definition: matrix.h:218
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:90
Definition: matrix.h:38
int size() const
Size.
Definition: var_row_vector.h:197
Basic shape class for matrices.
Definition: types.h:100
const AT & e(int i) const
Element operator.
Definition: var_row_vector.h:178
int end() const
Last element.
Definition: range.h:95
int inc() const
Increment.
Definition: var_row_vector.h:205
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