Révision 106

pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage (revision 106)
1 1
load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"
2 2
print "sagePolynomialOperations loading..."
3
def spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
4
                                            pCoefficients, 
3
def spo_add_polynomial_coeffs_to_matrix_row(poly, 
5 4
                                            knownMonomials, 
6 5
                                            protoMatrixRows, 
7 6
                                            columnsWidth=0):
8 7
    """
9
    For a given polynomial (under the form of monomials and coefficents lists),
8
    For a given polynomial ,
10 9
    add the coefficients of the protoMatrix (a list of proto matrix rows).
11 10
    Coefficients are added to the protoMatrix row in the order imposed by the 
12 11
    monomials discovery list (the knownMonomials list) built as construction
13 12
    goes on. 
14 13
    As a bonus, data can be printed out for a visual check.
15
    pMonomials     : the list of the monomials coming form some polynomial;
16
    pCoefficients  : the list of the corresponding coefficients to add to
17
                     the protoMatrix in the exact same order as the monomials;
18
    knownMonomials : the list of the already knonw monomials;
14
    poly           : the polynomial; in argument;
15
    knownMonomials : the list of the already known monomials; will determine
16
                     the order of the coefficients appending to a row; in-out
17
                     argument (new monomials may be discovered and then 
18
                     appended the the knowMonomials list);
19 19
    protoMatrixRows: a list of lists, each one holding the coefficients of the 
20
                     monomials
20
                     monomials of a polynomial; in-out argument: a new row is
21
                     added at each call;
21 22
    columnWith     : the width, in characters, of the displayed column ; if 0, 
22
                     do not display anything.
23
                     do not display anything; in argument.
23 24
    """
25
    pMonomials   = poly.monomials()
26
    pCoefficients = poly.coefficients()
24 27
    # We have started with the smaller degrees in the first variable.
25 28
    pMonomials.reverse()
26 29
    pCoefficients.reverse()
......
200 203
                    # polynomialAtPower == 1 here. Next line should be commented
201 204
                    # out but it does not work! Some conversion problem?
202 205
                    currentPolynomial = pRing(currentExpression)
203
                    polynomialsList.append(polExpStr) 
206
                    polynomialsList.append(currentPolynomial) 
204 207
                    pMonomials = currentPolynomial.monomials()
205 208
                    pCoefficients = currentPolynomial.coefficients()
206 209
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
220 223
                polExpStr = spo_expression_as_string(0, 0, pPower, alpha-pPower)
221 224
                print "->", polExpStr
222 225
            currentPolynomial = polynomialAtPower * nAtPower
223
            polynomialsList.append(polExpStr) 
226
            polynomialsList.append(currentPolynomial) 
224 227
            pMonomials = currentPolynomial.monomials()
225 228
            pCoefficients = currentPolynomial.coefficients()
226 229
            spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
245 248
                    print "->", polExpStr
246 249
                currentExpression = i^iPower * nAtPower
247 250
                currentPolynomial = pRing(currentExpression) * polynomialAtPower
248
                polynomialsList.append(polExpStr) 
251
                polynomialsList.append(currentPolynomial) 
249 252
                pMonomials = currentPolynomial.monomials()
250 253
                pCoefficients = currentPolynomial.coefficients()
251 254
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,      \
......
268 271
                    print "->", polExpStr
269 272
                currentExpression = i^(iPower * pIdegree) * t^tPower * nAtAlpha
270 273
                currentPolynomial = pRing(currentExpression)
271
                polynomialsList.append(polExpStr) 
274
                polynomialsList.append(currentPolynomial) 
272 275
                pMonomials = currentPolynomial.monomials()
273 276
                pCoefficients = currentPolynomial.coefficients()
274 277
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
302 305
                        print "->", polExpStr
303 306
                    currentExpression = i^iPower * t^outerTpower * nAtAlpha
304 307
                    currentPolynomial = pRing(currentExpression)
305
                    polynomialsList.append(polExpStr) 
308
                    polynomialsList.append(currentPolynomial) 
306 309
                    pMonomials = currentPolynomial.monomials()
307 310
                    pCoefficients = currentPolynomial.coefficients()
308 311
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
334 337
                            currentExpression = \
335 338
                                    i^(iPower) * t^(innerTpower) * nAtAlpha
336 339
                            currentPolynomial = pRing(currentExpression)
337
                            polynomialsList.append(polExpStr) 
340
                            polynomialsList.append(currentPolynomial) 
338 341
                            pMonomials = currentPolynomial.monomials()
339 342
                            pCoefficients = currentPolynomial.coefficients()
340 343
                            spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
363 366
                    currentExpression = i^iShift * t^outerTpower * nAtPower
364 367
                    currentPolynomial = pRing(currentExpression) * \
365 368
                                            polynomialAtPower
366
                    polynomialsList.append(polExpStr) 
369
                    polynomialsList.append(currentPolynomial) 
367 370
                    pMonomials = currentPolynomial.monomials()
368 371
                    pCoefficients = currentPolynomial.coefficients()
369 372
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
......
380 383
    return ((protoMatrixRows, knownMonomials, polynomialsList))
381 384
# End spo_polynomial_to_proto_matrix
382 385

  
383
def spo_polynomial_to_proto_matrix_2(p, alpha, N, columnsWidth=0):
386
def spo_polynomial_to_polynomials_list_2(p, alpha, N, columnsWidth=0):
384 387
    """
388
    From p, alpha, N build a list of polynomials...
389
    TODO: clean up the comments below!
390
    
385 391
    From a (bivariate) polynomial and some other parameters build a proto 
386 392
    matrix (an array of "rows") to be converted into a "true" matrix and 
387 393
    eventually by reduced by fpLLL.
......
408 414
    """
409 415
    pRing = p.parent()
410 416
    knownMonomials = []
411
    protoMatrixRows = []
412 417
    polynomialsList = []
413 418
    pVariables = p.variables()
414 419
    iVariable = pVariables[0]
......
441 446
                # out but it does not work! Some conversion problem?
442 447
                currentPolynomial = pRing(currentExpression)
443 448
                polynomialsList.append(currentPolynomial) 
444
                pMonomials = currentPolynomial.monomials()
445
                pCoefficients = currentPolynomial.coefficients()
446
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,                                                             
447
                                                        pCoefficients, 
448
                                                        knownMonomials, 
449
                                                        protoMatrixRows, 
450
                                                        columnsWidth)
451 449
            # End for iPower.
452 450
        else: # pPower > 0: (p^1..p^alpha)
453 451
            # This where we introduce the p^pPower * N^(alpha-pPower)
......
458 456
                print "->", polExpStr
459 457
            currentPolynomial = polynomialAtPower * nAtPower
460 458
            polynomialsList.append(currentPolynomial) 
461
            pMonomials = currentPolynomial.monomials()
462
            pCoefficients = currentPolynomial.coefficients()
463
            spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
464
                                                    pCoefficients, 
465
                                                    knownMonomials, 
466
                                                    protoMatrixRows, 
467
                                                    columnsWidth)
468
            
459
            # Exit when pPower == alpha
460
            if pPower == alpha:
461
                return((knownMonomials, polynomialsList))
469 462
            # This is where the "i-shifts" take place. Mixed terms, i^k * t^l
470 463
            # (that were introduced at a previous
471 464
            # stage or are introduced now) are only shifted to already existing 
......
488 481
                    currentExpression = i^internalIpower * t^tPower * nAtAlpha
489 482
                    currentPolynomial = pRing(currentExpression)
490 483
                    polynomialsList.append(currentPolynomial) 
491
                    pMonomials = currentPolynomial.monomials()
492
                    pCoefficients = currentPolynomial.coefficients()
493
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials,      \
494
                                                            pCoefficients,   \
495
                                                            knownMonomials,  \
496
                                                            protoMatrixRows, \
497
                                                            columnsWidth)
498 484
                    internalIpower += pIdegree
499 485
                # End for tPower
500 486
                # The i^iPower * p^pPower * N^(alpha-pPower) i-shift.
......
507 493
                currentExpression = i^iPower * nAtPower
508 494
                currentPolynomial = pRing(currentExpression) * polynomialAtPower
509 495
                polynomialsList.append(currentPolynomial) 
510
                pMonomials = currentPolynomial.monomials()
511
                pCoefficients = currentPolynomial.coefficients()
512
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,      \
513
                                                        pCoefficients,   \
514
                                                        knownMonomials,  \
515
                                                        protoMatrixRows, \
516
                                                        columnsWidth)
517 496
            # End for iPower
518 497
        polynomialAtPower *= p  
519 498
        nAtPower /= N
520 499
    # End for pPower loop
521
    return ((protoMatrixRows, knownMonomials, polynomialsList))
500
    return((knownMonomials, polynomialsList))
522 501
# End spo_polynomial_to_proto_matrix_2
523 502

  
524 503
def spo_proto_to_column_matrix(protoMatrixColumns, \
pobysoPythonSage/src/sageSLZ/sageSLZ.sage (revision 106)
87 87

  
88 88
def slz_compute_reduced_polynomials(reducedMatrix,
89 89
                                    knownMonomials,
90
                                    var1,
90 91
                                    var1Bound,
92
                                    var2,
91 93
                                    var2Bound):
92 94
    """
93 95
    From a reduced matrix, holding the coefficients, from a monomials list,
......
99 101
    """
100 102
    
101 103
    # TODO: check input arguments.
102
    if len(knownMonomials) == 0:
103
        return []
104
    # Search in knowMonomials until we find a bivariate one, otherwise
105
    # the call to variables does not give the expected result.
106
    monomialIndex = 1
107
    while len(knownMonomials[monomialIndex].variables()) != 2 :
108
        monomialIndex +=1
109
    (var1, var2) = knownMonomials[monomialIndex].variables()[0:2]
110
    #print "Variable 1:", var1
111
    #print "Variable 2:", var2
112 104
    reducedPolynomials = []
113
    currentPolynomial = 0
105
    #print "type var1:", type(var1), " - type var2:", type(var2)
114 106
    for matrixRow in reducedMatrix.rows():
115 107
        currentPolynomial = 0
116 108
        for colIndex in xrange(0, len(knownMonomials)):
117 109
            currentCoefficient = matrixRow[colIndex]
118
            #print knownMonomials[colIndex]
119
            currentMonomial = knownMonomials[colIndex]
120
            #print "Monomial as multivariate polynomial:", \
121
            currentMonomial, type(currentMonomial)
122
            degreeInVar1 = currentMonomial.degree(var1)
123
            #print "Degree in var", var1, ":", degreeInVar1
124
            degreeInVar2 = currentMonomial.degree(var2)
125
            #print "Degree in var", var2, ":", degreeInVar2
126
            if degreeInVar1 != 0:
127
                currentCoefficient  /= (var1Bound^degreeInVar1)
128
            if degreeInVar2 != 0:
129
                currentCoefficient /= (var2Bound^degreeInVar2)
130
            #print "Current reduced monomial:", (currentCoefficient * \
131
            #                                    currentMonomial)
132
            currentPolynomial += (currentCoefficient * currentMonomial)
110
            if currentCoefficient != 0:
111
                #print "Current coefficient:", currentCoefficient
112
                currentMonomial = knownMonomials[colIndex]
113
                parentRing = currentMonomial.parent()
114
                #print "Monomial as multivariate polynomial:", \
115
                #currentMonomial, type(currentMonomial)
116
                degreeInVar1 = currentMonomial.degree(parentRing(var1))
117
                #print "Degree in var", var1, ":", degreeInVar1
118
                degreeInVar2 = currentMonomial.degree(parentRing(var2))
119
                #print "Degree in var", var2, ":", degreeInVar2
120
                if degreeInVar1 > 0:
121
                    currentCoefficient = \
122
                        currentCoefficient / var1Bound^degreeInVar1
123
                    #print "varBound1 in degree:", var1Bound^degreeInVar1
124
                    #print "Current coefficient(1)", currentCoefficient
125
                if degreeInVar2 > 0:
126
                    currentCoefficient = \
127
                        currentCoefficient / var2Bound^degreeInVar2
128
                    #print "Current coefficient(2)", currentCoefficient
129
                #print "Current reduced monomial:", (currentCoefficient * \
130
                #                                    currentMonomial)
131
                currentPolynomial += (currentCoefficient * currentMonomial)
132
                #print "Current polynomial:", currentPolynomial
133
            # End if
134
        # End for colIndex.
133 135
        #print "Type of the current polynomial:", type(currentPolynomial)
134 136
        reducedPolynomials.append(currentPolynomial)
135 137
    return reducedPolynomials 

Formats disponibles : Unified diff