fmatvec 0.0.0

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> {
37 using Matrix<General,Ref,Ref,AT>::m;
38 using Matrix<General,Ref,Ref,AT>::n;
39 using Matrix<General,Ref,Ref,AT>::lda;
40 using Matrix<General,Ref,Ref,AT>::ele;
41 using Matrix<General,Ref,Ref,AT>::memory;
42 using Matrix<General,Ref,Ref,AT>::elePtr;
43
44 public:
45 static constexpr bool isVector {true};
46
47 using iterator = AT *;
48 using const_iterator = const AT *;
49
50 using value_type = AT;
51
53
54 friend class Vector<Ref,AT>;
55
56 protected:
57
58 template<class Col> inline RowVector<Ref,AT>& copy(const RowVector<Col,AT> &x);
59
60 const AT* elePtr(int i) const {
61 return ele+lda*i;
62 }
63
64 AT* elePtr(int i) {
65 return ele+lda*i;
66 }
67
69
70 public:
71
76 explicit RowVector() : Matrix<General,Ref,Ref,AT>() { m=1; }
77
78 explicit RowVector(int n, Noinit ini) : Matrix<General,Ref,Ref,AT>(1,n,ini) { }
79 explicit RowVector(int n, Init ini=INIT, const AT &a=AT()) : Matrix<General,Ref,Ref,AT>(1,n,ini,a) { }
80
86 RowVector(const RowVector<Ref,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
87 }
88
89 template<class Row>
90 RowVector(const RowVector<Row,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
91 }
92
93 template<class Type, class Row, class Col>
94 explicit RowVector(const Matrix<Type,Row,Col,AT> &x) : Matrix<General,Ref,Ref,AT>(x) {
95 FMATVEC_ASSERT(x.rows()==1, AT);
96 }
97
104 explicit RowVector(int n, AT* ele) : Matrix<General,Ref,Ref,AT>(1,n,ele) {
105 }
106
119 RowVector(const char *str) : Matrix<General,Ref,Ref,AT>(str) {
120 FMATVEC_ASSERT(m==1, AT);
121 }
122
123 RowVector<Ref,AT>& resize(int n, Noinit) {
124 Matrix<General,Ref,Ref,AT>::resize(1,n,Noinit());
125 return *this;
126 }
127
128 RowVector<Ref,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) {
129 Matrix<General,Ref,Ref,AT>::resize(1,n,ini,a);
130 return *this;
131 }
132
139 inline RowVector<Ref,AT>& operator=(const RowVector<Ref,AT> &x) {
140 FMATVEC_ASSERT(n == x.size(), AT);
141 return copy(x);
142 }
143
150 template <class Row>
151 inline RowVector<Ref,AT>& operator=(const RowVector<Row,AT> &x) {
152 FMATVEC_ASSERT(n == x.size(), AT);
153 return copy(x);
154 }
155
162 inline RowVector<Ref,AT>& operator&=(RowVector<Ref,AT> &x) {
163 m = 1;
164 n = x.n;
165 memory = x.memory;
166 ele = x.ele;
167 lda = x.lda;
168 return *this;
169 }
170
177 inline RowVector<Ref,AT>& operator&=(Matrix<General,Ref,Ref,AT> &A) {
178 FMATVEC_ASSERT(A.rows() == 1, AT);
179 m = 1;
180 n = A.cols();
181 memory = A.memory;
182 ele = A.ele;
183 lda = A.lda;
184 return *this;
185 }
186
193 template <class Row>
194 inline RowVector<Ref,AT>& operator<<=(const RowVector<Row,AT> &x) {
195 if(n!=x.size()) resize(x.size(),NONINIT);
196 return copy(x);
197 }
198
199 template <class AT2>
200 operator Vector<Ref,AT2>() const {
201 Vector<Ref,AT2> ret(size());
202 for(size_t i=0; i<size(); ++i)
203 ret(i) = (*this)(i);
204 return ret;
205 }
206
215 AT& operator()(int i) {
216 FMATVEC_ASSERT(i>=0, AT);
217 FMATVEC_ASSERT(i<n, AT);
218 return e(i);
219 }
220
225 const AT& operator()(int i) const {
226 FMATVEC_ASSERT(i>=0, AT);
227 FMATVEC_ASSERT(i<n, AT);
228 return e(i);
229 }
230
231 iterator begin() { FMATVEC_ASSERT(lda==1, AT); return &ele[0]; }
232 iterator end() { FMATVEC_ASSERT(lda==1, AT); return &ele[n*lda]; }
233 const_iterator begin() const { FMATVEC_ASSERT(lda==1, AT); return &ele[0]; }
234 const_iterator end() const { FMATVEC_ASSERT(lda==1, AT); return &ele[n*lda]; }
235 const_iterator cbegin() const noexcept { FMATVEC_ASSERT(lda==1, AT); return &ele[0]; }
236 const_iterator cend() const noexcept { FMATVEC_ASSERT(lda==1, AT); return &ele[n*lda]; }
237
238 AT& e(int i) {
239 return ele[i*lda];
240 }
241
242 const AT& e(int i) const {
243 return ele[i*lda];
244 }
245
253 inline RowVector<Ref,AT>& init(const AT& val);
254 inline RowVector<Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
255 inline RowVector<Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
256
261 int size() const {return n;}
262
269 int inc() const {return lda;}
270
273 inline const RowVector<Ref,AT> operator()(const Range<Var,Var> &I) const;
274
275 using Matrix<General,Ref,Ref,AT>::operator();
276
281 explicit inline operator std::vector<AT>() const;
282
287 explicit inline RowVector(const std::vector<AT> &v);
288
289 // /*! \brief Cast to AT.
290 // *
291 // * \return The AT representation of the vector
292 // * */
293 // explicit operator AT() const {
294 // FMATVEC_ASSERT(n==1, AT);
295 // return e(0);
296 // }
297 //
298 // /*! \brief AT Constructor.
299 // * Constructs and initializes a vector with a AT object.
300 // * \param x The AT the vector will be initialized with.
301 // * */
302 // explicit RowVector(const AT &x) : Matrix<General,Ref,Ref,AT>(x) { }
303
304 template<class Row> inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
305
306 template<class Row> inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
307
308 inline const RowVector<Ref,AT> operator()(const Indices &I) const;
309
310 template<class Row> inline void set(const Indices &I, const RowVector<Row,AT> &x);
311
312 inline void ref(RowVector<Ref,AT> &x, const Range<Var,Var> &I);
313
314 inline void ref(Matrix<General,Ref,Ref,AT> &A, int i);
315
316 inline void ref(Matrix<General,Ref,Ref,AT> &A, int i, const Range<Var,Var> &J);
317
318 const Vector<Ref,AT> T() const;
319 };
320
322
323 template <class AT>
324 inline RowVector<Ref,AT>& RowVector<Ref,AT>::init(const AT &val) {
325 for(int i=0; i<n; i++)
326 ele[i*lda] = val;
327 return *this;
328 }
329
330 template <class AT>
331 inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(const Range<Var,Var> &I) const {
332
333 FMATVEC_ASSERT(I.end()<n, AT);
334 RowVector<Ref,AT> x(I.size(),NONINIT);
335
336 for(int i=0; i<x.size(); i++)
337 x.e(i) = e(I.start()+i);
338
339 return x;
340 }
341
342 template <class AT>
343 inline RowVector<Ref,AT>::operator std::vector<AT>() const {
344 std::vector<AT> ret(size());
345 for(int i=0; i<size(); ++i)
346 ret[i] = e(i);
347 return ret;
348 }
349
350 template <class AT>
351 inline RowVector<Ref,AT>::RowVector(const std::vector<AT> &v) : Matrix<General,Ref,Ref,AT>(1,v.size(),NONINIT) {
352 for(int i=0; i<size(); ++i)
353 e(i) = v[i];
354 }
355
357
358 template <class AT> template <class Col>
359 inline RowVector<Ref,AT>& RowVector<Ref,AT>::copy(const RowVector<Col,AT> &x) {
360 for(int i=0; i<size(); i++)
361 e(i) = x.e(i);
362 return *this;
363 }
364
365 template <class AT> template <class Row>
366 inline void RowVector<Ref,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
367
368 FMATVEC_ASSERT(I.end()<size(), AT);
369 FMATVEC_ASSERT(I.size()==x.size(), AT);
370
371 for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
372 e(i) = x.e(ii);
373 }
374
375 template <class AT> template <class Row>
376
377 inline void RowVector<Ref,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
378 FMATVEC_ASSERT(I.end()<size(), AT);
379 FMATVEC_ASSERT(I.size()==x.size(), AT);
380
381 for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
382 e(i) += x.e(ii);
383 }
384
385 template <class AT>
386 inline const RowVector<Ref,AT> RowVector<Ref,AT>::operator()(const Indices &I) const {
387 FMATVEC_ASSERT(I.max()<size(), AT);
388
389 RowVector<Ref,AT> x(I.size(),NONINIT);
390
391 for(int i=0; i<x.size(); i++)
392 x.e(i) = e(I[i]);
393
394 return x;
395 }
396
397 template <class AT> template <class Row>
398 inline void RowVector<Ref,AT>::set(const Indices &I, const RowVector<Row,AT> &x) {
399 FMATVEC_ASSERT(I.max()<size(), AT);
400 FMATVEC_ASSERT(I.size()==x.size(), AT);
401 for(int i=0; i<I.size(); i++)
402 e(I[i]) = x.e(i);
403 }
404
405 template <class AT>
406 inline void RowVector<Ref,AT>::ref(RowVector<Ref,AT> &x, const Range<Var,Var> &I) {
407
408 FMATVEC_ASSERT(I.end()<x.size(), AT);
409
410 m=1;
411 n=I.size();
412 memory = x.memory;
413 ele = x.elePtr(I.start());
414 lda = x.lda;
415 }
416
417 template <class AT>
418 inline void RowVector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, int i) {
419
420 FMATVEC_ASSERT(i<A.rows(), AT);
421
422 m=1;
423 n=A.cols();
424 memory = A.memory;
425 ele = A.elePtr(i,0);
426 lda = A.lda;
427 }
428
429 template <class AT>
430 inline void RowVector<Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, int i, const Range<Var,Var> &J) {
431
432 FMATVEC_ASSERT(i<A.rows(), AT);
433 FMATVEC_ASSERT(J.end()<A.cols(), AT);
434
435 m=1;
436 n=J.size();
437 memory = A.memory;
438 ele = A.elePtr(i,J.start());
439 lda = A.lda;
440 }
441
442 template <class AT>
443 inline const Vector<Ref,AT> RowVector<Ref,AT>::T() const {
444 Vector<Ref,AT> x(n,NONINIT);
445 for(int i=0; i<n; i++)
446 x.e(i) = e(i);
447 return x;
448 }
449
451
452 }
453
454 #endif
fmatvec::Range< Var, Var >::end
int end() const
Last element.
Definition: range.h:91
fmatvec::RowVector< Ref, AT >::operator()
AT & operator()(int i)
Element operator.
Definition: row_vector.h:215
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::Matrix< General, Ref, Ref, AT >::rows
int rows() const
Number of rows.
Definition: general_matrix.h:307
fmatvec::RowVector< Ref, AT >::RowVector
RowVector(int n, AT *ele)
Regular Constructor.
Definition: row_vector.h:104
fmatvec::RowVector< Ref, AT >::operator=
RowVector< Ref, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: row_vector.h:151
fmatvec::RowVector
Definition: fmatvec.h:51
fmatvec::RowVector< Ref, AT >::set
void set(const Range< Var, Var > &I, const RowVector< Row, AT > &x)
Cast to AT.
fmatvec::RowVector< Ref, AT >::operator()
const AT & operator()(int i) const
Element operator.
Definition: row_vector.h:225
fmatvec::RowVector< Ref, AT >::size
int size() const
Size.
Definition: row_vector.h:261
fmatvec::Ref
Definition: types.h:98
fmatvec::Indices
Definition: indices.h:32
fmatvec::RowVector< Ref, AT >::inc
int inc() const
Increment.
Definition: row_vector.h:269
fmatvec::RowVector< Ref, AT >
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
fmatvec::Range< Var, Var >::start
int start() const
First element.
Definition: range.h:85
fmatvec::Vector
Definition: fmatvec.h:48
fmatvec::RowVector< Ref, AT >::RowVector
RowVector(const RowVector< Ref, AT > &x)
Copy Constructor.
Definition: row_vector.h:86
fmatvec::Matrix< General, Ref, Ref, AT >
This is a matrix class for general matrices.
Definition: general_matrix.h:42
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::Matrix< General, Ref, Ref, AT >::cols
int cols() const
Number of columns.
Definition: general_matrix.h:313
fmatvec::RowVector< Ref, AT >::RowVector
RowVector(const char *str)
String Constructor.
Definition: row_vector.h:119
fmatvec::Range< Var, Var >::size
int size() const
Size.
Definition: range.h:97
fmatvec::Matrix< General, Ref, Ref, AT >::operator()
AT * operator()()
Pointer operator.
Definition: general_matrix.h:295
fmatvec::Noinit
Definition: types.h:88
fmatvec::General
Shape class for general matrices.
Definition: types.h:112
fmatvec::Vector< Ref, AT >
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
fmatvec::RowVector< Ref, AT >::operator=
RowVector< Ref, AT > & operator=(const RowVector< Ref, AT > &x)
Assignment operator.
Definition: row_vector.h:139
fmatvec::RowVector< Ref, AT >::RowVector
RowVector()
Standard constructor.
Definition: row_vector.h:76
fmatvec::Range< Var, Var >
This is an index class for creating submatrices.
Definition: range.h:44
fmatvec::RowVector< Ref, AT >::operator&=
RowVector< Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: row_vector.h:177
fmatvec::RowVector< Ref, AT >::operator&=
RowVector< Ref, AT > & operator&=(RowVector< Ref, AT > &x)
Reference operator.
Definition: row_vector.h:162