Statistiques
| Révision :

root / pobysoPythonSage / src / sageSLZ / sageSLZ.sage @ 167

Historique | Voir | Annoter | Télécharger (51,97 ko)

1
r"""
2
Sage core functions needed for the implementation of SLZ.
3

    
4
AUTHORS:
5
- S.T. (2013-08): initial version
6

    
7
Examples:
8

    
9
TODO::
10
"""
11
print "sageSLZ loading..."
12
#
13
def slz_check_htr_value(function, htrValue, lowerBound, upperBound, precision, \
14
                        degree, targetHardnessToRound, alpha):
15
    """
16
    Check an Hard-to-round value.
17
    TODO::
18
    Full rewriting: this is hardly a draft.
19
    """
20
    polyApproxPrec = targetHardnessToRound + 1
21
    polyTargetHardnessToRound = targetHardnessToRound + 1
22
    internalSollyaPrec = ceil((RR('1.5') * targetHardnessToRound) / 64) * 64
23
    RRR = htrValue.parent()
24
    #
25
    ## Compute the scaled function.
26
    fff = slz_compute_scaled_function(f, lowerBound, upperBound, precision)[0]
27
    print "Scaled function:", fff
28
    #
29
    ## Compute the scaling.
30
    boundsIntervalRifSa = RealIntervalField(precision)
31
    domainBoundsInterval = boundsIntervalRifSa(lowerBound, upperBound)
32
    scalingExpressions = \
33
        slz_interval_scaling_expression(domainBoundsInterval, i)
34
    #
35
    ## Get the polynomials, bounds, etc. for all the interval.
36
    resultListOfTuplesOfSo = \
37
        slz_get_intervals_and_polynomials(f, degree, lowerBound, upperBound, \
38
                                          precision, internalSollyaPrec,\
39
                                          2^-(polyApproxPrec))
40
    #
41
    ## We only want one interval.
42
    if len(resultListOfTuplesOfSo) > 1:
43
        print "Too many intervals! Aborting!"
44
        exit
45
    #
46
    ## Get the first tuple of Sollya objects as Sage objects. 
47
    firstTupleSa = \
48
        slz_interval_and_polynomial_to_sage(resultListOfTuplesOfSo[0])
49
    pobyso_set_canonical_on()
50
    #
51
    print "Floatting point polynomial:", firstTupleSa[0]
52
    print "with coefficients precision:", firstTupleSa[0].base_ring().prec()
53
    #
54
    ## From a polynomial over a real ring, create a polynomial over the 
55
    #  rationals ring.
56
    rationalPolynomial = \
57
        slz_float_poly_of_float_to_rat_poly_of_rat(firstTupleSa[0])
58
    print "Rational polynomial:", rationalPolynomial
59
    #
60
    ## Create a polynomial over the rationals that will take integer 
61
    # variables instead of rational. 
62
    rationalPolynomialOfIntegers = \
63
        slz_rat_poly_of_rat_to_rat_poly_of_int(rationalPolynomial, precision)
64
    print "Type:", type(rationalPolynomialOfIntegers)
65
    print "Rational polynomial of integers:", rationalPolynomialOfIntegers
66
    #
67
    ## Check the rational polynomial of integers variables.
68
    # (check against the scaled function).
69
    toIntegerFactor = 2^(precision-1)
70
    intervalCenterAsIntegerSa = int(firstTupleSa[3] * toIntegerFactor)
71
    print "Interval center as integer:", intervalCenterAsIntegerSa
72
    lowerBoundAsIntegerSa = int(firstTupleSa[2].endpoints()[0] * \
73
                                toIntegerFactor) - intervalCenterAsIntegerSa
74
    upperBoundAsIntegerSa = int(firstTupleSa[2].endpoints()[1] * \
75
                                toIntegerFactor) - intervalCenterAsIntegerSa
76
    print "Lower bound as integer:", lowerBoundAsIntegerSa
77
    print "Upper bound as integer:", upperBoundAsIntegerSa
78
    print "Image of the lower bound by the scaled function", \
79
            fff(firstTupleSa[2].endpoints()[0])
80
    print "Image of the lower bound by the approximation polynomial of ints:", \
81
            RRR(rationalPolynomialOfIntegers(lowerBoundAsIntegerSa))
82
    print "Image of the center by the scaled function", fff(firstTupleSa[3])
83
    print "Image of the center by the approximation polynomial of ints:", \
84
            RRR(rationalPolynomialOfIntegers(0))
85
    print "Image of the upper bound by the scaled function", \
86
            fff(firstTupleSa[2].endpoints()[1])
87
    print "Image of the upper bound by the approximation polynomial of ints:", \
88
            RRR(rationalPolynomialOfIntegers(upperBoundAsIntegerSa))
89

    
90
# End slz_check_htr_value.
91

    
92
def slz_compute_binade(number):
93
    """"
94
    For a given number, compute the "binade" that is integer m such that
95
    2^m <= number < 2^(m+1). If number == 0 return None.
96
    """
97
    # Checking the parameter.
98
    # The exception construction is used to detect if numbre is a RealNumber
99
    # since not all numbers have
100
    # the mro() method. sage.rings.real_mpfr.RealNumber do.
101
    try:
102
        classTree = [number.__class__] + number.mro()
103
        # If the number is not a RealNumber (of offspring thereof) try
104
        # to transform it.
105
        if not sage.rings.real_mpfr.RealNumber in classTree:
106
            numberAsRR = RR(number)
107
        else:
108
            numberAsRR = number
109
    except AttributeError:
110
        return None
111
    # Zero special case.
112
    if numberAsRR == 0:
113
        return RR(-infinity)
114
    else:
115
        return floor(numberAsRR.abs().log2())
116
# End slz_compute_binade
117

    
118
#
119
def slz_compute_binade_bounds(number, emin, emax=sys.maxint):
120
    """
121
    For given "real number", compute the bounds of the binade it belongs to.
122
    
123
    NOTE::
124
        When number >= 2^(emax+1), we return the "fake" binade 
125
        [2^(emax+1), +infinity]. Ditto for number <= -2^(emax+1)
126
        with interval [-infinity, -2^(emax+1)]. We want to distinguish
127
        this case from that of "really" invalid arguments.
128
        
129
    """
130
    # Check the parameters.
131
    # RealNumbers or RealNumber offspring only.
132
    # The exception construction is necessary since not all objects have
133
    # the mro() method. sage.rings.real_mpfr.RealNumber do.
134
    try:
135
        classTree = [number.__class__] + number.mro()
136
        if not sage.rings.real_mpfr.RealNumber in classTree:
137
            return None
138
    except AttributeError:
139
        return None
140
    # Non zero negative integers only for emin.
141
    if emin >= 0 or int(emin) != emin:
142
        return None
143
    # Non zero positive integers only for emax.
144
    if emax <= 0 or int(emax) != emax:
145
        return None
146
    precision = number.precision()
147
    RF  = RealField(precision)
148
    if number == 0:
149
        return (RF(0),RF(2^(emin)) - RF(2^(emin-precision)))
150
    # A more precise RealField is needed to avoid unwanted rounding effects
151
    # when computing number.log2().
152
    RRF = RealField(max(2048, 2 * precision))
153
    # number = 0 special case, the binade bounds are 
154
    # [0, 2^emin - 2^(emin-precision)]
155
    # Begin general case
156
    l2 = RRF(number).abs().log2()
157
    # Another special one: beyond largest representable -> "Fake" binade.
158
    if l2 >= emax + 1:
159
        if number > 0:
160
            return (RF(2^(emax+1)), RF(+infinity) )
161
        else:
162
            return (RF(-infinity), -RF(2^(emax+1)))
163
    # Regular case cont'd.
164
    offset = int(l2)
165
    # number.abs() >= 1.
166
    if l2 >= 0:
167
        if number >= 0:
168
            lb = RF(2^offset)
169
            ub = RF(2^(offset + 1) - 2^(-precision+offset+1))
170
        else: #number < 0
171
            lb = -RF(2^(offset + 1) - 2^(-precision+offset+1))
172
            ub = -RF(2^offset)
173
    else: # log2 < 0, number.abs() < 1.
174
        if l2 < emin: # Denormal
175
           # print "Denormal:", l2
176
            if number >= 0:
177
                lb = RF(0)
178
                ub = RF(2^(emin)) - RF(2^(emin-precision))
179
            else: # number <= 0
180
                lb = - RF(2^(emin)) + RF(2^(emin-precision))
181
                ub = RF(0)
182
        elif l2 > emin: # Normal number other than +/-2^emin.
183
            if number >= 0:
184
                if int(l2) == l2:
185
                    lb = RF(2^(offset))
186
                    ub = RF(2^(offset+1)) - RF(2^(-precision+offset+1))
187
                else:
188
                    lb = RF(2^(offset-1))
189
                    ub = RF(2^(offset)) - RF(2^(-precision+offset))
190
            else: # number < 0
191
                if int(l2) == l2: # Binade limit.
192
                    lb = -RF(2^(offset+1) - 2^(-precision+offset+1))
193
                    ub = -RF(2^(offset))
194
                else:
195
                    lb = -RF(2^(offset) - 2^(-precision+offset))
196
                    ub = -RF(2^(offset-1))
197
        else: # l2== emin, number == +/-2^emin
198
            if number >= 0:
199
                lb = RF(2^(offset))
200
                ub = RF(2^(offset+1)) - RF(2^(-precision+offset+1))
201
            else: # number < 0
202
                lb = -RF(2^(offset+1) - 2^(-precision+offset+1))
203
                ub = -RF(2^(offset))
204
    return (lb, ub)
205
# End slz_compute_binade_bounds
206
#
207
def slz_compute_coppersmith_reduced_polynomials(inputPolynomial,
208
                                                 alpha,
209
                                                 N,
210
                                                 iBound,
211
                                                 tBound):
212
    """
213
    For a given set of arguments (see below), compute a list
214
    of "reduced polynomials" that could be used to compute roots
215
    of the inputPolynomial.
216
    INPUT:
217
    
218
    - "inputPolynomial" -- (no default) a bivariate integer polynomial;
219
    - "alpha" -- the alpha parameter of the Coppersmith algorithm;
220
    - "N" -- the modulus;
221
    - "iBound" -- the bound on the first variable;
222
    - "tBound" -- the bound on the second variable.
223
    
224
    OUTPUT:
225
    
226
    A list of bivariate integer polynomial obtained using the Coppersmith
227
    algorithm. The polynomials correspond to the rows of the LLL-reduce
228
    reduced base that comply with the Coppersmith condition.
229
    """
230
    # Arguments check.
231
    if iBound == 0 or tBound == 0:
232
        return ()
233
    # End arguments check.
234
    nAtAlpha = N^alpha
235
    ## Building polynomials for matrix.
236
    polyRing   = inputPolynomial.parent()
237
    # Whatever the 2 variables are actually called, we call them
238
    # 'i' and 't' in all the variable names.
239
    (iVariable, tVariable) = inputPolynomial.variables()[:2]
240
    #print polyVars[0], type(polyVars[0])
241
    initialPolynomial = inputPolynomial.subs({iVariable:iVariable * iBound,
242
                                              tVariable:tVariable * tBound})
243
    polynomialsList = \
244
        spo_polynomial_to_polynomials_list_5(initialPolynomial,
245
                                             alpha,
246
                                             N,
247
                                             iBound,
248
                                             tBound,
249
                                             0)
250
    #print "Polynomials list:", polynomialsList
251
    ## Building the proto matrix.
252
    knownMonomials = []
253
    protoMatrix    = []
254
    for poly in polynomialsList:
255
        spo_add_polynomial_coeffs_to_matrix_row(poly,
256
                                                knownMonomials,
257
                                                protoMatrix,
258
                                                0)
259
    matrixToReduce = spo_proto_to_row_matrix(protoMatrix)
260
    #print matrixToReduce
261
    ## Reduction and checking.
262
    ## S.T. changed 'fp' to None as of Sage 6.6 complying to
263
    #  error message issued when previous code was used.
264
    #reducedMatrix = matrixToReduce.LLL(fp='fp')
265
    reducedMatrix = matrixToReduce.LLL(fp=None)
266
    isLLLReduced  = reducedMatrix.is_LLL_reduced()
267
    if not isLLLReduced:
268
        return ()
269
    monomialsCount     = len(knownMonomials)
270
    monomialsCountSqrt = sqrt(monomialsCount)
271
    #print "Monomials count:", monomialsCount, monomialsCountSqrt.n()
272
    #print reducedMatrix
273
    ## Check the Coppersmith condition for each row and build the reduced 
274
    #  polynomials.
275
    ccReducedPolynomialsList = []
276
    for row in reducedMatrix.rows():
277
        l2Norm = row.norm(2)
278
        if (l2Norm * monomialsCountSqrt) < nAtAlpha:
279
            #print (l2Norm * monomialsCountSqrt).n()
280
            #print l2Norm.n()
281
            ccReducedPolynomial = \
282
                slz_compute_reduced_polynomial(row,
283
                                               knownMonomials,
284
                                               iVariable,
285
                                               iBound,
286
                                               tVariable,
287
                                               tBound)
288
            if not ccReducedPolynomial is None:
289
                ccReducedPolynomialsList.append(ccReducedPolynomial)
290
        else:
291
            #print l2Norm.n() , ">", nAtAlpha
292
            pass
293
    if len(ccReducedPolynomialsList) < 2:
294
        print "Less than 2 Coppersmith condition compliant vectors."
295
        return ()
296
    
297
    #print ccReducedPolynomialsList
298
    return ccReducedPolynomialsList
299
# End slz_compute_coppersmith_reduced_polynomials
300

    
301
def slz_compute_integer_polynomial_modular_roots(inputPolynomial,
302
                                                 alpha,
303
                                                 N,
304
                                                 iBound,
305
                                                 tBound):
306
    """
307
    For a given set of arguments (see below), compute the polynomial modular 
308
    roots, if any.
309
    
310
    """
311
    # Arguments check.
312
    if iBound == 0 or tBound == 0:
313
        return set()
314
    # End arguments check.
315
    nAtAlpha = N^alpha
316
    ## Building polynomials for matrix.
317
    polyRing   = inputPolynomial.parent()
318
    # Whatever the 2 variables are actually called, we call them
319
    # 'i' and 't' in all the variable names.
320
    (iVariable, tVariable) = inputPolynomial.variables()[:2]
321
    ccReducedPolynomialsList = \
322
        slz_compute_coppersmith_reduced_polynomials (inputPolynomial,
323
                                                     alpha,
324
                                                     N,
325
                                                     iBound,
326
                                                     tBound)
327
    if len(ccReducedPolynomialsList) == 0:
328
        return set()   
329
    ## Create the valid (poly1 and poly2 are algebraically independent) 
330
    # resultant tuples (poly1, poly2, resultant(poly1, poly2)).
331
    # Try to mix and match all the polynomial pairs built from the 
332
    # ccReducedPolynomialsList to obtain non zero resultants.
333
    resultantsInITuplesList = []
334
    for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList)-1):
335
        for polyInnerIndex in xrange(polyOuterIndex+1, 
336
                                     len(ccReducedPolynomialsList)):
337
            # Compute the resultant in resultants in the
338
            # first variable (is it the optimal choice?).
339
            resultantInI = \
340
               ccReducedPolynomialsList[polyOuterIndex].resultant(ccReducedPolynomialsList[polyInnerIndex],
341
                                                                  ccReducedPolynomialsList[0].parent(str(iVariable)))
342
            #print "Resultant", resultantInI
343
            # Test algebraic independence.
344
            if not resultantInI.is_zero():
345
                resultantsInITuplesList.append((ccReducedPolynomialsList[polyOuterIndex],
346
                                                 ccReducedPolynomialsList[polyInnerIndex],
347
                                                 resultantInI))
348
    # If no non zero resultant was found: we can't get no algebraically 
349
    # independent polynomials pair. Give up!
350
    if len(resultantsInITuplesList) == 0:
351
        return set()
352
    #print resultantsInITuplesList
353
    # Compute the roots.
354
    Zi = ZZ[str(iVariable)]
355
    Zt = ZZ[str(tVariable)]
356
    polynomialRootsSet = set()
357
    # First, solve in the second variable since resultants are in the first
358
    # variable.
359
    for resultantInITuple in resultantsInITuplesList:
360
        tRootsList = Zt(resultantInITuple[2]).roots()
361
        # For each tRoot, compute the corresponding iRoots and check 
362
        # them in the input polynomial.
363
        for tRoot in tRootsList:
364
            #print "tRoot:", tRoot
365
            # Roots returned by root() are (value, multiplicity) tuples.
366
            iRootsList = \
367
                Zi(resultantInITuple[0].subs({resultantInITuple[0].variables()[1]:tRoot[0]})).roots()
368
            print iRootsList
369
            # The iRootsList can be empty, hence the test.
370
            if len(iRootsList) != 0:
371
                for iRoot in iRootsList:
372
                    polyEvalModN = inputPolynomial(iRoot[0], tRoot[0]) / N
373
                    # polyEvalModN must be an integer.
374
                    if polyEvalModN == int(polyEvalModN):
375
                        polynomialRootsSet.add((iRoot[0],tRoot[0]))
376
    return polynomialRootsSet
377
# End slz_compute_integer_polynomial_modular_roots.
378
#
379
def slz_compute_integer_polynomial_modular_roots_2(inputPolynomial,
380
                                                 alpha,
381
                                                 N,
382
                                                 iBound,
383
                                                 tBound):
384
    """
385
    For a given set of arguments (see below), compute the polynomial modular 
386
    roots, if any.
387
    This version differs in the way resultants are computed.   
388
    """
389
    # Arguments check.
390
    if iBound == 0 or tBound == 0:
391
        return set()
392
    # End arguments check.
393
    nAtAlpha = N^alpha
394
    ## Building polynomials for matrix.
395
    polyRing   = inputPolynomial.parent()
396
    # Whatever the 2 variables are actually called, we call them
397
    # 'i' and 't' in all the variable names.
398
    (iVariable, tVariable) = inputPolynomial.variables()[:2]
399
    #print polyVars[0], type(polyVars[0])
400
    ccReducedPolynomialsList = \
401
        slz_compute_coppersmith_reduced_polynomials (inputPolynomial,
402
                                                     alpha,
403
                                                     N,
404
                                                     iBound,
405
                                                     tBound)
406
    if len(ccReducedPolynomialsList) == 0:
407
        return set()   
408
    ## Create the valid (poly1 and poly2 are algebraically independent) 
409
    # resultant tuples (poly1, poly2, resultant(poly1, poly2)).
410
    # Try to mix and match all the polynomial pairs built from the 
411
    # ccReducedPolynomialsList to obtain non zero resultants.
412
    resultantsInTTuplesList = []
413
    for polyOuterIndex in xrange(0, len(ccReducedPolynomialsList)-1):
414
        for polyInnerIndex in xrange(polyOuterIndex+1, 
415
                                     len(ccReducedPolynomialsList)):
416
            # Compute the resultant in resultants in the
417
            # first variable (is it the optimal choice?).
418
            resultantInT = \
419
               ccReducedPolynomialsList[polyOuterIndex].resultant(ccReducedPolynomialsList[polyInnerIndex],
420
                                                                  ccReducedPolynomialsList[0].parent(str(tVariable)))
421
            #print "Resultant", resultantInT
422
            # Test algebraic independence.
423
            if not resultantInT.is_zero():
424
                resultantsInTTuplesList.append((ccReducedPolynomialsList[polyOuterIndex],
425
                                                 ccReducedPolynomialsList[polyInnerIndex],
426
                                                 resultantInT))
427
    # If no non zero resultant was found: we can't get no algebraically 
428
    # independent polynomials pair. Give up!
429
    if len(resultantsInTTuplesList) == 0:
430
        return set()
431
    #print resultantsInITuplesList
432
    # Compute the roots.
433
    Zi = ZZ[str(iVariable)]
434
    Zt = ZZ[str(tVariable)]
435
    polynomialRootsSet = set()
436
    # First, solve in the second variable since resultants are in the first
437
    # variable.
438
    for resultantInTTuple in resultantsInTTuplesList:
439
        iRootsList = Zi(resultantInTTuple[2]).roots()
440
        # For each iRoot, compute the corresponding tRoots and check 
441
        # them in the input polynomial.
442
        for iRoot in iRootsList:
443
            #print "iRoot:", iRoot
444
            # Roots returned by root() are (value, multiplicity) tuples.
445
            tRootsList = \
446
                Zt(resultantInTTuple[0].subs({resultantInTTuple[0].variables()[0]:iRoot[0]})).roots()
447
            print tRootsList
448
            # The tRootsList can be empty, hence the test.
449
            if len(tRootsList) != 0:
450
                for tRoot in tRootsList:
451
                    polyEvalModN = inputPolynomial(iRoot[0],tRoot[0]) / N
452
                    # polyEvalModN must be an integer.
453
                    if polyEvalModN == int(polyEvalModN):
454
                        polynomialRootsSet.add((iRoot[0],tRoot[0]))
455
    return polynomialRootsSet
456
# End slz_compute_integer_polynomial_modular_roots_2.
457
#
458
def slz_compute_polynomial_and_interval(functionSo, degreeSo, lowerBoundSa, 
459
                                        upperBoundSa, approxPrecSa, 
460
                                        sollyaPrecSa=None):
461
    """
462
    Under the assumptions listed for slz_get_intervals_and_polynomials, compute
463
    a polynomial that approximates the function on a an interval starting
464
    at lowerBoundSa and finishing at a value that guarantees that the polynomial
465
    approximates with the expected precision.
466
    The interval upper bound is lowered until the expected approximation 
467
    precision is reached.
468
    The polynomial, the bounds, the center of the interval and the error
469
    are returned.
470
    OUTPUT:
471
    A tuple made of 4 Sollya objects:
472
    - a polynomial;
473
    - an range (an interval, not in the sense of number given as an interval);
474
    - the center of the interval;
475
    - the maximum error in the approximation of the input functionSo by the
476
      output polynomial ; this error <= approxPrecSaS.
477
    
478
    """
479
    ## Superficial argument check.
480
    if lowerBoundSa > upperBoundSa:
481
        return None
482
    RRR = lowerBoundSa.parent()
483
    intervalShrinkConstFactorSa = RRR('0.8')
484
    absoluteErrorTypeSo = pobyso_absolute_so_so()
485
    currentRangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa)
486
    currentUpperBoundSa = upperBoundSa
487
    currentLowerBoundSa = lowerBoundSa
488
    # What we want here is the polynomial without the variable change, 
489
    # since our actual variable will be x-intervalCenter defined over the 
490
    # domain [lowerBound-intervalCenter , upperBound-intervalCenter]. 
491
    (polySo, intervalCenterSo, maxErrorSo) = \
492
        pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, 
493
                                                    currentRangeSo, 
494
                                                    absoluteErrorTypeSo)
495
    maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
496
    while maxErrorSa > approxPrecSa:
497
        print "++Approximation error:", maxErrorSa
498
        sollya_lib_clear_obj(polySo)
499
        sollya_lib_clear_obj(intervalCenterSo)
500
        sollya_lib_clear_obj(maxErrorSo)
501
        shrinkFactorSa = RRR('5')/(maxErrorSa/approxPrecSa).log2().abs()
502
        #shrinkFactorSa = 1.5/(maxErrorSa/approxPrecSa)
503
        #errorRatioSa = approxPrecSa/maxErrorSa
504
        #print "Error ratio: ", errorRatioSa
505
        if shrinkFactorSa > intervalShrinkConstFactorSa:
506
            actualShrinkFactorSa = intervalShrinkConstFactorSa
507
            #print "Fixed"
508
        else:
509
            actualShrinkFactorSa = shrinkFactorSa
510
            #print "Computed",shrinkFactorSa,maxErrorSa
511
            #print shrinkFactorSa, maxErrorSa
512
        #print "Shrink factor", actualShrinkFactorSa
513
        currentUpperBoundSa = currentLowerBoundSa + \
514
                                  (currentUpperBoundSa - currentLowerBoundSa) * \
515
                                   actualShrinkFactorSa
516
        #print "Current upper bound:", currentUpperBoundSa
517
        sollya_lib_clear_obj(currentRangeSo)
518
        if currentUpperBoundSa <= currentLowerBoundSa or \
519
              currentUpperBoundSa == currentLowerBoundSa.nextabove():
520
            sollya_lib_clear_obj(absoluteErrorTypeSo)
521
            print "Can't find an interval."
522
            print "Use either or both a higher polynomial degree or a higher",
523
            print "internal precision."
524
            print "Aborting!"
525
            return (None, None, None, None)
526
        currentRangeSo = pobyso_bounds_to_range_sa_so(currentLowerBoundSa, 
527
                                                      currentUpperBoundSa)
528
        # print "New interval:",
529
        # pobyso_autoprint(currentRangeSo)
530
        #print "Second Taylor expansion call."
531
        (polySo, intervalCenterSo, maxErrorSo) = \
532
            pobyso_taylor_expansion_no_change_var_so_so(functionSo, degreeSo, 
533
                                                        currentRangeSo, 
534
                                                        absoluteErrorTypeSo)
535
        #maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo, RRR)
536
        #print "Max errorSo:",
537
        #pobyso_autoprint(maxErrorSo)
538
        maxErrorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
539
        #print "Max errorSa:", maxErrorSa
540
        #print "Sollya prec:",
541
        #pobyso_autoprint(sollya_lib_get_prec(None))
542
    sollya_lib_clear_obj(absoluteErrorTypeSo)
543
    return((polySo, currentRangeSo, intervalCenterSo, maxErrorSo))
544
# End slz_compute_polynomial_and_interval
545

    
546
def slz_compute_reduced_polynomial(matrixRow,
547
                                    knownMonomials,
548
                                    var1,
549
                                    var1Bound,
550
                                    var2,
551
                                    var2Bound):
552
    """
553
    Compute a polynomial from a single reduced matrix row.
554
    This function was introduced in order to avoid the computation of the
555
    all the polynomials  from the full matrix (even those built from rows 
556
    that do no verify the Coppersmith condition) as this may involves 
557
    expensive operations over (large) integers.
558
    """
559
    ## Check arguments.
560
    if len(knownMonomials) == 0:
561
        return None
562
    # varNounds can be zero since 0^0 returns 1.
563
    if (var1Bound < 0) or (var2Bound < 0):
564
        return None
565
    ## Initialisations. 
566
    polynomialRing = knownMonomials[0].parent() 
567
    currentPolynomial = polynomialRing(0)
568
    # TODO: use zip instead of indices.
569
    for colIndex in xrange(0, len(knownMonomials)):
570
        currentCoefficient = matrixRow[colIndex]
571
        if currentCoefficient != 0:
572
            #print "Current coefficient:", currentCoefficient
573
            currentMonomial = knownMonomials[colIndex]
574
            #print "Monomial as multivariate polynomial:", \
575
            #currentMonomial, type(currentMonomial)
576
            degreeInVar1 = currentMonomial.degree(var1)
577
            #print "Degree in var1", var1, ":", degreeInVar1
578
            degreeInVar2 = currentMonomial.degree(var2)
579
            #print "Degree in var2", var2, ":", degreeInVar2
580
            if degreeInVar1 > 0:
581
                currentCoefficient = \
582
                    currentCoefficient / (var1Bound^degreeInVar1)
583
                #print "varBound1 in degree:", var1Bound^degreeInVar1
584
                #print "Current coefficient(1)", currentCoefficient
585
            if degreeInVar2 > 0:
586
                currentCoefficient = \
587
                    currentCoefficient / (var2Bound^degreeInVar2)
588
                #print "Current coefficient(2)", currentCoefficient
589
            #print "Current reduced monomial:", (currentCoefficient * \
590
            #                                    currentMonomial)
591
            currentPolynomial += (currentCoefficient * currentMonomial)
592
            #print "Current polynomial:", currentPolynomial
593
        # End if
594
    # End for colIndex.
595
    #print "Type of the current polynomial:", type(currentPolynomial)
596
    return(currentPolynomial)
597
# End slz_compute_reduced_polynomial
598
#
599
def slz_compute_reduced_polynomials(reducedMatrix,
600
                                        knownMonomials,
601
                                        var1,
602
                                        var1Bound,
603
                                        var2,
604
                                        var2Bound):
605
    """
606
    Legacy function, use slz_compute_reduced_polynomials_list
607
    """
608
    return(slz_compute_reduced_polynomials_list(reducedMatrix,
609
                                                knownMonomials,
610
                                                var1,
611
                                                var1Bound,
612
                                                var2,
613
                                                var2Bound)
614
    )
615
def slz_compute_reduced_polynomials_list(reducedMatrix,
616
                                         knownMonomials,
617
                                         var1,
618
                                         var1Bound,
619
                                         var2,
620
                                         var2Bound):
621
    """
622
    From a reduced matrix, holding the coefficients, from a monomials list,
623
    from the bounds of each variable, compute the corresponding polynomials
624
    scaled back by dividing by the "right" powers of the variables bounds.
625
    
626
    The elements in knownMonomials must be of the "right" polynomial type.
627
    They set the polynomial type of the output polynomials list.
628
    @param reducedMatrix:  the reduced matrix as output from LLL;
629
    @param kwnonMonomials: the ordered list of the monomials used to
630
                           build the polynomials;
631
    @param var1:           the first variable (of the "right" type);
632
    @param var1Bound:      the first variable bound;
633
    @param var2:           the second variable (of the "right" type);
634
    @param var2Bound:      the second variable bound.
635
    @return: a list of polynomials obtained with the reduced coefficients
636
             and scaled down with the bounds
637
    """
638
    
639
    # TODO: check input arguments.
640
    reducedPolynomials = []
641
    #print "type var1:", type(var1), " - type var2:", type(var2)
642
    for matrixRow in reducedMatrix.rows():
643
        currentPolynomial = 0
644
        for colIndex in xrange(0, len(knownMonomials)):
645
            currentCoefficient = matrixRow[colIndex]
646
            if currentCoefficient != 0:
647
                #print "Current coefficient:", currentCoefficient
648
                currentMonomial = knownMonomials[colIndex]
649
                parentRing = currentMonomial.parent()
650
                #print "Monomial as multivariate polynomial:", \
651
                #currentMonomial, type(currentMonomial)
652
                degreeInVar1 = currentMonomial.degree(parentRing(var1))
653
                #print "Degree in var", var1, ":", degreeInVar1
654
                degreeInVar2 = currentMonomial.degree(parentRing(var2))
655
                #print "Degree in var", var2, ":", degreeInVar2
656
                if degreeInVar1 > 0:
657
                    currentCoefficient /= var1Bound^degreeInVar1
658
                    #print "varBound1 in degree:", var1Bound^degreeInVar1
659
                    #print "Current coefficient(1)", currentCoefficient
660
                if degreeInVar2 > 0:
661
                    currentCoefficient /= var2Bound^degreeInVar2
662
                    #print "Current coefficient(2)", currentCoefficient
663
                #print "Current reduced monomial:", (currentCoefficient * \
664
                #                                    currentMonomial)
665
                currentPolynomial += (currentCoefficient * currentMonomial)
666
                #print "Current polynomial:", currentPolynomial
667
            # End if
668
        # End for colIndex.
669
        #print "Type of the current polynomial:", type(currentPolynomial)
670
        reducedPolynomials.append(currentPolynomial)
671
    return reducedPolynomials 
672
# End slz_compute_reduced_polynomials.
673

    
674
def slz_compute_scaled_function(functionSa,
675
                                lowerBoundSa,
676
                                upperBoundSa,
677
                                floatingPointPrecSa,
678
                                debug=False):
679
    """
680
    From a function, compute the scaled function whose domain
681
    is included in [1, 2) and whose image is also included in [1,2).
682
    Return a tuple: 
683
    [0]: the scaled function
684
    [1]: the scaled domain lower bound
685
    [2]: the scaled domain upper bound
686
    [3]: the scaled image lower bound
687
    [4]: the scaled image upper bound
688
    """
689
    x = functionSa.variables()[0]
690
    # Reassert f as a function (an not a mere expression).
691
    
692
    # Scalling the domain -> [1,2[.
693
    boundsIntervalRifSa = RealIntervalField(floatingPointPrecSa)
694
    domainBoundsIntervalSa = boundsIntervalRifSa(lowerBoundSa, upperBoundSa)
695
    (invDomainScalingExpressionSa, domainScalingExpressionSa) = \
696
        slz_interval_scaling_expression(domainBoundsIntervalSa, x)
697
    if debug:
698
        print "domainScalingExpression for argument :", \
699
        invDomainScalingExpressionSa
700
        print "f: ", f
701
    ff = f.subs({x : domainScalingExpressionSa})
702
    #ff = f.subs_expr(x==domainScalingExpressionSa)
703
    domainScalingFunction(x) = invDomainScalingExpressionSa
704
    scaledLowerBoundSa = \
705
        domainScalingFunction(lowerBoundSa).n(prec=floatingPointPrecSa)
706
    scaledUpperBoundSa = \
707
        domainScalingFunction(upperBoundSa).n(prec=floatingPointPrecSa)
708
    if debug:
709
        print 'ff:', ff, "- Domain:", scaledLowerBoundSa, \
710
              scaledUpperBoundSa
711
    #
712
    # Scalling the image -> [1,2[.
713
    flbSa = ff(scaledLowerBoundSa).n(prec=floatingPointPrecSa)
714
    fubSa = ff(scaledUpperBoundSa).n(prec=floatingPointPrecSa)
715
    if flbSa <= fubSa: # Increasing
716
        imageBinadeBottomSa = floor(flbSa.log2())
717
    else: # Decreasing
718
        imageBinadeBottomSa = floor(fubSa.log2())
719
    if debug:
720
        print 'ff:', ff, '- Image:', flbSa, fubSa, imageBinadeBottomSa
721
    imageBoundsIntervalSa = boundsIntervalRifSa(flbSa, fubSa)
722
    (invImageScalingExpressionSa,imageScalingExpressionSa) = \
723
        slz_interval_scaling_expression(imageBoundsIntervalSa, x)
724
    if debug:
725
        print "imageScalingExpression for argument :", \
726
              invImageScalingExpressionSa
727
    iis = invImageScalingExpressionSa.function(x)
728
    fff = iis.subs({x:ff})
729
    if debug:
730
        print "fff:", fff,
731
        print " - Image:", fff(scaledLowerBoundSa), fff(scaledUpperBoundSa)
732
    return([fff, scaledLowerBoundSa, scaledUpperBoundSa, \
733
            fff(scaledLowerBoundSa), fff(scaledUpperBoundSa)])
734
# End slz_compute_scaled_function
735

    
736
def slz_float_poly_of_float_to_rat_poly_of_rat(polyOfFloat):
737
    # Create a polynomial over the rationals.
738
    polynomialRing = QQ[str(polyOfFloat.variables()[0])]
739
    return(polynomialRing(polyOfFloat))
740
# End slz_float_poly_of_float_to_rat_poly_of_rat.
741

    
742
def slz_get_intervals_and_polynomials(functionSa, degreeSa, 
743
                                      lowerBoundSa, 
744
                                      upperBoundSa, floatingPointPrecSa, 
745
                                      internalSollyaPrecSa, approxPrecSa):
746
    """
747
    Under the assumption that:
748
    - functionSa is monotonic on the [lowerBoundSa, upperBoundSa] interval;
749
    - lowerBound and upperBound belong to the same binade.
750
    from a:
751
    - function;
752
    - a degree
753
    - a pair of bounds;
754
    - the floating-point precision we work on;
755
    - the internal Sollya precision;
756
    - the requested approximation error
757
    The initial interval is, possibly, splitted into smaller intervals.
758
    It return a list of tuples, each made of:
759
    - a first polynomial (without the changed variable f(x) = p(x-x0));
760
    - a second polynomial (with a changed variable f(x) = q(x))
761
    - the approximation interval;
762
    - the center, x0, of the interval;
763
    - the corresponding approximation error.
764
    TODO: fix endless looping for some parameters sets.
765
    """
766
    resultArray = []
767
    # Set Sollya to the necessary internal precision.
768
    precChangedSa = False
769
    currentSollyaPrecSo = pobyso_get_prec_so()
770
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
771
    if internalSollyaPrecSa > currentSollyaPrecSa:
772
        pobyso_set_prec_sa_so(internalSollyaPrecSa)
773
        precChangedSa = True
774
    #
775
    x = functionSa.variables()[0] # Actual variable name can be anything.
776
    # Scaled function: [1=,2] -> [1,2].
777
    (fff, scaledLowerBoundSa, scaledUpperBoundSa,                             \
778
            scaledLowerBoundImageSa, scaledUpperBoundImageSa) =               \
779
                slz_compute_scaled_function(functionSa,                       \
780
                                            lowerBoundSa,                     \
781
                                            upperBoundSa,                     \
782
                                            floatingPointPrecSa)
783
    # In case bounds were in the negative real one may need to
784
    # switch scaled bounds.
785
    if scaledLowerBoundSa > scaledUpperBoundSa:
786
        scaledLowerBoundSa, scaledUpperBoundSa = \
787
            scaledUpperBoundSa, scaledLowerBoundSa
788
        #print "Switching!"
789
    print "Approximation precision: ", RR(approxPrecSa)
790
    # Prepare the arguments for the Taylor expansion computation with Sollya.
791
    functionSo = \
792
      pobyso_parse_string_sa_so(fff._assume_str().replace('_SAGE_VAR_', ''))
793
    degreeSo = pobyso_constant_from_int_sa_so(degreeSa)
794
    scaledBoundsSo = pobyso_bounds_to_range_sa_so(scaledLowerBoundSa, 
795
                                                  scaledUpperBoundSa)
796
    # Compute the first Taylor expansion.
797
    print "Computing first Taylor expansion..."
798
    (polySo, boundsSo, intervalCenterSo, maxErrorSo) = \
799
        slz_compute_polynomial_and_interval(functionSo, degreeSo,
800
                                        scaledLowerBoundSa, scaledUpperBoundSa,
801
                                        approxPrecSa, internalSollyaPrecSa)
802
    print "...done."
803
    if polySo is None:
804
        print "slz_get_intervals_and_polynomials: Aborting and returning None!"
805
        if precChangedSa:
806
            pobyso_set_prec_so_so(currentSollyaPrecSo)
807
            sollya_lib_clear_obj(currentSollyaPrecSo)
808
        sollya_lib_clear_obj(functionSo)
809
        sollya_lib_clear_obj(degreeSo)
810
        sollya_lib_clear_obj(scaledBoundsSo)
811
        return None
812
    realIntervalField = RealIntervalField(max(lowerBoundSa.parent().precision(),
813
                                              upperBoundSa.parent().precision()))
814
    boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField)
815
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
816
    print "First approximation error:", errorSa.n(digits=10)
817
    # If the error and interval are OK a the first try, just return.
818
    if boundsSa.endpoints()[1] >= scaledUpperBoundSa:
819
        # Change variable stuff in Sollya x -> x0-x.
820
        changeVarExpressionSo = sollya_lib_build_function_sub( \
821
                              sollya_lib_build_function_free_variable(), \
822
                              sollya_lib_copy_obj(intervalCenterSo))
823
        polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpressionSo) 
824
        sollya_lib_clear_obj(changeVarExpressionSo)
825
        resultArray.append((polySo, polyVarChangedSo, boundsSo, \
826
                            intervalCenterSo, maxErrorSo))
827
        if internalSollyaPrecSa != currentSollyaPrecSa:
828
            pobyso_set_prec_sa_so(currentSollyaPrecSa)
829
        sollya_lib_clear_obj(currentSollyaPrecSo)
830
        sollya_lib_clear_obj(functionSo)
831
        sollya_lib_clear_obj(degreeSo)
832
        sollya_lib_clear_obj(scaledBoundsSo)
833
        #print "Approximation error:", errorSa
834
        return resultArray
835
    # The returned interval upper bound does not reach the requested upper
836
    # upper bound: compute the next upper bound.
837
    # The following ratio is always >= 1
838
    currentErrorRatio = approxPrecSa / errorSa
839
    # Starting point for the next upper bound.
840
    currentScaledUpperBoundSa = boundsSa.endpoints()[1]
841
    boundsWidthSa = boundsSa.endpoints()[1] - boundsSa.endpoints()[0]
842
    # Compute the increment. 
843
    if currentErrorRatio > RR('1000'): # ]1.5, infinity[
844
        currentScaledUpperBoundSa += \
845
                        currentErrorRatio * boundsWidthSa * 2
846
    else:  # [1, 1.5]
847
        currentScaledUpperBoundSa += \
848
                        (RR('1.0') + currentErrorRatio.log() / 500) * boundsWidthSa
849
    # Take into account the original interval upper bound.                     
850
    if currentScaledUpperBoundSa > scaledUpperBoundSa:
851
        currentScaledUpperBoundSa = scaledUpperBoundSa
852
    # Compute the other expansions.
853
    while boundsSa.endpoints()[1] < scaledUpperBoundSa:
854
        currentScaledLowerBoundSa = boundsSa.endpoints()[1]
855
        (polySo, boundsSo, intervalCenterSo, maxErrorSo) = \
856
            slz_compute_polynomial_and_interval(functionSo, degreeSo,
857
                                            currentScaledLowerBoundSa, 
858
                                            currentScaledUpperBoundSa, 
859
                                            approxPrecSa, 
860
                                            internalSollyaPrecSa)
861
        errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
862
        if  errorSa < approxPrecSa:
863
            # Change variable stuff
864
            #print "Approximation error:", errorSa
865
            changeVarExpressionSo = sollya_lib_build_function_sub(
866
                                    sollya_lib_build_function_free_variable(), 
867
                                    sollya_lib_copy_obj(intervalCenterSo))
868
            polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpressionSo) 
869
            sollya_lib_clear_obj(changeVarExpressionSo)
870
            resultArray.append((polySo, polyVarChangedSo, boundsSo, \
871
                                intervalCenterSo, maxErrorSo))
872
        boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField)
873
        # Compute the next upper bound.
874
        # The following ratio is always >= 1
875
        currentErrorRatio = approxPrecSa / errorSa
876
        # Starting point for the next upper bound.
877
        currentScaledUpperBoundSa = boundsSa.endpoints()[1]
878
        boundsWidthSa = boundsSa.endpoints()[1] - boundsSa.endpoints()[0]
879
        # Compute the increment. 
880
        if currentErrorRatio > RR('1000'): # ]1.5, infinity[
881
            currentScaledUpperBoundSa += \
882
                            currentErrorRatio * boundsWidthSa * 2
883
        else:  # [1, 1.5]
884
            currentScaledUpperBoundSa += \
885
                            (RR('1.0') + currentErrorRatio.log()/500) * boundsWidthSa
886
        #print "currentErrorRatio:", currentErrorRatio
887
        #print "currentScaledUpperBoundSa", currentScaledUpperBoundSa
888
        # Test for insufficient precision.
889
        if currentScaledUpperBoundSa == scaledLowerBoundSa:
890
            print "Can't shrink the interval anymore!"
891
            print "You should consider increasing the Sollya internal precision"
892
            print "or the polynomial degree."
893
            print "Giving up!"
894
            if internalSollyaPrecSa != currentSollyaPrecSa:
895
                pobyso_set_prec_sa_so(currentSollyaPrecSa)
896
            sollya_lib_clear_obj(currentSollyaPrecSo)
897
            sollya_lib_clear_obj(functionSo)
898
            sollya_lib_clear_obj(degreeSo)
899
            sollya_lib_clear_obj(scaledBoundsSo)
900
            return None
901
        if currentScaledUpperBoundSa > scaledUpperBoundSa:
902
            currentScaledUpperBoundSa = scaledUpperBoundSa
903
    if internalSollyaPrecSa > currentSollyaPrecSa:
904
        pobyso_set_prec_so_so(currentSollyaPrecSo)
905
    sollya_lib_clear_obj(currentSollyaPrecSo)
906
    sollya_lib_clear_obj(functionSo)
907
    sollya_lib_clear_obj(degreeSo)
908
    sollya_lib_clear_obj(scaledBoundsSo)
909
    return(resultArray)
910
# End slz_get_intervals_and_polynomials
911

    
912

    
913
def slz_interval_scaling_expression(boundsInterval, expVar):
914
    """
915
    Compute the scaling expression to map an interval that spans at most
916
    a single binade into [1, 2) and the inverse expression as well.
917
    If the interval spans more than one binade, result is wrong since
918
    scaling is only based on the lower bound.
919
    Not very sure that the transformation makes sense for negative numbers.
920
    """
921
    # The "one of the bounds is 0" special case. Here we consider
922
    # the interval as the subnormals binade.
923
    if boundsInterval.endpoints()[0] == 0 or boundsInterval.endpoints()[1] == 0:
924
        # The upper bound is (or should be) positive.
925
        if boundsInterval.endpoints()[0] == 0:
926
            if boundsInterval.endpoints()[1] == 0:
927
                return None
928
            binade = slz_compute_binade(boundsInterval.endpoints()[1])
929
            l2     = boundsInterval.endpoints()[1].abs().log2()
930
            # The upper bound is a power of two
931
            if binade == l2:
932
                scalingCoeff    = 2^(-binade)
933
                invScalingCoeff = 2^(binade)
934
                scalingOffset   = 1
935
                return((scalingCoeff * expVar + scalingOffset),\
936
                       ((expVar - scalingOffset) * invScalingCoeff))
937
            else:
938
                scalingCoeff    = 2^(-binade-1)
939
                invScalingCoeff = 2^(binade+1)
940
                scalingOffset   = 1
941
                return((scalingCoeff * expVar + scalingOffset),\
942
                    ((expVar - scalingOffset) * invScalingCoeff))
943
        # The lower bound is (or should be) negative.
944
        if boundsInterval.endpoints()[1] == 0:
945
            if boundsInterval.endpoints()[0] == 0:
946
                return None
947
            binade = slz_compute_binade(boundsInterval.endpoints()[0])
948
            l2     = boundsInterval.endpoints()[0].abs().log2()
949
            # The upper bound is a power of two
950
            if binade == l2:
951
                scalingCoeff    = -2^(-binade)
952
                invScalingCoeff = -2^(binade)
953
                scalingOffset   = 1
954
                return((scalingCoeff * expVar + scalingOffset),\
955
                    ((expVar - scalingOffset) * invScalingCoeff))
956
            else:
957
                scalingCoeff    = -2^(-binade-1)
958
                invScalingCoeff = -2^(binade+1)
959
                scalingOffset   = 1
960
                return((scalingCoeff * expVar + scalingOffset),\
961
                   ((expVar - scalingOffset) * invScalingCoeff))
962
    # General case
963
    lbBinade = slz_compute_binade(boundsInterval.endpoints()[0])
964
    ubBinade = slz_compute_binade(boundsInterval.endpoints()[1])
965
    # We allow for a single exception in binade spanning is when the
966
    # "outward bound" is a power of 2 and its binade is that of the
967
    # "inner bound" + 1.
968
    if boundsInterval.endpoints()[0] > 0:
969
        ubL2 = boundsInterval.endpoints()[1].abs().log2()
970
        if lbBinade != ubBinade:
971
            print "Different binades."
972
            if ubL2 != ubBinade:
973
                print "Not a power of 2."
974
                return None
975
            elif abs(ubBinade - lbBinade) > 1:
976
                print "Too large span:", abs(ubBinade - lbBinade)
977
                return None
978
    else:
979
        lbL2 = boundsInterval.endpoints()[0].abs().log2()
980
        if lbBinade != ubBinade:
981
            print "Different binades."
982
            if lbL2 != lbBinade:
983
                print "Not a power of 2."
984
                return None
985
            elif abs(ubBinade - lbBinade) > 1:
986
                print "Too large span:", abs(ubBinade - lbBinade)
987
                return None
988
    #print "Lower bound binade:", binade
989
    if boundsInterval.endpoints()[0] > 0:
990
        return((2^(-lbBinade) * expVar),(2^(lbBinade) * expVar))
991
    else:
992
        return((-(2^(-ubBinade)) * expVar),(-(2^(ubBinade)) * expVar))
993
"""
994
    # Code sent to attic. Should be the base for a 
995
    # "slz_interval_translate_expression" rather than scale.
996
    # Extra control and special cases code  added in  
997
    # slz_interval_scaling_expression could (should ?) be added to
998
    # this new function.
999
    # The scaling offset is only used for negative numbers.
1000
    # When the absolute value of the lower bound is < 0.
1001
    if abs(boundsInterval.endpoints()[0]) < 1:
1002
        if boundsInterval.endpoints()[0] >= 0:
1003
            scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2())
1004
            invScalingCoeff = 1/scalingCoeff
1005
            return((scalingCoeff * expVar, 
1006
                    invScalingCoeff * expVar))
1007
        else:
1008
            scalingCoeff = \
1009
                2^(floor((-boundsInterval.endpoints()[0]).log2()) - 1)
1010
            scalingOffset = -3 * scalingCoeff
1011
            return((scalingCoeff * expVar + scalingOffset,
1012
                    1/scalingCoeff * expVar + 3))
1013
    else: 
1014
        if boundsInterval.endpoints()[0] >= 0:
1015
            scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2())
1016
            scalingOffset = 0
1017
            return((scalingCoeff * expVar, 
1018
                    1/scalingCoeff * expVar))
1019
        else:
1020
            scalingCoeff = \
1021
                2^(floor((-boundsInterval.endpoints()[1]).log2()))
1022
            scalingOffset =  -3 * scalingCoeff
1023
            #scalingOffset = 0
1024
            return((scalingCoeff * expVar + scalingOffset,
1025
                    1/scalingCoeff * expVar + 3))
1026
"""
1027
# End slz_interval_scaling_expression
1028
   
1029
def slz_interval_and_polynomial_to_sage(polyRangeCenterErrorSo):
1030
    """
1031
    Compute the Sage version of the Taylor polynomial and it's
1032
    companion data (interval, center...)
1033
    The input parameter is a five elements tuple:
1034
    - [0]: the polyomial (without variable change), as polynomial over a
1035
           real ring;
1036
    - [1]: the polyomial (with variable change done in Sollya), as polynomial
1037
           over a real ring;
1038
    - [2]: the interval (as Sollya range);
1039
    - [3]: the interval center;
1040
    - [4]: the approximation error. 
1041
    
1042
    The function return a 5 elements tuple: formed with all the 
1043
    input elements converted into their Sollya counterpart.
1044
    """
1045
    polynomialSa = pobyso_get_poly_so_sa(polyRangeCenterErrorSo[0])
1046
    polynomialChangedVarSa = pobyso_get_poly_so_sa(polyRangeCenterErrorSo[1])
1047
    intervalSa = \
1048
            pobyso_get_interval_from_range_so_sa(polyRangeCenterErrorSo[2])
1049
    centerSa = \
1050
            pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[3])
1051
    errorSa = \
1052
            pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[4])
1053
    return((polynomialSa, polynomialChangedVarSa, intervalSa, centerSa, errorSa))
1054
    # End slz_interval_and_polynomial_to_sage
1055

    
1056
def slz_rat_poly_of_int_to_poly_for_coppersmith(ratPolyOfInt, 
1057
                                                precision,
1058
                                                targetHardnessToRound,
1059
                                                variable1,
1060
                                                variable2):
1061
    """
1062
    Creates a new multivariate polynomial with integer coefficients for use
1063
     with the Coppersmith method.
1064
    A the same time it computes :
1065
    - 2^K (N);
1066
    - 2^k (bound on the second variable)
1067
    - lcm
1068

    
1069
    :param ratPolyOfInt: a polynomial with rational coefficients and integer
1070
                         variables.
1071
    :param precision: the precision of the floating-point coefficients.
1072
    :param targetHardnessToRound: the hardness to round we want to check.
1073
    :param variable1: the first variable of the polynomial (an expression).
1074
    :param variable2: the second variable of the polynomial (an expression).
1075
    
1076
    :returns: a 4 elements tuple:
1077
                - the polynomial;
1078
                - the modulus (N);
1079
                - the t bound;
1080
                - the lcm used to compute the integral coefficients and the 
1081
                  module.
1082
    """
1083
    # Create a new integer polynomial ring.
1084
    IP = PolynomialRing(ZZ, name=str(variable1) + "," + str(variable2))
1085
    # Coefficients are issued in the increasing power order.
1086
    ratPolyCoefficients = ratPolyOfInt.coefficients()
1087
    # Print the reversed list for debugging.
1088
    print "Rational polynomial coefficients:", ratPolyCoefficients[::-1]
1089
    # Build the list of number we compute the lcm of.
1090
    coefficientDenominators = sro_denominators(ratPolyCoefficients)
1091
    coefficientDenominators.append(2^precision)
1092
    coefficientDenominators.append(2^(targetHardnessToRound + 1))
1093
    leastCommonMultiple = lcm(coefficientDenominators)
1094
    # Compute the expression corresponding to the new polynomial
1095
    coefficientNumerators =  sro_numerators(ratPolyCoefficients)
1096
    #print coefficientNumerators
1097
    polynomialExpression = 0
1098
    power = 0
1099
    # Iterate over two lists at the same time, stop when the shorter is
1100
    # exhausted.
1101
    for numerator, denominator in \
1102
                        zip(coefficientNumerators, coefficientDenominators):
1103
        multiplicator = leastCommonMultiple / denominator 
1104
        newCoefficient = numerator * multiplicator
1105
        polynomialExpression += newCoefficient * variable1^power
1106
        power +=1
1107
    polynomialExpression += - variable2
1108
    return (IP(polynomialExpression),
1109
            leastCommonMultiple / 2^precision, # 2^K or N.
1110
            leastCommonMultiple / 2^(targetHardnessToRound + 1), # tBound
1111
            leastCommonMultiple) # If we want to make test computations.
1112
        
1113
# End slz_ratPoly_of_int_to_poly_for_coppersmith
1114

    
1115
def slz_rat_poly_of_rat_to_rat_poly_of_int(ratPolyOfRat, 
1116
                                          precision):
1117
    """
1118
    Makes a variable substitution into the input polynomial so that the output
1119
    polynomial can take integer arguments.
1120
    All variables of the input polynomial "have precision p". That is to say
1121
    that they are rationals with denominator == 2^(precision - 1): 
1122
    x = y/2^(precision - 1).
1123
    We "incorporate" these denominators into the coefficients with, 
1124
    respectively, the "right" power.
1125
    """
1126
    polynomialField = ratPolyOfRat.parent()
1127
    polynomialVariable = ratPolyOfRat.variables()[0]
1128
    #print "The polynomial field is:", polynomialField
1129
    return \
1130
        polynomialField(ratPolyOfRat.subs({polynomialVariable : \
1131
                                   polynomialVariable/2^(precision-1)}))
1132
    
1133
    # Return a tuple:
1134
    # - the bivariate integer polynomial in (i,j);
1135
    # - 2^K
1136
# End slz_rat_poly_of_rat_to_rat_poly_of_int
1137

    
1138

    
1139
print "\t...sageSLZ loaded"