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 AtomicType;
51 
53 
54  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
55  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
56 
57  friend class RowVector<Ref,AT>;
58 
60  friend const Vector<Ref,AT> Matrix<General,Ref,Ref,AT>::col(int i) const;
61 
62  protected:
63 
64  inline void deepCopy(const Vector<Ref,AT> &x);
65  template<class Row> inline void deepCopy(const Vector<Row,AT> &x);
66 
67  AT* elePtr(int i) {
68  return tp ? ele+lda*i : ele+i;
69  };
70 
71  const AT* elePtr(int i) const {
72  return tp ? ele+lda*i : ele+i;
73  };
74 
75  Vector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(n_, 1, lda_, tp, memory, ele_) { }
76 
78 
79  public:
80 
85  Vector() : Matrix<General,Ref,Ref,AT>() { n=1; }
86 
87 // template<class Ini=All<AT> >
88 // Vector(int m, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
89 
90  Vector(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
91  Vector(int m, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(m,1,ini,a) { }
92 
99  Vector(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,1,ele) {
100  }
101 
114  Vector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
115 #ifndef FMATVEC_NO_SIZE_CHECK
116  assert(n==1);
117 #endif
118  }
119 
127  Vector(const Vector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
128  }
129 
130  template<class Row>
131  Vector(const Vector<Row,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
132  }
133 
134  template<class Type, class Row, class Col>
135  explicit Vector(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
136 
137 #ifndef FMATVEC_NO_SIZE_CHECK
138  assert(x.cols()==1);
139 #endif
140  }
141 
142  Vector<Ref,AT>& resize() {
143  Matrix<General,Ref,Ref,AT>::resize();
144  return *this;
145  }
146 
147  Vector<Ref,AT>& resize(int m, Noinit) {
148  Matrix<General,Ref,Ref,AT>::resize(m,1,Noinit());
149  return *this;
150  }
151 
152  Vector<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=0) {
153  Matrix<General,Ref,Ref,AT>::resize(m,1,ini,a);
154  return *this;
155  }
156 
163  template <class Row>
164  inline Vector<Ref,AT>& operator<<(const Vector<Row,AT> &x);
165 
172  inline Vector<Ref,AT>& operator>>(const Vector<Ref,AT> &x);
173 
174  inline Vector<Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT> &A);
175 
184  inline Vector<Ref,AT>& operator=(const Vector<Ref,AT> &x);
185 
186  template <class Row>
187  inline Vector<Ref,AT>& operator=(const Vector<Row,AT> &x);
188 
199  AT& operator()(int i) {
200 
201 #ifndef FMATVEC_NO_BOUNDS_CHECK
202  assert(i>=0);
203  assert(i<m);
204 #endif
205 
206  return e(i);
207  };
208 
213  const AT& operator()(int i) const {
214 
215 #ifndef FMATVEC_NO_BOUNDS_CHECK
216  assert(i>=0);
217  assert(i<m);
218 #endif
219 
220  return e(i);
221  };
222 
223  AT& er(int i) {
224  return ele[i];
225  };
226 
227  const AT& er(int i) const {
228  return ele[i];
229  };
230 
231  AT& et(int i) {
232  return ele[i*lda];
233  };
234 
235  const AT& et(int i) const {
236  return ele[i*lda];
237  };
238 
239  AT& e(int i) {
240  return tp ? et(i) : er(i);
241  };
242 
243  const AT& e(int i) const {
244  return tp ? et(i) : er(i);
245  };
246 
254  inline Vector<Ref,AT>& init(const AT& a=0);
255  inline Vector<Ref,AT>& init(Init, const AT& a=0) { return init(a); }
256  inline Vector<Ref,AT>& init(Noinit, const AT& a=0) { return *this; }
257 
262  int size() const {return m;};
263 
270  int inc() const {return tp?lda:1;};
271 
277  inline Vector<Ref,AT> copy() const;
278 
288  inline Vector<Ref,AT> operator()(int i1, int i2);
289 
294  inline const Vector<Ref,AT> operator()(int i1, int i2) const;
295 
304  inline Vector<Ref,AT> operator()(const Range<Var,Var> &I);
305 
310  inline const Vector<Ref,AT> operator()(const Range<Var,Var> &I) const;
311 
313 
318  inline operator std::vector<AT>();
319 
324  inline Vector(std::vector<AT> v);
325 
326  RowVector<Ref,AT> T() {
327  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
328  };
329 
330  const RowVector<Ref,AT> T() const {
331  return RowVector<Ref,AT>(m,lda,tp?false:true,memory,ele);
332  }
333 
334  };
335 
336  template <class AT>
337  inline Vector<Ref,AT>& Vector<Ref,AT>::init(const AT &val) {
338  if(tp)
339  for(int i=0; i<m; i++)
340  et(i) = val;
341  else
342  for(int i=0; i<m; i++)
343  er(i) = val;
344  return *this;
345  }
346 
347  template <class AT>
349 
350  if(!ele) {
351  m = x.size();
352  n = 1;
353  lda = m;
354  tp = false;
355  memory.resize(m);
356  ele = (AT*)memory.get();
357  } else {
358 #ifndef FMATVEC_NO_SIZE_CHECK
359  assert(m == x.size());
360 #endif
361  }
362 
363  deepCopy(x);
364 
365  return *this;
366  }
367 
368  template <class AT> template<class Row>
370 
371  if(!ele) {
372  m = x.size();
373  n = 1;
374  lda = m;
375  tp = false;
376  memory.resize(m);
377  ele = (AT*)memory.get();
378  } else {
379 #ifndef FMATVEC_NO_SIZE_CHECK
380  assert(m == x.size());
381 #endif
382  }
383 
384  deepCopy(x);
385 
386  return *this;
387  }
388 
389  template <class AT> template<class Row>
391 
392  if(m!=x.size()) {
393  m = x.size();
394  n = 1;
395  lda = m;
396  tp = false;
397  memory.resize(m);
398  ele = (AT*)memory.get();
399  }
400 
401  deepCopy(x);
402 
403  return *this;
404  }
405 
406  template <class AT>
408 
409  m = x.m;
410  n = 1;
411  memory = x.memory;
412  ele = x.ele;
413  lda = x.lda;
414  tp = x.tp;
415 
416  return *this;
417  }
418 
419  template <class AT>
421 
422 #ifndef FMATVEC_NO_SIZE_CHECK
423  assert(A.cols() == 1);
424 #endif
425  m = A.rows();
426  n = 1;
427  memory = A.memory;
428  ele = A.ele;
429  lda = A.lda;
430  tp = A.tp;
431 
432  return *this;
433  }
434 
435  template <class AT>
437 
438  Vector<Ref,AT> x(m,NONINIT);
439  x.deepCopy(*this);
440 
441  return x;
442  }
443 
444  template <class AT>
446  return operator()(Range<Var,Var>(i1,i2));
447  }
448 
449  template <class AT>
450  inline const Vector<Ref,AT> Vector<Ref,AT>::operator()(int i1, int i2) const {
451  return operator()(Range<Var,Var>(i1,i2));
452  }
453 
454  template <class AT>
456 
457 #ifndef FMATVEC_NO_BOUNDS_CHECK
458  assert(I.end()<m);
459 #endif
460 
461  return Vector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
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>
475  inline Vector<Ref,AT>::operator std::vector<AT>() {
476  std::vector<AT> ret(size());
477  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
478  return ret;
479  }
480 
481  template <class AT>
482  inline Vector<Ref,AT>::Vector(std::vector<AT> v) : Matrix<General,Ref,Ref,AT>(v.size(),1) {
483  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
484  }
485 
487 
488  template <class AT>
489  inline void Vector<Ref,AT>::deepCopy(const Vector<Ref,AT> &x) {
490  if(tp) {
491  if(x.tp)
492  for(int i=0; i<size(); i++)
493  et(i) = x.et(i);
494  else
495  for(int i=0; i<size(); i++)
496  et(i) = x.er(i);
497  }
498  else {
499  if(x.tp)
500  for(int i=0; i<size(); i++)
501  er(i) = x.et(i);
502  else
503  for(int i=0; i<size(); i++)
504  er(i) = x.er(i);
505  }
506  }
507 
508  template <class AT> template <class Row>
509  inline void Vector<Ref,AT>::deepCopy(const Vector<Row,AT> &x) {
510  if(tp)
511  for(int i=0; i<size(); i++)
512  et(i) = x.e(i);
513  else
514  for(int i=0; i<size(); i++)
515  er(i) = x.e(i);
516  }
517 
519 
520 }
521 
522 #endif
const AT & operator()(int i) const
Element operator.
Definition: vector.h:213
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i)
Element operator.
Definition: vector.h:199
int rows() const
Number of rows.
Definition: general_matrix.h:272
Vector(const Vector< Ref, AT > &x)
Copy Constructor.
Definition: vector.h:127
Definition: fmatvec.h:50
Vector()
Standard constructor.
Definition: vector.h:85
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:114
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:171
int size() const
Size.
Definition: vector.h:262
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 end() const
Last element.
Definition: range.h:95
Vector(int m, AT *ele)
Regular Constructor.
Definition: vector.h:99
This is an index class for creating submatrices.
Definition: range.h:44
int inc() const
Increment.
Definition: vector.h:270
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:278

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML