fmatvec  0.0.0
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  using iterator = AT *;
51  using const_iterator = const AT *;
52 
53  using value_type = AT;
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 const Vector<Ref,AT> operator()(const Indices &I) const;
319 
320  template<class Row> inline void set(const Indices &I, const Vector<Row,AT> &x);
321 
322  inline void ref(Vector<Ref,AT> &x, const fmatvec::Range<Var,Var> &I);
323 
324  inline void ref(Matrix<General,Ref,Ref,AT> &A, int j);
325 
326  inline void ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, int j);
327 
328  const RowVector<Ref,AT> T() const;
329  };
330 
331  template <class AT>
332  inline Vector<Ref,AT>& Vector<Ref,AT>::init(const AT &val) {
333  for(int i=0; i<m; i++)
334  e(i) = val;
335  return *this;
336  }
337 
338  template <class AT>
340 
341  FMATVEC_ASSERT(I.end()<m, AT);
342 
343  Vector<Ref,AT> x(I.end()-I.start()+1,NONINIT);
344 
345  for(int i=0; i<x.size(); i++)
346  x.e(i) = e(I.start()+i);
347 
348  return x;
349  }
350 
351  template <class AT>
352  inline Vector<Ref,AT>::operator std::vector<AT>() const {
353  std::vector<AT> ret(size());
354  for(int i=0; i<size(); ++i)
355  ret[i] = e(i);
356  return ret;
357  }
358 
359  template <class AT>
360  inline Vector<Ref,AT>::Vector(const std::vector<AT> &v) : Matrix<General,Ref,Ref,AT>(v.size(),1,NONINIT) {
361  for(int i=0; i<size(); ++i)
362  e(i) = v[i];
363  }
364 
366 
367  template <class AT> template <class Row>
369  for(int i=0; i<size(); i++)
370  e(i) = x.e(i);
371  return *this;
372  }
373 
374  template <class AT> template<class Row>
375  inline void Vector<Ref,AT>::set(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
376 
377  FMATVEC_ASSERT(I.end()<size(), AT);
378  FMATVEC_ASSERT(I.size()==x.size(), AT);
379 
380  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
381  e(i) = x.e(ii);
382  }
383 
384  template <class AT> template<class Row>
385  inline void Vector<Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const Vector<Row,AT> &x) {
386 
387  FMATVEC_ASSERT(I.end()<size(), AT);
388  FMATVEC_ASSERT(I.size()==x.size(), AT);
389 
390  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
391  e(i) += x.e(ii);
392  }
393 
394  template <class AT>
395  inline const Vector<Ref,AT> Vector<Ref,AT>::operator()(const Indices &I) const {
396  FMATVEC_ASSERT(I.max()<size(), AT);
397 
398  Vector<Ref,AT> x(I.size(),NONINIT);
399 
400  for(int i=0; i<x.size(); i++)
401  x.e(i) = e(I[i]);
402 
403  return x;
404  }
405 
406  template <class AT> template <class Row>
407  inline void Vector<Ref,AT>::set(const Indices &I, const Vector<Row,AT> &x) {
408  FMATVEC_ASSERT(I.max()<size(), AT);
409  FMATVEC_ASSERT(I.size()==x.size(), AT);
410  for(int i=0; i<I.size(); i++)
411  e(I[i]) = x.e(i);
412  }
413 
414  template <class AT>
415  inline void Vector<Ref,AT>::ref(Vector<Ref,AT> &x, const fmatvec::Range<Var,Var> &I) {
416 
417  FMATVEC_ASSERT(I.end()<x.size(), AT);
418 
419  m=I.size();
420  n=1;
421  memory = x.memory;
422  ele = x.elePtr(I.start());
423  lda = x.lda;
424  }
425 
426  template <class AT>
427  inline void Vector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, int j) {
428 
429  FMATVEC_ASSERT(j<A.cols(), AT);
430 
431  m=A.rows();
432  n=1;
433  memory = A.memory;
434  ele = A.elePtr(0,j);
435  lda = A.lda;
436  }
437 
438  template <class AT>
439  inline void Vector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, int j) {
440 
441  FMATVEC_ASSERT(I.end()<A.rows(), AT);
442  FMATVEC_ASSERT(j<A.cols(), AT);
443 
444  m=I.size();
445  n=1;
446  memory = A.memory;
447  ele = A.elePtr(I.start(),j);
448  lda = A.lda;
449  }
450 
451  template <class AT>
452  inline const RowVector<Ref,AT> Vector<Ref,AT>::T() const {
453  RowVector<Ref,AT> x(m,NONINIT);
454  for(int i=0; i<m; i++)
455  x.e(i) = e(i);
456  return x;
457  }
458 
460 
461 }
462 
463 #endif
Vector< Ref, AT > & operator=(const Vector< Ref, AT > &x)
Assignment operator.
Definition: vector.h:142
int end() const
Last element.
Definition: range.h:92
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
AT & operator()(int i)
Element operator.
Definition: vector.h:218
int rows() const
Number of rows.
Definition: general_matrix.h:307
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:51
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:86
int inc() const
Increment.
Definition: vector.h:276
Definition: fmatvec.h:48
Vector(const char *str)
String Constructor.
Definition: vector.h:122
This is a matrix class for general matrices.
Definition: general_matrix.h:42
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:313
int size() const
Size.
Definition: range.h:98
Definition: types.h:88
Shape class for general matrices.
Definition: types.h:112
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:45
Vector< Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: vector.h:180