36 #ifndef GMM_SCALED_H__
37 #define GMM_SCALED_H__
47 template <
typename IT,
typename S>
struct scaled_const_iterator {
48 typedef typename strongest_numeric_type<typename std::iterator_traits<IT>::value_type,
51 typedef typename std::iterator_traits<IT>::pointer pointer;
52 typedef typename std::iterator_traits<IT>::reference reference;
53 typedef typename std::iterator_traits<IT>::difference_type difference_type;
54 typedef typename std::iterator_traits<IT>::iterator_category
60 scaled_const_iterator(
void) {}
61 scaled_const_iterator(
const IT &i, S x) : it(i), r(x) {}
63 inline size_type index(
void)
const {
return it.index(); }
64 inline scaled_const_iterator operator ++(
int)
65 { scaled_const_iterator tmp = *
this; ++it;
return tmp; }
66 inline scaled_const_iterator operator --(
int)
67 { scaled_const_iterator tmp = *
this; --it;
return tmp; }
68 inline scaled_const_iterator &operator ++() { ++it;
return *
this; }
69 inline scaled_const_iterator &operator --() { --it;
return *
this; }
70 inline scaled_const_iterator &operator +=(difference_type i)
71 { it += i;
return *
this; }
72 inline scaled_const_iterator &operator -=(difference_type i)
73 { it -= i;
return *
this; }
74 inline scaled_const_iterator
operator +(difference_type i)
const
75 { scaled_const_iterator itb = *
this;
return (itb += i); }
76 inline scaled_const_iterator
operator -(difference_type i)
const
77 { scaled_const_iterator itb = *
this;
return (itb -= i); }
78 inline difference_type
operator -(
const scaled_const_iterator &i)
const
79 {
return difference_type(it - i.it); }
81 inline value_type operator *()
const {
return (*it) * value_type(r); }
82 inline value_type operator [](
size_type ii)
const {
return it[ii] * r; }
84 inline bool operator ==(
const scaled_const_iterator &i)
const
85 {
return (i.it == it); }
86 inline bool operator !=(
const scaled_const_iterator &i)
const
87 {
return (i.it != it); }
88 inline bool operator < (
const scaled_const_iterator &i)
const
89 {
return (it < i.it); }
90 inline bool operator > (
const scaled_const_iterator &i)
const
91 {
return (it > i.it); }
92 inline bool operator >=(
const scaled_const_iterator &i)
const
93 {
return (it >= i.it); }
96 template <
typename V,
typename S>
struct scaled_vector_const_ref {
97 typedef scaled_vector_const_ref<V,S> this_type;
98 typedef typename linalg_traits<this_type>::value_type value_type;
99 typedef typename linalg_traits<V>::const_iterator iterator;
100 typedef typename linalg_traits<this_type>::reference reference;
101 typedef typename linalg_traits<this_type>::origin_type origin_type;
103 iterator begin_, end_;
104 const origin_type *origin;
108 scaled_vector_const_ref(
const V &v, S rr)
109 : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
110 origin(linalg_origin(v)), size_(vect_size(v)), r(rr) {}
113 {
return value_type(r) * linalg_traits<V>::access(origin, begin_, end_, i); }
117 template<
typename V,
typename S> std::ostream &
operator <<
118 (std::ostream &o,
const scaled_vector_const_ref<V,S>& m)
119 { gmm::write(o,m);
return o; }
125 template <
typename M,
typename S>
struct scaled_row_const_iterator {
126 typedef scaled_row_const_iterator<M,S> iterator;
127 typedef typename linalg_traits<M>::const_row_iterator ITER;
128 typedef ptrdiff_t difference_type;
134 inline iterator operator ++(
int) { iterator tmp=*
this; it++;
return tmp; }
135 inline iterator operator --(
int) { iterator tmp=*
this; it--;
return tmp; }
136 inline iterator &operator ++() { it++;
return *
this; }
137 inline iterator &operator --() { it--;
return *
this; }
138 iterator &operator +=(difference_type i) { it += i;
return *
this; }
139 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
141 { iterator itt = *
this;
return (itt += i); }
143 { iterator itt = *
this;
return (itt -= i); }
144 difference_type
operator -(
const iterator &i)
const
145 {
return it - i.it; }
147 inline ITER operator *()
const {
return it; }
148 inline ITER operator [](
int i) {
return it + i; }
150 inline bool operator ==(
const iterator &i)
const {
return (it == i.it); }
151 inline bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
152 inline bool operator < (
const iterator &i)
const {
return (it < i.it); }
153 inline bool operator >=(
const iterator &i)
const {
return (it >= i.it); }
154 inline bool operator > (
const iterator &i)
const {
return (it > i.it); }
156 scaled_row_const_iterator(
void) {}
157 scaled_row_const_iterator(
const ITER &i, S rr)
162 template <
typename M,
typename S>
struct scaled_row_matrix_const_ref {
164 typedef scaled_row_matrix_const_ref<M,S> this_type;
165 typedef typename linalg_traits<M>::const_row_iterator iterator;
166 typedef typename linalg_traits<this_type>::value_type value_type;
167 typedef typename linalg_traits<this_type>::origin_type origin_type;
169 iterator begin_, end_;
170 const origin_type *origin;
174 scaled_row_matrix_const_ref(
const M &m, S rr)
175 : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
176 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
179 {
return r * linalg_traits<M>::access(begin_+i, j); }
183 template<
typename M,
typename S> std::ostream &
operator <<
184 (std::ostream &o,
const scaled_row_matrix_const_ref<M,S>& m)
185 { gmm::write(o,m);
return o; }
188 template <
typename M,
typename S>
struct scaled_col_const_iterator {
189 typedef scaled_col_const_iterator<M,S> iterator;
190 typedef typename linalg_traits<M>::const_col_iterator ITER;
191 typedef ptrdiff_t difference_type;
197 iterator operator ++(
int) { iterator tmp = *
this; it++;
return tmp; }
198 iterator operator --(
int) { iterator tmp = *
this; it--;
return tmp; }
199 iterator &operator ++() { it++;
return *
this; }
200 iterator &operator --() { it--;
return *
this; }
201 iterator &operator +=(difference_type i) { it += i;
return *
this; }
202 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
204 { iterator itt = *
this;
return (itt += i); }
206 { iterator itt = *
this;
return (itt -= i); }
207 difference_type
operator -(
const iterator &i)
const
208 {
return it - i.it; }
210 ITER operator *()
const {
return it; }
211 ITER operator [](
int i) {
return it + i; }
213 bool operator ==(
const iterator &i)
const {
return (it == i.it); }
214 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
215 bool operator < (
const iterator &i)
const {
return (it < i.it); }
216 bool operator > (
const iterator &i)
const {
return (it > i.it); }
217 bool operator >=(
const iterator &i)
const {
return (it >= i.it); }
219 scaled_col_const_iterator(
void) {}
220 scaled_col_const_iterator(
const ITER &i, S rr)
225 template <
typename M,
typename S>
struct scaled_col_matrix_const_ref {
227 typedef scaled_col_matrix_const_ref<M,S> this_type;
228 typedef typename linalg_traits<M>::const_col_iterator iterator;
229 typedef typename linalg_traits<this_type>::value_type value_type;
230 typedef typename linalg_traits<this_type>::origin_type origin_type;
232 iterator begin_, end_;
233 const origin_type *origin;
237 scaled_col_matrix_const_ref(
const M &m, S rr)
238 : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
239 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
242 {
return r * linalg_traits<M>::access(begin_+j, i); }
247 template<
typename M,
typename S> std::ostream &
operator <<
248 (std::ostream &o,
const scaled_col_matrix_const_ref<M,S>& m)
249 { gmm::write(o,m);
return o; }
252 template <
typename L,
typename S,
typename R>
struct scaled_return__ {
253 typedef abstract_null_type return_type;
255 template <
typename L,
typename S>
struct scaled_return__<L, S, row_major>
256 {
typedef scaled_row_matrix_const_ref<L,S> return_type; };
257 template <
typename L,
typename S>
struct scaled_return__<L, S, col_major>
258 {
typedef scaled_col_matrix_const_ref<L,S> return_type; };
261 template <
typename L,
typename S,
typename LT>
struct scaled_return_ {
262 typedef abstract_null_type return_type;
264 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_vector>
265 {
typedef scaled_vector_const_ref<L,S> return_type; };
266 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_matrix> {
267 typedef typename scaled_return__<L, S,
268 typename principal_orientation_type<
typename
269 linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
272 template <
typename L,
typename S>
struct scaled_return {
273 typedef typename scaled_return_<L, S,
typename
274 linalg_traits<L>::linalg_type>::return_type return_type;
277 template <
typename L,
typename S>
inline
278 typename scaled_return<L,S>::return_type
279 scaled(
const L &v, S x)
280 {
return scaled(v, x,
typename linalg_traits<L>::linalg_type()); }
282 template <
typename V,
typename S>
inline
283 typename scaled_return<V,S>::return_type
284 scaled(
const V &v, S x, abstract_vector)
285 {
return scaled_vector_const_ref<V,S>(v, x); }
287 template <
typename M,
typename S>
inline
288 typename scaled_return<M,S>::return_type
289 scaled(
const M &m, S x,abstract_matrix) {
290 return scaled(m, x,
typename principal_orientation_type<
typename
291 linalg_traits<M>::sub_orientation>::potype());
294 template <
typename M,
typename S>
inline
295 typename scaled_return<M,S>::return_type
296 scaled(
const M &m, S x, row_major) {
297 return scaled_row_matrix_const_ref<M,S>(m, x);
300 template <
typename M,
typename S>
inline
301 typename scaled_return<M,S>::return_type
302 scaled(
const M &m, S x, col_major) {
303 return scaled_col_matrix_const_ref<M,S>(m, x);
311 template <
typename L>
inline
312 void scale(L& l,
typename linalg_traits<L>::value_type a)
313 { scale(l, a,
typename linalg_traits<L>::linalg_type()); }
315 template <
typename L>
inline
316 void scale(
const L& l,
typename linalg_traits<L>::value_type a)
317 { scale(linalg_const_cast(l), a); }
319 template <
typename L>
inline
320 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_vector) {
321 typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
322 for ( ; it != ite; ++it) *it *= a;
325 template <
typename L>
326 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_matrix) {
327 scale(l, a,
typename principal_orientation_type<
typename
328 linalg_traits<L>::sub_orientation>::potype());
331 template <
typename L>
332 void scale(L& l,
typename linalg_traits<L>::value_type a, row_major) {
333 typename linalg_traits<L>::row_iterator it = mat_row_begin(l),
334 ite = mat_row_end(l);
335 for ( ; it != ite; ++it) scale(linalg_traits<L>::row(it), a);
338 template <
typename L>
339 void scale(L& l,
typename linalg_traits<L>::value_type a, col_major) {
340 typename linalg_traits<L>::col_iterator it = mat_col_begin(l),
341 ite = mat_col_end(l);
342 for ( ; it != ite; ++it) scale(linalg_traits<L>::col(it), a);
345 template <
typename V,
typename S>
struct linalg_traits<scaled_vector_const_ref<V,S> > {
346 typedef scaled_vector_const_ref<V,S> this_type;
347 typedef linalg_const is_reference;
348 typedef abstract_vector linalg_type;
349 typedef typename strongest_numeric_type<S, typename linalg_traits<V>::value_type>::T value_type;
350 typedef typename linalg_traits<V>::origin_type origin_type;
351 typedef value_type reference;
352 typedef abstract_null_type iterator;
353 typedef scaled_const_iterator<typename linalg_traits<V>::const_iterator, S>
355 typedef typename linalg_traits<V>::storage_type storage_type;
356 typedef typename linalg_traits<V>::index_sorted index_sorted;
357 static size_type size(
const this_type &v) {
return v.size_; }
358 static const_iterator begin(
const this_type &v)
359 {
return const_iterator(v.begin_, v.r); }
360 static const_iterator end(
const this_type &v)
361 {
return const_iterator(v.end_, v.r); }
362 static const origin_type* origin(
const this_type &v) {
return v.origin; }
363 static value_type access(
const origin_type *o,
const const_iterator &it,
365 {
return it.r * (linalg_traits<V>::access(o, it.it, ite.it, i)); }
370 template <
typename M,
typename S>
struct linalg_traits<scaled_row_matrix_const_ref<M,S> > {
371 typedef scaled_row_matrix_const_ref<M,S> this_type;
372 typedef linalg_const is_reference;
373 typedef abstract_matrix linalg_type;
374 typedef typename linalg_traits<M>::origin_type origin_type;
375 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
376 typedef value_type reference;
377 typedef typename linalg_traits<M>::storage_type storage_type;
378 typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
379 typedef scaled_vector_const_ref<vector_type,S> sub_row_type;
380 typedef scaled_vector_const_ref<vector_type,S> const_sub_row_type;
381 typedef scaled_row_const_iterator<M,S> row_iterator;
382 typedef scaled_row_const_iterator<M,S> const_row_iterator;
383 typedef abstract_null_type const_sub_col_type;
384 typedef abstract_null_type sub_col_type;
385 typedef abstract_null_type const_col_iterator;
386 typedef abstract_null_type col_iterator;
387 typedef row_major sub_orientation;
388 typedef typename linalg_traits<M>::index_sorted index_sorted;
389 static size_type nrows(
const this_type &m)
391 static size_type ncols(
const this_type &m)
393 static const_sub_row_type row(
const const_row_iterator &it)
394 {
return scaled(linalg_traits<M>::row(it.it), it.r); }
395 static const_row_iterator row_begin(
const this_type &m)
396 {
return const_row_iterator(m.begin_, m.r); }
397 static const_row_iterator row_end(
const this_type &m)
398 {
return const_row_iterator(m.end_, m.r); }
399 static const origin_type* origin(
const this_type &m) {
return m.origin; }
400 static value_type access(
const const_row_iterator &it,
size_type i)
401 {
return it.r * (linalg_traits<M>::access(it.it, i)); }
404 template <
typename M,
typename S>
struct linalg_traits<scaled_col_matrix_const_ref<M,S> > {
405 typedef scaled_col_matrix_const_ref<M,S> this_type;
406 typedef linalg_const is_reference;
407 typedef abstract_matrix linalg_type;
408 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
409 typedef typename linalg_traits<M>::origin_type origin_type;
410 typedef value_type reference;
411 typedef typename linalg_traits<M>::storage_type storage_type;
412 typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
413 typedef abstract_null_type sub_col_type;
414 typedef scaled_vector_const_ref<vector_type,S> const_sub_col_type;
415 typedef abstract_null_type col_iterator;
416 typedef scaled_col_const_iterator<M,S> const_col_iterator;
417 typedef abstract_null_type const_sub_row_type;
418 typedef abstract_null_type sub_row_type;
419 typedef abstract_null_type const_row_iterator;
420 typedef abstract_null_type row_iterator;
421 typedef col_major sub_orientation;
422 typedef typename linalg_traits<M>::index_sorted index_sorted;
423 static size_type ncols(
const this_type &m)
425 static size_type nrows(
const this_type &m)
427 static const_sub_col_type col(
const const_col_iterator &it)
428 {
return scaled(linalg_traits<M>::col(it.it), it.r); }
429 static const_col_iterator col_begin(
const this_type &m)
430 {
return const_col_iterator(m.begin_, m.r); }
431 static const_col_iterator col_end(
const this_type &m)
432 {
return const_col_iterator(m.end_, m.r); }
433 static const origin_type* origin(
const this_type &m) {
return m.origin; }
434 static value_type access(
const const_col_iterator &it,
size_type i)
435 {
return it.r * (linalg_traits<M>::access(it.it, i)); }
Basic definitions and tools of GMM.
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