fmatvec  2.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 
44  typedef AT* iterator;
45  typedef const AT* const_iterator;
46 
47  typedef AT value_type;
48 
50 
51  protected:
52 
53  template<class Col> inline RowVector<Fixed<N>,AT>& copy(const RowVector<Col,AT> &x);
54 
56 
57  public:
58 
59 // template<class Ini=All<AT>>
60 // RowVector(Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
61 // template<class Ini=All<AT>>
62 // RowVector(int n, Ini ini=All<AT>()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
63 
64  explicit RowVector(Noinit ini) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { }
65  explicit RowVector(Init ini=INIT, const AT &a=AT()) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini,a) { }
66  explicit RowVector(int n, Noinit ini) : Matrix<General,Fixed<1>,Fixed<N>,AT>(ini) { FMATVEC_ASSERT(n==N, AT); }
67  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); }
68 
73  RowVector(const RowVector<Fixed<N>,AT> &A) = default;
74 
79  template<class Row>
80  RowVector(const RowVector<Row,AT> &A) : Matrix<General,Fixed<1>,Fixed<N>,AT>(A) {
81  }
82 
87  template<class Type, class Row, class Col>
88  explicit RowVector(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<1>,Fixed<N>,AT>(A) {
89  }
90 
103  RowVector(const char *str) : Matrix<General,Fixed<1>,Fixed<N>,AT>(str) {
104  }
105 
112  inline RowVector<Fixed<N>,AT>& operator=(const RowVector<Fixed<N>,AT> &x) = default;
113 
120  template <class Row>
122  FMATVEC_ASSERT(x.size() == N, AT);
123  return copy(x);
124  }
125 
132  template <class Row>
133  inline RowVector<Fixed<N>,AT>& operator<<=(const RowVector<Row,AT> &x) { return operator=(x); }
134 
143  AT& operator()(int i) {
144  FMATVEC_ASSERT(i>=0, AT);
145  FMATVEC_ASSERT(i<N, AT);
146  return e(i);
147  }
148 
153  const AT& operator()(int i) const {
154  FMATVEC_ASSERT(i>=0, AT);
155  FMATVEC_ASSERT(i<N, AT);
156  return e(i);
157  }
158 
159  iterator begin() { return &ele[0][0]; }
160  iterator end() { return &ele[0][N]; }
161  const_iterator begin() const { return &ele[0][0]; }
162  const_iterator end() const { return &ele[0][N]; }
163  const_iterator cbegin() const noexcept { return &ele[0][0]; }
164  const_iterator cend() const noexcept { return &ele[0][N]; }
165 
166  AT& e(int i) {
167  return ele[0][i];
168  }
169 
174  const AT& e(int i) const {
175  return ele[0][i];
176  }
177 
185  inline RowVector<Fixed<N>,AT>& init(const AT &val=AT());
186  inline RowVector<Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
187  inline RowVector<Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
188 
193  int size() const {return N;}
194 
197  void resize(int n) {
198  if(n!=N)
199  throw std::runtime_error("A fixed row vector cannot be resized.");
200  }
201 
208  int inc() const {return 1;}
209 
210  using Matrix<General,Fixed<1>,Fixed<N>,AT>::operator();
211 
212 // /*! \brief Cast to AT.
213 // *
214 // * \return The AT representation of the vector
215 // * */
216 // explicit operator AT() const {
217 // FMATVEC_ASSERT(N==1, AT);
218 // return ele[0];
219 // }
220 //
221 // /*! \brief AT Constructor.
222 // * Constructs and initializes a vector with a AT object.
223 // * \param x The AT the vector will be initialized with.
224 // * */
225 // explicit RowVector(const AT &x) : Matrix<General,Fixed<1>,Fixed<N>,AT>(x) { }
226 
227  inline const Vector<Fixed<N>,AT> T() const;
228  };
229 
230  template <int N, class AT>
231  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::init(const AT &val) {
232  for(int i=0; i<N; i++)
233  e(i) = val;
234  return *this;
235  }
236 
237  template <int N, class AT>
238  inline const Vector<Fixed<N>,AT> RowVector<Fixed<N>,AT>::T() const {
239  Vector<Fixed<N>,AT> x(NONINIT);
240  for(int i=0; i<N; i++)
241  x.e(i) = e(i);
242  return x;
243  }
244 
246 
247  template <int N, class AT> template <class Col>
248  inline RowVector<Fixed<N>,AT>& RowVector<Fixed<N>,AT>::copy(const RowVector<Col,AT> &x) {
249  for(int i=0; i<N; i++)
250  e(i) = x.e(i);
251  return *this;
252  }
253 
255 
256 }
257 #endif
Definition: fmatvec.h:38
int size() const
Size.
Definition: fixed_row_vector.h:193
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:153
const AT & e(int i) const
Element operator.
Definition: fixed_row_vector.h:174
RowVector(const RowVector< Row, AT > &A)
Copy Constructor.
Definition: fixed_row_vector.h:80
Definition: fmatvec.h:47
RowVector(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_row_vector.h:88
int inc() const
Increment.
Definition: fixed_row_vector.h:208
Namespace fmatvec.
Definition: _memory.cc:28
void resize(int n)
Definition: fixed_row_vector.h:197
RowVector< Fixed< N >, AT > & operator=(const RowVector< Row, AT > &x)
Assignment operator.
Definition: fixed_row_vector.h:121
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
AT & operator()(int i)
Element operator.
Definition: fixed_row_vector.h:143
RowVector(const char *str)
String Constructor.
Definition: fixed_row_vector.h:103