fmatvec  0.0.0
fixed_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 fixed_vector_h
23#define fixed_vector_h
24
25#include "fixed_general_matrix.h"
26#include <vector>
27#include <cstring>
28#include <stdexcept>
29
30namespace fmatvec {
31
40 template <int M, class AT>
41 class Vector<Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<1>,AT> {
42 using Matrix<General,Fixed<M>,Fixed<1>,AT>::ele;
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<Fixed<M>,AT>& copy(const Vector<Row,AT> &x);
57
59
60 public:
61
62 explicit Vector(Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { }
63 explicit Vector(Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { }
64 explicit Vector(int m, Noinit ini) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini) { FMATVEC_ASSERT(m==M, AT); }
65 explicit Vector(int m, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<M>,Fixed<1>,AT>(ini,a) { FMATVEC_ASSERT(m==M, AT); }
66
74 Vector(const Vector<Fixed<M>,AT> &x) = default;
75
80 template<class Row>
81 Vector(const Vector<Row,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
82 }
83
88 template<class Type, class Row, class Col>
89 explicit Vector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<1>,AT>(A) {
90 }
91
104 Vector(const char *str) : Matrix<General,Fixed<M>,Fixed<1>,AT>(str) {
105 }
106
113 inline Vector<Fixed<M>,AT>& operator=(const Vector<Fixed<M>,AT> &x) = default;
114
121 template <class Row>
122 inline Vector<Fixed<M>,AT>& operator=(const Vector<Row,AT> &x) {
123 FMATVEC_ASSERT(x.size() == M, AT);
124 return copy(x);
125 }
126
133 template <class Row>
134 inline Vector<Fixed<M>,AT>& operator<<=(const Vector<Row,AT> &x) { return operator=(x); }
135
136 template <class AT2>
137 operator Vector<Fixed<M>,AT2>() const {
138 Vector<Fixed<M>,AT2> ret;
139 for(size_t i=0; i<M; ++i)
140 ret(i) = (*this)(i);
141 return ret;
142 }
143
152 AT& operator()(int i) {
153
154 FMATVEC_ASSERT(i>=0, AT);
155 FMATVEC_ASSERT(i<M, AT);
156
157 return e(i);
158 }
159
164 const AT& operator()(int i) const {
165
166 FMATVEC_ASSERT(i>=0, AT);
167 FMATVEC_ASSERT(i<M, AT);
168
169 return e(i);
170 }
171
172 iterator begin() { return &ele[0][0]; }
173 iterator end() { return &ele[M-1][1]; }
174 const_iterator begin() const { return &ele[0][0]; }
175 const_iterator end() const { return &ele[M-1][1]; }
176 const_iterator cbegin() const noexcept { return &ele[0][0]; }
177 const_iterator cend() const noexcept { return &ele[M-1][1]; }
178
179 AT& e(int i) {
180 return ele[i][0];
181 }
182
187 const AT& e(int i) const {
188 return ele[i][0];
189 }
190
198 inline Vector<Fixed<M>,AT>& init(const AT& val=AT());
199 inline Vector<Fixed<M>,AT>& init(Init, const AT& a=AT()) { return init(a); }
200 inline Vector<Fixed<M>,AT>& init(Noinit, const AT& a=AT()) { return *this; }
201
206 constexpr int size() const {return M;}
207
210 void resize(int m) {
211 if(m!=M)
212 throw std::runtime_error("A fixed vector cannot be resized.");
213 }
214
221 int inc() const {return 1;}
222
223 template <int M1, int M2>
224 inline const Vector<Fixed<M2-M1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I) const;
225
226 using Matrix<General,Fixed<M>,Fixed<1>,AT>::operator();
227
232 explicit inline operator std::vector<AT>() const;
233
238 explicit inline Vector(const std::vector<AT> &v);
239
240// /*! \brief Cast to AT.
241// *
242// * \return The AT representation of the vector
243// * */
244// explicit operator AT() const {
245// FMATVEC_ASSERT(M==1, AT);
246// return ele[0][0];
247// }
248//
249// /*! \brief AT Constructor.
250// * Constructs and initializes a vector with a AT object.
251// * \param x The AT the vector will be initialized with.
252// * */
253// explicit Vector(const AT &x) : Matrix<General,Fixed<M>,Fixed<1>,AT>(x) { }
254
258 inline const RowVector<Fixed<M>,AT> T() const;
259
263 template<class Type, class Row, class Col>
264 inline void set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A);
265 };
266
267 template <int M, class AT>
268 inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::init(const AT &val) {
269 for(int i=0; i<M; i++)
270 e(i) = val;
271 return *this;
272 }
273
274 template <int M, class AT> template <int M1, int M2>
275 inline const Vector<Fixed<M2-M1+1>,AT> Vector<Fixed<M>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2>> &I) const {
276 FMATVEC_ASSERT(M2<M, AT);
277 Vector<Fixed<M2-M1+1>,AT> x(NONINIT);
278
279 for(int i=0; i<x.size(); i++)
280 x.e(i) = e(M1+i);
281
282 return x;
283 }
284
285 template <int M, class AT>
286 inline const RowVector<Fixed<M>,AT> Vector<Fixed<M>,AT>::T() const {
287 RowVector<Fixed<M>,AT> x(NONINIT);
288 for(int i=0; i<M; i++)
289 x.e(i) = e(i);
290 return x;
291 }
292
293 template <int M, class AT>
294 inline Vector<Fixed<M>,AT>::operator std::vector<AT>() const {
295 std::vector<AT> ret(size());
296 for(int i=0; i<size(); ++i)
297 ret[i] = e(i);
298 return ret;
299 }
300
301 template <int M, class AT>
302 inline Vector<Fixed<M>,AT>::Vector(const std::vector<AT> &v) : Matrix<General,Fixed<M>,Fixed<1>,AT>(NONINIT) {
303 for(int i=0; i<size(); ++i)
304 e(i) = v[i];
305 }
306
308
309 template <int M, class AT> template<class Row>
310 inline Vector<Fixed<M>,AT>& Vector<Fixed<M>,AT>::copy(const Vector<Row,AT> &x) {
311 for(int i=0; i<M; i++)
312 e(i) = x.e(i);
313 return *this;
314 }
315
317
318 template<int M, class AT> template<class Type, class Row, class Col>
319 inline void Vector<Fixed<M>,AT>::set(const Range<Var,Var> &I, const Matrix<Type,Row,Col,AT> &A) {
321 }
322
323}
324
325#endif
Definition: types.h:108
Shape class for general matrices.
Definition: types.h:116
Definition: types.h:93
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:52
Definition: types.h:92
This is an index class for creating submatrices.
Definition: range.h:44
This is an index class for creating submatrices.
Definition: range.h:35
Definition: matrix.h:170
void resize(int m)
Definition: fixed_vector.h:210
constexpr int size() const
Size.
Definition: fixed_vector.h:206
const AT & operator()(int i) const
Element operator.
Definition: fixed_vector.h:164
Vector(const char *str)
String Constructor.
Definition: fixed_vector.h:104
const AT & e(int i) const
Element operator.
Definition: fixed_vector.h:187
Vector< Fixed< M >, AT > & operator=(const Vector< Fixed< M >, AT > &x)=default
Assignment operator.
AT & operator()(int i)
Element operator.
Definition: fixed_vector.h:152
Vector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:89
int inc() const
Increment.
Definition: fixed_vector.h:221
Vector(const Vector< Fixed< M >, AT > &x)=default
Copy Constructor.
Vector(const Vector< Row, AT > &A)
Copy Constructor.
Definition: fixed_vector.h:81
Definition: matrix.h:167
Namespace fmatvec.
Definition: _memory.cc:28