fmatvec  0.0.0
Fast Matrix/Vector Library
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  typedef AT* iterator;
46  typedef const AT* const_iterator;
47 
48  typedef AT value_type;
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 
213 // /*! \brief Cast to AT.
214 // *
215 // * \return The AT representation of the vector
216 // * */
217 // explicit operator AT() const {
218 // FMATVEC_ASSERT(N==1, AT);
219 // return ele[0];
220 // }
221 //
222 // /*! \brief AT Constructor.
223 // * Constructs and initializes a vector with a AT object.
224 // * \param x The AT the vector will be initialized with.
225 // * */
226 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Fixed<N>,AT>(x) { }
227 
228  inline const Vector<Fixed<N>,AT> T() const;
229  };
230 
231  template <int N, class AT>
232  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::init(const AT &val) {
233  for(int i=0; i<N; i++)
234  e(i) = val;
235  return *this;
236  }
237 
238  template <int N, class AT>
239  inline const Vector<Fixed<N>,AT> RowVector<Fixed<N>,AT>::T() const {
240  Vector<Fixed<N>,AT> x(NONINIT);
241  for(int i=0; i<N; i++)
242  x.e(i) = e(i);
243  return x;
244  }
245 
247 
248  template <int N, class AT> template <class Col>
249  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::copy(const RowVector<Col,AT> &x) {
250  for(int i=0; i<N; i++)
251  e(i) = x.e(i);
252  return *this;
253  }
254 
256 
257 }
258 #endif
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Definition: types.h:85
Definition: fmatvec.h:50
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:47
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:84
Basic shape class for matrices.
Definition: types.h:132
AT & operator()(int i)
Element operator.
Definition: fixed_row_vector.h:144
RowVector(const char *str)
String Constructor.
Definition: fixed_row_vector.h:104