38 #ifndef GMM_INTERFACE_H__
39 #define GMM_INTERFACE_H__
85 template <
typename PT>
struct simple_vector_ref {
86 typedef simple_vector_ref<PT> this_type;
87 typedef typename std::iterator_traits<PT>::value_type V;
89 typedef typename std::iterator_traits<PT>::reference ref_V;
90 typedef typename linalg_traits<this_type>::iterator iterator;
91 typedef typename linalg_traits<this_type>::reference reference;
92 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
94 iterator begin_, end_;
98 simple_vector_ref(ref_V v) : begin_(vect_begin(const_cast<V&>(v))),
99 end_(vect_end(const_cast<V&>(v))),
100 origin(linalg_origin(const_cast<V&>(v))),
101 size_(vect_size(v)) {}
103 simple_vector_ref(
const simple_vector_ref<CPT> &cr)
104 : begin_(cr.begin_),end_(cr.end_),origin(cr.origin),size_(cr.size_) {}
106 simple_vector_ref() {}
109 {
return linalg_traits<V>::access(origin, begin_, end_, i); }
112 template <
typename IT,
typename ORG,
typename PT>
inline
113 void set_to_begin(IT &it, ORG o, simple_vector_ref<PT> *,linalg_modifiable) {
114 typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
115 set_to_begin(it, o, PT(), ref_t());
118 template <
typename IT,
typename ORG,
typename PT>
inline
119 void set_to_begin(IT &it, ORG o,
const simple_vector_ref<PT> *,
121 typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
122 set_to_begin(it, o, PT(), ref_t());
125 template <
typename IT,
typename ORG,
typename PT>
inline
126 void set_to_end(IT &it, ORG o, simple_vector_ref<PT> *, linalg_modifiable) {
127 typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
128 set_to_end(it, o, PT(), ref_t());
131 template <
typename IT,
typename ORG,
typename PT>
inline
132 void set_to_end(IT &it, ORG o,
const simple_vector_ref<PT> *,
134 typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
135 set_to_end(it, o, PT(), ref_t());
139 template <
typename PT>
struct linalg_traits<simple_vector_ref<PT> > {
140 typedef simple_vector_ref<PT> this_type;
141 typedef this_type *pthis_type;
142 typedef typename std::iterator_traits<PT>::value_type V;
143 typedef typename linalg_traits<V>::origin_type origin_type;
145 typedef typename linalg_traits<V>::is_reference V_reference;
146 typedef typename which_reference<PT>::is_reference is_reference;
147 typedef abstract_vector linalg_type;
148 typedef typename linalg_traits<V>::value_type value_type;
149 typedef typename select_ref<value_type,
typename
150 linalg_traits<V>::reference, PT>::ref_type reference;
151 typedef typename select_ref<
const origin_type *, origin_type *,
152 PT>::ref_type porigin_type;
153 typedef typename select_ref<typename linalg_traits<V>::const_iterator,
154 typename linalg_traits<V>::iterator, PT>::ref_type iterator;
155 typedef typename linalg_traits<V>::const_iterator const_iterator;
156 typedef typename linalg_traits<V>::storage_type storage_type;
157 typedef linalg_true index_sorted;
158 static size_type size(
const this_type &v) {
return v.size_; }
159 static inline iterator begin(this_type &v) {
160 iterator it = v.begin_;
161 set_to_begin(it, v.origin, pthis_type(), is_reference());
164 static inline const_iterator begin(
const this_type &v) {
165 const_iterator it = v.begin_;
166 set_to_begin(it, v.origin, pthis_type(), is_reference());
169 static inline iterator end(this_type &v) {
170 iterator it = v.end_;
171 set_to_end(it, v.origin, pthis_type(), is_reference());
174 static inline const_iterator end(
const this_type &v) {
175 const_iterator it = v.end_;
176 set_to_end(it, v.origin, pthis_type(), is_reference());
179 static origin_type* origin(this_type &v) {
return v.origin; }
180 static const origin_type* origin(
const this_type &v) {
return v.origin; }
181 static void clear(origin_type* o,
const iterator &it,
const iterator &ite)
182 { linalg_traits<V>::clear(o, it, ite); }
183 static void do_clear(this_type &v) {
clear(v.origin, v.begin_, v.end_); }
184 static value_type access(
const origin_type *o,
const const_iterator &it,
186 {
return linalg_traits<V>::access(o, it, ite, i); }
187 static reference access(origin_type *o,
const iterator &it,
189 {
return linalg_traits<V>::access(o, it, ite, i); }
192 template <
typename PT>
193 std::ostream &operator << (std::ostream &o,
const simple_vector_ref<PT>& v)
194 { gmm::write(o,v);
return o; }
196 template <
typename T,
typename alloc>
197 simple_vector_ref<const std::vector<T,alloc> *>
198 vref(
const std::vector<T, alloc> &vv)
199 {
return simple_vector_ref<const std::vector<T,alloc> *>(vv); }
208 template <
typename T,
typename alloc>
209 struct linalg_traits<std::vector<T, alloc> > {
210 typedef std::vector<T, alloc> this_type;
211 typedef this_type origin_type;
212 typedef linalg_false is_reference;
213 typedef abstract_vector linalg_type;
214 typedef T value_type;
215 typedef T& reference;
216 typedef typename this_type::iterator iterator;
217 typedef typename this_type::const_iterator const_iterator;
218 typedef abstract_dense storage_type;
219 typedef linalg_true index_sorted;
220 static size_type size(
const this_type &v) {
return v.size(); }
221 static iterator begin(this_type &v) {
return v.begin(); }
222 static const_iterator begin(
const this_type &v) {
return v.begin(); }
223 static iterator end(this_type &v) {
return v.end(); }
224 static const_iterator end(
const this_type &v) {
return v.end(); }
225 static origin_type* origin(this_type &v) {
return &v; }
226 static const origin_type* origin(
const this_type &v) {
return &v; }
227 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
228 { std::fill(it, ite, value_type(0)); }
229 static void do_clear(this_type &v) { std::fill(v.begin(), v.end(), T(0)); }
230 static value_type access(
const origin_type *,
const const_iterator &it,
233 static reference access(origin_type *,
const iterator &it,
241 template <
typename T>
242 inline size_type nnz(
const std::vector<T>& l) {
return l.size(); }
250 template <
typename IT,
typename V>
251 struct tab_ref_with_origin :
public gmm::tab_ref<IT> {
252 typedef tab_ref_with_origin<IT, V> this_type;
255 typedef typename linalg_traits<V>::origin_type origin_type;
256 typedef typename std::iterator_traits<IT>::pointer PT;
257 typedef typename select_ref<
const origin_type *, origin_type *,
258 PT>::ref_type porigin_type;
263 tab_ref_with_origin() {}
264 template <
class PT> tab_ref_with_origin(
const IT &b,
const IT &e, PT p)
265 : gmm::tab_ref<IT>(b,e), origin(porigin_type(p)) {}
266 tab_ref_with_origin(
const IT &b,
const IT &e, porigin_type p)
267 : gmm::tab_ref<IT>(b,e), origin(p) {}
269 tab_ref_with_origin(
const V &v,
const sub_interval &si)
270 : gmm::tab_ref<IT>(vect_begin(const_cast<V&>(v))+si.min,
271 vect_begin(const_cast<V&>(v))+si.max),
272 origin(linalg_origin(const_cast<V&>(v))) {}
273 tab_ref_with_origin(V &v,
const sub_interval &si)
274 : gmm::tab_ref<IT>(vect_begin(const_cast<V&>(v))+si.min,
275 vect_begin(const_cast<V&>(v))+si.max),
276 origin(linalg_origin(const_cast<V&>(v))) {}
279 template <
typename IT,
typename V>
280 struct linalg_traits<tab_ref_with_origin<IT, V> > {
281 typedef typename std::iterator_traits<IT>::pointer PT;
282 typedef typename linalg_traits<V>::origin_type origin_type;
283 typedef tab_ref_with_origin<IT, V> this_type;
284 typedef typename which_reference<PT>::is_reference is_reference;
285 typedef abstract_vector linalg_type;
286 typedef typename select_ref<
const origin_type *, origin_type *,
287 PT>::ref_type porigin_type;
288 typedef typename std::iterator_traits<IT>::value_type value_type;
289 typedef typename std::iterator_traits<IT>::reference reference;
290 typedef typename this_type::iterator iterator;
291 typedef typename this_type::iterator const_iterator;
292 typedef abstract_dense storage_type;
293 typedef linalg_true index_sorted;
294 static size_type size(
const this_type &v) {
return v.size(); }
295 static iterator begin(this_type &v) {
return v.begin(); }
296 static const_iterator begin(
const this_type &v) {
return v.begin(); }
297 static iterator end(this_type &v) {
return v.end(); }
298 static const_iterator end(
const this_type &v) {
return v.end(); }
299 static origin_type* origin(this_type &v) {
return v.origin; }
300 static const origin_type* origin(
const this_type &v) {
return v.origin; }
301 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
302 { std::fill(it, ite, value_type(0)); }
303 static inline void do_clear(this_type &v)
304 { std::fill(v.begin(), v.end(), value_type(0)); }
305 static value_type access(
const origin_type *,
const const_iterator &it,
308 static reference access(origin_type *,
const iterator &it,
313 template <
typename IT,
typename V> std::ostream &
operator <<
314 (std::ostream &o,
const tab_ref_with_origin<IT, V>& m)
315 { gmm::write(o,m);
return o; }
318 template <
typename IT,
typename V>
320 typedef tab_ref_reg_spaced_with_origin<IT, V> this_type;
321 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
325 tab_ref_reg_spaced_with_origin() {}
327 const porigin_type p)
328 : gmm::tab_ref_reg_spaced<IT>(b,n,s), origin(p) {}
329 tab_ref_reg_spaced_with_origin(
const V &v,
const sub_slice &si)
330 : gmm::tab_ref_reg_spaced<IT>(vect_begin(const_cast<V&>(v)) + si.min,
331 si.N, (si.max - si.min)/si.N),
332 origin(linalg_origin(const_cast<V&>(v))) {}
333 tab_ref_reg_spaced_with_origin(V &v,
const sub_slice &si)
334 : gmm::tab_ref_reg_spaced<IT>(vect_begin(const_cast<V&>(v)) + si.min,
335 si.N, (si.max - si.min)/si.N),
336 origin(linalg_origin(const_cast<V&>(v))) {}
339 template <
typename IT,
typename V>
340 struct linalg_traits<tab_ref_reg_spaced_with_origin<IT, V> > {
341 typedef typename std::iterator_traits<IT>::pointer PT;
342 typedef tab_ref_reg_spaced_with_origin<IT, V> this_type;
343 typedef typename linalg_traits<V>::origin_type origin_type;
344 typedef typename select_ref<
const origin_type *, origin_type *,
345 PT>::ref_type porigin_type;
346 typedef typename which_reference<PT>::is_reference is_reference;
347 typedef abstract_vector linalg_type;
348 typedef typename std::iterator_traits<IT>::value_type value_type;
349 typedef typename std::iterator_traits<IT>::reference reference;
350 typedef typename this_type::iterator iterator;
351 typedef typename this_type::iterator const_iterator;
352 typedef abstract_dense storage_type;
353 typedef linalg_true index_sorted;
354 static size_type size(
const this_type &v) {
return v.size(); }
355 static iterator begin(this_type &v) {
return v.begin(); }
356 static const_iterator begin(
const this_type &v) {
return v.begin(); }
357 static iterator end(this_type &v) {
return v.end(); }
358 static const_iterator end(
const this_type &v) {
return v.end(); }
359 static origin_type* origin(this_type &v) {
return v.origin; }
360 static const origin_type* origin(
const this_type &v) {
return v.origin; }
361 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
362 { std::fill(it, ite, value_type(0)); }
363 static void do_clear(this_type &v)
364 { std::fill(v.begin(), v.end(), value_type(0)); }
365 static value_type access(
const origin_type *,
const const_iterator &it,
368 static reference access(origin_type *,
const iterator &it,
373 template <
typename IT,
typename V> std::ostream &
operator <<
374 (std::ostream &o,
const tab_ref_reg_spaced_with_origin<IT, V>& m)
375 { gmm::write(o,m);
return o; }
378 template <
typename IT,
typename ITINDEX,
typename V>
379 struct tab_ref_index_ref_with_origin
381 typedef tab_ref_index_ref_with_origin<IT, ITINDEX, V> this_type;
382 typedef typename linalg_traits<this_type>::porigin_type porigin_type;
386 tab_ref_index_ref_with_origin() {}
387 tab_ref_index_ref_with_origin(
const IT &b,
const ITINDEX &bi,
388 const ITINDEX &ei, porigin_type p)
389 : gmm::tab_ref_index_ref<IT, ITINDEX>(b, bi, ei), origin(p) {}
391 tab_ref_index_ref_with_origin(
const V &v,
const sub_index &si)
392 : gmm::tab_ref_index_ref<IT, ITINDEX>(vect_begin(const_cast<V&>(v)),
393 si.begin(), si.end()),
394 origin(linalg_origin(const_cast<V&>(v))) {}
395 tab_ref_index_ref_with_origin(V &v,
const sub_index &si)
396 : gmm::tab_ref_index_ref<IT, ITINDEX>(vect_begin(const_cast<V&>(v)),
397 si.begin(), si.end()),
398 origin(linalg_origin(const_cast<V&>(v))) {}
401 template <
typename IT,
typename ITINDEX,
typename V>
402 struct linalg_traits<tab_ref_index_ref_with_origin<IT, ITINDEX, V> > {
403 typedef typename std::iterator_traits<IT>::pointer PT;
404 typedef tab_ref_index_ref_with_origin<IT, ITINDEX, V> this_type;
405 typedef typename linalg_traits<V>::origin_type origin_type;
406 typedef typename select_ref<
const origin_type *, origin_type *,
407 PT>::ref_type porigin_type;
408 typedef typename which_reference<PT>::is_reference is_reference;
409 typedef abstract_vector linalg_type;
410 typedef typename std::iterator_traits<IT>::value_type value_type;
411 typedef typename std::iterator_traits<IT>::reference reference;
412 typedef typename this_type::iterator iterator;
413 typedef typename this_type::iterator const_iterator;
414 typedef abstract_dense storage_type;
415 typedef linalg_true index_sorted;
416 static size_type size(
const this_type &v) {
return v.size(); }
417 static iterator begin(this_type &v) {
return v.begin(); }
418 static const_iterator begin(
const this_type &v) {
return v.begin(); }
419 static iterator end(this_type &v) {
return v.end(); }
420 static const_iterator end(
const this_type &v) {
return v.end(); }
421 static origin_type* origin(this_type &v) {
return v.origin; }
422 static const origin_type* origin(
const this_type &v) {
return v.origin; }
423 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
424 { std::fill(it, ite, value_type(0)); }
425 static void do_clear(this_type &v)
426 { std::fill(v.begin(), v.end(), value_type(0)); }
427 static value_type access(
const origin_type *,
const const_iterator &it,
430 static reference access(origin_type *,
const iterator &it,
435 template <
typename IT,
typename ITINDEX,
typename V>
436 std::ostream &
operator <<
437 (std::ostream &o,
const tab_ref_index_ref_with_origin<IT, ITINDEX, V>& m)
438 { gmm::write(o,m);
return o; }
441 template<
typename ITER,
typename MIT,
typename PT>
442 struct dense_compressed_iterator {
443 typedef ITER value_type;
444 typedef ITER *pointer;
445 typedef ITER &reference;
446 typedef ptrdiff_t difference_type;
447 typedef std::random_access_iterator_tag iterator_category;
449 typedef dense_compressed_iterator<ITER, MIT, PT> iterator;
450 typedef typename std::iterator_traits<PT>::value_type *MPT;
456 iterator operator ++(
int) { iterator tmp = *
this; i++;
return tmp; }
457 iterator operator --(
int) { iterator tmp = *
this; i--;
return tmp; }
458 iterator &operator ++() { ++i;
return *
this; }
459 iterator &operator --() { --i;
return *
this; }
460 iterator &operator +=(difference_type ii) { i += ii;
return *
this; }
461 iterator &operator -=(difference_type ii) { i -= ii;
return *
this; }
463 { iterator itt = *
this;
return (itt += ii); }
465 { iterator itt = *
this;
return (itt -= ii); }
466 difference_type
operator -(
const iterator &ii)
const
467 {
return (N ? (it - ii.it) / N : 0) + i - ii.i; }
469 ITER operator *()
const {
return it+i*N; }
470 ITER operator [](
int ii)
const {
return it + (i+ii) * N; }
472 bool operator ==(
const iterator &ii)
const
473 {
return (*
this - ii) == difference_type(0); }
474 bool operator !=(
const iterator &ii)
const {
return !(ii == *
this); }
475 bool operator < (
const iterator &ii)
const
476 {
return (*
this - ii) < difference_type(0); }
477 bool operator > (
const iterator &ii)
const
478 {
return (*
this - ii) > difference_type(0); }
479 bool operator >=(
const iterator &ii)
const
480 {
return (*
this - ii) >= difference_type(0); }
482 dense_compressed_iterator() {}
483 dense_compressed_iterator(
const dense_compressed_iterator<MIT,MIT,MPT> &ii)
484 : it(ii.it), N(ii.N), nrows(ii.nrows), ncols(ii.ncols), i(ii.i),
488 : it(iter), N(n), nrows(r), ncols(c), i(ii), origin(o) { }
496 template <
typename PT1,
typename PT2,
int shift = 0>
497 struct cs_vector_ref_iterator {
501 typedef typename std::iterator_traits<PT1>::value_type value_type;
503 typedef typename std::iterator_traits<PT1>::reference reference;
505 typedef ptrdiff_t difference_type;
506 typedef std::bidirectional_iterator_tag iterator_category;
507 typedef cs_vector_ref_iterator<PT1, PT2, shift> iterator;
509 cs_vector_ref_iterator() {}
510 cs_vector_ref_iterator(PT1 p1, PT2 p2) : pr(p1), ir(p2) {}
512 inline size_type index()
const {
return (*ir) - shift; }
513 iterator &operator ++() { ++pr; ++ir;
return *
this; }
514 iterator operator ++(
int) { iterator tmp = *
this; ++(*this);
return tmp; }
515 iterator &operator --() { --pr; --ir;
return *
this; }
516 iterator operator --(
int) { iterator tmp = *
this; --(*this);
return tmp; }
518 reference operator *()
const {
return *pr; }
519 pointer operator ->()
const {
return pr; }
521 bool operator ==(
const iterator &i)
const {
return (i.pr==pr);}
522 bool operator !=(
const iterator &i)
const {
return (i.pr!=pr);}
525 template <
typename PT1,
typename PT2,
int shift = 0>
struct cs_vector_ref {
530 typedef cs_vector_ref<PT1, PT2, shift> this_type;
531 typedef typename std::iterator_traits<PT1>::value_type value_type;
532 typedef typename linalg_traits<this_type>::const_iterator const_iterator;
535 : pr(pt1), ir(pt2), n(
nnz), size_(ns) {}
540 const_iterator begin()
const {
return const_iterator(pr, ir); }
541 const_iterator end()
const {
return const_iterator(pr+n, ir+n); }
544 {
return linalg_traits<this_type>::access(pr, begin(), end(),i); }
547 template <
typename PT1,
typename PT2,
int shift>
548 struct linalg_traits<cs_vector_ref<PT1, PT2, shift> > {
549 typedef cs_vector_ref<PT1, PT2, shift> this_type;
550 typedef linalg_const is_reference;
551 typedef abstract_vector linalg_type;
552 typedef typename std::iterator_traits<PT1>::value_type value_type;
553 typedef value_type origin_type;
554 typedef typename std::iterator_traits<PT1>::value_type reference;
555 typedef cs_vector_ref_iterator<typename const_pointer<PT1>::pointer,
556 typename const_pointer<PT2>::pointer, shift> const_iterator;
557 typedef abstract_null_type iterator;
558 typedef abstract_sparse storage_type;
559 typedef linalg_true index_sorted;
560 static size_type size(
const this_type &v) {
return v.size(); }
561 static iterator begin(this_type &v) {
return v.begin(); }
562 static const_iterator begin(
const this_type &v) {
return v.begin(); }
563 static iterator end(this_type &v) {
return v.end(); }
564 static const_iterator end(
const this_type &v) {
return v.end(); }
565 static const origin_type* origin(
const this_type &v) {
return v.pr; }
566 static value_type access(
const origin_type *,
const const_iterator &b,
568 if (b.ir == e.ir)
return value_type(0);
569 PT2 p = std::lower_bound(b.ir, e.ir, i+shift);
570 return (*p == i+shift && p != e.ir) ? b.pr[p-b.ir] : value_type(0);
574 template <
typename PT1,
typename PT2,
int shift>
575 std::ostream &
operator <<
576 (std::ostream &o,
const cs_vector_ref<PT1, PT2, shift>& m)
577 { gmm::write(o,m);
return o; }
579 template <
typename PT1,
typename PT2,
int shift>
580 inline size_type nnz(
const cs_vector_ref<PT1, PT2, shift>& l) {
return l.n; }
586 template <
typename PT1,
typename PT2,
typename PT3,
int shift = 0>
587 struct sparse_compressed_iterator {
588 typedef typename std::iterator_traits<PT1>::value_type value_type;
589 typedef const value_type *pointer;
590 typedef const value_type &reference;
591 typedef ptrdiff_t difference_type;
593 typedef std::random_access_iterator_tag iterator_category;
594 typedef sparse_compressed_iterator<PT1, PT2, PT3, shift> iterator;
600 const value_type *origin;
602 iterator operator ++(
int) { iterator tmp = *
this; jc++;
return tmp; }
603 iterator operator --(
int) { iterator tmp = *
this; jc--;
return tmp; }
604 iterator &operator ++() { jc++;
return *
this; }
605 iterator &operator --() { jc--;
return *
this; }
606 iterator &operator +=(difference_type i) { jc += i;
return *
this; }
607 iterator &operator -=(difference_type i) { jc -= i;
return *
this; }
609 { iterator itt = *
this;
return (itt += i); }
611 { iterator itt = *
this;
return (itt -= i); }
612 difference_type
operator -(
const iterator &i)
const {
return jc - i.jc; }
614 reference operator *()
const {
return pr + *jc - shift; }
615 reference operator [](
int ii) {
return pr + *(jc+ii) - shift; }
617 bool operator ==(
const iterator &i)
const {
return (jc == i.jc); }
618 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
619 bool operator < (
const iterator &i)
const {
return (jc < i.jc); }
620 bool operator > (
const iterator &i)
const {
return (jc > i.jc); }
621 bool operator >=(
const iterator &i)
const {
return (jc >= i.jc); }
623 sparse_compressed_iterator() {}
624 sparse_compressed_iterator(PT1 p1, PT2 p2, PT3 p3,
size_type nn,
626 : pr(p1), ir(p2), jc(p3), n(nn), origin(o) { }
630 template <
typename PT1,
typename PT2,
typename PT3,
int shift = 0>
631 struct csc_matrix_ref {
637 typedef typename std::iterator_traits<PT1>::value_type value_type;
639 : pr(pt1), ir(pt2), jc(pt3), nc(ncc), nr(nrr) {}
646 {
return mat_col(*
this, j)[i]; }
649 template <
typename PT1,
typename PT2,
typename PT3,
int shift>
650 struct linalg_traits<csc_matrix_ref<PT1, PT2, PT3, shift> > {
651 typedef csc_matrix_ref<PT1, PT2, PT3, shift> this_type;
652 typedef linalg_const is_reference;
653 typedef abstract_matrix linalg_type;
654 typedef typename std::iterator_traits<PT1>::value_type value_type;
655 typedef typename std::iterator_traits<PT1>::value_type reference;
656 typedef value_type origin_type;
657 typedef abstract_sparse storage_type;
658 typedef abstract_null_type sub_row_type;
659 typedef abstract_null_type const_sub_row_type;
660 typedef abstract_null_type row_iterator;
661 typedef abstract_null_type const_row_iterator;
662 typedef abstract_null_type sub_col_type;
663 typedef cs_vector_ref<typename const_pointer<PT1>::pointer,
664 typename const_pointer<PT2>::pointer, shift> const_sub_col_type;
665 typedef sparse_compressed_iterator<typename const_pointer<PT1>::pointer,
666 typename const_pointer<PT2>::pointer,
667 typename const_pointer<PT3>::pointer,
668 shift> const_col_iterator;
669 typedef abstract_null_type col_iterator;
670 typedef col_major sub_orientation;
671 typedef linalg_true index_sorted;
672 static size_type nrows(
const this_type &m) {
return m.nrows(); }
673 static size_type ncols(
const this_type &m) {
return m.ncols(); }
674 static const_col_iterator col_begin(
const this_type &m)
675 {
return const_col_iterator(m.pr, m.ir, m.jc, m.nr, m.pr); }
676 static const_col_iterator col_end(
const this_type &m)
677 {
return const_col_iterator(m.pr, m.ir, m.jc + m.nc, m.nr, m.pr); }
678 static const_sub_col_type col(
const const_col_iterator &it) {
679 return const_sub_col_type(it.pr + *(it.jc) - shift,
680 it.ir + *(it.jc) - shift, *(it.jc + 1) - *(it.jc), it.n);
682 static const origin_type* origin(
const this_type &m) {
return m.pr; }
683 static value_type access(
const const_col_iterator &itcol,
size_type j)
684 {
return col(itcol)[j]; }
688 template <
typename PT1,
typename PT2,
typename PT3,
int shift>
689 std::ostream &
operator <<
690 (std::ostream &o,
const csc_matrix_ref<PT1, PT2, PT3, shift>& m)
691 { gmm::write(o,m);
return o; }
697 template <
typename PT1,
typename PT2,
typename PT3,
int shift = 0>
698 struct csr_matrix_ref {
704 typedef typename std::iterator_traits<PT1>::value_type value_type;
706 : pr(pt1), ir(pt2), jc(pt3), nc(ncc), nr(nrr) {}
713 {
return mat_row(*
this, i)[j]; }
716 template <
typename PT1,
typename PT2,
typename PT3,
int shift>
717 struct linalg_traits<csr_matrix_ref<PT1, PT2, PT3, shift> > {
718 typedef csr_matrix_ref<PT1, PT2, PT3, shift> this_type;
719 typedef linalg_const is_reference;
720 typedef abstract_matrix linalg_type;
721 typedef typename std::iterator_traits<PT1>::value_type value_type;
722 typedef typename std::iterator_traits<PT1>::value_type reference;
723 typedef value_type origin_type;
724 typedef abstract_sparse storage_type;
725 typedef abstract_null_type sub_col_type;
726 typedef abstract_null_type const_sub_col_type;
727 typedef abstract_null_type col_iterator;
728 typedef abstract_null_type const_col_iterator;
729 typedef abstract_null_type sub_row_type;
730 typedef cs_vector_ref<typename const_pointer<PT1>::pointer,
731 typename const_pointer<PT2>::pointer, shift>
733 typedef sparse_compressed_iterator<typename const_pointer<PT1>::pointer,
734 typename const_pointer<PT2>::pointer,
735 typename const_pointer<PT3>::pointer,
736 shift> const_row_iterator;
737 typedef abstract_null_type row_iterator;
738 typedef row_major sub_orientation;
739 typedef linalg_true index_sorted;
740 static size_type nrows(
const this_type &m) {
return m.nrows(); }
741 static size_type ncols(
const this_type &m) {
return m.ncols(); }
742 static const_row_iterator row_begin(
const this_type &m)
743 {
return const_row_iterator(m.pr, m.ir, m.jc, m.nc, m.pr); }
744 static const_row_iterator row_end(
const this_type &m)
745 {
return const_row_iterator(m.pr, m.ir, m.jc + m.nr, m.nc, m.pr); }
746 static const_sub_row_type row(
const const_row_iterator &it) {
747 return const_sub_row_type(it.pr + *(it.jc) - shift,
748 it.ir + *(it.jc) - shift, *(it.jc + 1) - *(it.jc), it.n);
750 static const origin_type* origin(
const this_type &m) {
return m.pr; }
751 static value_type access(
const const_row_iterator &itrow,
size_type j)
752 {
return row(itrow)[j]; }
755 template <
typename PT1,
typename PT2,
typename PT3,
int shift>
756 std::ostream &
operator <<
757 (std::ostream &o,
const csr_matrix_ref<PT1, PT2, PT3, shift>& m)
758 { gmm::write(o,m);
return o; }
766 template <
class PT>
struct array1D_reference {
768 typedef typename std::iterator_traits<PT>::value_type value_type;
772 const value_type &operator[](
size_type i)
const {
return *(begin+i); }
773 value_type &operator[](
size_type i) {
return *(begin+i); }
775 array1D_reference(PT begin_,
size_type s) : begin(begin_), end(begin_+s) {}
778 template <
typename PT>
779 struct linalg_traits<array1D_reference<PT> > {
780 typedef array1D_reference<PT> this_type;
781 typedef this_type origin_type;
782 typedef typename which_reference<PT>::is_reference is_reference;
783 typedef abstract_vector linalg_type;
784 typedef typename std::iterator_traits<PT>::value_type value_type;
785 typedef typename std::iterator_traits<PT>::reference reference;
787 typedef PT const_iterator;
788 typedef abstract_dense storage_type;
789 typedef linalg_true index_sorted;
790 static size_type size(
const this_type &v) {
return v.end - v.begin; }
791 static iterator begin(this_type &v) {
return v.begin; }
792 static const_iterator begin(
const this_type &v) {
return v.begin; }
793 static iterator end(this_type &v) {
return v.end; }
794 static const_iterator end(
const this_type &v) {
return v.end; }
795 static origin_type* origin(this_type &v) {
return &v; }
796 static const origin_type* origin(
const this_type &v) {
return &v; }
797 static void clear(origin_type*,
const iterator &it,
const iterator &ite)
798 { std::fill(it, ite, value_type(0)); }
799 static void do_clear(this_type &v)
800 { std::fill(v.begin, v.end, value_type(0)); }
801 static value_type access(
const origin_type *,
const const_iterator &it,
804 static reference access(origin_type *,
const iterator &it,
808 { GMM_ASSERT1(
false,
"Not resizable vector"); }
811 template<
typename PT> std::ostream &
operator <<
812 (std::ostream &o,
const array1D_reference<PT>& v)
813 { gmm::write(o,v);
return o; }
815 template <
class PT>
struct array2D_col_reference {
817 typedef typename std::iterator_traits<PT>::value_type T;
818 typedef typename std::iterator_traits<PT>::reference reference;
819 typedef typename const_reference<reference>::reference const_reference;
821 typedef typename const_pointer<PT>::pointer const_iterator;
827 GMM_ASSERT2(l < nbl && c < nbc,
"out of range");
828 return *(begin_ + c*nbl+l);
831 GMM_ASSERT2(l < nbl && c < nbc,
"out of range");
832 return *(begin_ + c*nbl+l);
837 GMM_ASSERT2(n*m == nbl*nbc,
"dimensions mismatch");
841 void fill(T a, T b = T(0)) {
842 std::fill(begin_, begin_+nbc*nbl, b);
843 iterator p = begin_, e = begin_+nbc*nbl;
844 while (p < e) { *p = a; p += nbl+1; }
846 inline size_type nrows()
const {
return nbl; }
847 inline size_type ncols()
const {
return nbc; }
849 iterator begin() {
return begin_; }
850 const_iterator begin()
const {
return begin_; }
851 iterator end() {
return begin_+nbl*nbc; }
852 const_iterator end()
const {
return begin_+nbl*nbc; }
855 : begin_(begin__), nbl(nrows_), nbc(ncols_) {}
858 template <
typename PT>
struct linalg_traits<array2D_col_reference<PT> > {
859 typedef array2D_col_reference<PT> this_type;
860 typedef this_type origin_type;
861 typedef typename which_reference<PT>::is_reference is_reference;
862 typedef abstract_matrix linalg_type;
863 typedef typename std::iterator_traits<PT>::value_type value_type;
864 typedef typename std::iterator_traits<PT>::reference reference;
865 typedef abstract_dense storage_type;
866 typedef tab_ref_reg_spaced_with_origin<
typename this_type::iterator,
867 this_type> sub_row_type;
868 typedef tab_ref_reg_spaced_with_origin<
typename this_type::const_iterator,
869 this_type> const_sub_row_type;
870 typedef dense_compressed_iterator<
typename this_type::iterator,
871 typename this_type::iterator,
872 this_type *> row_iterator;
873 typedef dense_compressed_iterator<
typename this_type::const_iterator,
874 typename this_type::iterator,
875 const this_type *> const_row_iterator;
876 typedef tab_ref_with_origin<
typename this_type::iterator,
877 this_type> sub_col_type;
878 typedef tab_ref_with_origin<
typename this_type::const_iterator,
879 this_type> const_sub_col_type;
880 typedef dense_compressed_iterator<
typename this_type::iterator,
881 typename this_type::iterator,
882 this_type *> col_iterator;
883 typedef dense_compressed_iterator<
typename this_type::const_iterator,
884 typename this_type::iterator,
885 const this_type *> const_col_iterator;
886 typedef col_and_row sub_orientation;
887 typedef linalg_true index_sorted;
888 static size_type nrows(
const this_type &m) {
return m.nrows(); }
889 static size_type ncols(
const this_type &m) {
return m.ncols(); }
890 static const_sub_row_type row(
const const_row_iterator &it)
891 {
return const_sub_row_type(*it, it.nrows, it.ncols, it.origin); }
892 static const_sub_col_type col(
const const_col_iterator &it)
893 {
return const_sub_col_type(*it, *it + it.nrows, it.origin); }
894 static sub_row_type row(
const row_iterator &it)
895 {
return sub_row_type(*it, it.nrows, it.ncols, it.origin); }
896 static sub_col_type col(
const col_iterator &it)
897 {
return sub_col_type(*it, *it + it.nrows, it.origin); }
898 static row_iterator row_begin(this_type &m)
899 {
return row_iterator(m.begin(), 1, m.nrows(), m.ncols(), 0, &m); }
900 static row_iterator row_end(this_type &m)
901 {
return row_iterator(m.begin(), 1, m.nrows(), m.ncols(), m.nrows(), &m); }
902 static const_row_iterator row_begin(
const this_type &m)
903 {
return const_row_iterator(m.begin(), 1, m.nrows(), m.ncols(), 0, &m); }
904 static const_row_iterator row_end(
const this_type &m) {
905 return const_row_iterator(m.begin(), 1, m.nrows(),
906 m.ncols(), m.nrows(), &m);
908 static col_iterator col_begin(this_type &m)
909 {
return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
910 static col_iterator col_end(this_type &m) {
911 return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(),
914 static const_col_iterator col_begin(
const this_type &m) {
915 return const_col_iterator(m.begin(), m.nrows(), m.nrows(),
918 static const_col_iterator col_end(
const this_type &m) {
919 return const_col_iterator(m.begin(), m.nrows(),m.nrows(),m.ncols(),
922 static origin_type* origin(this_type &m) {
return &m; }
923 static const origin_type* origin(
const this_type &m) {
return &m; }
924 static void do_clear(this_type &m) { m.fill(value_type(0)); }
925 static value_type access(
const const_col_iterator &itcol,
size_type j)
926 {
return (*itcol)[j]; }
927 static reference access(
const col_iterator &itcol,
size_type j)
928 {
return (*itcol)[j]; }
935 template<
typename PT> std::ostream &
operator <<
936 (std::ostream &o,
const array2D_col_reference<PT>& m)
937 { gmm::write(o,m);
return o; }
941 template <
class PT>
struct array2D_row_reference {
943 typedef typename std::iterator_traits<PT>::value_type T;
944 typedef typename std::iterator_traits<PT>::reference reference;
945 typedef typename const_reference<reference>::reference const_reference;
947 typedef typename const_pointer<PT>::pointer const_iterator;
953 GMM_ASSERT2(l < nbl && c < nbc,
"out of range");
954 return *(begin_ + l*nbc+c);
957 GMM_ASSERT2(l < nbl && c < nbc,
"out of range");
958 return *(begin_ + l*nbc+c);
963 GMM_ASSERT2(n*m == nbl*nbc,
"dimensions mismatch");
967 void fill(T a, T b = T(0)) {
968 std::fill(begin_, begin_+nbc*nbl, b);
969 iterator p = begin_, e = begin_+nbc*nbl;
970 while (p < e) { *p = a; p += nbc+1; }
972 inline size_type nrows()
const {
return nbl; }
973 inline size_type ncols()
const {
return nbc; }
975 iterator begin() {
return begin_; }
976 const_iterator begin()
const {
return begin_; }
977 iterator end() {
return begin_+nbl*nbc; }
978 const_iterator end()
const {
return begin_+nbl*nbc; }
981 : begin_(begin__), nbl(nrows_), nbc(ncols_) {}
984 template <
typename PT>
struct linalg_traits<array2D_row_reference<PT> > {
985 typedef array2D_row_reference<PT> this_type;
986 typedef this_type origin_type;
987 typedef typename which_reference<PT>::is_reference is_reference;
988 typedef abstract_matrix linalg_type;
989 typedef typename std::iterator_traits<PT>::value_type value_type;
990 typedef typename std::iterator_traits<PT>::reference reference;
991 typedef abstract_dense storage_type;
992 typedef tab_ref_reg_spaced_with_origin<
typename this_type::iterator,
993 this_type> sub_col_type;
994 typedef tab_ref_reg_spaced_with_origin<
typename this_type::const_iterator,
995 this_type> const_sub_col_type;
996 typedef dense_compressed_iterator<
typename this_type::iterator,
997 typename this_type::iterator,
998 this_type *> col_iterator;
999 typedef dense_compressed_iterator<
typename this_type::const_iterator,
1000 typename this_type::iterator,
1001 const this_type *> const_col_iterator;
1002 typedef tab_ref_with_origin<
typename this_type::iterator,
1003 this_type> sub_row_type;
1004 typedef tab_ref_with_origin<
typename this_type::const_iterator,
1005 this_type> const_sub_row_type;
1006 typedef dense_compressed_iterator<
typename this_type::iterator,
1007 typename this_type::iterator,
1008 this_type *> row_iterator;
1009 typedef dense_compressed_iterator<
typename this_type::const_iterator,
1010 typename this_type::iterator,
1011 const this_type *> const_row_iterator;
1012 typedef col_and_row sub_orientation;
1013 typedef linalg_true index_sorted;
1014 static size_type ncols(
const this_type &m) {
return m.ncols(); }
1015 static size_type nrows(
const this_type &m) {
return m.nrows(); }
1016 static const_sub_col_type col(
const const_col_iterator &it)
1017 {
return const_sub_col_type(*it, it.ncols, it.nrows, it.origin); }
1018 static const_sub_row_type row(
const const_row_iterator &it)
1019 {
return const_sub_row_type(*it, *it + it.ncols, it.origin); }
1020 static sub_col_type col(
const col_iterator &it)
1021 {
return sub_col_type(*it, *it, it.ncols, it.nrows, it.origin); }
1022 static sub_row_type row(
const row_iterator &it)
1023 {
return sub_row_type(*it, *it + it.ncols, it.origin); }
1024 static col_iterator col_begin(this_type &m)
1025 {
return col_iterator(m.begin(), 1, m.ncols(), m.nrows(), 0, &m); }
1026 static col_iterator col_end(this_type &m)
1027 {
return col_iterator(m.begin(), 1, m.ncols(), m.nrows(), m.ncols(), &m); }
1028 static const_col_iterator col_begin(
const this_type &m)
1029 {
return const_col_iterator(m.begin(), 1, m.ncols(), m.nrows(), 0, &m); }
1030 static const_col_iterator col_end(
const this_type &m) {
1031 return const_col_iterator(m.begin(), 1, m.ncols(),
1032 m.nrows(), m.ncols(), &m);
1034 static row_iterator row_begin(this_type &m)
1035 {
return row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(), 0, &m); }
1036 static row_iterator row_end(this_type &m) {
1037 return row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
1040 static const_row_iterator row_begin(
const this_type &m) {
1041 return const_row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
1044 static const_row_iterator row_end(
const this_type &m) {
1045 return const_row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
1048 static origin_type* origin(this_type &m) {
return &m; }
1049 static const origin_type* origin(
const this_type &m) {
return &m; }
1050 static void do_clear(this_type &m) { m.fill(value_type(0)); }
1051 static value_type access(
const const_row_iterator &itrow,
size_type j)
1052 {
return (*itrow)[j]; }
1053 static reference access(
const row_iterator &itrow,
size_type j)
1054 {
return (*itrow)[j]; }
1058 { v.reshape(m, n); }
1061 template<
typename PT> std::ostream &
operator <<
1062 (std::ostream &o,
const array2D_row_reference<PT>& m)
1063 { gmm::write(o,m);
return o; }
indexed array reference (given a container X, and a set of indexes I, this class provides a pseudo-co...
provide a "strided" view a of container
Basic linear algebra functions.
size_type nnz(const L &l)
count the number of non-zero entries of a vector or matrix.
void reshape(M &v, size_type m, size_type n)
*/
void fill(L &l, typename gmm::linalg_traits< L >::value_type x)
*/
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void resize(V &v, size_type n)
*/
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