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); }
93 template <
typename V,
typename S>
struct scaled_vector_const_ref {
94 typedef scaled_vector_const_ref<V,S> this_type;
95 typedef typename linalg_traits<this_type>::value_type value_type;
96 typedef typename linalg_traits<V>::const_iterator iterator;
97 typedef typename linalg_traits<this_type>::reference reference;
98 typedef typename linalg_traits<this_type>::origin_type origin_type;
100 iterator begin_, end_;
101 const origin_type *origin;
105 scaled_vector_const_ref(
const V &v, S rr)
106 : begin_(vect_const_begin(v)), end_(vect_const_end(v)),
107 origin(linalg_origin(v)), size_(vect_size(v)), r(rr) {}
110 {
return value_type(r) * linalg_traits<V>::access(origin, begin_, end_, i); }
114 template<
typename V,
typename S> std::ostream &
operator <<
115 (std::ostream &o,
const scaled_vector_const_ref<V,S>& m)
116 { gmm::write(o,m);
return o; }
122 template <
typename M,
typename S>
struct scaled_row_const_iterator {
123 typedef scaled_row_const_iterator<M,S> iterator;
124 typedef typename linalg_traits<M>::const_row_iterator ITER;
125 typedef ptrdiff_t difference_type;
131 inline iterator operator ++(
int) { iterator tmp=*
this; it++;
return tmp; }
132 inline iterator operator --(
int) { iterator tmp=*
this; it--;
return tmp; }
133 inline iterator &operator ++() { it++;
return *
this; }
134 inline iterator &operator --() { it--;
return *
this; }
135 iterator &operator +=(difference_type i) { it += i;
return *
this; }
136 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
138 { iterator itt = *
this;
return (itt += i); }
140 { iterator itt = *
this;
return (itt -= i); }
141 difference_type
operator -(
const iterator &i)
const
142 {
return it - i.it; }
144 inline ITER operator *()
const {
return it; }
145 inline ITER operator [](
int i) {
return it + i; }
147 inline bool operator ==(
const iterator &i)
const {
return (it == i.it); }
148 inline bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
149 inline bool operator < (
const iterator &i)
const {
return (it < i.it); }
151 scaled_row_const_iterator(
void) {}
152 scaled_row_const_iterator(
const ITER &i, S rr)
157 template <
typename M,
typename S>
struct scaled_row_matrix_const_ref {
159 typedef scaled_row_matrix_const_ref<M,S> this_type;
160 typedef typename linalg_traits<M>::const_row_iterator iterator;
161 typedef typename linalg_traits<this_type>::value_type value_type;
162 typedef typename linalg_traits<this_type>::origin_type origin_type;
164 iterator begin_, end_;
165 const origin_type *origin;
169 scaled_row_matrix_const_ref(
const M &m, S rr)
170 : begin_(mat_row_begin(m)), end_(mat_row_end(m)),
171 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
174 {
return r * linalg_traits<M>::access(begin_+i, j); }
178 template<
typename M,
typename S> std::ostream &
operator <<
179 (std::ostream &o,
const scaled_row_matrix_const_ref<M,S>& m)
180 { gmm::write(o,m);
return o; }
183 template <
typename M,
typename S>
struct scaled_col_const_iterator {
184 typedef scaled_col_const_iterator<M,S> iterator;
185 typedef typename linalg_traits<M>::const_col_iterator ITER;
186 typedef ptrdiff_t difference_type;
192 iterator operator ++(
int) { iterator tmp = *
this; it++;
return tmp; }
193 iterator operator --(
int) { iterator tmp = *
this; it--;
return tmp; }
194 iterator &operator ++() { it++;
return *
this; }
195 iterator &operator --() { it--;
return *
this; }
196 iterator &operator +=(difference_type i) { it += i;
return *
this; }
197 iterator &operator -=(difference_type i) { it -= i;
return *
this; }
199 { iterator itt = *
this;
return (itt += i); }
201 { iterator itt = *
this;
return (itt -= i); }
202 difference_type
operator -(
const iterator &i)
const
203 {
return it - i.it; }
205 ITER operator *()
const {
return it; }
206 ITER operator [](
int i) {
return it + i; }
208 bool operator ==(
const iterator &i)
const {
return (it == i.it); }
209 bool operator !=(
const iterator &i)
const {
return !(i == *
this); }
210 bool operator < (
const iterator &i)
const {
return (it < i.it); }
212 scaled_col_const_iterator(
void) {}
213 scaled_col_const_iterator(
const ITER &i, S rr)
218 template <
typename M,
typename S>
struct scaled_col_matrix_const_ref {
220 typedef scaled_col_matrix_const_ref<M,S> this_type;
221 typedef typename linalg_traits<M>::const_col_iterator iterator;
222 typedef typename linalg_traits<this_type>::value_type value_type;
223 typedef typename linalg_traits<this_type>::origin_type origin_type;
225 iterator begin_, end_;
226 const origin_type *origin;
230 scaled_col_matrix_const_ref(
const M &m, S rr)
231 : begin_(mat_col_begin(m)), end_(mat_col_end(m)),
232 origin(linalg_origin(m)), r(rr), nr(mat_nrows(m)), nc(mat_ncols(m)) {}
235 {
return r * linalg_traits<M>::access(begin_+j, i); }
240 template<
typename M,
typename S> std::ostream &
operator <<
241 (std::ostream &o,
const scaled_col_matrix_const_ref<M,S>& m)
242 { gmm::write(o,m);
return o; }
245 template <
typename L,
typename S,
typename R>
struct scaled_return__ {
246 typedef abstract_null_type return_type;
248 template <
typename L,
typename S>
struct scaled_return__<L, S, row_major>
249 {
typedef scaled_row_matrix_const_ref<L,S> return_type; };
250 template <
typename L,
typename S>
struct scaled_return__<L, S, col_major>
251 {
typedef scaled_col_matrix_const_ref<L,S> return_type; };
254 template <
typename L,
typename S,
typename LT>
struct scaled_return_ {
255 typedef abstract_null_type return_type;
257 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_vector>
258 {
typedef scaled_vector_const_ref<L,S> return_type; };
259 template <
typename L,
typename S>
struct scaled_return_<L, S, abstract_matrix> {
260 typedef typename scaled_return__<L, S,
261 typename principal_orientation_type<
typename
262 linalg_traits<L>::sub_orientation>::potype>::return_type return_type;
265 template <
typename L,
typename S>
struct scaled_return {
266 typedef typename scaled_return_<L, S,
typename
267 linalg_traits<L>::linalg_type>::return_type return_type;
270 template <
typename L,
typename S>
inline
271 typename scaled_return<L,S>::return_type
272 scaled(
const L &v, S x)
273 {
return scaled(v, x,
typename linalg_traits<L>::linalg_type()); }
275 template <
typename V,
typename S>
inline
276 typename scaled_return<V,S>::return_type
277 scaled(
const V &v, S x, abstract_vector)
278 {
return scaled_vector_const_ref<V,S>(v, x); }
280 template <
typename M,
typename S>
inline
281 typename scaled_return<M,S>::return_type
282 scaled(
const M &m, S x,abstract_matrix) {
283 return scaled(m, x,
typename principal_orientation_type<
typename
284 linalg_traits<M>::sub_orientation>::potype());
287 template <
typename M,
typename S>
inline
288 typename scaled_return<M,S>::return_type
289 scaled(
const M &m, S x, row_major) {
290 return scaled_row_matrix_const_ref<M,S>(m, x);
293 template <
typename M,
typename S>
inline
294 typename scaled_return<M,S>::return_type
295 scaled(
const M &m, S x, col_major) {
296 return scaled_col_matrix_const_ref<M,S>(m, x);
304 template <
typename L>
inline
305 void scale(L& l,
typename linalg_traits<L>::value_type a)
306 { scale(l, a,
typename linalg_traits<L>::linalg_type()); }
308 template <
typename L>
inline
309 void scale(
const L& l,
typename linalg_traits<L>::value_type a)
310 { scale(linalg_const_cast(l), a); }
312 template <
typename L>
inline
313 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_vector) {
314 typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
315 for ( ; it != ite; ++it) *it *= a;
318 template <
typename L>
319 void scale(L& l,
typename linalg_traits<L>::value_type a, abstract_matrix) {
320 scale(l, a,
typename principal_orientation_type<
typename
321 linalg_traits<L>::sub_orientation>::potype());
324 template <
typename L>
325 void scale(L& l,
typename linalg_traits<L>::value_type a, row_major) {
326 typename linalg_traits<L>::row_iterator it = mat_row_begin(l),
327 ite = mat_row_end(l);
328 for ( ; it != ite; ++it) scale(linalg_traits<L>::row(it), a);
331 template <
typename L>
332 void scale(L& l,
typename linalg_traits<L>::value_type a, col_major) {
333 typename linalg_traits<L>::col_iterator it = mat_col_begin(l),
334 ite = mat_col_end(l);
335 for ( ; it != ite; ++it) scale(linalg_traits<L>::col(it), a);
338 template <
typename V,
typename S>
struct linalg_traits<scaled_vector_const_ref<V,S> > {
339 typedef scaled_vector_const_ref<V,S> this_type;
340 typedef linalg_const is_reference;
341 typedef abstract_vector linalg_type;
342 typedef typename strongest_numeric_type<S, typename linalg_traits<V>::value_type>::T value_type;
343 typedef typename linalg_traits<V>::origin_type origin_type;
344 typedef value_type reference;
345 typedef abstract_null_type iterator;
346 typedef scaled_const_iterator<typename linalg_traits<V>::const_iterator, S>
348 typedef typename linalg_traits<V>::storage_type storage_type;
349 typedef typename linalg_traits<V>::index_sorted index_sorted;
350 static size_type size(
const this_type &v) {
return v.size_; }
351 static const_iterator begin(
const this_type &v)
352 {
return const_iterator(v.begin_, v.r); }
353 static const_iterator end(
const this_type &v)
354 {
return const_iterator(v.end_, v.r); }
355 static const origin_type* origin(
const this_type &v) {
return v.origin; }
356 static value_type access(
const origin_type *o,
const const_iterator &it,
358 {
return it.r * (linalg_traits<V>::access(o, it.it, ite.it, i)); }
363 template <
typename M,
typename S>
struct linalg_traits<scaled_row_matrix_const_ref<M,S> > {
364 typedef scaled_row_matrix_const_ref<M,S> this_type;
365 typedef linalg_const is_reference;
366 typedef abstract_matrix linalg_type;
367 typedef typename linalg_traits<M>::origin_type origin_type;
368 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
369 typedef value_type reference;
370 typedef typename linalg_traits<M>::storage_type storage_type;
371 typedef typename org_type<typename linalg_traits<M>::const_sub_row_type>::t vector_type;
372 typedef scaled_vector_const_ref<vector_type,S> sub_row_type;
373 typedef scaled_vector_const_ref<vector_type,S> const_sub_row_type;
374 typedef scaled_row_const_iterator<M,S> row_iterator;
375 typedef scaled_row_const_iterator<M,S> const_row_iterator;
376 typedef abstract_null_type const_sub_col_type;
377 typedef abstract_null_type sub_col_type;
378 typedef abstract_null_type const_col_iterator;
379 typedef abstract_null_type col_iterator;
380 typedef row_major sub_orientation;
381 typedef typename linalg_traits<M>::index_sorted index_sorted;
382 static size_type nrows(
const this_type &m)
384 static size_type ncols(
const this_type &m)
386 static const_sub_row_type row(
const const_row_iterator &it)
387 {
return scaled(linalg_traits<M>::row(it.it), it.r); }
388 static const_row_iterator row_begin(
const this_type &m)
389 {
return const_row_iterator(m.begin_, m.r); }
390 static const_row_iterator row_end(
const this_type &m)
391 {
return const_row_iterator(m.end_, m.r); }
392 static const origin_type* origin(
const this_type &m) {
return m.origin; }
393 static value_type access(
const const_row_iterator &it,
size_type i)
394 {
return it.r * (linalg_traits<M>::access(it.it, i)); }
397 template <
typename M,
typename S>
struct linalg_traits<scaled_col_matrix_const_ref<M,S> > {
398 typedef scaled_col_matrix_const_ref<M,S> this_type;
399 typedef linalg_const is_reference;
400 typedef abstract_matrix linalg_type;
401 typedef typename strongest_numeric_type<S, typename linalg_traits<M>::value_type>::T value_type;
402 typedef typename linalg_traits<M>::origin_type origin_type;
403 typedef value_type reference;
404 typedef typename linalg_traits<M>::storage_type storage_type;
405 typedef typename org_type<typename linalg_traits<M>::const_sub_col_type>::t vector_type;
406 typedef abstract_null_type sub_col_type;
407 typedef scaled_vector_const_ref<vector_type,S> const_sub_col_type;
408 typedef abstract_null_type col_iterator;
409 typedef scaled_col_const_iterator<M,S> const_col_iterator;
410 typedef abstract_null_type const_sub_row_type;
411 typedef abstract_null_type sub_row_type;
412 typedef abstract_null_type const_row_iterator;
413 typedef abstract_null_type row_iterator;
414 typedef col_major sub_orientation;
415 typedef typename linalg_traits<M>::index_sorted index_sorted;
416 static size_type ncols(
const this_type &m)
418 static size_type nrows(
const this_type &m)
420 static const_sub_col_type col(
const const_col_iterator &it)
421 {
return scaled(linalg_traits<M>::col(it.it), it.r); }
422 static const_col_iterator col_begin(
const this_type &m)
423 {
return const_col_iterator(m.begin_, m.r); }
424 static const_col_iterator col_end(
const this_type &m)
425 {
return const_col_iterator(m.end_, m.r); }
426 static const origin_type* origin(
const this_type &m) {
return m.origin; }
427 static value_type access(
const const_col_iterator &it,
size_type i)
428 {
return it.r * (linalg_traits<M>::access(it.it, i)); }
434 #endif // GMM_SCALED_H__