35 #ifndef GMM_VECTOR_TO_MATRIX_H__
36 #define GMM_VECTOR_TO_MATRIX_H__
46 template <
typename PT>
struct gen_row_vector {
47 typedef gen_row_vector<PT> this_type;
48 typedef typename std::iterator_traits<PT>::value_type V;
50 typedef typename std::iterator_traits<PT>::reference ref_V;
51 typedef typename linalg_traits<this_type>::reference reference;
53 simple_vector_ref<PT> vec;
58 size_type ncols()
const {
return vect_size(vec); }
60 gen_row_vector(ref_V v) : vec(v) {}
62 gen_row_vector(
const gen_row_vector<CPT> &cr) : vec(cr.vec) {}
65 template <
typename PT>
66 struct gen_row_vector_iterator {
67 typedef gen_row_vector<PT> this_type;
68 typedef typename modifiable_pointer<PT>::pointer MPT;
69 typedef typename std::iterator_traits<PT>::value_type V;
70 typedef simple_vector_ref<PT> value_type;
71 typedef const simple_vector_ref<PT> *pointer;
72 typedef const simple_vector_ref<PT> &reference;
73 typedef ptrdiff_t difference_type;
75 typedef std::random_access_iterator_tag iterator_category;
76 typedef gen_row_vector_iterator<PT> iterator;
78 simple_vector_ref<PT> vec;
81 iterator &operator ++() { isend =
true;
return *
this; }
82 iterator &operator --() { isend =
false;
return *
this; }
83 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
84 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
85 iterator &operator +=(difference_type i)
86 {
if (i) isend =
false;
return *
this; }
87 iterator &operator -=(difference_type i)
88 {
if (i) isend =
true;
return *
this; }
90 { iterator itt = *
this;
return (itt += i); }
92 { iterator itt = *
this;
return (itt -= i); }
93 difference_type
operator -(
const iterator &i)
const {
94 return (isend ==
true) ? ((i.isend ==
true) ? 0 : 1)
95 : ((i.isend ==
true) ? -1 : 0);
98 const simple_vector_ref<PT>& operator *()
const {
return vec; }
99 const simple_vector_ref<PT>& operator [](
int i) {
return vec; }
101 bool operator ==(
const iterator &i)
const {
return (isend == i.isend); }
102 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
103 bool operator < (
const iterator &i)
const {
return (*
this - i < 0); }
104 bool operator > (
const iterator &i)
const {
return (*
this - i > 0); }
105 bool operator >=(
const iterator &i)
const {
return (*
this - i >= 0); }
107 gen_row_vector_iterator() {}
108 gen_row_vector_iterator(
const gen_row_vector_iterator<MPT> &itm)
109 : vec(itm.vec), isend(itm.isend) {}
110 gen_row_vector_iterator(
const gen_row_vector<PT> &m,
bool iis_end)
111 : vec(m.vec), isend(iis_end) { }
115 template <
typename PT>
116 struct linalg_traits<gen_row_vector<PT> > {
117 typedef gen_row_vector<PT> this_type;
118 typedef typename std::iterator_traits<PT>::value_type V;
119 typedef typename which_reference<PT>::is_reference is_reference;
120 typedef abstract_matrix linalg_type;
121 typedef typename linalg_traits<V>::origin_type origin_type;
122 typedef typename select_ref<
const origin_type *, origin_type *,
123 PT>::ref_type porigin_type;
124 typedef typename linalg_traits<V>::value_type value_type;
125 typedef typename select_ref<value_type,
126 typename linalg_traits<V>::reference, PT>::ref_type reference;
127 typedef abstract_null_type sub_col_type;
128 typedef abstract_null_type col_iterator;
129 typedef abstract_null_type const_sub_col_type;
130 typedef abstract_null_type const_col_iterator;
131 typedef simple_vector_ref<const V *> const_sub_row_type;
132 typedef typename select_ref<abstract_null_type,
133 simple_vector_ref<V *>, PT>::ref_type sub_row_type;
134 typedef gen_row_vector_iterator<typename const_pointer<PT>::pointer>
136 typedef typename select_ref<abstract_null_type,
137 gen_row_vector_iterator<PT>, PT>::ref_type row_iterator;
138 typedef typename linalg_traits<V>::storage_type storage_type;
139 typedef row_major sub_orientation;
140 typedef typename linalg_traits<V>::index_sorted index_sorted;
141 static size_type nrows(
const this_type &) {
return 1; }
142 static size_type ncols(
const this_type &m) {
return m.ncols(); }
143 static const_sub_row_type row(
const const_row_iterator &it) {
return *it; }
144 static sub_row_type row(
const row_iterator &it) {
return *it; }
145 static const_row_iterator row_begin(
const this_type &m)
146 {
return const_row_iterator(m,
false); }
147 static row_iterator row_begin(this_type &m)
148 {
return row_iterator(m,
false); }
149 static const_row_iterator row_end(
const this_type &m)
150 {
return const_row_iterator(m,
true); }
151 static row_iterator row_end(this_type &m)
152 {
return row_iterator(m,
true); }
153 static origin_type* origin(this_type &m) {
return m.vec.origin; }
154 static const origin_type* origin(
const this_type &m)
155 {
return m.vec.origin; }
156 static void do_clear(this_type &m)
157 {
clear(row(mat_row_begin(m))); }
158 static value_type access(
const const_row_iterator &itrow,
size_type i)
159 {
return itrow.vec[i]; }
160 static reference access(
const row_iterator &itrow,
size_type i)
161 {
return itrow.vec[i]; }
164 template <
typename PT>
165 std::ostream &operator <<(std::ostream &o,
const gen_row_vector<PT>& m)
166 { gmm::write(o,m);
return o; }
172 template <
typename PT>
struct gen_col_vector {
173 typedef gen_col_vector<PT> this_type;
174 typedef typename std::iterator_traits<PT>::value_type V;
176 typedef typename std::iterator_traits<PT>::reference ref_V;
177 typedef typename linalg_traits<this_type>::reference reference;
179 simple_vector_ref<PT> vec;
184 size_type nrows()
const {
return vect_size(vec); }
186 gen_col_vector(ref_V v) : vec(v) {}
188 gen_col_vector(
const gen_col_vector<CPT> &cr) : vec(cr.vec) {}
191 template <
typename PT>
192 struct gen_col_vector_iterator {
193 typedef gen_col_vector<PT> this_type;
194 typedef typename modifiable_pointer<PT>::pointer MPT;
195 typedef typename std::iterator_traits<PT>::value_type V;
196 typedef simple_vector_ref<PT> value_type;
197 typedef const simple_vector_ref<PT> *pointer;
198 typedef const simple_vector_ref<PT> &reference;
199 typedef ptrdiff_t difference_type;
201 typedef std::random_access_iterator_tag iterator_category;
202 typedef gen_col_vector_iterator<PT> iterator;
204 simple_vector_ref<PT> vec;
207 iterator &operator ++() { isend =
true;
return *
this; }
208 iterator &operator --() { isend =
false;
return *
this; }
209 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
210 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
211 iterator &operator +=(difference_type i)
212 {
if (i) isend =
false;
return *
this; }
213 iterator &operator -=(difference_type i)
214 {
if (i) isend =
true;
return *
this; }
216 { iterator itt = *
this;
return (itt += i); }
218 { iterator itt = *
this;
return (itt -= i); }
219 difference_type
operator -(
const iterator &i)
const {
220 return (isend ==
true) ? ((i.isend ==
true) ? 0 : 1)
221 : ((i.isend ==
true) ? -1 : 0);
224 const simple_vector_ref<PT>& operator *()
const {
return vec; }
225 const simple_vector_ref<PT>& operator [](
int i) {
return vec; }
227 bool operator ==(
const iterator &i)
const {
return (isend == i.isend); }
228 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
229 bool operator < (
const iterator &i)
const {
return (*
this - i < 0); }
230 bool operator > (
const iterator &i)
const {
return (*
this - i > 0); }
231 bool operator >=(
const iterator &i)
const {
return (*
this - i >= 0); }
233 gen_col_vector_iterator() {}
234 gen_col_vector_iterator(
const gen_col_vector_iterator<MPT> &itm)
235 : vec(itm.vec), isend(itm.isend) {}
236 gen_col_vector_iterator(
const gen_col_vector<PT> &m,
bool iis_end)
237 : vec(m.vec), isend(iis_end) { }
241 template <
typename PT>
242 struct linalg_traits<gen_col_vector<PT> > {
243 typedef gen_col_vector<PT> this_type;
244 typedef typename std::iterator_traits<PT>::value_type V;
245 typedef typename which_reference<PT>::is_reference is_reference;
246 typedef abstract_matrix linalg_type;
247 typedef typename linalg_traits<V>::origin_type origin_type;
248 typedef typename select_ref<
const origin_type *, origin_type *,
249 PT>::ref_type porigin_type;
250 typedef typename linalg_traits<V>::value_type value_type;
251 typedef typename select_ref<value_type,
252 typename linalg_traits<V>::reference, PT>::ref_type reference;
253 typedef abstract_null_type sub_row_type;
254 typedef abstract_null_type row_iterator;
255 typedef abstract_null_type const_sub_row_type;
256 typedef abstract_null_type const_row_iterator;
257 typedef simple_vector_ref<const V *> const_sub_col_type;
258 typedef typename select_ref<abstract_null_type,
259 simple_vector_ref<V *>, PT>::ref_type sub_col_type;
260 typedef gen_col_vector_iterator<typename const_pointer<PT>::pointer>
262 typedef typename select_ref<abstract_null_type,
263 gen_col_vector_iterator<PT>, PT>::ref_type col_iterator;
264 typedef typename linalg_traits<V>::storage_type storage_type;
265 typedef col_major sub_orientation;
266 typedef typename linalg_traits<V>::index_sorted index_sorted;
267 static size_type ncols(
const this_type &) {
return 1; }
268 static size_type nrows(
const this_type &m) {
return m.nrows(); }
269 static const_sub_col_type col(
const const_col_iterator &it) {
return *it; }
270 static sub_col_type col(
const col_iterator &it) {
return *it; }
271 static const_col_iterator col_begin(
const this_type &m)
272 {
return const_col_iterator(m,
false); }
273 static col_iterator col_begin(this_type &m)
274 {
return col_iterator(m,
false); }
275 static const_col_iterator col_end(
const this_type &m)
276 {
return const_col_iterator(m,
true); }
277 static col_iterator col_end(this_type &m)
278 {
return col_iterator(m,
true); }
279 static origin_type* origin(this_type &m) {
return m.vec.origin; }
280 static const origin_type* origin(
const this_type &m)
281 {
return m.vec.origin; }
282 static void do_clear(this_type &m)
283 {
clear(col(mat_col_begin(m))); }
284 static value_type access(
const const_col_iterator &itcol,
size_type i)
285 {
return itcol.vec[i]; }
286 static reference access(
const col_iterator &itcol,
size_type i)
287 {
return itcol.vec[i]; }
290 template <
typename PT>
291 std::ostream &operator <<(std::ostream &o,
const gen_col_vector<PT>& m)
292 { gmm::write(o,m);
return o; }
299 template <
class V>
inline
300 typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
301 const V *>::return_type
302 row_vector(
const V& v) {
303 return typename select_return< gen_row_vector<const V *>,
304 gen_row_vector<V *>,
const V *>::return_type(linalg_cast(v));
307 template <
class V>
inline
308 typename select_return< gen_row_vector<const V *>, gen_row_vector<V *>,
311 return typename select_return< gen_row_vector<const V *>,
312 gen_row_vector<V *>, V *>::return_type(linalg_cast(v));
315 template <
class V>
inline gen_row_vector<const V *>
316 const_row_vector(V& v)
317 {
return gen_row_vector<const V *>(v); }
320 template <
class V>
inline
321 typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
322 const V *>::return_type
323 col_vector(
const V& v) {
324 return typename select_return< gen_col_vector<const V *>,
325 gen_col_vector<V *>,
const V *>::return_type(linalg_cast(v));
328 template <
class V>
inline
329 typename select_return< gen_col_vector<const V *>, gen_col_vector<V *>,
332 return typename select_return< gen_col_vector<const V *>,
333 gen_col_vector<V *>, V *>::return_type(linalg_cast(v));
336 template <
class V>
inline gen_col_vector<const V *>
337 const_col_vector(V& v)
338 {
return gen_col_vector<const V *>(v); }
void clear(L &l)
clear (fill with zeros) a vector or matrix.
gmm interface for STL vectors.
rational_fraction< T > operator-(const polynomial< T > &P, const rational_fraction< T > &Q)
Subtract Q from P.
rational_fraction< T > operator+(const polynomial< T > &P, const rational_fraction< T > &Q)
Add Q to P.
size_t size_type
used as the common size type in the library