File: rational-reconstruction-base.h

package info (click to toggle)
linbox 1.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,640 kB
  • sloc: cpp: 106,991; lisp: 5,469; makefile: 1,293; sh: 1,049; csh: 95; python: 74; perl: 2
file content (370 lines) | stat: -rw-r--r-- 9,391 bytes parent folder | download | duplicates (3)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* linbox/blackbox/rational-reconstruction-base.h
 * Copyright (C) 2009 Anna Marszalek
 *
 * Written by Anna Marszalek <aniau@astronet.pl>
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
  * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

#ifndef __LINBOX_reconstruction_base_H
#define __LINBOX_reconstruction_base_H

#define DEF_RR_THRESH  1

#include <iostream>
#include <deque>
#include <cmath>

#include <givaro/zring.h>

namespace LinBox
{

	enum RReconstructionSchedule {
		INCREMENTAL, QUADRATIC, GEOMETRIC, CERTIFIED
	};

	template <class Ring=Givaro::ZRing<Integer> >
	class RReconstructionBase;

	/*
	 * Class to be used for repeated rational reconstruction in schemes such as Rational CRA and p-adic lifting
	 * together with method reconstructRational(a,b,x,m)
	 * implements scheduling INCREMENTAL, QUADRATIC, GEOMETRIC, CERTIFIED
	 * implements vector reconstruction
	 * _intRing is the integer ring used for reconstruction, default Givaro::ZRing<Integer>
	 * _RR is the rational reconstruction method, see fast-ratioinal-reconstruction.h, classic-rational-reconstruction.h
	 * THRESHOLD_ - treshold for INCREMENTAL schedule
	 * rbound_ - min number of iterations for all schedule
	 */
	template <class Ring=Givaro::ZRing<Integer>, class RRBase=RReconstructionBase<Givaro::ZRing<Integer> > >
	struct RReconstruction {
	protected:
		Ring _intRing;
		RRBase _RR;
		mutable size_t RecCounter;
		const RReconstructionSchedule  _meth;
		const size_t THRESHOLD_;
		const size_t rbound_;
	public:
		typedef typename Ring::Element Element;

		RReconstruction(const Ring& Z=Ring(), const RReconstructionSchedule Meth = GEOMETRIC, size_t T=DEF_RR_THRESH, size_t b= 0) :
			_intRing(Z), _RR(Z), _meth(Meth), THRESHOLD_(T), rbound_(b)
		{
			RecCounter =0;
			if (_meth == QUADRATIC) {
				RecCounter = (size_t)sqrt((double)rbound_);//RecCounter^2 < rbound_ <=(RecCounter+1)^2
			}
			else if (_meth == GEOMETRIC) {
				if (rbound_!=0)
					RecCounter = (size_t) log((double)rbound_) ;//2^RecCounter < rbound_ <=2^(RecCounter+1)
			}
		}

		RReconstruction(const RRBase& RR, const RReconstructionSchedule Meth = GEOMETRIC, size_t T=DEF_RR_THRESH, size_t b = 0) :
			_intRing(RR._intRing), _RR(RR),_meth(Meth), THRESHOLD_(T), rbound_(b)
		{
			RecCounter =0;
			if (_meth == QUADRATIC) {
				RecCounter = (size_t)sqrt((double)rbound_);//RecCounter^2 < rbound_ <=(RecCounter+1)^2
			}
			else if (_meth == GEOMETRIC) {
				if (rbound_!=0)
					RecCounter = (size_t)((double)log((double)rbound_)/log(2.));//2^RecCounter < rbound_ <=2^(RecCounter+1)
			}

		}

		int getCounter()
		{
		       	return RecCounter;
		}

		bool scheduled(const size_t i) const
		{
			//if (RecCounter ==0)  return true;
			if (i < rbound_) return false; //skip first rbound iterations
			if (_meth == INCREMENTAL) {
				if (RecCounter%THRESHOLD_==0 ) return true;
				else return false;
			}
			else if (_meth == QUADRATIC) {
				if (RecCounter*RecCounter < i) return true;
				else return false;
			}
			else if (_meth == GEOMETRIC) {
				if ((1UL << RecCounter) < i) return true;
				else return false;
			}
			else if (_meth == CERTIFIED) {
				if ( i > rbound_) return true;
				else return false;
			}
			return true;
		}

		template <class Vect>
		bool reconstructRational(Vect& a, Element& b, const Vect& x, const Element m, const int inc = 1) const
		{
			++RecCounter;
			b = 1;
			if (a.size() != x.size()) return false;
			typename Vect::iterator it_a,it2_a;
			typename Vect::const_iterator it_x;
			bool res = true;
			Element new_den, old_den;
			old_den = 1;
			if (inc==1) {
				for (it_a = a.begin(), it_x = x.begin(); it_a != a.end(); ++it_a, ++it_x) {
					//use previous modula
					Element x_in(*it_x);
					x_in *=old_den;
					if (x_in <0) {
						if ((-x_in) > m) x_in %= m;
						if (x_in < 0) x_in += m;
					}
					else {
						if (x_in > m) x_in %= m;
					}
					if (x_in > 0) res = res && _RR.reconstructRational(*it_a, new_den,x_in,m);
					else {
						res = true;
						*it_a = 0;
						new_den = 1;
					}
					if (!res) return res;
					else {
						if (new_den > 1) {
							for (it2_a = a.begin(); it2_a != it_a; ++it2_a) {
								*it2_a *=new_den;
							}
							b *= new_den;
							old_den *= new_den;
						}
					}

				}
			}
			else {//if (inc == -1)
				int i = (int)x.size()-1;
				for (; i >=0; --i ) {
					Element x_in(x[(size_t)i]);
					x_in *=old_den;
					if (x_in <0) {
						if ((-(x_in)) > m) x_in %= m;
						if (x_in < 0) x_in += m;
					}
					else {
						if (x_in > m) x_in %= m;
					}
					if (x_in > 0) res = res && _RR.reconstructRational(a[(size_t)i], new_den,x_in,m);
					else {
						res = true;
						*it_a = 0;
						new_den = 1;
					}
					if (!res) return res;
					else {
						//std::cout << a[(size_t)i] << "/" << b*new_den << "\n";
						if (new_den > 1) {
							for (int j = (int)a.size()-1; j > i ; --j) {
								a[(size_t)j] *=new_den;
							}
							b *= new_den;
							old_den *= new_den;
						}
					}
				}
			}
#if 0
			else if (inc==0) {//no prec
			}
#endif
			return res;

		}

		bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m) const
		{
			++RecCounter;
			Element x_in(x);
			if (x<0) {
				if ((-x)>m)
					x_in %= m;
				if (x<0)
					x_in += m;
			}
			else {
				if (x>m)
					x_in %= m;
			}

			bool res;
			if (x_in >0) res = _RR.reconstructRational(a,b,x_in,m);
			else { a = 0; b =1; res = true;}
			//_RR.write(std::cout);
			return res;
		}

		bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m, const Element& a_bound) const
		{
			++RecCounter;
			Element x_in(x);
			if (x<0) {
				if ((-x)>m)
					x_in %= m;
				if (x<0)
					x_in += m;
			}
			else {
				if (x>m)
					x_in %= m;
			}
			bool res;
			if (x_in >0) res = _RR.reconstructRational(a,b,x_in,m,a_bound);
			else { a = 0; b =1; res = true;}
			//_RR.write(std::cout);
			return res;
		}

		bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m, const Element& a_bound, const Element& b_bound) const
		{
			++RecCounter;
			Element x_in(x);
			if (x<0) {
				if ((-x)>m)
					x_in %= m;
				if (x<0)
					x_in += m;
			}
			else {
				if (x>m)
					x_in %= m;
			}
			Element bound = x_in/b_bound;
			if (x_in > 0) _RR.reconstructRational(a,b,x_in,m,(bound>a_bound?bound:a_bound));
			else  { a = 0; b =1; }
			bool res=  (b > b_bound)? false: true;
			//_RR.write(std::cout);
			return res;
		}

		//fastReconstruction();

		//classicReconstruction();

	};

	class OpCounter {
	public:
		size_t div_counter;
		size_t mul_counter;
		size_t gcd_counter;

		OpCounter() {
			div_counter=0; mul_counter=0; gcd_counter=0;
		}

		void write(std::ostream& is) {
			is << div_counter << " divisions\n";
			is << mul_counter << " multiplications\n";
			is << gcd_counter << " gcds\n";
		}
	};

	template <class Ring>
	class RReconstructionBase {
	public:
		Ring _intRing;
		mutable OpCounter C;
		typedef typename Ring::Element Element;

		RReconstructionBase(const Ring& Z) :
			_intRing(Z)
		{}
		RReconstructionBase(const RReconstructionBase<Ring>& RR) :
			_intRing(RR._intRing)
		{}

		virtual bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m) const =0;

		virtual bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m, const Element& a_bound) const =0;

		virtual ~RReconstructionBase() {}

		void write(std::ostream& is) const
		{
			C.write(is);
		}
	};

	/*
	 * This is the default RReconstruction, using ZRing<Integer> and ClassicRationalReconstruction of Wang
	 */
	template <>
	class RReconstructionBase<Givaro::ZRing<Integer> > {
	public:
		typedef Givaro::ZRing<Integer> Ring;
		Ring _intRing;
		mutable OpCounter C;
		typedef Ring::Element Element;

		RReconstructionBase(const Ring& Z) :
			_intRing(Z)
		{}
		RReconstructionBase(const RReconstructionBase<Ring>& RR) :
			_intRing(RR._intRing)
		{}

		bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m)
		{
			Element a_bound; _intRing.sqrt(a_bound,m/2);
			return _intRing.reconstructRational(a,b,x,m,a_bound,a_bound);
		}

		bool reconstructRational(Element& a, Element& b, const Element& x, const Element& m, const Element& a_bound)
		{
			_intRing.reconstructRational(a,b,x,m,a_bound);
			return true;
		}

		~RReconstructionBase() {}
#if 0
		const void write(std::ostream& is) {
			C.write(is);
		}
#endif
	};

} //namespace LinBox

#undef DEF_RR_THRESH
#endif



// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End: