fmatvec 0.0.0

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