All Classes Namespaces Functions 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 
48 
49  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
50  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
51  //friend RowVector<Ref,double> trans(const Vector<Ref,double> &x);
52 
53  friend class Vector<Ref,AT>;
54 
56  friend const RowVector<Ref,AT> Matrix<General,Ref,Ref,AT>::row(int i) const;
57 
58  protected:
59 
60  inline void deepCopy(const RowVector<Ref,AT> &x);
61  template<class Col> inline void deepCopy(const RowVector<Col,AT> &x);
62 
63  const AT* elePtr(int i) const {
64  return tp ? ele+i : ele+lda*i;
65  };
66 
67  AT* elePtr(int i) {
68  return tp ? ele+i : ele+lda*i;
69  };
70 
71  RowVector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(1,n_, lda_, tp, memory, ele_) {
72  }
73 
75 
76  public:
77 
82  RowVector() : Matrix<General,Ref,Ref,AT>() { m=1; }
83 
84 // template<class Ini=All<AT> >
85 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
86 
87  RowVector(int n, Noinit ini) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
88  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(1,n,ini,a) { }
89 
96  RowVector(int n, AT* ele) : Matrix<General,Ref,Ref,AT>(1,n,ele) {
97  }
98 
111  RowVector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
112 #ifndef FMATVEC_NO_SIZE_CHECK
113  assert(m==1);
114 #endif
115  }
116 
121  explicit RowVector(const Matrix<General,Ref,Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
122  LogicAssert(x.rows()==1,"Number of cols() must be 1 in RowVector<Ref,AT>::RowVector(const Matrix<General,Ref,Ref,AT> &x)");
123  }
124 
125  RowVector<Ref,AT>& resize() {
127  return *this;
128  }
129 
130  RowVector<Ref,AT>& resize(int n, Noinit) {
131  Matrix<General,Ref,Ref,AT>::resize(1,n,Noinit());
132  return *this;
133  }
134 
135  RowVector<Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
136  Matrix<General,Ref,Ref,AT>::resize(1,n,ini,a);
137  return *this;
138  }
139 
147  RowVector(const RowVector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
148  }
149 
156  inline RowVector<Ref,AT>& operator<<(const RowVector<Ref,AT> &x);
157 
165 
174  inline RowVector<Ref,AT>& operator=(const RowVector<Ref,AT> &x);
175 
176  template <class Row>
177  inline RowVector<Ref,AT>& operator=(const RowVector<Row,AT> &x);
178 
189  AT& operator()(int i) {
190 #ifndef FMATVEC_NO_BOUNDS_CHECK
191  assert(i>=0);
192  assert(i<n);
193 #endif
194  return e(i);
195  };
196 
201  const AT& operator()(int i) const {
202 #ifndef FMATVEC_NO_BOUNDS_CHECK
203  assert(i>=0);
204  assert(i<n);
205 #endif
206  return e(i);
207  };
208 
209  AT& er(int i) {
210  return ele[i*lda];
211  };
212 
213  const AT& er(int i) const {
214  return ele[i*lda];
215  };
216 
217  AT& et(int i) {
218  return ele[i];
219  };
220 
221  const AT& et(int i) const {
222  return ele[i];
223  };
224 
225  AT& e(int i) {
226  return tp ? et(i) : er(i);
227  };
228 
229  const AT& e(int i) const {
230  return tp ? et(i) : er(i);
231  };
232 
240  inline RowVector<Ref,AT>& init(const AT& a);
241  inline RowVector<Ref,AT>& init(Init, const AT &a=0) { return init(a); }
242  inline RowVector<Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
243 
248  int size() const {return n;};
249 
256  int inc() const {return tp?1:lda;};
257 
263  inline RowVector<Ref,AT> copy() const;
264 
274  inline RowVector<Ref,AT> operator()(int i1, int i2);
275 
280  inline const RowVector<Ref,AT> operator()(int i1, int i2) const;
281 
290  inline RowVector<Ref,AT> operator()(const Index &I);
291 
296  inline const RowVector<Ref,AT> operator()(const Index &I) const;
297 
299 
300  Vector<Ref,AT> T() {
301  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
302  }
303 
304  const Vector<Ref,AT> T() const {
305  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
306  }
307  };
308 
310 
311 
312  template <class AT>
314 
315  if(tp) {
316  for(int i=0; i<n; i++)
317  ele[i] = val;
318  }
319  else {
320  for(int i=0; i<n; i++)
321  ele[i*lda] = val;
322  }
323 
324  return *this;
325  }
326 
327  template <class AT>
329 
330  if(!ele) {
331  m = 1;
332  n = x.size();
333  lda = m;
334  tp = false;
335  memory.resize(m);
336  ele = (AT*)memory.get();
337  } else {
338 #ifndef FMATVEC_NO_SIZE_CHECK
339  assert(n == x.size());
340 #endif
341  }
342 
343  deepCopy(x);
344 
345  return *this;
346  }
347 
348  template <class AT> template<class Row>
350 
351  if(!ele) {
352  m = 1;
353  n = x.size();
354  lda = m;
355  tp = false;
356  memory.resize(m);
357  ele = (AT*)memory.get();
358  } else {
359 #ifndef FMATVEC_NO_SIZE_CHECK
360  assert(n == x.size());
361 #endif
362  }
363 
364  deepCopy(x);
365 
366  return *this;
367  }
368 
369  template <class AT>
371 
372  if(n!=x.size()) {
373  m = 1;
374  n = x.size();
375  lda = m;
376  tp = false;
377  memory.resize(n);
378  ele = (AT*)memory.get();
379  }
380 
381  deepCopy(x);
382 
383  return *this;
384  }
385 
386  template <class AT>
388 
389  m = 1;
390  n = x.n;
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  RowVector<Ref,AT> x(n,NONINIT);
403  x.deepCopy(*this);
404 
405  return x;
406  }
407 
408  template <class AT>
410  return operator()(Index(i1,i2));
411  }
412 
413  template <class AT>
414  inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(int i1, int i2) const {
415  return operator()(Index(i1,i2));
416  }
417 
418  template <class AT>
420 
421 #ifndef FMATVEC_NO_BOUNDS_CHECK
422  assert(I.end()<n);
423 #endif
424 
425  return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
426  }
427 
428  template <class AT>
430 
431 #ifndef FMATVEC_NO_BOUNDS_CHECK
432  assert(I.end()<n);
433 #endif
434 
435  return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
436  }
437 
439 
440  template <class AT>
441  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Ref,AT> &x) {
442  if(tp) {
443  if(x.tp)
444  for(int i=0; i<size(); i++)
445  et(i) = x.et(i);
446  else
447  for(int i=0; i<size(); i++)
448  et(i) = x.er(i);
449  }
450  else {
451  if(x.tp)
452  for(int i=0; i<size(); i++)
453  er(i) = x.et(i);
454  else
455  for(int i=0; i<size(); i++)
456  er(i) = x.er(i);
457  }
458  }
459 
460  template <class AT> template <class Col>
461  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Col,AT> &x) {
462  if(tp)
463  for(int i=0; i<size(); i++)
464  et(i) = x.e(i);
465  else
466  for(int i=0; i<size(); i++)
467  er(i) = x.e(i);
468  }
469 
471 
472 }
473 
474 #endif
const AT & operator()(int i) const
Element operator.
Definition: row_vector.h:201
AT & operator()(int i)
Element operator.
Definition: row_vector.h:189
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
int size() const
Size.
Definition: row_vector.h:248
RowVector(int n, AT *ele)
Regular Constructor.
Definition: row_vector.h:96
int rows() const
Number of rows.
Definition: general_matrix.h:270
Definition: matrix.h:218
int end() const
Last element.
Definition: index.h:85
Definition: types.h:62
int inc() const
Increment.
Definition: row_vector.h:256
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: matrix.h:215
RowVector(const RowVector< Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:147
This is a matrix class for general matrices.
Definition: general_matrix.h:40
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:170
RowVector(const char *str)
String Constructor.
Definition: row_vector.h:111
This is an index class for creating submatrices.
Definition: index.h:34
Definition: matrix.h:38
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
RowVector(const Matrix< General, Ref, Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:121
RowVector()
Standard constructor.
Definition: row_vector.h:82
int start() const
First element.
Definition: index.h:79

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML