fmatvec  2.0.0
Fast Matrix/Vector Library
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> {
46 
47  public:
48 
49  typedef AT* iterator;
50  typedef const AT* const_iterator;
51 
52  typedef AT value_type;
53 
55 
56  friend class RowVector<Ref,AT>;
57 
58  protected:
59 
60  template<class Row> inline Vector<Ref,AT>& copy(const Vector<Row,AT> &x);
61 
62  AT* elePtr(int i) {
63  return ele+i;
64  }
65 
66  const AT* elePtr(int i) const {
67  return ele+i;
68  }
69 
71 
72  public:
73 
78  explicit Vector() : Matrix<General,Ref,Ref,AT>() { n=1; }
79 
80  explicit Vector(int m, Noinit ini) : Matrix<General,Ref,Ref,AT>(m,1,ini) { }
81  explicit Vector(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Ref,Ref,AT>(m,1,ini,a) { }
82 
88  Vector(const Vector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
89  }
90 
91  template<class Row>
92  Vector(const Vector<Row,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
93  }
94 
95  template<class Type, class Row, class Col>
96  explicit Vector(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
97  FMATVEC_ASSERT(x.cols()==1, AT);
98  }
99 
106  explicit Vector(int m, AT* ele) : Matrix<General,Ref,Ref,AT>(m,1,ele) {
107  }
108 
121  Vector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
122  FMATVEC_ASSERT(n==1, AT);
123  }
124 
125  Vector<Ref,AT>& resize(int m, Noinit) {
127  return *this;
128  }
129 
130  Vector<Ref,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) {
131  Matrix<General,Ref,Ref,AT>::resize(m,1,ini,a);
132  return *this;
133  }
134 
142  FMATVEC_ASSERT(m == x.size(), AT);
143  return copy(x);
144  }
145 
152  template <class Row>
154  FMATVEC_ASSERT(m == x.size(), AT);
155  return copy(x);
156  }
157 
165  m = x.m;
166  n = 1;
167  memory = x.memory;
168  ele = x.ele;
169  lda = x.lda;
170  return *this;
171  }
172 
180  FMATVEC_ASSERT(A.cols() == 1, AT);
181  m = A.rows();
182  n = 1;
183  memory = A.memory;
184  ele = A.ele;
185  lda = A.lda;
186  return *this;
187  }
188 
195  template <class Row>
196  inline Vector<Ref,AT>& operator<<=(const Vector<Row,AT> &x) {
197  if(m!=x.size()) resize(x.size(),NONINIT);
198  return copy(x);
199  }
200 
201  template <class AT2>
202  operator Vector<Ref,AT2>() const {
203  Vector<Ref,AT2> ret(size());
204  for(size_t i=0; i<size(); ++i)
205  ret(i) = (*this)(i);
206  return ret;
207  }
208 
217  AT& operator()(int i) {
218 
219  FMATVEC_ASSERT(i>=0, AT);
220  FMATVEC_ASSERT(i<m, AT);
221 
222  return e(i);
223  }
224 
229  const AT& operator()(int i) const {
230 
231  FMATVEC_ASSERT(i>=0, AT);
232  FMATVEC_ASSERT(i<m, AT);
233 
234  return e(i);
235  }
236 
237  iterator begin() { return &ele[0]; }
238  iterator end() { return &ele[m]; }
239  const_iterator begin() const { return &ele[0]; }
240  const_iterator end() const { return &ele[m]; }
241  const_iterator cbegin() const noexcept { return &ele[0]; }
242  const_iterator cend() const noexcept { return &ele[m]; }
243 
244  AT& e(int i) {
245  return ele[i];
246  }
247 
248  const AT& e(int i) const {
249  return ele[i];
250  }
251 
259  inline Vector<Ref,AT>& init(const AT& val=AT());
260  inline Vector<Ref,AT>& init(Init, const AT& a=AT()) { return init(a); }
261  inline Vector<Ref,AT>& init(Noinit, const AT& a=AT()) { return *this; }
262 
267  int size() const {return m;}
268 
275  int inc() const {return 1;}
276 
281  inline const Vector<Ref,AT> operator()(const Range<Var,Var> &I) const;
282 
283  AT* operator()() { return Matrix<General, Ref, Ref, AT>::operator()(); }
284  const AT* operator()() const { return Matrix<General, Ref, Ref, AT>::operator()(); }
285 
290  explicit inline operator std::vector<AT>() const;
291 
296  explicit inline Vector(const std::vector<AT> &v);
297 
298 // /*! \brief Cast to AT.
299 // *
300 // * \return The AT representation of the vector
301 // * */
302 // explicit operator AT() const {
303 // FMATVEC_ASSERT(m==1, AT);
304 // return e(0);
305 // }
306 //
307 // /*! \brief AT Constructor.
308 // * Constructs and initializes a vector with a AT object.
309 // * \param x The AT the vector will be initialized with.
310 // * */
311 // explicit Vector(const AT &x) : Matrix<General,Ref,Ref,AT>(x) { }
312 
313  template<class Row> inline void set(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x);
314 
315  template<class Row> inline void add(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x);
316 
317  inline void ref(Vector<Ref,AT> &x, const fmatvec::Range<Var,Var> &I);
318 
319  inline void ref(Matrix<General,Ref,Ref,AT> &A, int j);
320 
321  inline void ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, int j);
322 
323  const RowVector<Ref,AT> T() const;
324  };
325 
326  template <class AT>
327  inline Vector<Ref,AT>& Vector<Ref,AT>::init(const AT &val) {
328  for(int i=0; i<m; i++)
329  e(i) = val;
330  return *this;
331  }
332 
333  template <class AT>
335 
336  FMATVEC_ASSERT(I.end()<m, AT);
337 
338  Vector<Ref,AT> x(I.end()-I.start()+1,NONINIT);
339 
340  for(int i=0; i<x.size(); i++)
341  x.e(i) = e(I.start()+i);
342 
343  return x;
344  }
345 
346  template <class AT>
347  inline Vector<Ref,AT>::operator std::vector<AT>() const {
348  std::vector<AT> ret(size());
349  if(size()>0) memcpy(&ret[0], &operator()(0), sizeof(AT)*size());
350  return ret;
351  }
352 
353  template <class AT>
354  inline Vector<Ref,AT>::Vector(const std::vector<AT> &v) : Matrix<General,Ref,Ref,AT>(v.size(),1) {
355  if(size()>0) memcpy(&operator()(0), &v[0], sizeof(AT)*size());
356  }
357 
359 
360  template <class AT> template <class Row>
362  for(int i=0; i<size(); i++)
363  e(i) = x.e(i);
364  return *this;
365  }
366 
367  template <class AT> template<class Row>
368  inline void Vector<Ref,AT>::set(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
369 
370  FMATVEC_ASSERT(I.end()<size(), AT);
371  FMATVEC_ASSERT(I.size()==x.size(), AT);
372 
373  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
374  e(i) = x.e(ii);
375  }
376 
377  template <class AT> template<class Row>
378  inline void Vector<Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
379 
380  FMATVEC_ASSERT(I.end()<size(), AT);
381  FMATVEC_ASSERT(I.size()==x.size(), AT);
382 
383  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
384  e(i) += x.e(ii);
385  }
386 
387  template <class AT>
388  inline void Vector<Ref,AT>::ref(Vector<Ref,AT> &x, const fmatvec::Range<Var,Var> &I) {
389 
390  FMATVEC_ASSERT(I.end()<x.size(), AT);
391 
392  m=I.size();
393  n=1;
394  memory = x.memory;
395  ele = x.elePtr(I.start());
396  lda = x.lda;
397  }
398 
399  template <class AT>
400  inline void Vector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, int j) {
401 
402  FMATVEC_ASSERT(j<A.cols(), AT);
403 
404  m=A.rows();
405  n=1;
406  memory = A.memory;
407  ele = A.elePtr(0,j);
408  lda = A.lda;
409  }
410 
411  template <class AT>
412  inline void Vector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, int j) {
413 
414  FMATVEC_ASSERT(I.end()<A.rows(), AT);
415  FMATVEC_ASSERT(j<A.cols(), AT);
416 
417  m=I.size();
418  n=1;
419  memory = A.memory;
420  ele = A.elePtr(I.start(),j);
421  lda = A.lda;
422  }
423 
424  template <class AT>
425  inline const RowVector<Ref,AT> Vector<Ref,AT>::T() const {
426  RowVector<Ref,AT> x(m,NONINIT);
427  for(int i=0; i<m; i++)
428  x.e(i) = e(i);
429  return x;
430  }
431 
433 
434 }
435 
436 #endif
Vector< Ref, AT > & operator=(const Vector< Ref, AT > &x)
Assignment operator.
Definition: vector.h:141
int end() const
Last element.
Definition: range.h:91
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i)
Element operator.
Definition: vector.h:217
int rows() const
Number of rows.
Definition: general_matrix.h:278
Vector< Ref, AT > & operator&=(Vector< Ref, AT > &x)
Reference operator.
Definition: vector.h:164
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
const AT & operator()(int i) const
Element operator.
Definition: vector.h:229
Vector(const Vector< Ref, AT > &x)
Copy Constructor.
Definition: vector.h:88
Definition: fmatvec.h:50
int size() const
Size.
Definition: vector.h:267
Vector()
Standard constructor.
Definition: vector.h:78
Vector< Ref, AT > & operator=(const Vector< Row, AT > &x)
Assignment operator.
Definition: vector.h:153
Definition: types.h:94
int start() const
First element.
Definition: range.h:85
int inc() const
Increment.
Definition: vector.h:275
Definition: fmatvec.h:47
Vector(const char *str)
String Constructor.
Definition: vector.h:121
This is a matrix class for general matrices.
Definition: general_matrix.h:41
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:284
int size() const
Size.
Definition: range.h:97
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
void set(const fmatvec::Range< Var, Var > &I, const Vector< Row, AT > &x)
Cast to AT.
Vector(int m, AT *ele)
Regular Constructor.
Definition: vector.h:106
This is an index class for creating submatrices.
Definition: range.h:44
Vector< Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: vector.h:179