fmatvec  0.0.0
fixed_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 fixed_row_vector_h
23 #define fixed_row_vector_h
24 
25 #include "fixed_general_matrix.h"
26 #include <stdexcept>
27 
28 namespace fmatvec {
29 
38  template <int N, class AT> class RowVector<Fixed<N>,AT> : public Matrix<General,Fixed<1>,Fixed<N>,AT> {
39 
40  using Matrix<General,Fixed<1>,Fixed<N>,AT>::ele;
41 
42  public:
43  static constexpr bool isVector {true};
44 
45  using iterator = AT *;
46  using const_iterator = const AT *;
47 
48  using value_type = AT;
49 
51 
52  protected:
53 
54  template<class Col> inline RowVector<Fixed<N>,AT>& copy(const RowVector<Col,AT> &x);
55 
57 
58  public:
59 
60 // template<class Ini=All<AT>>
61 // RowVector(Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
62 // template<class Ini=All<AT>>
63 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
64 
65  explicit RowVector(Noinit ini) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
66  explicit RowVector(Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini,a) { }
67  explicit RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { FMATVEC_ASSERT(n==N, AT); }
68  explicit RowVector(int n, Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini,a) { FMATVEC_ASSERT(n==N, AT); }
69 
74  RowVector(const RowVector<Fixed<N>,AT> &A) = default;
75 
80  template<class Row>
81  RowVector(const RowVector<Row,AT> &A) : Matrix<General,Fixed<1>,Fixed<N>,AT>(A) {
82  }
83 
88  template<class Type, class Row, class Col>
89  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Fixed<N>,AT>(A) {
90  }
91 
104  RowVector(const char *str) : Matrix<General,Fixed<1>,Fixed<N>,AT>(str) {
105  }
106 
113  inline RowVector<Fixed<N>,AT>& operator=(const RowVector<Fixed<N>,AT> &x) = default;
114 
121  template <class Row>
123  FMATVEC_ASSERT(x.size() == N, AT);
124  return copy(x);
125  }
126 
133  template <class Row>
134  inline RowVector<Fixed<N>,AT>& operator<<=(const RowVector<Row,AT> &x) { return operator=(x); }
135 
144  AT& operator()(int i) {
145  FMATVEC_ASSERT(i>=0, AT);
146  FMATVEC_ASSERT(i<N, AT);
147  return e(i);
148  }
149 
154  const AT& operator()(int i) const {
155  FMATVEC_ASSERT(i>=0, AT);
156  FMATVEC_ASSERT(i<N, AT);
157  return e(i);
158  }
159 
160  iterator begin() { return &ele[0][0]; }
161  iterator end() { return &ele[0][N]; }
162  const_iterator begin() const { return &ele[0][0]; }
163  const_iterator end() const { return &ele[0][N]; }
164  const_iterator cbegin() const noexcept { return &ele[0][0]; }
165  const_iterator cend() const noexcept { return &ele[0][N]; }
166 
167  AT& e(int i) {
168  return ele[0][i];
169  }
170 
175  const AT& e(int i) const {
176  return ele[0][i];
177  }
178 
186  inline RowVector<Fixed<N>,AT>& init(const AT &val=AT());
187  inline RowVector<Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
188  inline RowVector<Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
189 
194  constexpr int size() const {return N;}
195 
198  void resize(int n) {
199  if(n!=N)
200  throw std::runtime_error("A fixed row vector cannot be resized.");
201  }
202 
209  int inc() const {return 1;}
210 
211  using Matrix<General,Fixed<1>,Fixed<N>,AT>::operator();
212 
217  explicit inline operator std::vector<AT>() const;
218 
223  explicit inline RowVector(const std::vector<AT> &v);
224 
225 // /*! \brief Cast to AT.
226 // *
227 // * \return The AT representation of the vector
228 // * */
229 // explicit operator AT() const {
230 // FMATVEC_ASSERT(N==1, AT);
231 // return ele[0];
232 // }
233 //
234 // /*! \brief AT Constructor.
235 // * Constructs and initializes a vector with a AT object.
236 // * \param x The AT the vector will be initialized with.
237 // * */
238 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Fixed<N>,AT>(x) { }
239 
240  inline const Vector<Fixed<N>,AT> T() const;
241  };
242 
243  template <int N, class AT>
244  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::init(const AT &val) {
245  for(int i=0; i<N; i++)
246  e(i) = val;
247  return *this;
248  }
249 
250  template <int N, class AT>
251  inline const Vector<Fixed<N>,AT> RowVector<Fixed<N>,AT>::T() const {
252  Vector<Fixed<N>,AT> x(NONINIT);
253  for(int i=0; i<N; i++)
254  x.e(i) = e(i);
255  return x;
256  }
257 
258  template <int N, class AT>
259  inline RowVector<Fixed<N>,AT>::operator std::vector<AT>() const {
260  std::vector<AT> ret(size());
261  for(int i=0; i<size(); ++i)
262  ret[i] = e(i);
263  return ret;
264  }
265 
266  template <int N, class AT>
267  inline RowVector<Fixed<N>,AT>::RowVector(const std::vector<AT> &v) : Matrix<General,Fixed<1>,Fixed<N>,AT>(NONINIT) {
268  for(int i=0; i<size(); ++i)
269  e(i) = v[i];
270  }
271 
273 
274  template <int N, class AT> template <class Col>
275  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::copy(const RowVector<Col,AT> &x) {
276  for(int i=0; i<N; i++)
277  e(i) = x.e(i);
278  return *this;
279  }
280 
282 
283 }
284 #endif
Definition: fmatvec.h:39
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
Definition: types.h:89
Definition: fmatvec.h:51
const AT & operator()(int i) const
Element operator.
Definition: fixed_row_vector.h:154
const AT & e(int i) const
Element operator.
Definition: fixed_row_vector.h:175
RowVector(const RowVector< Row, AT > &A)
Copy Constructor.
Definition: fixed_row_vector.h:81
constexpr int size() const
Size.
Definition: fixed_row_vector.h:194
Definition: fmatvec.h:48
RowVector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_row_vector.h:89
int inc() const
Increment.
Definition: fixed_row_vector.h:209
Namespace fmatvec.
Definition: _memory.cc:28
void resize(int n)
Definition: fixed_row_vector.h:198
RowVector< Fixed< N >, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: fixed_row_vector.h:122
Definition: types.h:88
Shape class for general matrices.
Definition: types.h:112
AT & operator()(int i)
Element operator.
Definition: fixed_row_vector.h:144
RowVector(const char *str)
String Constructor.
Definition: fixed_row_vector.h:104