37 #ifndef GMM_SCALED_H__
38 #define GMM_SCALED_H__
48 template <
typename IT,
typename S>
struct scaled_const_iterator {
49 typedef typename strongest_numeric_type<typename std::iterator_traits<IT>::value_type,
52 typedef typename std::iterator_traits<IT>::pointer pointer;
53 typedef typename std::iterator_traits<IT>::reference reference;
54 typedef typename std::iterator_traits<IT>::difference_type difference_type;
55 typedef typename std::iterator_traits<IT>::iterator_category
61 scaled_const_iterator(
void) {}
62 scaled_const_iterator(
const IT &i, S x) : it(i), r(x) {}
64 inline size_type index(
void)
const {
return it.index(); }
65 inline scaled_const_iterator operator ++(
int)
66 { scaled_const_iterator tmp = *
this; ++it;
return tmp; }
67 inline scaled_const_iterator operator --(
int)
68 { scaled_const_iterator tmp = *
this; --it;
return tmp; }
69 inline scaled_const_iterator &operator ++() { ++it;
return *
this; }
70 inline scaled_const_iterator &operator --() { --it;
return *
this; }
71 inline scaled_const_iterator &operator +=(difference_type i)
72 { it += i;
return *
this; }
73 inline scaled_const_iterator &operator -=(difference_type i)
74 { it -= i;
return *
this; }
75 inline scaled_const_iterator
operator +(difference_type i)
const
76 { scaled_const_iterator itb = *
this;
return (itb += i); }
77 inline scaled_const_iterator
operator -(difference_type i)
const
78 { scaled_const_iterator itb = *
this;
return (itb -= i); }
79 inline difference_type
operator -(
const scaled_const_iterator &i)
const
80 {
return difference_type(it - i.it); }
82 inline value_type operator *()
const {
return (*it) * value_type(r); }
83 inline value_type operator [](
size_type ii)
const {
return it[ii] * r; }
85 inline bool operator ==(
const scaled_const_iterator &i)
const
86 {
return (i.it == it); }
87 inline bool operator !=(
const scaled_const_iterator &i)
const
88 {
return (i.it != it); }
89 inline bool operator < (
const scaled_const_iterator &i)
const
90 {
return (it < i.it); }
91 inline bool operator > (
const scaled_const_iterator &i)
const
92 {
return (it > i.it); }
93 inline bool operator >=(
const scaled_const_iterator &i)
const
94 {
return (it >= i.it); }
97 template <
typename V,
typename S>
struct scaled_vector_const_ref {
98 typedef scaled_vector_const_ref<V,S> this_type;
99 typedef typename linalg_traits<this_type>::value_type value_type;
100 typedef typename linalg_traits<V>::const_iterator iterator;
101 typedef typename linalg_traits<this_type>::reference reference;
102 typedef typename linalg_traits<this_type>::origin_type origin_type;
104 iterator begin_, end_;
105 const origin_type *origin;
109 scaled_vector_const_ref(
const V &v, S rr)
110 : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
111 origin(linalg_origin(v)), size_(vect_size(v)), r(rr) {}
114 {
return value_type(r) * linalg_traits<V>::access(origin, begin_, end_, i); }
118 template<
typename V,
typename S> std::ostream &
operator <<
119 (std::ostream &o,
const scaled_vector_const_ref<V,S>& m)
120 { gmm::write(o,m);
return o; }
126 template <
typename M,
typename S>
struct scaled_row_const_iterator {
127 typedef scaled_row_const_iterator<M,S> iterator;
128 typedef typename linalg_traits<M>::const_row_iterator ITER;
129 typedef ptrdiff_t difference_type;
135 inline iterator operator ++(
int) { iterator tmp=*
this; it++;
return tmp; }
136 inline iterator operator --(
int) { iterator tmp=*
this; it--;
return tmp; }
137 inline iterator &operator ++() { it++;
return *
this; }
138 inline iterator &operator --() { it--;
return *
this; }
139 iterator &operator +=(difference_type i) { it += i;
return *
this; }
140 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
142 { iterator itt = *
this;
return (itt += i); }
144 { iterator itt = *
this;
return (itt -= i); }
145 difference_type
operator -(
const iterator &i)
const
146 {
return it - i.it; }
148 inline ITER operator *()
const {
return it; }
149 inline ITER operator [](
int i) {
return it + i; }
151 inline bool operator ==(
const iterator &i)
const {
return (it == i.it); }
152 inline bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
153 inline bool operator < (
const iterator &i)
const {
return (it < i.it); }
154 inline bool operator >=(
const iterator &i)
const {
return (it >= i.it); }
155 inline bool operator > (
const iterator &i)
const {
return (it > i.it); }
157 scaled_row_const_iterator(
void) {}
158 scaled_row_const_iterator(
const ITER &i, S rr)
163 template <
typename M,
typename S>
struct scaled_row_matrix_const_ref {
165 typedef scaled_row_matrix_const_ref<M,S> this_type;
166 typedef typename linalg_traits<M>::const_row_iterator iterator;
167 typedef typename linalg_traits<this_type>::value_type value_type;
168 typedef typename linalg_traits<this_type>::origin_type origin_type;
170 iterator begin_, end_;
171 const origin_type *origin;
175 scaled_row_matrix_const_ref(
const M &m, S rr)
176 : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
177 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
180 {
return r * linalg_traits<M>::access(begin_+i, j); }
184 template<
typename M,
typename S> std::ostream &
operator <<
185 (std::ostream &o,
const scaled_row_matrix_const_ref<M,S>& m)
186 { gmm::write(o,m);
return o; }
189 template <
typename M,
typename S>
struct scaled_col_const_iterator {
190 typedef scaled_col_const_iterator<M,S> iterator;
191 typedef typename linalg_traits<M>::const_col_iterator ITER;
192 typedef ptrdiff_t difference_type;
198 iterator operator ++(
int) { iterator tmp = *
this; it++;
return tmp; }
199 iterator operator --(
int) { iterator tmp = *
this; it--;
return tmp; }
200 iterator &operator ++() { it++;
return *
this; }
201 iterator &operator --() { it--;
return *
this; }
202 iterator &operator +=(difference_type i) { it += i;
return *
this; }
203 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
205 { iterator itt = *
this;
return (itt += i); }
207 { iterator itt = *
this;
return (itt -= i); }
208 difference_type
operator -(
const iterator &i)
const
209 {
return it - i.it; }
211 ITER operator *()
const {
return it; }
212 ITER operator [](
int i) {
return it + i; }
214 bool operator ==(
const iterator &i)
const {
return (it == i.it); }
215 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
216 bool operator < (
const iterator &i)
const {
return (it < i.it); }
217 bool operator > (
const iterator &i)
const {
return (it > i.it); }
218 bool operator >=(
const iterator &i)
const {
return (it >= i.it); }
220 scaled_col_const_iterator(
void) {}
221 scaled_col_const_iterator(
const ITER &i, S rr)
226 template <
typename M,
typename S>
struct scaled_col_matrix_const_ref {
228 typedef scaled_col_matrix_const_ref<M,S> this_type;
229 typedef typename linalg_traits<M>::const_col_iterator iterator;
230 typedef typename linalg_traits<this_type>::value_type value_type;
231 typedef typename linalg_traits<this_type>::origin_type origin_type;
233 iterator begin_, end_;
234 const origin_type *origin;
238 scaled_col_matrix_const_ref(
const M &m, S rr)
239 : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
240 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
243 {
return r * linalg_traits<M>::access(begin_+j, i); }
248 template<
typename M,
typename S> std::ostream &
operator <<
249 (std::ostream &o,
const scaled_col_matrix_const_ref<M,S>& m)
250 { gmm::write(o,m);
return o; }
253 template <
typename L,
typename S,
typename R>
struct scaled_return__ {
254 typedef abstract_null_type return_type;
256 template <
typename L,
typename S>
struct scaled_return__<L, S, row_major>
257 {
typedef scaled_row_matrix_const_ref<L,S> return_type; };
258 template <
typename L,
typename S>
struct scaled_return__<L, S, col_major>
259 {
typedef scaled_col_matrix_const_ref<L,S> return_type; };
262 template <
typename L,
typename S,
typename LT>
struct scaled_return_ {
263 typedef abstract_null_type return_type;
265 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_vector>
266 {
typedef scaled_vector_const_ref<L,S> return_type; };
267 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_matrix> {
268 typedef typename scaled_return__<L, S,
269 typename principal_orientation_type<
typename
270 linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
273 template <
typename L,
typename S>
struct scaled_return {
274 typedef typename scaled_return_<L, S,
typename
275 linalg_traits<L>::linalg_type>::return_type return_type;
278 template <
typename L,
typename S>
inline
279 typename scaled_return<L,S>::return_type
280 scaled(
const L &v, S x)
281 {
return scaled(v, x,
typename linalg_traits<L>::linalg_type()); }
283 template <
typename V,
typename S>
inline
284 typename scaled_return<V,S>::return_type
285 scaled(
const V &v, S x, abstract_vector)
286 {
return scaled_vector_const_ref<V,S>(v, x); }
288 template <
typename M,
typename S>
inline
289 typename scaled_return<M,S>::return_type
290 scaled(
const M &m, S x,abstract_matrix) {
291 return scaled(m, x,
typename principal_orientation_type<
typename
292 linalg_traits<M>::sub_orientation>::potype());
295 template <
typename M,
typename S>
inline
296 typename scaled_return<M,S>::return_type
297 scaled(
const M &m, S x, row_major) {
298 return scaled_row_matrix_const_ref<M,S>(m, x);
301 template <
typename M,
typename S>
inline
302 typename scaled_return<M,S>::return_type
303 scaled(
const M &m, S x, col_major) {
304 return scaled_col_matrix_const_ref<M,S>(m, x);
312 template <
typename L>
inline
313 void scale(L& l,
typename linalg_traits<L>::value_type a)
314 { scale(l, a,
typename linalg_traits<L>::linalg_type()); }
316 template <
typename L>
inline
317 void scale(
const L& l,
typename linalg_traits<L>::value_type a)
318 { scale(linalg_const_cast(l), a); }
320 template <
typename L>
inline
321 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_vector) {
322 typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
323 for ( ; it != ite; ++it) *it *= a;
326 template <
typename L>
327 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_matrix) {
328 scale(l, a,
typename principal_orientation_type<
typename
329 linalg_traits<L>::sub_orientation>::potype());
332 template <
typename L>
333 void scale(L& l,
typename linalg_traits<L>::value_type a, row_major) {
334 typename linalg_traits<L>::row_iterator it = mat_row_begin(l),
335 ite = mat_row_end(l);
336 for ( ; it != ite; ++it) scale(linalg_traits<L>::row(it), a);
339 template <
typename L>
340 void scale(L& l,
typename linalg_traits<L>::value_type a, col_major) {
341 typename linalg_traits<L>::col_iterator it = mat_col_begin(l),
342 ite = mat_col_end(l);
343 for ( ; it != ite; ++it) scale(linalg_traits<L>::col(it), a);
346 template <
typename V,
typename S>
struct linalg_traits<scaled_vector_const_ref<V,S> > {
347 typedef scaled_vector_const_ref<V,S> this_type;
348 typedef linalg_const is_reference;
349 typedef abstract_vector linalg_type;
350 typedef typename strongest_numeric_type<S, typename linalg_traits<V>::value_type>::T value_type;
351 typedef typename linalg_traits<V>::origin_type origin_type;
352 typedef value_type reference;
353 typedef abstract_null_type iterator;
354 typedef scaled_const_iterator<typename linalg_traits<V>::const_iterator, S>
356 typedef typename linalg_traits<V>::storage_type storage_type;
357 typedef typename linalg_traits<V>::index_sorted index_sorted;
358 static size_type size(
const this_type &v) {
return v.size_; }
359 static const_iterator begin(
const this_type &v)
360 {
return const_iterator(v.begin_, v.r); }
361 static const_iterator end(
const this_type &v)
362 {
return const_iterator(v.end_, v.r); }
363 static const origin_type* origin(
const this_type &v) {
return v.origin; }
364 static value_type access(
const origin_type *o,
const const_iterator &it,
366 {
return it.r * (linalg_traits<V>::access(o, it.it, ite.it, i)); }
371 template <
typename M,
typename S>
struct linalg_traits<scaled_row_matrix_const_ref<M,S> > {
372 typedef scaled_row_matrix_const_ref<M,S> this_type;
373 typedef linalg_const is_reference;
374 typedef abstract_matrix linalg_type;
375 typedef typename linalg_traits<M>::origin_type origin_type;
376 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
377 typedef value_type reference;
378 typedef typename linalg_traits<M>::storage_type storage_type;
379 typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
380 typedef scaled_vector_const_ref<vector_type,S> sub_row_type;
381 typedef scaled_vector_const_ref<vector_type,S> const_sub_row_type;
382 typedef scaled_row_const_iterator<M,S> row_iterator;
383 typedef scaled_row_const_iterator<M,S> const_row_iterator;
384 typedef abstract_null_type const_sub_col_type;
385 typedef abstract_null_type sub_col_type;
386 typedef abstract_null_type const_col_iterator;
387 typedef abstract_null_type col_iterator;
388 typedef row_major sub_orientation;
389 typedef typename linalg_traits<M>::index_sorted index_sorted;
390 static size_type nrows(
const this_type &m)
392 static size_type ncols(
const this_type &m)
394 static const_sub_row_type row(
const const_row_iterator &it)
395 {
return scaled(linalg_traits<M>::row(it.it), it.r); }
396 static const_row_iterator row_begin(
const this_type &m)
397 {
return const_row_iterator(m.begin_, m.r); }
398 static const_row_iterator row_end(
const this_type &m)
399 {
return const_row_iterator(m.end_, m.r); }
400 static const origin_type* origin(
const this_type &m) {
return m.origin; }
401 static value_type access(
const const_row_iterator &it,
size_type i)
402 {
return it.r * (linalg_traits<M>::access(it.it, i)); }
405 template <
typename M,
typename S>
struct linalg_traits<scaled_col_matrix_const_ref<M,S> > {
406 typedef scaled_col_matrix_const_ref<M,S> this_type;
407 typedef linalg_const is_reference;
408 typedef abstract_matrix linalg_type;
409 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
410 typedef typename linalg_traits<M>::origin_type origin_type;
411 typedef value_type reference;
412 typedef typename linalg_traits<M>::storage_type storage_type;
413 typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
414 typedef abstract_null_type sub_col_type;
415 typedef scaled_vector_const_ref<vector_type,S> const_sub_col_type;
416 typedef abstract_null_type col_iterator;
417 typedef scaled_col_const_iterator<M,S> const_col_iterator;
418 typedef abstract_null_type const_sub_row_type;
419 typedef abstract_null_type sub_row_type;
420 typedef abstract_null_type const_row_iterator;
421 typedef abstract_null_type row_iterator;
422 typedef col_major sub_orientation;
423 typedef typename linalg_traits<M>::index_sorted index_sorted;
424 static size_type ncols(
const this_type &m)
426 static size_type nrows(
const this_type &m)
428 static const_sub_col_type col(
const const_col_iterator &it)
429 {
return scaled(linalg_traits<M>::col(it.it), it.r); }
430 static const_col_iterator col_begin(
const this_type &m)
431 {
return const_col_iterator(m.begin_, m.r); }
432 static const_col_iterator col_end(
const this_type &m)
433 {
return const_col_iterator(m.end_, m.r); }
434 static const origin_type* origin(
const this_type &m) {
return m.origin; }
435 static value_type access(
const const_col_iterator &it,
size_type i)
436 {
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