All Classes Namespaces Functions Typedefs Enumerations Pages
vector.h
1 /* Copyright (C) 2003-2005 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 vector_h
23 #define vector_h
24 
25 #include "general_matrix.h"
26 #include <vector>
27 #include <cstring>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Vector<Ref,AT> : public Matrix<General,Ref,Ref,AT> {
47 
48  public:
49 
51 
52  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
53  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
54 
55  friend class RowVector<Ref,AT>;
56 
58  friend const Vector<Ref,AT> Matrix<General,Ref,Ref,AT>::col(int i) const;
59 
60  protected:
61 
62  inline void deepCopy(const Vector<Ref,AT> &x);
63  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
64 
65  AT* elePtr(int i) {
66  return tp ? ele+lda*i : ele+i;
67  };
68 
69  const AT* elePtr(int i) const {
70  return tp ? ele+lda*i : ele+i;
71  };
72 
73  Vector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(n_, 1, lda_, tp, memory, ele_) { }
74 
76 
77  public:
78 
83  Vector() : Matrix<General,Ref,Ref,AT>() { n=1; }
84 
85 // template<class Ini=All<AT> >
86 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
87 
88  Vector(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
89  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(m,1,ini,a) { }
90 
97  Vector(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,1,ele) {
98  }
99 
112  Vector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
113 #ifndef FMATVEC_NO_SIZE_CHECK
114  assert(n==1);
115 #endif
116  }
117 
125  Vector(const Vector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
126  }
127 
128  template<class Row>
129  Vector(const Vector<Row,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
130  }
131 
132  template<class Type, class Row, class Col>
133  explicit Vector(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
134 
135 #ifndef FMATVEC_NO_SIZE_CHECK
136  assert(x.cols()==1);
137 #endif
138  }
139 
140  Vector<Ref,AT>& resize() {
141  Matrix<General,Ref,Ref,AT>::resize();
142  return *this;
143  }
144 
145  Vector<Ref,AT>& resize(int m, Noinit) {
146  Matrix<General,Ref,Ref,AT>::resize(m,1,Noinit());
147  return *this;
148  }
149 
150  Vector<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
151  Matrix<General,Ref,Ref,AT>::resize(m,1,ini,a);
152  return *this;
153  }
154 
161  template <class Row>
162  inline Vector<Ref,AT>& operator<<(const Vector<Row,AT> &x);
163 
170  inline Vector<Ref,AT>& operator>>(const Vector<Ref,AT> &x);
171 
172  inline Vector<Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT> &A);
173 
182  inline Vector<Ref,AT>& operator=(const Vector<Ref,AT> &x);
183 
184  template <class Row>
185  inline Vector<Ref,AT>& operator=(const Vector<Row,AT> &x);
186 
197  AT& operator()(int i) {
198 
199 #ifndef FMATVEC_NO_BOUNDS_CHECK
200  assert(i>=0);
201  assert(i<m);
202 #endif
203 
204  return e(i);
205  };
206 
211  const AT& operator()(int i) const {
212 
213 #ifndef FMATVEC_NO_BOUNDS_CHECK
214  assert(i>=0);
215  assert(i<m);
216 #endif
217 
218  return e(i);
219  };
220 
221  AT& er(int i) {
222  return ele[i];
223  };
224 
225  const AT& er(int i) const {
226  return ele[i];
227  };
228 
229  AT& et(int i) {
230  return ele[i*lda];
231  };
232 
233  const AT& et(int i) const {
234  return ele[i*lda];
235  };
236 
237  AT& e(int i) {
238  return tp ? et(i) : er(i);
239  };
240 
241  const AT& e(int i) const {
242  return tp ? et(i) : er(i);
243  };
244 
252  inline Vector<Ref,AT>& init(const AT& a=0);
253  inline Vector<Ref,AT>& init(Init, const AT& a=0) { return init(a); }
254  inline Vector<Ref,AT>& init(Noinit, const AT& a=0) { return *this; }
255 
260  int size() const {return m;};
261 
268  int inc() const {return tp?lda:1;};
269 
275  inline Vector<Ref,AT> copy() const;
276 
286  inline Vector<Ref,AT> operator()(int i1, int i2);
287 
292  inline const Vector<Ref,AT> operator()(int i1, int i2) const;
293 
302  inline Vector<Ref,AT> operator()(const Index &I);
303 
308  inline const Vector<Ref,AT> operator()(const Index &I) const;
309 
311 
316  inline operator std::vector<AT>();
317 
322  inline Vector(std::vector<AT> v);
323 
324  RowVector<Ref,AT> T() {
325  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
326  };
327 
328  const RowVector<Ref,AT> T() const {
329  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
330  }
331 
332  };
333 
334  template <class AT>
335  inline Vector<Ref,AT>& Vector<Ref,AT>::init(const AT &val) {
336  if(tp)
337  for(int i=0; i<m; i++)
338  et(i) = val;
339  else
340  for(int i=0; i<m; i++)
341  er(i) = val;
342  return *this;
343  }
344 
345  template <class AT>
347 
348  if(!ele) {
349  m = x.size();
350  n = 1;
351  lda = m;
352  tp = false;
353  memory.resize(m);
354  ele = (AT*)memory.get();
355  } else {
356 #ifndef FMATVEC_NO_SIZE_CHECK
357  assert(m == x.size());
358 #endif
359  }
360 
361  deepCopy(x);
362 
363  return *this;
364  }
365 
366  template <class AT> template<class Row>
368 
369  if(!ele) {
370  m = x.size();
371  n = 1;
372  lda = m;
373  tp = false;
374  memory.resize(m);
375  ele = (AT*)memory.get();
376  } else {
377 #ifndef FMATVEC_NO_SIZE_CHECK
378  assert(m == x.size());
379 #endif
380  }
381 
382  deepCopy(x);
383 
384  return *this;
385  }
386 
387  template <class AT> template<class Row>
389 
390  if(m!=x.size()) {
391  m = x.size();
392  n = 1;
393  lda = m;
394  tp = false;
395  memory.resize(m);
396  ele = (AT*)memory.get();
397  }
398 
399  deepCopy(x);
400 
401  return *this;
402  }
403 
404  template <class AT>
406 
407  m = x.m;
408  n = 1;
409  memory = x.memory;
410  ele = x.ele;
411  lda = x.lda;
412  tp = x.tp;
413 
414  return *this;
415  }
416 
417  template <class AT>
419 
420 #ifndef FMATVEC_NO_SIZE_CHECK
421  assert(A.cols() == 1);
422 #endif
423  m = A.rows();
424  n = 1;
425  memory = A.memory;
426  ele = A.ele;
427  lda = A.lda;
428  tp = A.tp;
429 
430  return *this;
431  }
432 
433  template <class AT>
435 
436  Vector<Ref,AT> x(m,NONINIT);
437  x.deepCopy(*this);
438 
439  return x;
440  }
441 
442  template <class AT>
444  return operator()(Index(i1,i2));
445  }
446 
447  template <class AT>
448  inline const Vector<Ref,AT> Vector<Ref,AT>::operator()(int i1, int i2) const {
449  return operator()(Index(i1,i2));
450  }
451 
452  template <class AT>
453  inline const Vector<Ref,AT> Vector<Ref,AT>::operator()(const Index &I) const {
454 
455 #ifndef FMATVEC_NO_BOUNDS_CHECK
456  assert(I.end()<m);
457 #endif
458 
459  return Vector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
460  }
461 
462  template <class AT>
464 
465 #ifndef FMATVEC_NO_BOUNDS_CHECK
466  assert(I.end()<m);
467 #endif
468 
469  return Vector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
470  }
471 
472  template <class AT>
473  inline Vector<Ref,AT>::operator std::vector<AT>() {
474  std::vector<AT> ret(size());
475  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
476  return ret;
477  }
478 
479  template <class AT>
480  inline Vector<Ref,AT>::Vector(std::vector<AT> v) : Matrix<General,Ref,Ref,AT>(v.size(),1) {
481  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
482  }
483 
485 
486  template <class AT>
487  inline void Vector<Ref,AT>::deepCopy(const Vector<Ref,AT> &x) {
488  if(tp) {
489  if(x.tp)
490  for(int i=0; i<size(); i++)
491  et(i) = x.et(i);
492  else
493  for(int i=0; i<size(); i++)
494  et(i) = x.er(i);
495  }
496  else {
497  if(x.tp)
498  for(int i=0; i<size(); i++)
499  er(i) = x.et(i);
500  else
501  for(int i=0; i<size(); i++)
502  er(i) = x.er(i);
503  }
504  }
505 
506  template <class AT> template <class Row>
507  inline void Vector<Ref,AT>::deepCopy(const Vector<Row,AT> &x) {
508  if(tp)
509  for(int i=0; i<size(); i++)
510  et(i) = x.e(i);
511  else
512  for(int i=0; i<size(); i++)
513  er(i) = x.e(i);
514  }
515 
517 
518 }
519 
520 #endif
const AT & operator()(int i) const
Element operator.
Definition: vector.h:211
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
AT & operator()(int i)
Element operator.
Definition: vector.h:197
int rows() const
Number of rows.
Definition: general_matrix.h:270
Vector(const Vector< Ref, AT > &x)
Copy Constructor.
Definition: vector.h:125
Definition: matrix.h:218
Vector()
Standard constructor.
Definition: vector.h:83
int end() const
Last element.
Definition: index.h:85
Definition: types.h:62
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: matrix.h:215
Vector(const char *str)
String Constructor.
Definition: vector.h:112
This is a matrix class for general matrices.
Definition: general_matrix.h:40
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:170
int size() const
Size.
Definition: vector.h:260
This is an index class for creating submatrices.
Definition: index.h:34
Definition: matrix.h:38
RowVector< Ref, AT > trans(const Vector< Ref, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1470
Basic shape class for matrices.
Definition: types.h:100
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
int start() const
First element.
Definition: index.h:79
Vector(int m, AT *ele)
Regular Constructor.
Definition: vector.h:97
int inc() const
Increment.
Definition: vector.h:268
int cols() const
Number of columns.
Definition: general_matrix.h:276

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML