All Classes Namespaces Functions Variables Typedefs Enumerations Pages
row_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 row_vector_h
23 #define row_vector_h
24 
25 #include "general_matrix.h"
26 
27 namespace fmatvec {
36  template <class AT> class RowVector<Ref,AT> : public Matrix<General,Ref,Ref,AT> {
44 
45  public:
46 
47  typedef AT* iterator;
48  typedef const AT* const_iterator;
49 
50  typedef AT value_type;
51 
53 
54  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
55  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
56  //friend RowVector<Ref,double> trans(const Vector<Ref,double> &x);
57 
58  friend class Vector<Ref,AT>;
59 
61  friend const RowVector<Ref,AT> Matrix<General,Ref,Ref,AT>::row(int i) const;
62 
63  protected:
64 
65  inline void deepCopy(const RowVector<Ref,AT> &x);
66  template<class Col> inline void deepCopy(const RowVector<Col,AT> &x);
67 
68  const AT* elePtr(int i) const {
69  return tp ? ele+i : ele+lda*i;
70  };
71 
72  AT* elePtr(int i) {
73  return tp ? ele+i : ele+lda*i;
74  };
75 
76  RowVector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(1,n_, lda_, tp, memory, ele_) {
77  }
78 
80 
81  public:
82 
87  RowVector() : Matrix<General,Ref,Ref,AT>() { m=1; }
88 
89 // template<class Ini=All<AT> >
90 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
91 
92  RowVector(int n, Noinit ini) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
93  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(1,n,ini,a) { }
94 
101  RowVector(int n, AT* ele) : Matrix<General,Ref,Ref,AT>(1,n,ele) {
102  }
103 
116  RowVector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
117 #ifndef FMATVEC_NO_SIZE_CHECK
118  assert(m==1);
119 #endif
120  }
121 
126  explicit RowVector(const Matrix<General,Ref,Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
127  LogicAssert(x.rows()==1,"Number of cols() must be 1 in RowVector<Ref,AT>::RowVector(const Matrix<General,Ref,Ref,AT> &x)");
128  }
129 
130  RowVector<Ref,AT>& resize() {
132  return *this;
133  }
134 
135  RowVector<Ref,AT>& resize(int n, Noinit) {
136  Matrix<General,Ref,Ref,AT>::resize(1,n,Noinit());
137  return *this;
138  }
139 
140  RowVector<Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
141  Matrix<General,Ref,Ref,AT>::resize(1,n,ini,a);
142  return *this;
143  }
144 
152  RowVector(const RowVector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
153  }
154 
161  inline RowVector<Ref,AT>& operator<<(const RowVector<Ref,AT> &x);
162 
170 
179  inline RowVector<Ref,AT>& operator=(const RowVector<Ref,AT> &x);
180 
181  template <class Row>
182  inline RowVector<Ref,AT>& operator=(const RowVector<Row,AT> &x);
183 
194  AT& operator()(int i) {
195 #ifndef FMATVEC_NO_BOUNDS_CHECK
196  assert(i>=0);
197  assert(i<n);
198 #endif
199  return e(i);
200  };
201 
206  const AT& operator()(int i) const {
207 #ifndef FMATVEC_NO_BOUNDS_CHECK
208  assert(i>=0);
209  assert(i<n);
210 #endif
211  return e(i);
212  };
213 
214  AT& er(int i) {
215  return ele[i*lda];
216  };
217 
218  iterator begin() { assert(lda==1); return &ele[0]; }
219  iterator end() { assert(lda==1); return &ele[n*lda]; }
220  const_iterator begin() const { assert(lda==1); return &ele[0]; }
221  const_iterator end() const { assert(lda==1); return &ele[n*lda]; }
222  const_iterator cbegin() const noexcept { assert(lda==1); return &ele[0]; }
223  const_iterator cend() const noexcept { assert(lda==1); return &ele[n*lda]; }
224 
225  const AT& er(int i) const {
226  return ele[i*lda];
227  };
228 
229  AT& et(int i) {
230  return ele[i];
231  };
232 
233  const AT& et(int i) const {
234  return ele[i];
235  };
236 
237  AT& e(int i) {
238  return tp ? et(i) : er(i);
239  };
240 
241  const AT& e(int i) const {
242  return tp ? et(i) : er(i);
243  };
244 
252  inline RowVector<Ref,AT>& init(const AT& val);
253  inline RowVector<Ref,AT>& init(Init, const AT &a=0) { return init(a); }
254  inline RowVector<Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
255 
260  int size() const {return n;};
261 
268  int inc() const {return tp?1:lda;};
269 
275  inline RowVector<Ref,AT> copy() const;
276 
286  inline RowVector<Ref,AT> operator()(int i1, int i2);
287 
292  inline const RowVector<Ref,AT> operator()(int i1, int i2) const;
293 
302  inline RowVector<Ref,AT> operator()(const Range<Var,Var> &I);
303 
308  inline const RowVector<Ref,AT> operator()(const Range<Var,Var> &I) const;
309 
311 
312  Vector<Ref,AT> T() {
313  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
314  }
315 
316  const Vector<Ref,AT> T() const {
317  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
318  }
319  };
320 
322 
323 
324  template <class AT>
326 
327  if(tp) {
328  for(int i=0; i<n; i++)
329  ele[i] = val;
330  }
331  else {
332  for(int i=0; i<n; i++)
333  ele[i*lda] = val;
334  }
335 
336  return *this;
337  }
338 
339  template <class AT>
341 
342  if(!ele) {
343  m = 1;
344  n = x.size();
345  lda = m;
346  tp = false;
347  memory.resize(m);
348  ele = (AT*)memory.get();
349  } else {
350 #ifndef FMATVEC_NO_SIZE_CHECK
351  assert(n == x.size());
352 #endif
353  }
354 
355  deepCopy(x);
356 
357  return *this;
358  }
359 
360  template <class AT> template<class Row>
362 
363  if(!ele) {
364  m = 1;
365  n = x.size();
366  lda = m;
367  tp = false;
368  memory.resize(m);
369  ele = (AT*)memory.get();
370  } else {
371 #ifndef FMATVEC_NO_SIZE_CHECK
372  assert(n == x.size());
373 #endif
374  }
375 
376  deepCopy(x);
377 
378  return *this;
379  }
380 
381  template <class AT>
383 
384  if(n!=x.size()) {
385  m = 1;
386  n = x.size();
387  lda = m;
388  tp = false;
389  memory.resize(n);
390  ele = (AT*)memory.get();
391  }
392 
393  deepCopy(x);
394 
395  return *this;
396  }
397 
398  template <class AT>
400 
401  m = 1;
402  n = x.n;
403  memory = x.memory;
404  ele = x.ele;
405  lda = x.lda;
406  tp = x.tp;
407 
408  return *this;
409  }
410 
411  template <class AT>
413 
414  RowVector<Ref,AT> x(n,NONINIT);
415  x.deepCopy(*this);
416 
417  return x;
418  }
419 
420  template <class AT>
422  return operator()(Range<Var,Var>(i1,i2));
423  }
424 
425  template <class AT>
426  inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(int i1, int i2) const {
427  return operator()(Range<Var,Var>(i1,i2));
428  }
429 
430  template <class AT>
432 
433 #ifndef FMATVEC_NO_BOUNDS_CHECK
434  assert(I.end()<n);
435 #endif
436 
437  return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
438  }
439 
440  template <class AT>
442 
443 #ifndef FMATVEC_NO_BOUNDS_CHECK
444  assert(I.end()<n);
445 #endif
446 
447  return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
448  }
449 
451 
452  template <class AT>
453  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Ref,AT> &x) {
454  if(tp) {
455  if(x.tp)
456  for(int i=0; i<size(); i++)
457  et(i) = x.et(i);
458  else
459  for(int i=0; i<size(); i++)
460  et(i) = x.er(i);
461  }
462  else {
463  if(x.tp)
464  for(int i=0; i<size(); i++)
465  er(i) = x.et(i);
466  else
467  for(int i=0; i<size(); i++)
468  er(i) = x.er(i);
469  }
470  }
471 
472  template <class AT> template <class Col>
473  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Col,AT> &x) {
474  if(tp)
475  for(int i=0; i<size(); i++)
476  et(i) = x.e(i);
477  else
478  for(int i=0; i<size(); i++)
479  er(i) = x.e(i);
480  }
481 
483 
484 }
485 
486 #endif
const AT & operator()(int i) const
Element operator.
Definition: row_vector.h:206
AT & operator()(int i)
Element operator.
Definition: row_vector.h:194
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int size() const
Size.
Definition: row_vector.h:260
RowVector(int n, AT *ele)
Regular Constructor.
Definition: row_vector.h:101
int rows() const
Number of rows.
Definition: general_matrix.h:273
Definition: fmatvec.h:50
Definition: types.h:62
int inc() const
Increment.
Definition: row_vector.h:268
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: fmatvec.h:47
RowVector(const RowVector< Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:152
This is a matrix class for general matrices.
Definition: general_matrix.h:41
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
RowVector(const char *str)
String Constructor.
Definition: row_vector.h:116
Definition: matrix.h:42
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
RowVector(const Matrix< General, Ref, Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:126
RowVector()
Standard constructor.
Definition: row_vector.h:87
This is an index class for creating submatrices.
Definition: range.h:44
int start() const
First element.
Definition: range.h:89

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home