root / pobysoPythonSage / src / sageSLZ / sageRunSLZ.sage @ 194
Historique | Voir | Annoter | Télécharger (41,71 ko)
1 | 194 | storres | """ |
---|---|---|---|
2 | 194 | storres | SLZ runtime function. |
3 | 194 | storres | """ |
4 | 194 | storres | |
5 | 194 | storres | def srs_run_SLZ_v01(inputFunction, |
6 | 194 | storres | inputLowerBound, |
7 | 194 | storres | inputUpperBound, |
8 | 194 | storres | alpha, |
9 | 194 | storres | degree, |
10 | 194 | storres | precision, |
11 | 194 | storres | emin, |
12 | 194 | storres | emax, |
13 | 194 | storres | targetHardnessToRound, |
14 | 194 | storres | debug = False): |
15 | 194 | storres | |
16 | 194 | storres | if debug: |
17 | 194 | storres | print "Function :", inputFunction |
18 | 194 | storres | print "Lower bound :", inputLowerBound |
19 | 194 | storres | print "Upper bounds :", inputUpperBound |
20 | 194 | storres | print "Alpha :", alpha |
21 | 194 | storres | print "Degree :", degree |
22 | 194 | storres | print "Precision :", precision |
23 | 194 | storres | print "Emin :", emin |
24 | 194 | storres | print "Emax :", emax |
25 | 194 | storres | print "Target hardness-to-round:", targetHardnessToRound |
26 | 194 | storres | |
27 | 194 | storres | ## Important constants. |
28 | 194 | storres | ### Stretch the interval if no error happens. |
29 | 194 | storres | noErrorIntervalStretch = 1 + 2^(-5) |
30 | 194 | storres | ### If no vector validates the Coppersmith condition, shrink the interval |
31 | 194 | storres | # by the following factor. |
32 | 194 | storres | noCoppersmithIntervalShrink = 1/2 |
33 | 194 | storres | ### If only (or at least) one vector validates the Coppersmith condition, |
34 | 194 | storres | # shrink the interval by the following factor. |
35 | 194 | storres | oneCoppersmithIntervalShrink = 3/4 |
36 | 194 | storres | #### If only null resultants are found, shrink the interval by the |
37 | 194 | storres | # following factor. |
38 | 194 | storres | onlyNullResultantsShrink = 3/4 |
39 | 194 | storres | ## Structures. |
40 | 194 | storres | RRR = RealField(precision) |
41 | 194 | storres | RRIF = RealIntervalField(precision) |
42 | 194 | storres | ## Converting input bound into the "right" field. |
43 | 194 | storres | lowerBound = RRR(inputLowerBound) |
44 | 194 | storres | upperBound = RRR(inputUpperBound) |
45 | 194 | storres | ## Before going any further, check domain and image binade conditions. |
46 | 194 | storres | print inputFunction(1).n() |
47 | 194 | storres | (lb,ub) = slz_fix_bounds_for_binades(lowerBound, upperBound, inputFunction) |
48 | 194 | storres | if lb != lowerBound or ub != upperBound: |
49 | 194 | storres | print "lb:", lb, " - ub:", ub |
50 | 194 | storres | print "Invalid domain/image binades. Domain:",\ |
51 | 194 | storres | lowerBound, upperBound, "Images:", \ |
52 | 194 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
53 | 194 | storres | raise Exception("Invalid domain/image binades.") |
54 | 194 | storres | # |
55 | 194 | storres | ## Progam initialization |
56 | 194 | storres | ### Approximation polynomial accuracy and hardness to round. |
57 | 194 | storres | polyApproxAccur = 2^(-(targetHardnessToRound + 1)) |
58 | 194 | storres | polyTargetHardnessToRound = targetHardnessToRound + 1 |
59 | 194 | storres | ### Significand to integer conversion ratio. |
60 | 194 | storres | toIntegerFactor = 2^(precision-1) |
61 | 194 | storres | print "Polynomial approximation required accuracy:", polyApproxAccur.n() |
62 | 194 | storres | ### Variables and rings for polynomials and root searching. |
63 | 194 | storres | i=var('i') |
64 | 194 | storres | t=var('t') |
65 | 194 | storres | inputFunctionVariable = inputFunction.variables()[0] |
66 | 194 | storres | function = inputFunction.subs({inputFunctionVariable:i}) |
67 | 194 | storres | # Polynomial Rings over the integers, for root finding. |
68 | 194 | storres | Zi = ZZ[i] |
69 | 194 | storres | Zt = ZZ[t] |
70 | 194 | storres | Zit = ZZ[i,t] |
71 | 194 | storres | ## Number of iterations limit. |
72 | 194 | storres | maxIter = 100000 |
73 | 194 | storres | # |
74 | 194 | storres | ## Compute the scaled function and the degree, in their Sollya version |
75 | 194 | storres | # once for all. |
76 | 194 | storres | (scaledf, sdlb, sdub, silb, siub) = \ |
77 | 194 | storres | slz_compute_scaled_function(function, lowerBound, upperBound, precision) |
78 | 194 | storres | print "Scaled function:", scaledf._assume_str().replace('_SAGE_VAR_', '') |
79 | 194 | storres | scaledfSo = sollya_lib_parse_string(scaledf._assume_str().replace('_SAGE_VAR_', '')) |
80 | 194 | storres | degreeSo = pobyso_constant_from_int_sa_so(degree) |
81 | 194 | storres | # |
82 | 194 | storres | ## Compute the scaling. boundsIntervalRifSa defined out of the loops. |
83 | 194 | storres | domainBoundsInterval = RRIF(lowerBound, upperBound) |
84 | 194 | storres | (unscalingFunction, scalingFunction) = \ |
85 | 194 | storres | slz_interval_scaling_expression(domainBoundsInterval, i) |
86 | 194 | storres | #print scalingFunction, unscalingFunction |
87 | 194 | storres | ## Set the Sollya internal precision (with an arbitrary minimum of 192). |
88 | 194 | storres | internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64 |
89 | 194 | storres | if internalSollyaPrec < 192: |
90 | 194 | storres | internalSollyaPrec = 192 |
91 | 194 | storres | pobyso_set_prec_sa_so(internalSollyaPrec) |
92 | 194 | storres | print "Sollya internal precision:", internalSollyaPrec |
93 | 194 | storres | ## Some variables. |
94 | 194 | storres | ### General variables |
95 | 194 | storres | lb = sdlb |
96 | 194 | storres | ub = sdub |
97 | 194 | storres | nbw = 0 |
98 | 194 | storres | intervalUlp = ub.ulp() |
99 | 194 | storres | #### Will be set by slz_interval_and_polynomila_to_sage. |
100 | 194 | storres | ic = 0 |
101 | 194 | storres | icAsInt = 0 # Set from ic. |
102 | 194 | storres | solutionsSet = set() |
103 | 194 | storres | tsErrorWidth = [] |
104 | 194 | storres | csErrorVectors = [] |
105 | 194 | storres | csVectorsResultants = [] |
106 | 194 | storres | floatP = 0 # Taylor polynomial. |
107 | 194 | storres | floatPcv = 0 # Ditto with variable change. |
108 | 194 | storres | intvl = "" # Taylor interval |
109 | 194 | storres | terr = 0 # Taylor error. |
110 | 194 | storres | iterCount = 0 |
111 | 194 | storres | htrnSet = set() |
112 | 194 | storres | ### Timers and counters. |
113 | 194 | storres | wallTimeStart = 0 |
114 | 194 | storres | cpuTimeStart = 0 |
115 | 194 | storres | taylCondFailedCount = 0 |
116 | 194 | storres | coppCondFailedCount = 0 |
117 | 194 | storres | resultCondFailedCount = 0 |
118 | 194 | storres | coppCondFailed = False |
119 | 194 | storres | resultCondFailed = False |
120 | 194 | storres | globalResultsList = [] |
121 | 194 | storres | basisConstructionsCount = 0 |
122 | 194 | storres | basisConstructionsFullTime = 0 |
123 | 194 | storres | basisConstructionTime = 0 |
124 | 194 | storres | reductionsCount = 0 |
125 | 194 | storres | reductionsFullTime = 0 |
126 | 194 | storres | reductionTime = 0 |
127 | 194 | storres | resultantsComputationsCount = 0 |
128 | 194 | storres | resultantsComputationsFullTime = 0 |
129 | 194 | storres | resultantsComputationTime = 0 |
130 | 194 | storres | rootsComputationsCount = 0 |
131 | 194 | storres | rootsComputationsFullTime = 0 |
132 | 194 | storres | rootsComputationTime = 0 |
133 | 194 | storres | |
134 | 194 | storres | ## Global times are started here. |
135 | 194 | storres | wallTimeStart = walltime() |
136 | 194 | storres | cpuTimeStart = cputime() |
137 | 194 | storres | ## Main loop. |
138 | 194 | storres | while True: |
139 | 194 | storres | if lb >= sdub: |
140 | 194 | storres | print "Lower bound reached upper bound." |
141 | 194 | storres | break |
142 | 194 | storres | if iterCount == maxIter: |
143 | 194 | storres | print "Reached maxIter. Aborting" |
144 | 194 | storres | break |
145 | 194 | storres | iterCount += 1 |
146 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
147 | 194 | storres | "log2(numbers)." |
148 | 194 | storres | ### Compute a Sollya polynomial that will honor the Taylor condition. |
149 | 194 | storres | prceSo = slz_compute_polynomial_and_interval(scaledfSo, |
150 | 194 | storres | degreeSo, |
151 | 194 | storres | lb, |
152 | 194 | storres | ub, |
153 | 194 | storres | polyApproxAccur) |
154 | 194 | storres | ### Convert back the data into Sage space. |
155 | 194 | storres | (floatP, floatPcv, intvl, ic, terr) = \ |
156 | 194 | storres | slz_interval_and_polynomial_to_sage((prceSo[0], prceSo[0], |
157 | 194 | storres | prceSo[1], prceSo[2], |
158 | 194 | storres | prceSo[3])) |
159 | 194 | storres | intvl = RRIF(intvl) |
160 | 194 | storres | ## Clean-up Sollya stuff. |
161 | 194 | storres | for elem in prceSo: |
162 | 194 | storres | sollya_lib_clear_obj(elem) |
163 | 194 | storres | #print floatP, floatPcv, intvl, ic, terr |
164 | 194 | storres | #print floatP |
165 | 194 | storres | #print intvl.endpoints()[0].n(), \ |
166 | 194 | storres | # ic.n(), |
167 | 194 | storres | #intvl.endpoints()[1].n() |
168 | 194 | storres | ### Check returned data. |
169 | 194 | storres | #### Is approximation error OK? |
170 | 194 | storres | if terr > polyApproxAccur: |
171 | 194 | storres | exceptionErrorMess = \ |
172 | 194 | storres | "Approximation failed - computed error:" + \ |
173 | 194 | storres | str(terr) + " - target error: " |
174 | 194 | storres | exceptionErrorMess += \ |
175 | 194 | storres | str(polyApproxAccur) + ". Aborting!" |
176 | 194 | storres | raise Exception(exceptionErrorMess) |
177 | 194 | storres | #### Is lower bound OK? |
178 | 194 | storres | if lb != intvl.endpoints()[0]: |
179 | 194 | storres | exceptionErrorMess = "Wrong lower bound:" + \ |
180 | 194 | storres | str(lb) + ". Aborting!" |
181 | 194 | storres | raise Exception(exceptionErrorMess) |
182 | 194 | storres | #### Set upper bound. |
183 | 194 | storres | if ub > intvl.endpoints()[1]: |
184 | 194 | storres | ub = intvl.endpoints()[1] |
185 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
186 | 194 | storres | "log2(numbers)." |
187 | 194 | storres | taylCondFailedCount += 1 |
188 | 194 | storres | #### Is interval not degenerate? |
189 | 194 | storres | if lb >= ub: |
190 | 194 | storres | exceptionErrorMess = "Degenerate interval: " + \ |
191 | 194 | storres | "lowerBound(" + str(lb) +\ |
192 | 194 | storres | ")>= upperBound(" + str(ub) + \ |
193 | 194 | storres | "). Aborting!" |
194 | 194 | storres | raise Exception(exceptionErrorMess) |
195 | 194 | storres | #### Is interval center ok? |
196 | 194 | storres | if ic <= lb or ic >= ub: |
197 | 194 | storres | exceptionErrorMess = "Invalid interval center for " + \ |
198 | 194 | storres | str(lb) + ',' + str(ic) + ',' + \ |
199 | 194 | storres | str(ub) + ". Aborting!" |
200 | 194 | storres | raise Exception(exceptionErrorMess) |
201 | 194 | storres | ##### Current interval width and reset future interval width. |
202 | 194 | storres | bw = ub - lb |
203 | 194 | storres | nbw = 0 |
204 | 194 | storres | icAsInt = int(ic * toIntegerFactor) |
205 | 194 | storres | #### The following ratio is always >= 1. In case we may want to |
206 | 194 | storres | # enlarge the interval |
207 | 194 | storres | curTaylErrRat = polyApproxAccur / terr |
208 | 194 | storres | ## Make the integral transformations. |
209 | 194 | storres | ### First for interval center and bounds. |
210 | 194 | storres | intIc = int(ic * toIntegerFactor) |
211 | 194 | storres | intLb = int(lb * toIntegerFactor) - intIc |
212 | 194 | storres | intUb = int(ub * toIntegerFactor) - intIc |
213 | 194 | storres | # |
214 | 194 | storres | #### For polynomials |
215 | 194 | storres | basisConstructionTime = cputime() |
216 | 194 | storres | ##### To a polynomial with rational coefficients with rational arguments |
217 | 194 | storres | ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP) |
218 | 194 | storres | ##### To a polynomial with rational coefficients with integer arguments |
219 | 194 | storres | ratIntP = \ |
220 | 194 | storres | slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision) |
221 | 194 | storres | ##### Ultimately a polynomial with integer coefficients with integer |
222 | 194 | storres | # arguments. |
223 | 194 | storres | coppersmithTuple = \ |
224 | 194 | storres | slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP, |
225 | 194 | storres | precision, |
226 | 194 | storres | targetHardnessToRound, |
227 | 194 | storres | i, t) |
228 | 194 | storres | #### Recover Coppersmith information. |
229 | 194 | storres | intIntP = coppersmithTuple[0] |
230 | 194 | storres | N = coppersmithTuple[1] |
231 | 194 | storres | nAtAlpha = N^alpha |
232 | 194 | storres | tBound = coppersmithTuple[2] |
233 | 194 | storres | leastCommonMultiple = coppersmithTuple[3] |
234 | 194 | storres | iBound = max(abs(intLb),abs(intUb)) |
235 | 194 | storres | basisConstructionsFullTime += cputime(basisConstructionTime) |
236 | 194 | storres | basisConstructionsCount += 1 |
237 | 194 | storres | reductionTime = cputime() |
238 | 194 | storres | # Compute the reduced polynomials. |
239 | 194 | storres | ccReducedPolynomialsList = \ |
240 | 194 | storres | slz_compute_coppersmith_reduced_polynomials(intIntP, |
241 | 194 | storres | alpha, |
242 | 194 | storres | N, |
243 | 194 | storres | iBound, |
244 | 194 | storres | tBound) |
245 | 194 | storres | if ccReducedPolynomialsList is None: |
246 | 194 | storres | raise Exception("Reduction failed.") |
247 | 194 | storres | reductionsFullTime += cputime(reductionTime) |
248 | 194 | storres | reductionsCount += 1 |
249 | 194 | storres | if len(ccReducedPolynomialsList) < 2: |
250 | 194 | storres | print "Nothing to form resultants with." |
251 | 194 | storres | |
252 | 194 | storres | coppCondFailedCount += 1 |
253 | 194 | storres | coppCondFailed = True |
254 | 194 | storres | ##### Apply a different shrink factor according to |
255 | 194 | storres | # the number of compliant polynomials. |
256 | 194 | storres | if len(ccReducedPolynomialsList) == 0: |
257 | 194 | storres | ub = lb + bw * noCoppersmithIntervalShrink |
258 | 194 | storres | else: # At least one compliant polynomial. |
259 | 194 | storres | ub = lb + bw * oneCoppersmithIntervalShrink |
260 | 194 | storres | if ub > sdub: |
261 | 194 | storres | ub = sdub |
262 | 194 | storres | if lb == ub: |
263 | 194 | storres | raise Exception("Cant shrink interval \ |
264 | 194 | storres | anymore to get Coppersmith condition.") |
265 | 194 | storres | nbw = 0 |
266 | 194 | storres | continue |
267 | 194 | storres | #### We have at least two polynomials. |
268 | 194 | storres | # Let us try to compute resultants. |
269 | 194 | storres | resultantsComputationTime = cputime() |
270 | 194 | storres | resultantsInTTuplesList = [] |
271 | 194 | storres | for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1): |
272 | 194 | storres | for polyInnerIndex in xrange(polyOuterIndex+1, |
273 | 194 | storres | len(ccReducedPolynomialsList)): |
274 | 194 | storres | resultantTuple = \ |
275 | 194 | storres | slz_resultant_tuple(ccReducedPolynomialsList[polyOuterIndex], |
276 | 194 | storres | ccReducedPolynomialsList[polyInnerIndex], |
277 | 194 | storres | t) |
278 | 194 | storres | if len(resultantTuple) > 2: |
279 | 194 | storres | #print resultantTuple[2] |
280 | 194 | storres | resultantsInTTuplesList.append(resultantTuple) |
281 | 194 | storres | else: |
282 | 194 | storres | print "No non nul resultant" |
283 | 194 | storres | print len(resultantsInTTuplesList), "resultant(s) in t tuple(s) created." |
284 | 194 | storres | resultantsComputationsFullTime += cputime(resultantsComputationTime) |
285 | 194 | storres | resultantsComputationsCount += 1 |
286 | 194 | storres | if len(resultantsInTTuplesList) == 0: |
287 | 194 | storres | print "Only null resultants, shrinking interval." |
288 | 194 | storres | resultCondFailed = True |
289 | 194 | storres | resultCondFailedCount += 1 |
290 | 194 | storres | ### Shrink interval for next iteration. |
291 | 194 | storres | ub = lb + bw * onlyNullResultantsShrink |
292 | 194 | storres | if ub > sdub: |
293 | 194 | storres | ub = sdub |
294 | 194 | storres | nbw = 0 |
295 | 194 | storres | continue |
296 | 194 | storres | #### Compute roots. |
297 | 194 | storres | rootsComputationTime = cputime() |
298 | 194 | storres | reducedPolynomialsRootsSet = set() |
299 | 194 | storres | ##### Solve in the second variable since resultants are in the first |
300 | 194 | storres | # variable. |
301 | 194 | storres | for resultantInTTuple in resultantsInTTuplesList: |
302 | 194 | storres | currentResultant = resultantInTTuple[2] |
303 | 194 | storres | ##### If the resultant degree is not at least 1, there are no roots. |
304 | 194 | storres | if currentResultant.degree() < 1: |
305 | 194 | storres | print "Resultant is constant:", currentResultant |
306 | 194 | storres | continue # Next resultantInTTuple |
307 | 194 | storres | ##### Compute i roots |
308 | 194 | storres | iRootsList = Zi(currentResultant).roots() |
309 | 194 | storres | ##### For each iRoot, compute the corresponding tRoots and check |
310 | 194 | storres | # them in the input polynomial. |
311 | 194 | storres | for iRoot in iRootsList: |
312 | 194 | storres | ####### Roots returned by roots() are (value, multiplicity) |
313 | 194 | storres | # tuples. |
314 | 194 | storres | #print "iRoot:", iRoot |
315 | 194 | storres | ###### Use the tRoot against each polynomial, alternatively. |
316 | 194 | storres | for indexInTuple in range(0,2): |
317 | 194 | storres | currentPolynomial = resultantInTTuple[indexInTuple] |
318 | 194 | storres | ####### If the polynomial is univariate, just drop it. |
319 | 194 | storres | if len(currentPolynomial.variables()) < 2: |
320 | 194 | storres | print " Current polynomial is not in two variables." |
321 | 194 | storres | continue # Next indexInTuple |
322 | 194 | storres | tRootsList = \ |
323 | 194 | storres | Zt(currentPolynomial.subs({currentPolynomial.variables()[0]:iRoot[0]})).roots() |
324 | 194 | storres | ####### The tRootsList can be empty, hence the test. |
325 | 194 | storres | if len(tRootsList) == 0: |
326 | 194 | storres | print " No t root." |
327 | 194 | storres | continue # Next indexInTuple |
328 | 194 | storres | for tRoot in tRootsList: |
329 | 194 | storres | reducedPolynomialsRootsSet.add((iRoot[0], tRoot[0])) |
330 | 194 | storres | # End of roots computation |
331 | 194 | storres | rootsComputationsFullTime = cputime(rootsComputationTime) |
332 | 194 | storres | rootsComputationsCount += 1 |
333 | 194 | storres | ##### Prepare for results. |
334 | 194 | storres | intervalResultsList = [] |
335 | 194 | storres | intervalResultsList.append((lb, ub)) |
336 | 194 | storres | #### Check roots. |
337 | 194 | storres | rootsResultsList = [] |
338 | 194 | storres | for root in reducedPolynomialsRootsSet: |
339 | 194 | storres | specificRootResultsList = [] |
340 | 194 | storres | failingBounds = [] |
341 | 194 | storres | intIntPdivN = intIntP(root[0], root[1]) / N |
342 | 194 | storres | if int(intIntPdivN) != intIntPdivN: |
343 | 194 | storres | continue # Next root |
344 | 194 | storres | # Root qualifies for modular equation, test it for hardness to round. |
345 | 194 | storres | hardToRoundCaseAsFloat = RRR((icAsInt + root[0]) / toIntegerFactor) |
346 | 194 | storres | #print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision) |
347 | 194 | storres | #print scalingFunction |
348 | 194 | storres | scaledHardToRoundCaseAsFloat = \ |
349 | 194 | storres | scalingFunction(hardToRoundCaseAsFloat) |
350 | 194 | storres | print "Candidate HTRNc at x =", \ |
351 | 194 | storres | scaledHardToRoundCaseAsFloat.n().str(base=2), |
352 | 194 | storres | if slz_is_htrn(scaledHardToRoundCaseAsFloat, |
353 | 194 | storres | function, |
354 | 194 | storres | 2^-(targetHardnessToRound), |
355 | 194 | storres | RRR): |
356 | 194 | storres | print hardToRoundCaseAsFloat, "is HTRN case." |
357 | 194 | storres | if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub: |
358 | 194 | storres | print "Found in interval." |
359 | 194 | storres | else: |
360 | 194 | storres | print "Found out of interval." |
361 | 194 | storres | specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2)) |
362 | 194 | storres | # Check the root is in the bounds |
363 | 194 | storres | if abs(root[0]) > iBound or abs(root[1]) > tBound: |
364 | 194 | storres | print "Root", root, "is out of bounds." |
365 | 194 | storres | if abs(root[0]) > iBound: |
366 | 194 | storres | print "root[0]:", root[0] |
367 | 194 | storres | print "i bound:", iBound |
368 | 194 | storres | failingBounds.append('i') |
369 | 194 | storres | failingBounds.append(root[0]) |
370 | 194 | storres | failingBounds.append(iBound) |
371 | 194 | storres | if abs(root[1]) > tBound: |
372 | 194 | storres | print "root[1]:", root[1] |
373 | 194 | storres | print "t bound:", tBound |
374 | 194 | storres | failingBounds.append('t') |
375 | 194 | storres | failingBounds.append(root[1]) |
376 | 194 | storres | failingBounds.append(tBound) |
377 | 194 | storres | if len(failingBounds) > 0: |
378 | 194 | storres | specificRootResultsList.append(failingBounds) |
379 | 194 | storres | else: # From slz_is_htrn... |
380 | 194 | storres | print "is not an HTRN case." |
381 | 194 | storres | if len(specificRootResultsList) > 0: |
382 | 194 | storres | rootsResultsList.append(specificRootResultsList) |
383 | 194 | storres | if len(rootsResultsList) > 0: |
384 | 194 | storres | intervalResultsList.append(rootsResultsList) |
385 | 194 | storres | #### An intervalResultsList has at least the bounds. |
386 | 194 | storres | globalResultsList.append(intervalResultsList) |
387 | 194 | storres | #### Compute an incremented width for next upper bound, only |
388 | 194 | storres | # if not Coppersmith condition nor resultant condition |
389 | 194 | storres | # failed at the previous run. |
390 | 194 | storres | if not coppCondFailed and not resultCondFailed: |
391 | 194 | storres | nbw = noErrorIntervalStretch * bw |
392 | 194 | storres | else: |
393 | 194 | storres | nbw = bw |
394 | 194 | storres | ##### Reset the failure flags. They will be raised |
395 | 194 | storres | # again if needed. |
396 | 194 | storres | coppCondFailed = False |
397 | 194 | storres | resultCondFailed = False |
398 | 194 | storres | #### For next iteration (at end of loop) |
399 | 194 | storres | #print "nbw:", nbw |
400 | 194 | storres | lb = ub |
401 | 194 | storres | ub += nbw |
402 | 194 | storres | if ub > sdub: |
403 | 194 | storres | ub = sdub |
404 | 194 | storres | |
405 | 194 | storres | # End while True |
406 | 194 | storres | ## Main loop just ended. |
407 | 194 | storres | globalWallTime = walltime(wallTimeStart) |
408 | 194 | storres | globalCpuTime = cputime(cpuTimeStart) |
409 | 194 | storres | ## Output results |
410 | 194 | storres | print ; print "Intervals and HTRNs" ; print |
411 | 194 | storres | for intervalResultsList in globalResultsList: |
412 | 194 | storres | print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]", |
413 | 194 | storres | if len(intervalResultsList) > 1: |
414 | 194 | storres | rootsResultsList = intervalResultsList[1] |
415 | 194 | storres | for specificRootResultsList in rootsResultsList: |
416 | 194 | storres | print "\t", specificRootResultsList[0], |
417 | 194 | storres | if len(specificRootResultsList) > 1: |
418 | 194 | storres | print specificRootResultsList[1], |
419 | 194 | storres | print ; print |
420 | 194 | storres | #print globalResultsList |
421 | 194 | storres | # |
422 | 194 | storres | print "Timers and counters" |
423 | 194 | storres | |
424 | 194 | storres | print "Number of iterations:", iterCount |
425 | 194 | storres | print "Taylor condition failures:", taylCondFailedCount |
426 | 194 | storres | print "Coppersmith condition failures:", coppCondFailedCount |
427 | 194 | storres | print "Resultant condition failures:", resultCondFailedCount |
428 | 194 | storres | print "Iterations count: ", iterCount |
429 | 194 | storres | print "Number of intervals:", len(globalResultsList) |
430 | 194 | storres | print "Number of basis constructions:", basisConstructionsCount |
431 | 194 | storres | print "Total CPU time spent in basis constructions:", \ |
432 | 194 | storres | basisConstructionsFullTime |
433 | 194 | storres | if basisConstructionsCount != 0: |
434 | 194 | storres | print "Average basis construction CPU time:", \ |
435 | 194 | storres | basisConstructionsFullTime/basisConstructionsCount |
436 | 194 | storres | print "Number of reductions:", reductionsCount |
437 | 194 | storres | print "Total CPU time spent in reductions:", reductionsFullTime |
438 | 194 | storres | if reductionsCount != 0: |
439 | 194 | storres | print "Average reduction CPU time:", \ |
440 | 194 | storres | reductionsFullTime/reductionsCount |
441 | 194 | storres | print "Number of resultants computation rounds:", \ |
442 | 194 | storres | resultantsComputationsCount |
443 | 194 | storres | print "Total CPU time spent in resultants computation rounds:", \ |
444 | 194 | storres | resultantsComputationsFullTime |
445 | 194 | storres | if resultantsComputationsCount != 0: |
446 | 194 | storres | print "Average resultants computation round CPU time:", \ |
447 | 194 | storres | resultantsComputationsFullTime/resultantsComputationsCount |
448 | 194 | storres | print "Number of root finding rounds:", rootsComputationsCount |
449 | 194 | storres | print "Total CPU time spent in roots finding rounds:", \ |
450 | 194 | storres | rootsComputationsFullTime |
451 | 194 | storres | if rootsComputationsCount != 0: |
452 | 194 | storres | print "Average roots finding round CPU time:", \ |
453 | 194 | storres | rootsComputationsFullTime/rootsComputationsCount |
454 | 194 | storres | print "Global Wall time:", globalWallTime |
455 | 194 | storres | print "Global CPU time:", globalCpuTime |
456 | 194 | storres | ## Output counters |
457 | 194 | storres | # End srs_runSLZ-v01 |
458 | 194 | storres | |
459 | 194 | storres | def srs_run_SLZ_v02(inputFunction, |
460 | 194 | storres | inputLowerBound, |
461 | 194 | storres | inputUpperBound, |
462 | 194 | storres | alpha, |
463 | 194 | storres | degree, |
464 | 194 | storres | precision, |
465 | 194 | storres | emin, |
466 | 194 | storres | emax, |
467 | 194 | storres | targetHardnessToRound, |
468 | 194 | storres | debug = False): |
469 | 194 | storres | """ |
470 | 194 | storres | Changes from V1: |
471 | 194 | storres | 1- check for roots as soon as a resultant is computed; |
472 | 194 | storres | 2- once a non null resultant is found, check for roots; |
473 | 194 | storres | 3- constant resultant == no root. |
474 | 194 | storres | """ |
475 | 194 | storres | |
476 | 194 | storres | if debug: |
477 | 194 | storres | print "Function :", inputFunction |
478 | 194 | storres | print "Lower bound :", inputLowerBound |
479 | 194 | storres | print "Upper bounds :", inputUpperBound |
480 | 194 | storres | print "Alpha :", alpha |
481 | 194 | storres | print "Degree :", degree |
482 | 194 | storres | print "Precision :", precision |
483 | 194 | storres | print "Emin :", emin |
484 | 194 | storres | print "Emax :", emax |
485 | 194 | storres | print "Target hardness-to-round:", targetHardnessToRound |
486 | 194 | storres | |
487 | 194 | storres | ## Important constants. |
488 | 194 | storres | ### Stretch the interval if no error happens. |
489 | 194 | storres | noErrorIntervalStretch = 1 + 2^(-5) |
490 | 194 | storres | ### If no vector validates the Coppersmith condition, shrink the interval |
491 | 194 | storres | # by the following factor. |
492 | 194 | storres | noCoppersmithIntervalShrink = 1/2 |
493 | 194 | storres | ### If only (or at least) one vector validates the Coppersmith condition, |
494 | 194 | storres | # shrink the interval by the following factor. |
495 | 194 | storres | oneCoppersmithIntervalShrink = 3/4 |
496 | 194 | storres | #### If only null resultants are found, shrink the interval by the |
497 | 194 | storres | # following factor. |
498 | 194 | storres | onlyNullResultantsShrink = 3/4 |
499 | 194 | storres | ## Structures. |
500 | 194 | storres | RRR = RealField(precision) |
501 | 194 | storres | RRIF = RealIntervalField(precision) |
502 | 194 | storres | ## Converting input bound into the "right" field. |
503 | 194 | storres | lowerBound = RRR(inputLowerBound) |
504 | 194 | storres | upperBound = RRR(inputUpperBound) |
505 | 194 | storres | ## Before going any further, check domain and image binade conditions. |
506 | 194 | storres | print inputFunction(1).n() |
507 | 194 | storres | (lb,ub) = slz_fix_bounds_for_binades(lowerBound, upperBound, inputFunction) |
508 | 194 | storres | if lb != lowerBound or ub != upperBound: |
509 | 194 | storres | print "lb:", lb, " - ub:", ub |
510 | 194 | storres | print "Invalid domain/image binades. Domain:",\ |
511 | 194 | storres | lowerBound, upperBound, "Images:", \ |
512 | 194 | storres | inputFunction(lowerBound), inputFunction(upperBound) |
513 | 194 | storres | raise Exception("Invalid domain/image binades.") |
514 | 194 | storres | # |
515 | 194 | storres | ## Progam initialization |
516 | 194 | storres | ### Approximation polynomial accuracy and hardness to round. |
517 | 194 | storres | polyApproxAccur = 2^(-(targetHardnessToRound + 1)) |
518 | 194 | storres | polyTargetHardnessToRound = targetHardnessToRound + 1 |
519 | 194 | storres | ### Significand to integer conversion ratio. |
520 | 194 | storres | toIntegerFactor = 2^(precision-1) |
521 | 194 | storres | print "Polynomial approximation required accuracy:", polyApproxAccur.n() |
522 | 194 | storres | ### Variables and rings for polynomials and root searching. |
523 | 194 | storres | i=var('i') |
524 | 194 | storres | t=var('t') |
525 | 194 | storres | inputFunctionVariable = inputFunction.variables()[0] |
526 | 194 | storres | function = inputFunction.subs({inputFunctionVariable:i}) |
527 | 194 | storres | # Polynomial Rings over the integers, for root finding. |
528 | 194 | storres | Zi = ZZ[i] |
529 | 194 | storres | Zt = ZZ[t] |
530 | 194 | storres | Zit = ZZ[i,t] |
531 | 194 | storres | ## Number of iterations limit. |
532 | 194 | storres | maxIter = 100000 |
533 | 194 | storres | # |
534 | 194 | storres | ## Compute the scaled function and the degree, in their Sollya version |
535 | 194 | storres | # once for all. |
536 | 194 | storres | (scaledf, sdlb, sdub, silb, siub) = \ |
537 | 194 | storres | slz_compute_scaled_function(function, lowerBound, upperBound, precision) |
538 | 194 | storres | print "Scaled function:", scaledf._assume_str().replace('_SAGE_VAR_', '') |
539 | 194 | storres | scaledfSo = sollya_lib_parse_string(scaledf._assume_str().replace('_SAGE_VAR_', '')) |
540 | 194 | storres | degreeSo = pobyso_constant_from_int_sa_so(degree) |
541 | 194 | storres | # |
542 | 194 | storres | ## Compute the scaling. boundsIntervalRifSa defined out of the loops. |
543 | 194 | storres | domainBoundsInterval = RRIF(lowerBound, upperBound) |
544 | 194 | storres | (unscalingFunction, scalingFunction) = \ |
545 | 194 | storres | slz_interval_scaling_expression(domainBoundsInterval, i) |
546 | 194 | storres | #print scalingFunction, unscalingFunction |
547 | 194 | storres | ## Set the Sollya internal precision (with an arbitrary minimum of 192). |
548 | 194 | storres | internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64 |
549 | 194 | storres | if internalSollyaPrec < 192: |
550 | 194 | storres | internalSollyaPrec = 192 |
551 | 194 | storres | pobyso_set_prec_sa_so(internalSollyaPrec) |
552 | 194 | storres | print "Sollya internal precision:", internalSollyaPrec |
553 | 194 | storres | ## Some variables. |
554 | 194 | storres | ### General variables |
555 | 194 | storres | lb = sdlb |
556 | 194 | storres | ub = sdub |
557 | 194 | storres | nbw = 0 |
558 | 194 | storres | intervalUlp = ub.ulp() |
559 | 194 | storres | #### Will be set by slz_interval_and_polynomila_to_sage. |
560 | 194 | storres | ic = 0 |
561 | 194 | storres | icAsInt = 0 # Set from ic. |
562 | 194 | storres | solutionsSet = set() |
563 | 194 | storres | tsErrorWidth = [] |
564 | 194 | storres | csErrorVectors = [] |
565 | 194 | storres | csVectorsResultants = [] |
566 | 194 | storres | floatP = 0 # Taylor polynomial. |
567 | 194 | storres | floatPcv = 0 # Ditto with variable change. |
568 | 194 | storres | intvl = "" # Taylor interval |
569 | 194 | storres | terr = 0 # Taylor error. |
570 | 194 | storres | iterCount = 0 |
571 | 194 | storres | htrnSet = set() |
572 | 194 | storres | ### Timers and counters. |
573 | 194 | storres | wallTimeStart = 0 |
574 | 194 | storres | cpuTimeStart = 0 |
575 | 194 | storres | taylCondFailedCount = 0 |
576 | 194 | storres | coppCondFailedCount = 0 |
577 | 194 | storres | resultCondFailedCount = 0 |
578 | 194 | storres | coppCondFailed = False |
579 | 194 | storres | resultCondFailed = False |
580 | 194 | storres | globalResultsList = [] |
581 | 194 | storres | basisConstructionsCount = 0 |
582 | 194 | storres | basisConstructionsFullTime = 0 |
583 | 194 | storres | basisConstructionTime = 0 |
584 | 194 | storres | reductionsCount = 0 |
585 | 194 | storres | reductionsFullTime = 0 |
586 | 194 | storres | reductionTime = 0 |
587 | 194 | storres | resultantsComputationsCount = 0 |
588 | 194 | storres | resultantsComputationsFullTime = 0 |
589 | 194 | storres | resultantsComputationTime = 0 |
590 | 194 | storres | rootsComputationsCount = 0 |
591 | 194 | storres | rootsComputationsFullTime = 0 |
592 | 194 | storres | rootsComputationTime = 0 |
593 | 194 | storres | |
594 | 194 | storres | ## Global times are started here. |
595 | 194 | storres | wallTimeStart = walltime() |
596 | 194 | storres | cpuTimeStart = cputime() |
597 | 194 | storres | ## Main loop. |
598 | 194 | storres | while True: |
599 | 194 | storres | if lb >= sdub: |
600 | 194 | storres | print "Lower bound reached upper bound." |
601 | 194 | storres | break |
602 | 194 | storres | if iterCount == maxIter: |
603 | 194 | storres | print "Reached maxIter. Aborting" |
604 | 194 | storres | break |
605 | 194 | storres | iterCount += 1 |
606 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
607 | 194 | storres | "log2(numbers)." |
608 | 194 | storres | ### Compute a Sollya polynomial that will honor the Taylor condition. |
609 | 194 | storres | prceSo = slz_compute_polynomial_and_interval(scaledfSo, |
610 | 194 | storres | degreeSo, |
611 | 194 | storres | lb, |
612 | 194 | storres | ub, |
613 | 194 | storres | polyApproxAccur) |
614 | 194 | storres | ### Convert back the data into Sage space. |
615 | 194 | storres | (floatP, floatPcv, intvl, ic, terr) = \ |
616 | 194 | storres | slz_interval_and_polynomial_to_sage((prceSo[0], prceSo[0], |
617 | 194 | storres | prceSo[1], prceSo[2], |
618 | 194 | storres | prceSo[3])) |
619 | 194 | storres | intvl = RRIF(intvl) |
620 | 194 | storres | ## Clean-up Sollya stuff. |
621 | 194 | storres | for elem in prceSo: |
622 | 194 | storres | sollya_lib_clear_obj(elem) |
623 | 194 | storres | #print floatP, floatPcv, intvl, ic, terr |
624 | 194 | storres | #print floatP |
625 | 194 | storres | #print intvl.endpoints()[0].n(), \ |
626 | 194 | storres | # ic.n(), |
627 | 194 | storres | #intvl.endpoints()[1].n() |
628 | 194 | storres | ### Check returned data. |
629 | 194 | storres | #### Is approximation error OK? |
630 | 194 | storres | if terr > polyApproxAccur: |
631 | 194 | storres | exceptionErrorMess = \ |
632 | 194 | storres | "Approximation failed - computed error:" + \ |
633 | 194 | storres | str(terr) + " - target error: " |
634 | 194 | storres | exceptionErrorMess += \ |
635 | 194 | storres | str(polyApproxAccur) + ". Aborting!" |
636 | 194 | storres | raise Exception(exceptionErrorMess) |
637 | 194 | storres | #### Is lower bound OK? |
638 | 194 | storres | if lb != intvl.endpoints()[0]: |
639 | 194 | storres | exceptionErrorMess = "Wrong lower bound:" + \ |
640 | 194 | storres | str(lb) + ". Aborting!" |
641 | 194 | storres | raise Exception(exceptionErrorMess) |
642 | 194 | storres | #### Set upper bound. |
643 | 194 | storres | if ub > intvl.endpoints()[1]: |
644 | 194 | storres | ub = intvl.endpoints()[1] |
645 | 194 | storres | print "[", lb, ",", ub, "]", ((ub - lb) / intervalUlp).log2().n(), \ |
646 | 194 | storres | "log2(numbers)." |
647 | 194 | storres | taylCondFailedCount += 1 |
648 | 194 | storres | #### Is interval not degenerate? |
649 | 194 | storres | if lb >= ub: |
650 | 194 | storres | exceptionErrorMess = "Degenerate interval: " + \ |
651 | 194 | storres | "lowerBound(" + str(lb) +\ |
652 | 194 | storres | ")>= upperBound(" + str(ub) + \ |
653 | 194 | storres | "). Aborting!" |
654 | 194 | storres | raise Exception(exceptionErrorMess) |
655 | 194 | storres | #### Is interval center ok? |
656 | 194 | storres | if ic <= lb or ic >= ub: |
657 | 194 | storres | exceptionErrorMess = "Invalid interval center for " + \ |
658 | 194 | storres | str(lb) + ',' + str(ic) + ',' + \ |
659 | 194 | storres | str(ub) + ". Aborting!" |
660 | 194 | storres | raise Exception(exceptionErrorMess) |
661 | 194 | storres | ##### Current interval width and reset future interval width. |
662 | 194 | storres | bw = ub - lb |
663 | 194 | storres | nbw = 0 |
664 | 194 | storres | icAsInt = int(ic * toIntegerFactor) |
665 | 194 | storres | #### The following ratio is always >= 1. In case we may want to |
666 | 194 | storres | # enlarge the interval |
667 | 194 | storres | curTaylErrRat = polyApproxAccur / terr |
668 | 194 | storres | ## Make the integral transformations. |
669 | 194 | storres | ### First for interval center and bounds. |
670 | 194 | storres | intIc = int(ic * toIntegerFactor) |
671 | 194 | storres | intLb = int(lb * toIntegerFactor) - intIc |
672 | 194 | storres | intUb = int(ub * toIntegerFactor) - intIc |
673 | 194 | storres | # |
674 | 194 | storres | #### For polynomials |
675 | 194 | storres | basisConstructionTime = cputime() |
676 | 194 | storres | ##### To a polynomial with rational coefficients with rational arguments |
677 | 194 | storres | ratRatP = slz_float_poly_of_float_to_rat_poly_of_rat_pow_two(floatP) |
678 | 194 | storres | ##### To a polynomial with rational coefficients with integer arguments |
679 | 194 | storres | ratIntP = \ |
680 | 194 | storres | slz_rat_poly_of_rat_to_rat_poly_of_int(ratRatP, precision) |
681 | 194 | storres | ##### Ultimately a polynomial with integer coefficients with integer |
682 | 194 | storres | # arguments. |
683 | 194 | storres | coppersmithTuple = \ |
684 | 194 | storres | slz_rat_poly_of_int_to_poly_for_coppersmith(ratIntP, |
685 | 194 | storres | precision, |
686 | 194 | storres | targetHardnessToRound, |
687 | 194 | storres | i, t) |
688 | 194 | storres | #### Recover Coppersmith information. |
689 | 194 | storres | intIntP = coppersmithTuple[0] |
690 | 194 | storres | N = coppersmithTuple[1] |
691 | 194 | storres | nAtAlpha = N^alpha |
692 | 194 | storres | tBound = coppersmithTuple[2] |
693 | 194 | storres | leastCommonMultiple = coppersmithTuple[3] |
694 | 194 | storres | iBound = max(abs(intLb),abs(intUb)) |
695 | 194 | storres | basisConstructionsFullTime += cputime(basisConstructionTime) |
696 | 194 | storres | basisConstructionsCount += 1 |
697 | 194 | storres | reductionTime = cputime() |
698 | 194 | storres | # Compute the reduced polynomials. |
699 | 194 | storres | ccReducedPolynomialsList = \ |
700 | 194 | storres | slz_compute_coppersmith_reduced_polynomials(intIntP, |
701 | 194 | storres | alpha, |
702 | 194 | storres | N, |
703 | 194 | storres | iBound, |
704 | 194 | storres | tBound) |
705 | 194 | storres | if ccReducedPolynomialsList is None: |
706 | 194 | storres | raise Exception("Reduction failed.") |
707 | 194 | storres | reductionsFullTime += cputime(reductionTime) |
708 | 194 | storres | reductionsCount += 1 |
709 | 194 | storres | if len(ccReducedPolynomialsList) < 2: |
710 | 194 | storres | print "Nothing to form resultants with." |
711 | 194 | storres | |
712 | 194 | storres | coppCondFailedCount += 1 |
713 | 194 | storres | coppCondFailed = True |
714 | 194 | storres | ##### Apply a different shrink factor according to |
715 | 194 | storres | # the number of compliant polynomials. |
716 | 194 | storres | if len(ccReducedPolynomialsList) == 0: |
717 | 194 | storres | ub = lb + bw * noCoppersmithIntervalShrink |
718 | 194 | storres | else: # At least one compliant polynomial. |
719 | 194 | storres | ub = lb + bw * oneCoppersmithIntervalShrink |
720 | 194 | storres | if ub > sdub: |
721 | 194 | storres | ub = sdub |
722 | 194 | storres | if lb == ub: |
723 | 194 | storres | raise Exception("Cant shrink interval \ |
724 | 194 | storres | anymore to get Coppersmith condition.") |
725 | 194 | storres | nbw = 0 |
726 | 194 | storres | continue |
727 | 194 | storres | #### We have at least two polynomials. |
728 | 194 | storres | # Let us try to compute resultants. |
729 | 194 | storres | # For each resultant computed, go for the solutions. |
730 | 194 | storres | resultantsComputationTime = cputime() |
731 | 194 | storres | resultantsInTTuplesList = [] |
732 | 194 | storres | hasNonNullResultant = False |
733 | 194 | storres | ##### Build the pairs list. |
734 | 194 | storres | polyPairsList = [] |
735 | 194 | storres | for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList) - 1): |
736 | 194 | storres | for polyInnerIndex in xrange(polyOuterIndex+1, |
737 | 194 | storres | len(ccReducedPolynomialsList)): |
738 | 194 | storres | polyPairsList.append((ccReducedPolynomialsList[polyOuterIndex], |
739 | 194 | storres | ccReducedPolynomialsList[polyInnerIndex])) |
740 | 194 | storres | for polyPair in polyPairsList: |
741 | 194 | storres | resultantTuple = \ |
742 | 194 | storres | slz_resultant_tuple(polyPair[0], |
743 | 194 | storres | polyPair[1], |
744 | 194 | storres | t) |
745 | 194 | storres | resultantsComputationsFullTime += cputime(resultantsComputationTime) |
746 | 194 | storres | resultantsComputationsCount += 1 |
747 | 194 | storres | if len(resultantTuple) > 2: |
748 | 194 | storres | hasNonNullResultant = True |
749 | 194 | storres | resultantsInTTuplesList.append(resultantTuple) |
750 | 194 | storres | else: |
751 | 194 | storres | print "Nul resultant" |
752 | 194 | storres | print len(resultantsInTTuplesList), "resultant(s) in t tuple(s) created." |
753 | 194 | storres | if len(resultantsInTTuplesList) == 0: |
754 | 194 | storres | print "Only null resultants, shrinking interval." |
755 | 194 | storres | resultCondFailed = True |
756 | 194 | storres | resultCondFailedCount += 1 |
757 | 194 | storres | ### Shrink interval for next iteration. |
758 | 194 | storres | ub = lb + bw * onlyNullResultantsShrink |
759 | 194 | storres | if ub > sdub: |
760 | 194 | storres | ub = sdub |
761 | 194 | storres | nbw = 0 |
762 | 194 | storres | continue |
763 | 194 | storres | #### Compute roots. |
764 | 194 | storres | rootsComputationTime = cputime() |
765 | 194 | storres | reducedPolynomialsRootsSet = set() |
766 | 194 | storres | ##### Solve in the second variable since resultants are in the first |
767 | 194 | storres | # variable. |
768 | 194 | storres | for resultantInTTuple in resultantsInTTuplesList: |
769 | 194 | storres | currentResultant = resultantInTTuple[2] |
770 | 194 | storres | ##### If the resultant degree is not at least 1, there are no roots. |
771 | 194 | storres | if currentResultant.degree() < 1: |
772 | 194 | storres | print "Resultant is constant:", currentResultant |
773 | 194 | storres | continue # Next resultantInTTuple |
774 | 194 | storres | ##### Compute i roots |
775 | 194 | storres | iRootsList = Zi(currentResultant).roots() |
776 | 194 | storres | ##### For each iRoot, compute the corresponding tRoots and check |
777 | 194 | storres | # them in the input polynomial. |
778 | 194 | storres | for iRoot in iRootsList: |
779 | 194 | storres | ####### Roots returned by roots() are (value, multiplicity) |
780 | 194 | storres | # tuples. |
781 | 194 | storres | #print "iRoot:", iRoot |
782 | 194 | storres | ###### Use the tRoot against each polynomial, alternatively. |
783 | 194 | storres | for indexInTuple in range(0,2): |
784 | 194 | storres | currentPolynomial = resultantInTTuple[indexInTuple] |
785 | 194 | storres | ####### If the polynomial is univariate, just drop it. |
786 | 194 | storres | if len(currentPolynomial.variables()) < 2: |
787 | 194 | storres | print " Current polynomial is not in two variables." |
788 | 194 | storres | continue # Next indexInTuple |
789 | 194 | storres | tRootsList = \ |
790 | 194 | storres | Zt(currentPolynomial.subs({currentPolynomial.variables()[0]:iRoot[0]})).roots() |
791 | 194 | storres | ####### The tRootsList can be empty, hence the test. |
792 | 194 | storres | if len(tRootsList) == 0: |
793 | 194 | storres | print " No t root." |
794 | 194 | storres | continue # Next indexInTuple |
795 | 194 | storres | for tRoot in tRootsList: |
796 | 194 | storres | reducedPolynomialsRootsSet.add((iRoot[0], tRoot[0])) |
797 | 194 | storres | # End of roots computation |
798 | 194 | storres | rootsComputationsFullTime = cputime(rootsComputationTime) |
799 | 194 | storres | rootsComputationsCount += 1 |
800 | 194 | storres | ##### Prepare for results. |
801 | 194 | storres | intervalResultsList = [] |
802 | 194 | storres | intervalResultsList.append((lb, ub)) |
803 | 194 | storres | #### Check roots. |
804 | 194 | storres | rootsResultsList = [] |
805 | 194 | storres | for root in reducedPolynomialsRootsSet: |
806 | 194 | storres | specificRootResultsList = [] |
807 | 194 | storres | failingBounds = [] |
808 | 194 | storres | intIntPdivN = intIntP(root[0], root[1]) / N |
809 | 194 | storres | if int(intIntPdivN) != intIntPdivN: |
810 | 194 | storres | continue # Next root |
811 | 194 | storres | # Root qualifies for modular equation, test it for hardness to round. |
812 | 194 | storres | hardToRoundCaseAsFloat = RRR((icAsInt + root[0]) / toIntegerFactor) |
813 | 194 | storres | #print "Before unscaling:", hardToRoundCaseAsFloat.n(prec=precision) |
814 | 194 | storres | #print scalingFunction |
815 | 194 | storres | scaledHardToRoundCaseAsFloat = \ |
816 | 194 | storres | scalingFunction(hardToRoundCaseAsFloat) |
817 | 194 | storres | print "Candidate HTRNc at x =", \ |
818 | 194 | storres | scaledHardToRoundCaseAsFloat.n().str(base=2), |
819 | 194 | storres | if slz_is_htrn(scaledHardToRoundCaseAsFloat, |
820 | 194 | storres | function, |
821 | 194 | storres | 2^-(targetHardnessToRound), |
822 | 194 | storres | RRR): |
823 | 194 | storres | print hardToRoundCaseAsFloat, "is HTRN case." |
824 | 194 | storres | if lb <= hardToRoundCaseAsFloat and hardToRoundCaseAsFloat <= ub: |
825 | 194 | storres | print "Found in interval." |
826 | 194 | storres | else: |
827 | 194 | storres | print "Found out of interval." |
828 | 194 | storres | specificRootResultsList.append(hardToRoundCaseAsFloat.n().str(base=2)) |
829 | 194 | storres | # Check the root is in the bounds |
830 | 194 | storres | if abs(root[0]) > iBound or abs(root[1]) > tBound: |
831 | 194 | storres | print "Root", root, "is out of bounds." |
832 | 194 | storres | if abs(root[0]) > iBound: |
833 | 194 | storres | print "root[0]:", root[0] |
834 | 194 | storres | print "i bound:", iBound |
835 | 194 | storres | failingBounds.append('i') |
836 | 194 | storres | failingBounds.append(root[0]) |
837 | 194 | storres | failingBounds.append(iBound) |
838 | 194 | storres | if abs(root[1]) > tBound: |
839 | 194 | storres | print "root[1]:", root[1] |
840 | 194 | storres | print "t bound:", tBound |
841 | 194 | storres | failingBounds.append('t') |
842 | 194 | storres | failingBounds.append(root[1]) |
843 | 194 | storres | failingBounds.append(tBound) |
844 | 194 | storres | if len(failingBounds) > 0: |
845 | 194 | storres | specificRootResultsList.append(failingBounds) |
846 | 194 | storres | else: # From slz_is_htrn... |
847 | 194 | storres | print "is not an HTRN case." |
848 | 194 | storres | if len(specificRootResultsList) > 0: |
849 | 194 | storres | rootsResultsList.append(specificRootResultsList) |
850 | 194 | storres | if len(rootsResultsList) > 0: |
851 | 194 | storres | intervalResultsList.append(rootsResultsList) |
852 | 194 | storres | #### An intervalResultsList has at least the bounds. |
853 | 194 | storres | globalResultsList.append(intervalResultsList) |
854 | 194 | storres | #### Compute an incremented width for next upper bound, only |
855 | 194 | storres | # if not Coppersmith condition nor resultant condition |
856 | 194 | storres | # failed at the previous run. |
857 | 194 | storres | if not coppCondFailed and not resultCondFailed: |
858 | 194 | storres | nbw = noErrorIntervalStretch * bw |
859 | 194 | storres | else: |
860 | 194 | storres | nbw = bw |
861 | 194 | storres | ##### Reset the failure flags. They will be raised |
862 | 194 | storres | # again if needed. |
863 | 194 | storres | coppCondFailed = False |
864 | 194 | storres | resultCondFailed = False |
865 | 194 | storres | #### For next iteration (at end of loop) |
866 | 194 | storres | #print "nbw:", nbw |
867 | 194 | storres | lb = ub |
868 | 194 | storres | ub += nbw |
869 | 194 | storres | if ub > sdub: |
870 | 194 | storres | ub = sdub |
871 | 194 | storres | |
872 | 194 | storres | # End while True |
873 | 194 | storres | ## Main loop just ended. |
874 | 194 | storres | globalWallTime = walltime(wallTimeStart) |
875 | 194 | storres | globalCpuTime = cputime(cpuTimeStart) |
876 | 194 | storres | ## Output results |
877 | 194 | storres | print ; print "Intervals and HTRNs" ; print |
878 | 194 | storres | for intervalResultsList in globalResultsList: |
879 | 194 | storres | print "[", intervalResultsList[0][0], ",",intervalResultsList[0][1], "]", |
880 | 194 | storres | if len(intervalResultsList) > 1: |
881 | 194 | storres | rootsResultsList = intervalResultsList[1] |
882 | 194 | storres | for specificRootResultsList in rootsResultsList: |
883 | 194 | storres | print "\t", specificRootResultsList[0], |
884 | 194 | storres | if len(specificRootResultsList) > 1: |
885 | 194 | storres | print specificRootResultsList[1], |
886 | 194 | storres | print ; print |
887 | 194 | storres | #print globalResultsList |
888 | 194 | storres | # |
889 | 194 | storres | print "Timers and counters" |
890 | 194 | storres | |
891 | 194 | storres | print "Number of iterations:", iterCount |
892 | 194 | storres | print "Taylor condition failures:", taylCondFailedCount |
893 | 194 | storres | print "Coppersmith condition failures:", coppCondFailedCount |
894 | 194 | storres | print "Resultant condition failures:", resultCondFailedCount |
895 | 194 | storres | print "Iterations count: ", iterCount |
896 | 194 | storres | print "Number of intervals:", len(globalResultsList) |
897 | 194 | storres | print "Number of basis constructions:", basisConstructionsCount |
898 | 194 | storres | print "Total CPU time spent in basis constructions:", \ |
899 | 194 | storres | basisConstructionsFullTime |
900 | 194 | storres | if basisConstructionsCount != 0: |
901 | 194 | storres | print "Average basis construction CPU time:", \ |
902 | 194 | storres | basisConstructionsFullTime/basisConstructionsCount |
903 | 194 | storres | print "Number of reductions:", reductionsCount |
904 | 194 | storres | print "Total CPU time spent in reductions:", reductionsFullTime |
905 | 194 | storres | if reductionsCount != 0: |
906 | 194 | storres | print "Average reduction CPU time:", \ |
907 | 194 | storres | reductionsFullTime/reductionsCount |
908 | 194 | storres | print "Number of resultants computation rounds:", \ |
909 | 194 | storres | resultantsComputationsCount |
910 | 194 | storres | print "Total CPU time spent in resultants computation rounds:", \ |
911 | 194 | storres | resultantsComputationsFullTime |
912 | 194 | storres | if resultantsComputationsCount != 0: |
913 | 194 | storres | print "Average resultants computation round CPU time:", \ |
914 | 194 | storres | resultantsComputationsFullTime/resultantsComputationsCount |
915 | 194 | storres | print "Number of root finding rounds:", rootsComputationsCount |
916 | 194 | storres | print "Total CPU time spent in roots finding rounds:", \ |
917 | 194 | storres | rootsComputationsFullTime |
918 | 194 | storres | if rootsComputationsCount != 0: |
919 | 194 | storres | print "Average roots finding round CPU time:", \ |
920 | 194 | storres | rootsComputationsFullTime/rootsComputationsCount |
921 | 194 | storres | print "Global Wall time:", globalWallTime |
922 | 194 | storres | print "Global CPU time:", globalCpuTime |
923 | 194 | storres | ## Output counters |
924 | 194 | storres | # End srs_runSLZ-v02 |