-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdiv.h
343 lines (314 loc) · 10.2 KB
/
tdiv.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* tdiv.h
*
* Created on: 1 июн. 2019 г.
* Author: Alexey Kozin
*
* Please report bugs to tpptensor@mail.ru
*/
#ifndef TDIV_H_
#define TDIV_H_
#include <tvalence.h>
#include <blas_tmpl.h>
#include <smplmath.h>
namespace iTTL
{
template <typename T, typename STR, typename STA, typename VD, typename BASE_LOOP>
struct div_suma_loop
{
};
template <typename T, typename STR, typename STA, typename VD, typename BASE_LOOP>
struct div_ra_loop: public BASE_LOOP
{
private:
public:
typedef typename vd_iterator_getter<STR, VD, 0>::template iterator_type<T> ritype;
typedef typename vd_iterator_getter<STA, VD, 1>::template iterator_type<T> aitype;
div_ra_loop(const STR& str, const STA& sta): BASE_LOOP(str, sta)
{}
div_ra_loop(const STR& str, const STA& sta, T alpha): BASE_LOOP(str, sta, alpha)
{}
void run_sign(T *r, const T *a)
{
ritype rit(this->str, r);
aitype ait(this->sta, a);
for (;rit.not_end(r);rit.move_one(r),ait.move_one(a))
BASE_LOOP::run_sign(r,a);
}
void run_div(T *r, const T *a)
{
ritype rit(this->str, r);
aitype ait(this->sta, a);
for (;rit.not_end(r);rit.move_one(r),ait.move_one(a))
BASE_LOOP::run_div(r,a);
}
void run_scal(T *r, const T *a)
{
ritype rit(this->str, r);
aitype ait(this->sta, a);
for (;rit.not_end(r);rit.move_one(r),ait.move_one(a))
BASE_LOOP::run_scal(r,a);
}
void spread(const T *r0, T *ri)
{
ritype rit(this->str, ri);
for (;rit.not_end(ri);rit.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
};
template <typename T, typename STR, typename STA>
struct div_common_loop
{
const STR& str;
const STA& sta;
const T alpha;
public:
div_common_loop(const STR& str, const STA& sta): str(str), sta(sta), alpha(1)
{}
div_common_loop(const STR& str, const STA& sta, T alpha): str(str), sta(sta), alpha(alpha)
{}
void run_sign(T *r, const T *a)
{
*r=sign<T>(*a);
}
void run_div(T *r, const T *a)
{
*r/=*a;
}
void run_scal(T *r, const T *a)
{
*r*=*a;
}
void spread(const T *r0, T *ri)
{
*ri=*r0;
}
};
template <typename T, typename STR, typename STA, typename ITTYPE, bool IS_FIRST, typename BASE_LOOP>
struct div_spread_loop;
// LAST; NOT FIRST
template <typename T, typename STR, typename STA, typename ITTYPE, typename BASE_LOOP>
struct div_spread_loop<T, STR, STA, ITTYPE, false, BASE_LOOP>: public BASE_LOOP
{
private:
public:
div_spread_loop(const STR& str, const STA& sta): BASE_LOOP(str, sta)
{}
div_spread_loop(const STR& str, const STA& sta, T alpha): BASE_LOOP(str, sta, alpha)
{}
void spread_first(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
if (!it.not_end(ri))
return;
it.move_one(ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void spread(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
};
// LAST; FIRST
template <typename T, typename STR, typename STA, typename ITTYPE, typename BASE_LOOP>
struct div_spread_loop<T, STR, STA, ITTYPE, true, BASE_LOOP>: public BASE_LOOP
{
private:
public:
div_spread_loop(const STR& str, const STA& sta): BASE_LOOP(str, sta)
{}
div_spread_loop(const STR& str, const STA& sta, T alpha): BASE_LOOP(str, sta, alpha)
{}
void spread_first(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
if (!it.not_end(ri))
return;
it.move_one(ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void spread(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void run_sign(T *r, const T *a)
{
T *r0=r;
T *ri=r;
BASE_LOOP::run_sign(r,a);
ri=r0;
spread_first(r0, ri);
}
void run_div(T *r, const T *a)
{
ITTYPE it(this->str,r);
for (;it.not_end(r);it.move_one(r))
BASE_LOOP::run_div(r,a);
}
void run_scal(T *r, const T *a)
{
ITTYPE it(this->str,r);
for (;it.not_end(r);it.move_one(r))
BASE_LOOP::run_scal(r,a);
}
};
// NOT LAST; NOT FIRST
template <typename T, typename STR, typename STA, typename ITTYPE, typename HEAD, typename BASE_BASE_LOOP>
struct div_spread_loop<T, STR, STA, ITTYPE, false, div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP> >: public div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP>
{
private:
typedef div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP> BASE_LOOP;
public:
div_spread_loop(const STR& str, const STA& sta): BASE_LOOP(str, sta)
{}
div_spread_loop(const STR& str, const STA& sta, T alpha): BASE_LOOP(str, sta, alpha)
{}
void spread_first(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
if (!it.not_end(ri))
return;
BASE_LOOP::spread_first(r0,ri);
it.move_one(ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void spread(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
};
// NOT LAST; FIRST
template <typename T, typename STR, typename STA, typename ITTYPE, typename HEAD, typename BASE_BASE_LOOP>
struct div_spread_loop<T, STR, STA, ITTYPE, true, div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP> >: public div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP>
{
private:
typedef div_spread_loop<T, STR, STA, HEAD, false, BASE_BASE_LOOP> BASE_LOOP;
public:
div_spread_loop(const STR& str, const STA& sta): BASE_LOOP(str, sta)
{}
div_spread_loop(const STR& str, const STA& sta, T alpha): BASE_LOOP(str, sta, alpha)
{}
void spread_first(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
if (!it.not_end(ri))
return;
BASE_LOOP::spread_first(r0,ri);
it.move_one(ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void spread(const T *r0, T *ri)
{
ITTYPE it(this->str,ri);
for (;it.not_end(ri);it.move_one(ri))
BASE_LOOP::spread(r0,ri);
}
void run_sign(T *r, const T *a)
{
T *r0=r;
T *ri=r;
BASE_LOOP::run_sign(r,a);
ri=r0;
spread_first(r0, ri);
}
void run_div(T *r, const T *a)
{
ITTYPE it(this->str,r);
for (;it.not_end(r);it.move_one(r))
BASE_LOOP::run_div(r,a);
}
void run_scal(T *r, const T *a)
{
ITTYPE it(this->str,r);
for (;it.not_end(r);it.move_one(r))
BASE_LOOP::run_scal(r,a);
}
};
template <typename T, typename STR, typename STA, typename SPREAD, typename SUMA, typename RA, typename BASE_LOOP>
struct div_create_general_loop
{
using type=BASE_LOOP;
};
template <typename T, typename STR, typename STA, typename SPREAD, typename HEAD, typename ... SUMA, typename RA, typename BASE_LOOP>
struct div_create_general_loop<T, STR, STA, SPREAD, type_sequence<HEAD, SUMA...>, RA, BASE_LOOP>:
public div_create_general_loop<T, STR, STA, SPREAD, type_sequence<SUMA...>, RA, div_suma_loop<T, STR, STA, HEAD, BASE_LOOP> >
{
};
template <typename T, typename STR, typename STA, typename SPREAD, typename HEAD, typename ... RA, typename BASE_LOOP>
struct div_create_general_loop<T, STR, STA, SPREAD, type_sequence<>, type_sequence<HEAD, RA...>, BASE_LOOP>:
public div_create_general_loop<T, STR, STA, SPREAD, type_sequence<>, type_sequence<RA...>, div_ra_loop<T, STR, STA, HEAD, BASE_LOOP> >
{
};
template <typename T, typename STR, typename STA, typename HEAD, typename ... SPREAD, typename BASE_LOOP>
struct div_create_general_loop<T, STR, STA, type_sequence<HEAD, SPREAD...>, type_sequence<>, type_sequence<>, BASE_LOOP>:
public div_create_general_loop<T, STR, STA, type_sequence<SPREAD...>, type_sequence<>, type_sequence<>, div_spread_loop<T, STR, STA, typename vd_iterator_getter<STR, HEAD, 0>::template iterator_type<T>, false, BASE_LOOP> >
{
};
template <typename T, typename STR, typename STA, typename HEAD, typename BASE_LOOP>
struct div_create_general_loop<T, STR, STA, type_sequence<HEAD>, type_sequence<>, type_sequence<>, BASE_LOOP>:
public div_create_general_loop<T, STR, STA, type_sequence<>, type_sequence<>, type_sequence<>, div_spread_loop<T, STR, STA, typename vd_iterator_getter<STR, HEAD, 0>::template iterator_type<T>, true, BASE_LOOP> >
{
};
template <typename T, typename STR, typename STA, typename VD>
struct div_just_spread_loop
{
const STR& str;
const STA& sta;
const T alpha;
private:
BLAS_INTEGER l;
BLAS_INTEGER s;
public:
using ittype=typename vd_iterator_getter<STR, VD, 0>::template iterator_type<T>;
public:
div_just_spread_loop(const STR& str, const STA& sta): str(str), sta(sta), alpha(1),
l(ittype::length(str)), s(ittype::step(str))
{}
div_just_spread_loop(const STR& str, const STA& sta, T alpha): str(str), sta(sta), alpha(alpha),
l(ittype::length(str)), s(ittype::step(str))
{}
void run_sign(T *r, const T *a)
{
T v=sign<T>(*a);
copy<T>(&l, &v, &type_constants<BLAS_INTEGER>::zero, r, &s);
}
void run_div(T *r, const T *a)
{
T alpha=1.0/(*a);
scal<T>(&l,&alpha,r,&s);
}
void run_scal(T *r, const T *a)
{
scal<T>(&l,a,r,&s);
}
void spread(const T *r0, T *ri)
{
copy<T>(&l, r0, &s, ri, &s);
}
};
template <typename T, typename STR, typename STA, typename VD>
struct div_runner;
// DONE just spread
template <typename T, typename STR, typename STA, typename CONT_MASK, int S_V_TYPE, int S_C_MASK, int S_NEXT_VALENCE, size_t ... S_POS, typename ... M10, typename ... M1>
struct div_runner<T, STR, STA, type_sequence<CONT_MASK, type_sequence<type_sequence<valence_data<S_V_TYPE, 1, S_C_MASK, 1, S_NEXT_VALENCE, S_POS...>, M10...>, M1...>, type_sequence<>, type_sequence<>, type_sequence<>, type_sequence<>, type_sequence<>, type_sequence<> > >:
public div_create_general_loop<T, STR, STA, type_sequence<M1...>, type_sequence<>, type_sequence<>, div_just_spread_loop<T, STR, STA, type_sequence<valence_data<S_V_TYPE, 1, S_C_MASK, 1, S_NEXT_VALENCE, S_POS...>, M10...> > >
{
};
// DONE COMMON
template <typename T, typename STR, typename STA, typename CONT_MASK, typename ... SPREAD, typename ... SUMA, typename ... RA>
struct div_runner<T, STR, STA, type_sequence<CONT_MASK, type_sequence<SPREAD...>, type_sequence<SUMA...>, type_sequence<RA...>, type_sequence<>, type_sequence<>, type_sequence<>, type_sequence<> > >:
public div_create_general_loop<T, STR, STA, type_sequence<SPREAD...>, type_sequence<SUMA...>, type_sequence<RA...>, div_common_loop<T, STR, STA> >
{
};
};
#endif /* TDIV_H_ */