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 : public Matrix<General, AT> {
44 
45  public:
46 
48 
49  template <class T> friend Vector<T> trans(const RowVector<T> &x);
50  template <class T> friend RowVector<T> trans(const Vector<T> &x);
51 
52  friend class Vector<AT>;
53 
55  friend const RowVector<AT> Matrix<General, AT>::row(int i) const;
56 
57  protected:
58 
59  void deepCopy(const RowVector<AT> &x);
60 
61  const AT* elePtr(int i) const {
62  return tp ? ele+i : ele+lda*i;
63  };
64 
65  AT* elePtr(int i) {
66  return tp ? ele+i : ele+lda*i;
67  };
68 
69  RowVector(int n_, int lda_, bool tp, Memory<AT> memory, const AT* ele_) : Matrix<General, AT>(1,n_, lda_, tp, memory, ele_) {
70  }
71 
73 
74  public:
75 
80  RowVector() : Matrix<General, AT>() {
81  }
82 
95  RowVector(const char *str) : Matrix<General, AT>(str) {
96 #ifdef FMATVEC_SIZE_CHECK
97  assert(m==1);
98 #endif
99  }
100 
109  RowVector(int n) : Matrix<General, AT>(1,n) {
110  }
111 
118  RowVector(int n, AT* ele) : Matrix<General, AT>(1,n,ele) {
119  }
120 
131  RowVector(int n, Initialization ini, const AT &a=0) : Matrix<General, AT>(1,n,ini,a) {
132  }
133 
138  explicit RowVector(const Matrix<General, AT> &x) : Matrix<General, AT>(x) {
139  LogicAssert(x.rows()==1,"Number of cols() must be 1 in RowVector<AT>::RowVector(const Matrix<General, AT> &x)");
140  }
141 
151  RowVector& resize(int n) {
153  return *this;
154  }
155 
167  RowVector& resize(int n, Initialization ini, const AT &a=0) {
168  Matrix<General, AT>::resize(1,n,ini,a);
169  return *this;
170  }
171 
179  RowVector(const RowVector<AT> &x) : Matrix<General, AT>(x) {
180  }
181 
188  RowVector<AT>& operator<<(const RowVector<AT> &x);
189 
197 
207 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
208  return operator<<(x);
209 #else
210  return operator>>(x);
211 #endif
212  }
213 
224  AT& operator()(int i) {
225 #ifndef FMATVEC_NO_BOUNDS_CHECK
226  assert(i>=0);
227  assert(i<n);
228 #endif
229  return tp ? ele[i] : ele[i*lda];
230  };
231 
236  const AT& operator()(int i) const {
237 #ifndef FMATVEC_NO_BOUNDS_CHECK
238  assert(i>=0);
239  assert(i<n);
240 #endif
241  return tp ? ele[i] : ele[i*lda];
242  };
243 
251  RowVector<AT>& init(const AT& a);
252 
257  int size() const {return n;};
258 
265  int inc() const {return tp?1:lda;};
266 
272  RowVector<AT> copy() const;
273 
283  RowVector<AT> operator()(int i1, int i2);
284 
289  const RowVector<AT> operator()(int i1, int i2) const;
290 
299  RowVector<AT> operator()(const Index &I);
300 
305  const RowVector<AT> operator()(const Index &I) const;
306 
309 
310  Vector<AT> T() {
311  return Vector<AT>(n,lda,tp?false:true,memory,ele);
312  }
313 
314  const Vector<AT> T() const {
315  return Vector<AT>(n,lda,tp?false:true,memory,ele);
316  }
317  };
318 
320 
321  template <class AT>
323 
324  if(tp) {
325  for(int i=0; i<n; i++)
326  ele[i] = val; // operator()(i) = val;
327  }
328  else {
329  for(int i=0; i<n; i++)
330  ele[i*lda] = val;
331  }
332 
333  return *this;
334  }
335 
336  template <class AT>
338 
339  if(x.size() == 0)
340  return *this;
341 
342  if(n==0) {
343  m = x.m;
344  n = x.n;
345  lda = m;
346  tp = false;
347  memory.resize(n);
348  ele = (AT*)memory.get();
349  } else {
350 #ifdef FMATVEC_SIZE_CHECK
351  assert(n == x.n);
352 #endif
353  }
354 
355  deepCopy(x);
356 
357  return *this;
358  }
359 
360  template <class AT>
362 
363  if(n==0) {
364  m = x.m;
365  n = x.n;
366  } else {
367 #ifdef FMATVEC_SIZE_CHECK
368  assert(n == x.n);
369 #endif
370  }
371 
372  memory = x.memory;
373  ele = x.ele;
374  lda = x.lda;
375  tp = x.tp;
376 
377  return *this;
378  }
379 
380  template <class AT>
382 
383  RowVector<AT> x(n,NONINIT);
384  x.deepCopy(*this);
385 
386  return x;
387  }
388 
389  template <class AT>
390  void RowVector<AT>::deepCopy(const RowVector<AT> &x) {
391  if(tp) {
392  if(x.tp) {
393  for(int i=0; i<size(); i++)
394  ele[i] = x.ele[i]; // operator()(i) = x(i);
395  }
396  else {
397  for(int i=0; i<size(); i++)
398  ele[i] = x.ele[i*x.lda];
399  }
400  }
401  else {
402  if(x.tp) {
403  for(int i=0; i<size(); i++)
404  ele[i*lda] = x.ele[i]; // operator()(i) = x(i);
405  }
406  else {
407  for(int i=0; i<size(); i++)
408  ele[i*lda] = x.ele[i*x.lda];
409  }
410  }
411  }
412 
413  template <> void RowVector<double>::deepCopy(const RowVector<double> &A);
414 
415  template <class AT> RowVector<AT> RowVector<AT>::operator()(int i1, int i2) {
416  return operator()(Index(i1,i2));
417  }
418 
419  template <class AT> const RowVector<AT> RowVector<AT>::operator()(int i1, int i2) const {
420  return operator()(Index(i1,i2));
421  }
422 
423  template <class AT>
425 
426 #ifndef FMATVEC_NO_BOUNDS_CHECK
427  assert(I.end()<n);
428 #endif
429 
430  return RowVector<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
431  }
432 
433  template <class AT>
435 
436 #ifndef FMATVEC_NO_BOUNDS_CHECK
437  assert(I.end()<n);
438 #endif
439 
440  return RowVector<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start()));
441  }
442 
443 
444 
445 }
446 #endif