All Classes Namespaces Functions Variables 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 
50  typedef AT* iterator;
51  typedef const AT* const_iterator;
52 
53  typedef AT value_type;
54 
56 
57  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
58  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
59 
60  friend class RowVector<Ref,AT>;
61 
63  friend const Vector<Ref,AT> Matrix<General,Ref,Ref,AT>::col(int i) const;
64 
65  protected:
66 
67  inline void deepCopy(const Vector<Ref,AT> &x);
68  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
69 
70  AT* elePtr(int i) {
71  return tp ? ele+lda*i : ele+i;
72  };
73 
74  const AT* elePtr(int i) const {
75  return tp ? ele+lda*i : ele+i;
76  };
77 
78  Vector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(n_, 1, lda_, tp, memory, ele_) { }
79 
81 
82  public:
83 
88  Vector() : Matrix<General,Ref,Ref,AT>() { n=1; }
89 
90 // template<class Ini=All<AT> >
91 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
92 
93  Vector(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
94  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(m,1,ini,a) { }
95 
102  Vector(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,1,ele) {
103  }
104 
117  Vector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
118 #ifndef FMATVEC_NO_SIZE_CHECK
119  assert(n==1);
120 #endif
121  }
122 
130  Vector(const Vector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
131  }
132 
133  template<class Row>
134  Vector(const Vector<Row,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
135  }
136 
137  template<class Type, class Row, class Col>
138  explicit Vector(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
139 
140 #ifndef FMATVEC_NO_SIZE_CHECK
141  assert(x.cols()==1);
142 #endif
143  }
144 
145  Vector<Ref,AT>& resize() {
146  Matrix<General,Ref,Ref,AT>::resize();
147  return *this;
148  }
149 
150  Vector<Ref,AT>& resize(int m, Noinit) {
151  Matrix<General,Ref,Ref,AT>::resize(m,1,Noinit());
152  return *this;
153  }
154 
155  Vector<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
156  Matrix<General,Ref,Ref,AT>::resize(m,1,ini,a);
157  return *this;
158  }
159 
166  template <class Row>
167  inline Vector<Ref,AT>& operator<<(const Vector<Row,AT> &x);
168 
175  inline Vector<Ref,AT>& operator>>(const Vector<Ref,AT> &x);
176 
177  inline Vector<Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT> &A);
178 
187  inline Vector<Ref,AT>& operator=(const Vector<Ref,AT> &x);
188 
189  template <class Row>
190  inline Vector<Ref,AT>& operator=(const Vector<Row,AT> &x);
191 
202  AT& operator()(int i) {
203 
204 #ifndef FMATVEC_NO_BOUNDS_CHECK
205  assert(i>=0);
206  assert(i<m);
207 #endif
208 
209  return e(i);
210  };
211 
216  const AT& operator()(int i) const {
217 
218 #ifndef FMATVEC_NO_BOUNDS_CHECK
219  assert(i>=0);
220  assert(i<m);
221 #endif
222 
223  return e(i);
224  };
225 
226  iterator begin() { return &ele[0]; }
227  iterator end() { return &ele[m]; }
228  const_iterator begin() const { return &ele[0]; }
229  const_iterator end() const { return &ele[m]; }
230  const_iterator cbegin() const noexcept { return &ele[0]; }
231  const_iterator cend() const noexcept { return &ele[m]; }
232 
233  AT& er(int i) {
234  return ele[i];
235  };
236 
237  const AT& er(int i) const {
238  return ele[i];
239  };
240 
241  AT& et(int i) {
242  return ele[i*lda];
243  };
244 
245  const AT& et(int i) const {
246  return ele[i*lda];
247  };
248 
249  AT& e(int i) {
250  return tp ? et(i) : er(i);
251  };
252 
253  const AT& e(int i) const {
254  return tp ? et(i) : er(i);
255  };
256 
264  inline Vector<Ref,AT>& init(const AT& val=0);
265  inline Vector<Ref,AT>& init(Init, const AT& a=0) { return init(a); }
266  inline Vector<Ref,AT>& init(Noinit, const AT& a=0) { return *this; }
267 
272  int size() const {return m;};
273 
280  int inc() const {return tp?lda:1;};
281 
287  inline Vector<Ref,AT> copy() const;
288 
298  inline Vector<Ref,AT> operator()(int i1, int i2);
299 
304  inline const Vector<Ref,AT> operator()(int i1, int i2) const;
305 
314  inline Vector<Ref,AT> operator()(const Range<Var,Var> &I);
315 
320  inline const Vector<Ref,AT> operator()(const Range<Var,Var> &I) const;
321 
323 
328  inline operator std::vector<AT>() const;
329 
334  inline Vector(std::vector<AT> v);
335 
336  RowVector<Ref,AT> T() {
337  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
338  };
339 
340  const RowVector<Ref,AT> T() const {
341  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
342  }
343 
344  };
345 
346  template <class AT>
347  inline Vector<Ref,AT>& Vector<Ref,AT>::init(const AT &val) {
348  if(tp)
349  for(int i=0; i<m; i++)
350  et(i) = val;
351  else
352  for(int i=0; i<m; i++)
353  er(i) = val;
354  return *this;
355  }
356 
357  template <class AT>
359 
360  if(!ele) {
361  m = x.size();
362  n = 1;
363  lda = m;
364  tp = false;
365  memory.resize(m);
366  ele = (AT*)memory.get();
367  } else {
368 #ifndef FMATVEC_NO_SIZE_CHECK
369  assert(m == x.size());
370 #endif
371  }
372 
373  deepCopy(x);
374 
375  return *this;
376  }
377 
378  template <class AT> template<class Row>
380 
381  if(!ele) {
382  m = x.size();
383  n = 1;
384  lda = m;
385  tp = false;
386  memory.resize(m);
387  ele = (AT*)memory.get();
388  } else {
389 #ifndef FMATVEC_NO_SIZE_CHECK
390  assert(m == x.size());
391 #endif
392  }
393 
394  deepCopy(x);
395 
396  return *this;
397  }
398 
399  template <class AT> template<class Row>
401 
402  if(m!=x.size()) {
403  m = x.size();
404  n = 1;
405  lda = m;
406  tp = false;
407  memory.resize(m);
408  ele = (AT*)memory.get();
409  }
410 
411  deepCopy(x);
412 
413  return *this;
414  }
415 
416  template <class AT>
418 
419  m = x.m;
420  n = 1;
421  memory = x.memory;
422  ele = x.ele;
423  lda = x.lda;
424  tp = x.tp;
425 
426  return *this;
427  }
428 
429  template <class AT>
431 
432 #ifndef FMATVEC_NO_SIZE_CHECK
433  assert(A.cols() == 1);
434 #endif
435  m = A.rows();
436  n = 1;
437  memory = A.memory;
438  ele = A.ele;
439  lda = A.lda;
440  tp = A.tp;
441 
442  return *this;
443  }
444 
445  template <class AT>
447 
448  Vector<Ref,AT> x(m,NONINIT);
449  x.deepCopy(*this);
450 
451  return x;
452  }
453 
454  template <class AT>
456  return operator()(Range<Var,Var>(i1,i2));
457  }
458 
459  template <class AT>
460  inline const Vector<Ref,AT> Vector<Ref,AT>::operator()(int i1, int i2) const {
461  return operator()(Range<Var,Var>(i1,i2));
462  }
463 
464  template <class AT>
466 
467 #ifndef FMATVEC_NO_BOUNDS_CHECK
468  assert(I.end()<m);
469 #endif
470 
471  return Vector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
472  }
473 
474  template <class AT>
476 
477 #ifndef FMATVEC_NO_BOUNDS_CHECK
478  assert(I.end()<m);
479 #endif
480 
481  return Vector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
482  }
483 
484  template <class AT>
485  inline Vector<Ref,AT>::operator std::vector<AT>() const {
486  std::vector<AT> ret(size());
487  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
488  return ret;
489  }
490 
491  template <class AT>
492  inline Vector<Ref,AT>::Vector(std::vector<AT> v) : Matrix<General,Ref,Ref,AT>(v.size(),1) {
493  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
494  }
495 
497 
498  template <class AT>
499  inline void Vector<Ref,AT>::deepCopy(const Vector<Ref,AT> &x) {
500  if(tp) {
501  if(x.tp)
502  for(int i=0; i<size(); i++)
503  et(i) = x.et(i);
504  else
505  for(int i=0; i<size(); i++)
506  et(i) = x.er(i);
507  }
508  else {
509  if(x.tp)
510  for(int i=0; i<size(); i++)
511  er(i) = x.et(i);
512  else
513  for(int i=0; i<size(); i++)
514  er(i) = x.er(i);
515  }
516  }
517 
518  template <class AT> template <class Row>
519  inline void Vector<Ref,AT>::deepCopy(const Vector<Row,AT> &x) {
520  if(tp)
521  for(int i=0; i<size(); i++)
522  et(i) = x.e(i);
523  else
524  for(int i=0; i<size(); i++)
525  er(i) = x.e(i);
526  }
527 
529 
530 }
531 
532 #endif
const AT & operator()(int i) const
Element operator.
Definition: vector.h:216
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i)
Element operator.
Definition: vector.h:202
int rows() const
Number of rows.
Definition: general_matrix.h:273
Vector(const Vector< Ref, AT > &x)
Copy Constructor.
Definition: vector.h:130
Definition: fmatvec.h:50
Vector()
Standard constructor.
Definition: vector.h:88
Definition: types.h:62
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: fmatvec.h:47
Vector(const char *str)
String Constructor.
Definition: vector.h:117
This is a matrix class for general matrices.
Definition: general_matrix.h:41
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
int size() const
Size.
Definition: vector.h:272
Definition: matrix.h:42
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 end() const
Last element.
Definition: range.h:95
Vector(int m, AT *ele)
Regular Constructor.
Definition: vector.h:102
This is an index class for creating submatrices.
Definition: range.h:44
int inc() const
Increment.
Definition: vector.h:280
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:279

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home