fmatvec 0.0.0

var_row_vector.h
1 /* Copyright (C) 2003-2012 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 var_row_vector_h
23 #define var_row_vector_h
24
25 #include "var_general_matrix.h"
26 #include <vector>
27 #include <cstring>
28
29 namespace fmatvec {
30
39 template <class AT> class RowVector<Var,AT> : public Matrix<General,Fixed<1>,Var,AT> {
40 using Matrix<General,Fixed<1>,Var,AT>::N;
41 using Matrix<General,Fixed<1>,Var,AT>::ele;
42 friend class Vector<Var,AT>;
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 protected:
55
56 template<class Row> inline RowVector<Var,AT>& copy(const RowVector<Row,AT> &x);
57
59
60 public:
61
66 explicit RowVector() : Matrix<General,Fixed<1>,Var,AT>() {
67 }
68
69 // move
70 RowVector(RowVector<Var,AT> &&src) noexcept : Matrix<General,Fixed<1>,Var,AT>(std::move(src)) {}
71 RowVector<Var,AT>& operator=(RowVector<Var,AT> &&src) noexcept {
72 FMATVEC_ASSERT(N == src.size(), AT);
73 src.N=0;
74 delete[]ele;
75 ele=src.ele;
76 src.ele=nullptr;
77 return *this;
78 }
79 RowVector(Transpose, Vector<Var, AT> &&src) : Matrix<General,Fixed<1>,Var,AT>() {
80 N=src.M;
81 src.M=0;
82 ele=src.ele;
83 src.ele=nullptr;
84 }
85
86 // template<class Ini=All<AT>>
87 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
88
89 explicit RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Var,AT>(n,ini) { }
90 explicit RowVector(int n, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Var,AT>(n,ini,a) { }
91
97 RowVector(const RowVector<Var,AT> &x) : Matrix<General,Fixed<1>,Var,AT>(x) {
98 }
99
105 template<class Row>
106 RowVector(const RowVector<Row,AT> &A) : Matrix<General,Var,Fixed<1>,AT>(A) {
107 }
108
114 template<class Type, class Row, class Col>
115 explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Var,AT>(A) {
116 }
117
130 RowVector(const char *str) : Matrix<General,Fixed<1>,Var,AT>(str) {
131 }
132
133 RowVector<Var,AT>& resize(int n, Noinit) {
134 Matrix<General,Fixed<1>,Var,AT>::resize(n,Noinit());
135 return *this;
136 }
137
138 RowVector<Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) {
139 Matrix<General,Fixed<1>,Var,AT>::resize(n,ini,a);
140 return *this;
141 }
142
149 inline RowVector<Var,AT>& operator=(const RowVector<Var,AT> &x) {
150 FMATVEC_ASSERT(N == x.size(), AT);
151 return copy(x);
152 }
153
160 template <class Row>
161 inline RowVector<Var,AT>& operator=(const RowVector<Row,AT> &x) {
162 FMATVEC_ASSERT(N == x.size(), AT);
163 return copy(x);
164 }
165
172 template <class Row>
173 inline RowVector<Var,AT>& operator<<=(const RowVector<Row,AT> &x) {
174 if(N!=x.size()) resize(x.size(),NONINIT);
175 return copy(x);
176 }
177 // move
178 inline RowVector<Var,AT>& operator<<=(RowVector<Var,AT> &&src) {
179 N=src.N;
180 src.N=0;
181 delete[]ele;
182 ele=src.ele;
183 src.ele=nullptr;
184 return *this;
185 }
186
195 AT& operator()(int i) {
196
197 FMATVEC_ASSERT(i>=0, AT);
198 FMATVEC_ASSERT(i<N, AT);
199
200 return e(i);
201 }
202
207 const AT& operator()(int i) const {
208
209 FMATVEC_ASSERT(i>=0, AT);
210 FMATVEC_ASSERT(i<N, AT);
211
212 return e(i);
213 }
214
215 iterator begin() { return &ele[0]; }
216 iterator end() { return &ele[N]; }
217 const_iterator begin() const { return &ele[0]; }
218 const_iterator end() const { return &ele[N]; }
219 const_iterator cbegin() const noexcept { return &ele[0]; }
220 const_iterator cend() const noexcept { return &ele[N]; }
221
222 AT& e(int i) {
223 return ele[i];
224 }
225
230 const AT& e(int i) const {
231 return ele[i];
232 }
233
241 inline RowVector<Var,AT>& init(const AT& val=AT());
242 inline RowVector<Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
243 inline RowVector<Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
244
249 constexpr int size() const {return N;}
250
257 int inc() const {return 1;}
258
259 using Matrix<General,Fixed<1>,Var,AT>::operator();
260
265 explicit inline operator std::vector<AT>() const;
266
271 explicit inline RowVector(const std::vector<AT> &v);
272
273 // /*! \brief Cast to AT.
274 // *
275 // * \return The AT representation of the vector
276 // * */
277 // explicit operator AT() const {
278 // FMATVEC_ASSERT(N==1, AT);
279 // return ele[0];
280 // }
281 //
282 // /*! \brief AT Constructor.
283 // * Constructs and initializes a vector with a AT object.
284 // * \param x The AT the vector will be initialized with.
285 // * */
286 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Var,AT>(x) { }
287
288 #if defined(SWIG) && SWIG_VERSION < 0x040000
289 inline const Vector<Var,AT> T() const;
290 inline Vector<Var,AT> T();
291 #else
292 inline const Vector<Var,AT> T() const &;
293 inline Vector<Var,AT> T() &;
294 #endif
295 // move
296 #ifndef SWIG
297 inline Vector<Var,AT> T() &&;
298 #endif
299
300 inline const RowVector<Var,AT> operator()(const Range<Var,Var> &I) const;
301
302 template <class Row> inline void set(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
303
304 template <class Row> inline void add(const Range<Var,Var> &I, const RowVector<Row,AT> &x);
305
306 inline const RowVector<Var,AT> operator()(const Indices &I) const;
307
308 template<class Row> inline void set(const Indices &I, const RowVector<Row,AT> &x);
309 };
310
311 template <class AT>
312 inline RowVector<Var,AT>& RowVector<Var,AT>::init(const AT &val) {
313 for(int i=0; i<N; i++)
314 e(i) = val;
315 return *this;
316 }
317
318 template <class AT>
319 #if defined(SWIG) && SWIG_VERSION < 0x040000
320 inline const Vector<Var,AT> RowVector<Var,AT>::T() const {
321 #else
322 inline const Vector<Var,AT> RowVector<Var,AT>::T() const & {
323 #endif
324 Vector<Var,AT> x(N,NONINIT);
325 for(int i=0; i<N; i++)
326 x.e(i) = e(i);
327 return x;
328 }
329 template <class AT>
330 #if defined(SWIG) && SWIG_VERSION < 0x040000
331 inline Vector<Var,AT> RowVector<Var,AT>::T() {
332 #else
333 inline Vector<Var,AT> RowVector<Var,AT>::T() & {
334 #endif
335 Vector<Var,AT> x(N,NONINIT);
336 for(int i=0; i<N; i++)
337 x.e(i) = e(i);
338 return x;
339 }
340 // move
341 #ifndef SWIG
342 template <class AT>
343 inline Vector<Var,AT> RowVector<Var,AT>::T() && {
344 return Vector<Var,AT>(Transpose(), std::move(*this));
345 }
346 #endif
347
348 template <class AT>
349 inline RowVector<Var,AT>::operator std::vector<AT>() const {
350 std::vector<AT> ret(size());
351 for(int i=0; i<size(); ++i)
352 ret[i] = e(i);
353 return ret;
354 }
355
356 template <class AT>
357 inline RowVector<Var,AT>::RowVector(const std::vector<AT> &v) : Matrix<General,Fixed<1>,Var,AT>(1,static_cast<int>(v.size()),NONINIT) {
358 for(int i=0; i<size(); ++i)
359 e(i) = v[i];
360 }
361
362 template <class AT>
363 inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Range<Var,Var> &I) const {
364 FMATVEC_ASSERT(I.end()<N, AT);
365 RowVector<Var,AT> x(I.size(),NONINIT);
366
367 for(int i=0; i<x.size(); i++)
368 x.e(i) = e(I.start()+i);
369
370 return x;
371 }
372
373 template <class AT> template <class Row>
374 inline void RowVector<Var,AT>::set(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
375 FMATVEC_ASSERT(I.end()<size(), AT);
376 FMATVEC_ASSERT(I.size()==x.size(), AT);
377
378 for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
379 e(i) = x.e(ii);
380 }
381
382 template <class AT> template <class Row>
383 inline void RowVector<Var,AT>::add(const Range<Var,Var> &I, const RowVector<Row,AT> &x) {
384 FMATVEC_ASSERT(I.end()<size(), AT);
385 FMATVEC_ASSERT(I.size()==x.size(), AT);
386
387 for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
388 e(i) += x.e(ii);
389 }
390
391 template <class AT>
392 inline const RowVector<Var,AT> RowVector<Var,AT>::operator()(const Indices &I) const {
393 FMATVEC_ASSERT(I.max()<size(), AT);
394
395 RowVector<Var,AT> x(I.size(),NONINIT);
396
397 for(int i=0; i<x.size(); i++)
398 x.e(i) = e(I[i]);
399
400 return x;
401 }
402
403 template <class AT> template <class Row>
404 inline void RowVector<Var,AT>::set(const Indices &I, const RowVector<Row,AT> &x) {
405 FMATVEC_ASSERT(I.max()<size(), AT);
406 FMATVEC_ASSERT(I.size()==x.size(), AT);
407 for(int i=0; i<I.size(); i++)
408 e(I[i]) = x.e(i);
409 }
410
411
413
414 template <class AT> template <class Row>
415 inline RowVector<Var,AT>& RowVector<Var,AT>::copy(const RowVector<Row,AT> &x) {
416 for(int i=0; i<N; i++)
417 e(i) = x.e(i);
418 return *this;
419 }
420
422
423 }
424
425 #endif
fmatvec::Range< Var, Var >::end
int end() const
Last element.
Definition: range.h:91
fmatvec::Fixed
Definition: fmatvec.h:39
fmatvec::RowVector< Var, AT >::e
const AT & e(int i) const
Element operator.
Definition: var_row_vector.h:230
fmatvec::RowVector< Var, AT >::operator()
const AT & operator()(int i) const
Element operator.
Definition: var_row_vector.h:207
fmatvec::RowVector< Var, AT >::RowVector
RowVector(const char *str)
String Constructor.
Definition: var_row_vector.h:130
fmatvec::Matrix
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
fmatvec::RowVector< Var, AT >::RowVector
RowVector()
Standard constructor.
Definition: var_row_vector.h:66
fmatvec::Vector< Var, AT >
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
fmatvec::RowVector< Var, AT >::operator()
AT & operator()(int i)
Element operator.
Definition: var_row_vector.h:195
fmatvec::Init
Definition: types.h:89
fmatvec::Var
Definition: types.h:101
fmatvec::RowVector< Var, AT >::T
const Vector< Var, AT > T() const &
Cast to AT.
Definition: var_row_vector.h:322
fmatvec::RowVector
Definition: fmatvec.h:51
fmatvec::RowVector< Var, AT >::size
constexpr int size() const
Size.
Definition: var_row_vector.h:249
fmatvec::RowVector< Var, AT >::RowVector
RowVector(const RowVector< Var, AT > &x)
Copy Constructor.
Definition: var_row_vector.h:97
fmatvec::Indices
Definition: indices.h:32
fmatvec::RowVector< Var, AT >::RowVector
RowVector(const RowVector< Row, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:106
fmatvec::RowVector< Var, AT >::inc
int inc() const
Increment.
Definition: var_row_vector.h:257
fmatvec::Range< Var, Var >::start
int start() const
First element.
Definition: range.h:85
fmatvec::Vector
Definition: fmatvec.h:48
fmatvec::RowVector< Var, AT >::operator=
RowVector< Var, AT > & operator=(const RowVector< Var, AT > &x)
Assignment operator.
Definition: var_row_vector.h:149
fmatvec
Namespace fmatvec.
Definition: _memory.cc:28
fmatvec::Range< Var, Var >::size
int size() const
Size.
Definition: range.h:97
fmatvec::Noinit
Definition: types.h:88
fmatvec::General
Shape class for general matrices.
Definition: types.h:112
fmatvec::RowVector< Var, AT >::operator=
RowVector< Var, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: var_row_vector.h:161
fmatvec::RowVector< Var, AT >::RowVector
RowVector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_row_vector.h:115
fmatvec::Range< Var, Var >
This is an index class for creating submatrices.
Definition: range.h:44
fmatvec::RowVector< Var, AT >
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39