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 AtomicType;
48 
50 
51  template <class T> friend Vector<Ref,T> trans(const RowVector<Ref,T> &x);
52  template <class T> friend RowVector<Ref,T> trans(const Vector<Ref,T> &x);
53  //friend RowVector<Ref,double> trans(const Vector<Ref,double> &x);
54 
55  friend class Vector<Ref,AT>;
56 
58  friend const RowVector<Ref,AT> Matrix<General,Ref,Ref,AT>::row(int i) const;
59 
60  protected:
61 
62  inline void deepCopy(const RowVector<Ref,AT> &x);
63  template<class Col> inline void deepCopy(const RowVector<Col,AT> &x);
64 
65  const AT* elePtr(int i) const {
66  return tp ? ele+i : ele+lda*i;
67  };
68 
69  AT* elePtr(int i) {
70  return tp ? ele+i : ele+lda*i;
71  };
72 
73  RowVector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General,Ref,Ref,AT>(1,n_, lda_, tp, memory, ele_) {
74  }
75 
77 
78  public:
79 
84  RowVector() : Matrix<General,Ref,Ref,AT>() { m=1; }
85 
86 // template<class Ini=All<AT> >
87 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
88 
89  RowVector(int n, Noinit ini) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
90  RowVector(int n, Init ini=INIT, const AT &a=0) : Matrix<General,Ref,Ref,AT>(1,n,ini,a) { }
91 
98  RowVector(int n, AT* ele) : Matrix<General,Ref,Ref,AT>(1,n,ele) {
99  }
100 
113  RowVector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
114 #ifndef FMATVEC_NO_SIZE_CHECK
115  assert(m==1);
116 #endif
117  }
118 
123  explicit RowVector(const Matrix<General,Ref,Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
124  LogicAssert(x.rows()==1,"Number of cols() must be 1 in RowVector<Ref,AT>::RowVector(const Matrix<General,Ref,Ref,AT> &x)");
125  }
126 
127  RowVector<Ref,AT>& resize() {
129  return *this;
130  }
131 
132  RowVector<Ref,AT>& resize(int n, Noinit) {
133  Matrix<General,Ref,Ref,AT>::resize(1,n,Noinit());
134  return *this;
135  }
136 
137  RowVector<Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) {
138  Matrix<General,Ref,Ref,AT>::resize(1,n,ini,a);
139  return *this;
140  }
141 
149  RowVector(const RowVector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
150  }
151 
158  inline RowVector<Ref,AT>& operator<<(const RowVector<Ref,AT> &x);
159 
167 
176  inline RowVector<Ref,AT>& operator=(const RowVector<Ref,AT> &x);
177 
178  template <class Row>
179  inline RowVector<Ref,AT>& operator=(const RowVector<Row,AT> &x);
180 
191  AT& operator()(int i) {
192 #ifndef FMATVEC_NO_BOUNDS_CHECK
193  assert(i>=0);
194  assert(i<n);
195 #endif
196  return e(i);
197  };
198 
203  const AT& operator()(int i) const {
204 #ifndef FMATVEC_NO_BOUNDS_CHECK
205  assert(i>=0);
206  assert(i<n);
207 #endif
208  return e(i);
209  };
210 
211  AT& er(int i) {
212  return ele[i*lda];
213  };
214 
215  const AT& er(int i) const {
216  return ele[i*lda];
217  };
218 
219  AT& et(int i) {
220  return ele[i];
221  };
222 
223  const AT& et(int i) const {
224  return ele[i];
225  };
226 
227  AT& e(int i) {
228  return tp ? et(i) : er(i);
229  };
230 
231  const AT& e(int i) const {
232  return tp ? et(i) : er(i);
233  };
234 
242  inline RowVector<Ref,AT>& init(const AT& a);
243  inline RowVector<Ref,AT>& init(Init, const AT &a=0) { return init(a); }
244  inline RowVector<Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
245 
250  int size() const {return n;};
251 
258  int inc() const {return tp?1:lda;};
259 
265  inline RowVector<Ref,AT> copy() const;
266 
276  inline RowVector<Ref,AT> operator()(int i1, int i2);
277 
282  inline const RowVector<Ref,AT> operator()(int i1, int i2) const;
283 
292  inline RowVector<Ref,AT> operator()(const Range<Var,Var> &I);
293 
298  inline const RowVector<Ref,AT> operator()(const Range<Var,Var> &I) const;
299 
301 
302  Vector<Ref,AT> T() {
303  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
304  }
305 
306  const Vector<Ref,AT> T() const {
307  return Vector<Ref,AT>(n,lda,tp?false:true,memory,ele);
308  }
309  };
310 
312 
313 
314  template <class AT>
316 
317  if(tp) {
318  for(int i=0; i<n; i++)
319  ele[i] = val;
320  }
321  else {
322  for(int i=0; i<n; i++)
323  ele[i*lda] = val;
324  }
325 
326  return *this;
327  }
328 
329  template <class AT>
331 
332  if(!ele) {
333  m = 1;
334  n = x.size();
335  lda = m;
336  tp = false;
337  memory.resize(m);
338  ele = (AT*)memory.get();
339  } else {
340 #ifndef FMATVEC_NO_SIZE_CHECK
341  assert(n == x.size());
342 #endif
343  }
344 
345  deepCopy(x);
346 
347  return *this;
348  }
349 
350  template <class AT> template<class Row>
352 
353  if(!ele) {
354  m = 1;
355  n = x.size();
356  lda = m;
357  tp = false;
358  memory.resize(m);
359  ele = (AT*)memory.get();
360  } else {
361 #ifndef FMATVEC_NO_SIZE_CHECK
362  assert(n == x.size());
363 #endif
364  }
365 
366  deepCopy(x);
367 
368  return *this;
369  }
370 
371  template <class AT>
373 
374  if(n!=x.size()) {
375  m = 1;
376  n = x.size();
377  lda = m;
378  tp = false;
379  memory.resize(n);
380  ele = (AT*)memory.get();
381  }
382 
383  deepCopy(x);
384 
385  return *this;
386  }
387 
388  template <class AT>
390 
391  m = 1;
392  n = x.n;
393  memory = x.memory;
394  ele = x.ele;
395  lda = x.lda;
396  tp = x.tp;
397 
398  return *this;
399  }
400 
401  template <class AT>
403 
404  RowVector<Ref,AT> x(n,NONINIT);
405  x.deepCopy(*this);
406 
407  return x;
408  }
409 
410  template <class AT>
412  return operator()(Range<Var,Var>(i1,i2));
413  }
414 
415  template <class AT>
416  inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(int i1, int i2) const {
417  return operator()(Range<Var,Var>(i1,i2));
418  }
419 
420  template <class AT>
422 
423 #ifndef FMATVEC_NO_BOUNDS_CHECK
424  assert(I.end()<n);
425 #endif
426 
427  return RowVector<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
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 
441 
442  template <class AT>
443  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Ref,AT> &x) {
444  if(tp) {
445  if(x.tp)
446  for(int i=0; i<size(); i++)
447  et(i) = x.et(i);
448  else
449  for(int i=0; i<size(); i++)
450  et(i) = x.er(i);
451  }
452  else {
453  if(x.tp)
454  for(int i=0; i<size(); i++)
455  er(i) = x.et(i);
456  else
457  for(int i=0; i<size(); i++)
458  er(i) = x.er(i);
459  }
460  }
461 
462  template <class AT> template <class Col>
463  inline void RowVector<Ref,AT>::deepCopy(const RowVector<Col,AT> &x) {
464  if(tp)
465  for(int i=0; i<size(); i++)
466  et(i) = x.e(i);
467  else
468  for(int i=0; i<size(); i++)
469  er(i) = x.e(i);
470  }
471 
473 
474 }
475 
476 #endif
const AT & operator()(int i) const
Element operator.
Definition: row_vector.h:203
AT & operator()(int i)
Element operator.
Definition: row_vector.h:191
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int size() const
Size.
Definition: row_vector.h:250
RowVector(int n, AT *ele)
Regular Constructor.
Definition: row_vector.h:98
int rows() const
Number of rows.
Definition: general_matrix.h:272
Definition: fmatvec.h:50
Definition: types.h:62
int inc() const
Increment.
Definition: row_vector.h:258
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:149
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:171
RowVector(const char *str)
String Constructor.
Definition: row_vector.h:113
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
int end() const
Last element.
Definition: range.h:95
RowVector(const Matrix< General, Ref, Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:123
RowVector()
Standard constructor.
Definition: row_vector.h:84
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 Valid HTML