Statistiques
| Révision :

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

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

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

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

    
643
def slz_compute_scaled_function(functionSa,
644
                                lowerBoundSa,
645
                                upperBoundSa,
646
                                floatingPointPrecSa):
647
    """
648
    From a function, compute the scaled function whose domain
649
    is included in [1, 2) and whose image is also included in [1,2).
650
    Return a tuple: 
651
    [0]: the scaled function
652
    [1]: the scaled domain lower bound
653
    [2]: the scaled domain upper bound
654
    [3]: the scaled image lower bound
655
    [4]: the scaled image upper bound
656
    """
657
    x = functionSa.variables()[0]
658
    # Reassert f as a function (an not a mere expression).
659
    
660
    # Scalling the domain -> [1,2[.
661
    boundsIntervalRifSa = RealIntervalField(floatingPointPrecSa)
662
    domainBoundsIntervalSa = boundsIntervalRifSa(lowerBoundSa, upperBoundSa)
663
    (domainScalingExpressionSa, invDomainScalingExpressionSa) = \
664
        slz_interval_scaling_expression(domainBoundsIntervalSa, x)
665
    print "domainScalingExpression for argument :", invDomainScalingExpressionSa
666
    print "f: ", f
667
    ff = f.subs({x : domainScalingExpressionSa})
668
    #ff = f.subs_expr(x==domainScalingExpressionSa)
669
    domainScalingFunction(x) = invDomainScalingExpressionSa
670
    scaledLowerBoundSa = \
671
        domainScalingFunction(lowerBoundSa).n(prec=floatingPointPrecSa)
672
    scaledUpperBoundSa = \
673
        domainScalingFunction(upperBoundSa).n(prec=floatingPointPrecSa)
674
    print 'ff:', ff, "- Domain:", scaledLowerBoundSa, scaledUpperBoundSa
675
    #
676
    # Scalling the image -> [1,2[.
677
    flbSa = ff(scaledLowerBoundSa).n(prec=floatingPointPrecSa)
678
    fubSa = ff(scaledUpperBoundSa).n(prec=floatingPointPrecSa)
679
    if flbSa <= fubSa: # Increasing
680
        imageBinadeBottomSa = floor(flbSa.log2())
681
    else: # Decreasing
682
        imageBinadeBottomSa = floor(fubSa.log2())
683
    print 'ff:', ff, '- Image:', flbSa, fubSa, imageBinadeBottomSa
684
    imageBoundsIntervalSa = boundsIntervalRifSa(flbSa, fubSa)
685
    (imageScalingExpressionSa, invImageScalingExpressionSa) = \
686
        slz_interval_scaling_expression(imageBoundsIntervalSa, x)
687
    print "imageScalingExpression for argument :", invImageScalingExpressionSa
688
    iis = invImageScalingExpressionSa.function(x)
689
    fff = iis.subs({x:ff})
690
    print "fff:", fff,
691
    print " - Image:", fff(scaledLowerBoundSa), fff(scaledUpperBoundSa)
692
    return([fff, scaledLowerBoundSa, scaledUpperBoundSa, \
693
            fff(scaledLowerBoundSa), fff(scaledUpperBoundSa)])
694
# End slz_compute_scaled_function
695

    
696
def slz_float_poly_of_float_to_rat_poly_of_rat(polyOfFloat):
697
    # Create a polynomial over the rationals.
698
    polynomialRing = QQ[str(polyOfFloat.variables()[0])]
699
    return(polynomialRing(polyOfFloat))
700
# End slz_float_poly_of_float_to_rat_poly_of_rat.
701

    
702
def slz_get_intervals_and_polynomials(functionSa, degreeSa, 
703
                                      lowerBoundSa, 
704
                                      upperBoundSa, floatingPointPrecSa, 
705
                                      internalSollyaPrecSa, approxPrecSa):
706
    """
707
    Under the assumption that:
708
    - functionSa is monotonic on the [lowerBoundSa, upperBoundSa] interval;
709
    - lowerBound and upperBound belong to the same binade.
710
    from a:
711
    - function;
712
    - a degree
713
    - a pair of bounds;
714
    - the floating-point precision we work on;
715
    - the internal Sollya precision;
716
    - the requested approximation error
717
    The initial interval is, possibly, splitted into smaller intervals.
718
    It return a list of tuples, each made of:
719
    - a first polynomial (without the changed variable f(x) = p(x-x0));
720
    - a second polynomial (with a changed variable f(x) = q(x))
721
    - the approximation interval;
722
    - the center, x0, of the interval;
723
    - the corresponding approximation error.
724
    TODO: fix endless looping for some parameters sets.
725
    """
726
    resultArray = []
727
    # Set Sollya to the necessary internal precision.
728
    precChangedSa = False
729
    currentSollyaPrecSo = pobyso_get_prec_so()
730
    currentSollyaPrecSa = pobyso_constant_from_int_so_sa(currentSollyaPrecSo)
731
    if internalSollyaPrecSa > currentSollyaPrecSa:
732
        pobyso_set_prec_sa_so(internalSollyaPrecSa)
733
        precChangedSa = True
734
    #
735
    x = functionSa.variables()[0] # Actual variable name can be anything.
736
    # Scaled function: [1=,2] -> [1,2].
737
    (fff, scaledLowerBoundSa, scaledUpperBoundSa,                             \
738
            scaledLowerBoundImageSa, scaledUpperBoundImageSa) =               \
739
                slz_compute_scaled_function(functionSa,                       \
740
                                            lowerBoundSa,                     \
741
                                            upperBoundSa,                     \
742
                                            floatingPointPrecSa)
743
    #
744
    print "Approximation precision: ", RR(approxPrecSa)
745
    # Prepare the arguments for the Taylor expansion computation with Sollya.
746
    functionSo = pobyso_parse_string_sa_so(fff._assume_str())
747
    degreeSo = pobyso_constant_from_int_sa_so(degreeSa)
748
    scaledBoundsSo = pobyso_bounds_to_range_sa_so(scaledLowerBoundSa, 
749
                                                  scaledUpperBoundSa)
750
    # Compute the first Taylor expansion.
751
    (polySo, boundsSo, intervalCenterSo, maxErrorSo) = \
752
        slz_compute_polynomial_and_interval(functionSo, degreeSo,
753
                                        scaledLowerBoundSa, scaledUpperBoundSa,
754
                                        approxPrecSa, internalSollyaPrecSa)
755
    if polySo is None:
756
        print "slz_get_intervals_and_polynomials: Aborting and returning None!"
757
        if precChangedSa:
758
            pobyso_set_prec_so_so(currentSollyaPrecSo)
759
            sollya_lib_clear_obj(currentSollyaPrecSo)
760
        sollya_lib_clear_obj(functionSo)
761
        sollya_lib_clear_obj(degreeSo)
762
        sollya_lib_clear_obj(scaledBoundsSo)
763
        return None
764
    realIntervalField = RealIntervalField(max(lowerBoundSa.parent().precision(),
765
                                              upperBoundSa.parent().precision()))
766
    boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField)
767
    errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
768
    print "First approximation error:", errorSa.n(digits=50)
769
    # If the error and interval are OK a the first try, just return.
770
    if boundsSa.endpoints()[1] >= scaledUpperBoundSa:
771
        # Change variable stuff in Sollya x -> x0-x.
772
        changeVarExpressionSo = sollya_lib_build_function_sub( \
773
                              sollya_lib_build_function_free_variable(), \
774
                              sollya_lib_copy_obj(intervalCenterSo))
775
        polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpressionSo) 
776
        sollya_lib_clear_obj(changeVarExpressionSo)
777
        resultArray.append((polySo, polyVarChangedSo, boundsSo, \
778
                            intervalCenterSo, maxErrorSo))
779
        if internalSollyaPrecSa != currentSollyaPrecSa:
780
            pobyso_set_prec_sa_so(currentSollyaPrecSa)
781
        sollya_lib_clear_obj(currentSollyaPrecSo)
782
        sollya_lib_clear_obj(functionSo)
783
        sollya_lib_clear_obj(degreeSo)
784
        sollya_lib_clear_obj(scaledBoundsSo)
785
        #print "Approximation error:", errorSa
786
        return resultArray
787
    # The returned interval upper bound does not reach the requested upper
788
    # upper bound: compute the next upper bound.
789
    # The following ratio is always >= 1
790
    currentErrorRatio = approxPrecSa / errorSa
791
    # Starting point for the next upper bound.
792
    currentScaledUpperBoundSa = boundsSa.endpoints()[1]
793
    boundsWidthSa = boundsSa.endpoints()[1] - boundsSa.endpoints()[0]
794
    # Compute the increment. 
795
    if currentErrorRatio > RR('1000'): # ]1.5, infinity[
796
        currentScaledUpperBoundSa += \
797
                        currentErrorRatio * boundsWidthSa * 2
798
    else:  # [1, 1.5]
799
        currentScaledUpperBoundSa += \
800
                        (RR('1.0') + currentErrorRatio.log() / 500) * boundsWidthSa
801
    # Take into account the original interval upper bound.                     
802
    if currentScaledUpperBoundSa > scaledUpperBoundSa:
803
        currentScaledUpperBoundSa = scaledUpperBoundSa
804
    # Compute the other expansions.
805
    while boundsSa.endpoints()[1] < scaledUpperBoundSa:
806
        currentScaledLowerBoundSa = boundsSa.endpoints()[1]
807
        (polySo, boundsSo, intervalCenterSo, maxErrorSo) = \
808
            slz_compute_polynomial_and_interval(functionSo, degreeSo,
809
                                            currentScaledLowerBoundSa, 
810
                                            currentScaledUpperBoundSa, 
811
                                            approxPrecSa, 
812
                                            internalSollyaPrecSa)
813
        errorSa = pobyso_get_constant_as_rn_with_rf_so_sa(maxErrorSo)
814
        if  errorSa < approxPrecSa:
815
            # Change variable stuff
816
            #print "Approximation error:", errorSa
817
            changeVarExpressionSo = sollya_lib_build_function_sub(
818
                                    sollya_lib_build_function_free_variable(), 
819
                                    sollya_lib_copy_obj(intervalCenterSo))
820
            polyVarChangedSo = sollya_lib_evaluate(polySo, changeVarExpressionSo) 
821
            sollya_lib_clear_obj(changeVarExpressionSo)
822
            resultArray.append((polySo, polyVarChangedSo, boundsSo, \
823
                                intervalCenterSo, maxErrorSo))
824
        boundsSa = pobyso_range_to_interval_so_sa(boundsSo, realIntervalField)
825
        # Compute the next upper bound.
826
        # The following ratio is always >= 1
827
        currentErrorRatio = approxPrecSa / errorSa
828
        # Starting point for the next upper bound.
829
        currentScaledUpperBoundSa = boundsSa.endpoints()[1]
830
        boundsWidthSa = boundsSa.endpoints()[1] - boundsSa.endpoints()[0]
831
        # Compute the increment. 
832
        if currentErrorRatio > RR('1000'): # ]1.5, infinity[
833
            currentScaledUpperBoundSa += \
834
                            currentErrorRatio * boundsWidthSa * 2
835
        else:  # [1, 1.5]
836
            currentScaledUpperBoundSa += \
837
                            (RR('1.0') + currentErrorRatio.log()/500) * boundsWidthSa
838
        #print "currentErrorRatio:", currentErrorRatio
839
        #print "currentScaledUpperBoundSa", currentScaledUpperBoundSa
840
        # Test for insufficient precision.
841
        if currentScaledUpperBoundSa == scaledLowerBoundSa:
842
            print "Can't shrink the interval anymore!"
843
            print "You should consider increasing the Sollya internal precision"
844
            print "or the polynomial degree."
845
            print "Giving up!"
846
            if internalSollyaPrecSa != currentSollyaPrecSa:
847
                pobyso_set_prec_sa_so(currentSollyaPrecSa)
848
            sollya_lib_clear_obj(currentSollyaPrecSo)
849
            sollya_lib_clear_obj(functionSo)
850
            sollya_lib_clear_obj(degreeSo)
851
            sollya_lib_clear_obj(scaledBoundsSo)
852
            return None
853
        if currentScaledUpperBoundSa > scaledUpperBoundSa:
854
            currentScaledUpperBoundSa = scaledUpperBoundSa
855
    if internalSollyaPrecSa > currentSollyaPrecSa:
856
        pobyso_set_prec_so_so(currentSollyaPrecSo)
857
    sollya_lib_clear_obj(currentSollyaPrecSo)
858
    sollya_lib_clear_obj(functionSo)
859
    sollya_lib_clear_obj(degreeSo)
860
    sollya_lib_clear_obj(scaledBoundsSo)
861
    return(resultArray)
862
# End slz_get_intervals_and_polynomials
863

    
864

    
865
def slz_interval_scaling_expression(boundsInterval, expVar):
866
    """
867
    Compute the scaling expression to map an interval that spans at most
868
    a single binade to [1, 2) and the inverse expression as well.
869
    Not very sure that the transformation makes sense for negative numbers.
870
    """
871
    # The scaling offset is only used for negative numbers.
872
    # When the absolute value of the lower bound is < 0.
873
    if abs(boundsInterval.endpoints()[0]) < 1:
874
        if boundsInterval.endpoints()[0] >= 0:
875
            scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2())
876
            invScalingCoeff = 1/scalingCoeff
877
            return((scalingCoeff * expVar, 
878
                    invScalingCoeff * expVar))
879
        else:
880
            scalingCoeff = \
881
                2^(floor((-boundsInterval.endpoints()[0]).log2()) - 1)
882
            scalingOffset = -3 * scalingCoeff
883
            return((scalingCoeff * expVar + scalingOffset,
884
                    1/scalingCoeff * expVar + 3))
885
    else: 
886
        if boundsInterval.endpoints()[0] >= 0:
887
            scalingCoeff = 2^floor(boundsInterval.endpoints()[0].log2())
888
            scalingOffset = 0
889
            return((scalingCoeff * expVar, 
890
                    1/scalingCoeff * expVar))
891
        else:
892
            scalingCoeff = \
893
                2^(floor((-boundsInterval.endpoints()[1]).log2()))
894
            scalingOffset =  -3 * scalingCoeff
895
            #scalingOffset = 0
896
            return((scalingCoeff * expVar + scalingOffset,
897
                    1/scalingCoeff * expVar + 3))
898
# End slz_interval_scaling_expression
899
   
900
def slz_interval_and_polynomial_to_sage(polyRangeCenterErrorSo):
901
    """
902
    Compute the Sage version of the Taylor polynomial and it's
903
    companion data (interval, center...)
904
    The input parameter is a five elements tuple:
905
    - [0]: the polyomial (without variable change), as polynomial over a
906
           real ring;
907
    - [1]: the polyomial (with variable change done in Sollya), as polynomial
908
           over a real ring;
909
    - [2]: the interval (as Sollya range);
910
    - [3]: the interval center;
911
    - [4]: the approximation error. 
912
    
913
    The function return a 5 elements tuple: formed with all the 
914
    input elements converted into their Sollya counterpart.
915
    """
916
    polynomialSa = pobyso_get_poly_so_sa(polyRangeCenterErrorSo[0])
917
    polynomialChangedVarSa = pobyso_get_poly_so_sa(polyRangeCenterErrorSo[1])
918
    intervalSa = \
919
            pobyso_get_interval_from_range_so_sa(polyRangeCenterErrorSo[2])
920
    centerSa = \
921
            pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[3])
922
    errorSa = \
923
            pobyso_get_constant_as_rn_with_rf_so_sa(polyRangeCenterErrorSo[4])
924
    return((polynomialSa, polynomialChangedVarSa, intervalSa, centerSa, errorSa))
925
    # End slz_interval_and_polynomial_to_sage
926

    
927
def slz_rat_poly_of_int_to_poly_for_coppersmith(ratPolyOfInt, 
928
                                                precision,
929
                                                targetHardnessToRound,
930
                                                variable1,
931
                                                variable2):
932
    """
933
    Creates a new multivariate polynomial with integer coefficients for use
934
     with the Coppersmith method.
935
    A the same time it computes :
936
    - 2^K (N);
937
    - 2^k (bound on the second variable)
938
    - lcm
939

    
940
    :param ratPolyOfInt: a polynomial with rational coefficients and integer
941
                         variables.
942
    :param precision: the precision of the floating-point coefficients.
943
    :param targetHardnessToRound: the hardness to round we want to check.
944
    :param variable1: the first variable of the polynomial (an expression).
945
    :param variable2: the second variable of the polynomial (an expression).
946
    
947
    :returns: a 4 elements tuple:
948
                - the polynomial;
949
                - the modulus (N);
950
                - the t bound;
951
                - the lcm used to compute the integral coefficients and the 
952
                  module.
953
    """
954
    # Create a new integer polynomial ring.
955
    IP = PolynomialRing(ZZ, name=str(variable1) + "," + str(variable2))
956
    # Coefficients are issued in the increasing power order.
957
    ratPolyCoefficients = ratPolyOfInt.coefficients()
958
    # Print the reversed list for debugging.
959
    print "Rational polynomial coefficients:", ratPolyCoefficients[::-1]
960
    # Build the list of number we compute the lcm of.
961
    coefficientDenominators = sro_denominators(ratPolyCoefficients)
962
    coefficientDenominators.append(2^precision)
963
    coefficientDenominators.append(2^(targetHardnessToRound + 1))
964
    leastCommonMultiple = lcm(coefficientDenominators)
965
    # Compute the expression corresponding to the new polynomial
966
    coefficientNumerators =  sro_numerators(ratPolyCoefficients)
967
    #print coefficientNumerators
968
    polynomialExpression = 0
969
    power = 0
970
    # Iterate over two lists at the same time, stop when the shorter is
971
    # exhausted.
972
    for numerator, denominator in \
973
                        zip(coefficientNumerators, coefficientDenominators):
974
        multiplicator = leastCommonMultiple / denominator 
975
        newCoefficient = numerator * multiplicator
976
        polynomialExpression += newCoefficient * variable1^power
977
        power +=1
978
    polynomialExpression += - variable2
979
    return (IP(polynomialExpression),
980
            leastCommonMultiple / 2^precision, # 2^K or N.
981
            leastCommonMultiple / 2^(targetHardnessToRound + 1), # tBound
982
            leastCommonMultiple) # If we want to make test computations.
983
        
984
# End slz_ratPoly_of_int_to_poly_for_coppersmith
985

    
986
def slz_rat_poly_of_rat_to_rat_poly_of_int(ratPolyOfRat, 
987
                                          precision):
988
    """
989
    Makes a variable substitution into the input polynomial so that the output
990
    polynomial can take integer arguments.
991
    All variables of the input polynomial "have precision p". That is to say
992
    that they are rationals with denominator == 2^(precision - 1): 
993
    x = y/2^(precision - 1).
994
    We "incorporate" these denominators into the coefficients with, 
995
    respectively, the "right" power.
996
    """
997
    polynomialField = ratPolyOfRat.parent()
998
    polynomialVariable = ratPolyOfRat.variables()[0]
999
    #print "The polynomial field is:", polynomialField
1000
    return \
1001
        polynomialField(ratPolyOfRat.subs({polynomialVariable : \
1002
                                   polynomialVariable/2^(precision-1)}))
1003
    
1004
    # Return a tuple:
1005
    # - the bivariate integer polynomial in (i,j);
1006
    # - 2^K
1007
# End slz_rat_poly_of_rat_to_rat_poly_of_int
1008

    
1009

    
1010
print "\t...sageSLZ loaded"