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 : public Matrix<General, AT> {
47 
48  public:
49 
51 
52  template <class T> friend RowVector<T> trans(const Vector<T> &x);
53  template <class T> friend Vector<T> trans(const RowVector<T> &x);
54 
55  friend class RowVector<AT>;
56 
58  friend const Vector<AT> Matrix<General, AT>::col(int i) const;
59 
60  protected:
61 
62  void deepCopy(const Vector<AT> &x);
63 
64  AT* elePtr(int i) {
65  return tp ? ele+lda*i : ele+i;
66  };
67 
68  const AT* elePtr(int i) const {
69  return tp ? ele+lda*i : ele+i;
70  };
71 
72  Vector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General, AT>(n_, 1, lda_, tp, memory, ele_) {
73  }
74 
76 
77  public:
78 
83  Vector() : Matrix<General, AT>() {
84  }
85 
94  Vector(int m) : Matrix<General, AT>(m,1) {
95  }
96 
103  Vector(int m, AT* ele) : Matrix<General, AT>(m,1,ele) {
104  }
105 
115  Vector(int m, Initialization ini, const AT &a=0) : Matrix<General, AT>(m,1,ini,a) {
116  }
117 
130  Vector(const char *str) : Matrix<General, AT>(str) {
131 #ifdef FMATVEC_SIZE_CHECK
132  assert(n==1);
133 #endif
134  }
135 
143  Vector(const Vector<AT> &x) : Matrix<General, AT>(x) {
144  }
145 
150  explicit Vector(const Matrix<General, AT> &x) : Matrix<General, AT>(x) {
151 
152 #ifdef FMATVEC_SIZE_CHECK
153  assert(x.cols()==1);
154 #endif
155  }
156 
163  Vector<AT>& operator<<(const Vector<AT> &x);
164 
171  Vector<AT>& operator>>(const Vector<AT> &x);
172 
182  Vector<AT>& resize(int n) {
184  return *this;
185  }
186 
197  Vector<AT>& resize(int n, Initialization ini, const AT &a=0) {
198  Matrix<General, AT>::resize(n,1,ini,a);
199  return *this;
200  }
201 
211 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
212  return operator<<(x);
213 #else
214  return operator>>(x);
215 #endif
216  }
217 
228  AT& operator()(int i) {
229 
230 #ifndef FMATVEC_NO_BOUNDS_CHECK
231  assert(i>=0);
232  assert(i<m);
233 #endif
234 
235  return tp ? ele[i*lda] : ele[i];
236  };
237 
242  const AT& operator()(int i) const {
243 
244 #ifndef FMATVEC_NO_BOUNDS_CHECK
245  assert(i>=0);
246  assert(i<m);
247 #endif
248 
249  return tp ? ele[i*lda] : ele[i];
250  };
251 
259  Vector<AT>& init(const AT& a);
260 
265  int size() const {return m;};
266 
273  int inc() const {return tp?lda:1;};
274 
280  Vector<AT> copy() const;
281 
291  Vector<AT> operator()(int i1, int i2);
292 
297  const Vector<AT> operator()(int i1, int i2) const;
298 
307  Vector<AT> operator()(const Index &I);
308 
313  const Vector<AT> operator()(const Index &I) const;
314 
317 
322  operator std::vector<AT>();
323 
328  Vector(std::vector<AT> v);
329 
330  RowVector<AT> T() {
331  return RowVector<AT>(m,lda,tp?false:true,memory,ele);
332  };
333 
334  const RowVector<AT> T() const {
335  return RowVector<AT>(m,lda,tp?false:true,memory,ele);
336  }
337 
338  };
339 
340  template <class AT>
341  Vector<AT>& Vector<AT>::init(const AT& val) {
342 
343  if(tp) {
344  for(int i=0; i<m; i++)
345  ele[i*lda] = val; // operator()(i) = val;
346  }
347  else {
348  for(int i=0; i<m; i++)
349  ele[i] = val; // operator()(i) = val;
350  }
351 
352  return *this;
353  }
354 
355  template <class AT>
357 
358  if(x.size() == 0)
359  return *this;
360 
361  if(m==0) {
362  m = x.m;
363  n = x.n;
364  lda = m;
365  tp = false;
366  memory.resize(m);
367  ele = (AT*)memory.get();
368  } else {
369 #ifdef FMATVEC_SIZE_CHECK
370  assert(m == x.m);
371 #endif
372  }
373 
374  deepCopy(x);
375 
376  return *this;
377  }
378 
379  template <class AT>
381 
382  if(m==0) {
383  m = x.m;
384  n = x.n;
385  } else {
386 #ifdef FMATVEC_SIZE_CHECK
387  assert(m == x.m);
388 #endif
389  }
390 
391  memory = x.memory;
392  ele = x.ele;
393  lda = x.lda;
394  tp = x.tp;
395 
396  return *this;
397  }
398 
399  template <class AT>
401 
402  Vector<AT> x(m,NONINIT);
403  x.deepCopy(*this);
404 
405  return x;
406  }
407 
408  template <class AT>
409  void Vector<AT>::deepCopy(const Vector<AT> &x) {
410  if(tp) {
411  if(x.tp) {
412  for(int i=0; i<size(); i++)
413  ele[i*lda] = x.ele[i*x.lda]; // operator()(i) = x(i);
414  }
415  else {
416  for(int i=0; i<size(); i++)
417  ele[i*lda] = x.ele[i];
418  }
419  }
420  else {
421  if(x.tp) {
422  for(int i=0; i<size(); i++)
423  ele[i] = x.ele[i*x.lda]; // operator()(i) = x(i);
424  }
425  else {
426  for(int i=0; i<size(); i++)
427  ele[i] = x.ele[i];
428  }
429  }
430  }
431 
432  template <> void Vector<double>::deepCopy(const Vector<double> &A);
433 
434 
435  template <class AT> Vector<AT> Vector<AT>::operator()(int i1, int i2) {
436  return operator()(Index(i1,i2));
437  }
438 
439  template <class AT> const Vector<AT> Vector<AT>::operator()(int i1, int i2) const {
440  return operator()(Index(i1,i2));
441  }
442 
443  template <class AT>
444  const Vector<AT> Vector<AT>::operator()(const Index &I) const {
445 
446 #ifndef FMATVEC_NO_BOUNDS_CHECK
447  assert(I.end()<m);
448 #endif
449 
450  return Vector<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
451  }
452 
453  template <class AT>
455 
456 #ifndef FMATVEC_NO_BOUNDS_CHECK
457  assert(I.end()<m);
458 #endif
459 
460  return Vector<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
461  }
462 
463  template <class AT>
464  Vector<AT>::operator std::vector<AT>() {
465  std::vector<AT> ret(size());
466  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
467  return ret;
468  }
469 
470  template <class AT>
471  Vector<AT>::Vector(std::vector<AT> v) : Matrix<General, AT>(v.size(),1) {
472  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
473  }
474 }
475 
476 #endif