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