Statistiques
| Révision :

root / pobysoPythonSage / src / sageSLZ / sagePolynomialOperations.sage @ 92

Historique | Voir | Annoter | Télécharger (21,73 ko)

1
load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"
2
print "sagePolynomialOperations loading..."
3
def spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
4
                                            pCoefficients, 
5
                                            knownMonomials, 
6
                                            protoMatrixRows, 
7
                                            columnsWidth=0):
8
    """
9
    For a given polynomial (under the form of monomials and coefficents lists),
10
    add the coefficients of the protoMatrix (a list of proto matrix rows).
11
    Coefficients are added to the protoMatrix row in the order imposed by the 
12
    monomials discovery list (the knownMonomials list) built as construction
13
    goes on. 
14
    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;
19
    protoMatrixRows: a list of lists, each one holding the coefficients of the 
20
                     monomials
21
    columnWith     : the width, in characters, of the displayed column ; if 0, 
22
                     do not display anything.
23
    """
24
    # We have started with the smaller degrees in the first variable.
25
    pMonomials.reverse()
26
    pCoefficients.reverse()
27
    # New empty proto matrix row.
28
    protoMatrixRowCoefficients = []
29
    # We work according to the order of the already known monomials
30
    # No known monomials yet: add the pMonomials to knownMonomials 
31
    # and add the coefficients to the proto matrix row.
32
    if len(knownMonomials) == 0: 
33
        for pmIdx in xrange(0, len(pMonomials)):
34
            knownMonomials.append(pMonomials[pmIdx])
35
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
36
            if columnsWidth != 0:
37
                monomialAsString = str(pCoefficients[pmIdx]) + " " + \
38
                                   str(pMonomials[pmIdx])
39
                print monomialAsString, " " * \
40
                      (columnsWidth - len(monomialAsString)),
41
    # There are some known monomials. We search for them in pMonomials and 
42
    # add their coefficients to the proto matrix row.
43
    else: 
44
        for knownMonomialIndex in xrange(0,len(knownMonomials)):
45
            # We lazily use an exception here since pMonomials.index() function
46
            # may fail throwing the ValueError exception.
47
            try:
48
                indexInPmonomials = \
49
                    pMonomials.index(knownMonomials[knownMonomialIndex])
50
                if columnsWidth != 0:
51
                    monomialAsString = str(pCoefficients[indexInPmonomials]) + \
52
                        " " + str(knownMonomials[knownMonomialIndex])
53
                    print monomialAsString, " " * \
54
                        (columnsWidth - len(monomialAsString)),
55
                # Add the coefficient to the proto matrix row and delete the \
56
                # known monomial from the current pMonomial list 
57
                #(and the corresponding coefficient as well).
58
                protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials])
59
                del pMonomials[indexInPmonomials]
60
                del pCoefficients[indexInPmonomials]
61
            # The knownMonomials element is not in pMonomials
62
            except ValueError:   
63
                protoMatrixRowCoefficients.append(0)
64
                if columnsWidth != 0:
65
                    monomialAsString = "0" + " "+ \
66
                        str(knownMonomials[knownMonomialIndex])
67
                    print monomialAsString, " " * \
68
                        (columnsWidth - len(monomialAsString)),
69
        # End for knownMonomialKey loop. 
70
        # We now append the remaining monomials of pMonomials to knownMonomials 
71
        # and the corresponding coefficients to proto matrix row.
72
        for pmIdx in xrange(0, len(pMonomials)):
73
            knownMonomials.append(pMonomials[pmIdx])
74
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
75
            if columnsWidth != 0:
76
                monomialAsString = str(pCoefficients[pmIdx]) + " " \
77
                    + str(pMonomials[pmIdx])
78
                print monomialAsString, " " * \
79
                    (columnsWidth - len(monomialAsString)),
80
        # End for pmIdx loop.
81
    # Add the new list row elements to the proto matrix.
82
    protoMatrixRows.append(protoMatrixRowCoefficients)
83
    if columnsWidth != 0:
84
        print    
85
# End spo_add_polynomial_coeffs_to_matrix_row
86

    
87
def spo_expression_as_string(powI, powT, powP, alpha):
88
    """
89
    Computes a string version of the i^k + t^l + p^m + N^n expression for
90
    output.
91
    """
92
    expressionAsString =""
93
    if powI != 0:
94
        expressionAsString += "i^" + str(powI)
95
    if powT != 0:
96
        if len(expressionAsString) != 0:
97
            expressionAsString += " * "
98
        expressionAsString += "t^" + str(powT)
99
    if powP != 0:
100
        if len(expressionAsString) != 0:
101
            expressionAsString += " * "
102
        expressionAsString += "p^" + str(powP)
103
    if (alpha - powP) != 0 :
104
        if len(expressionAsString) != 0:
105
            expressionAsString += " * "
106
        expressionAsString += "N^" + str(alpha - powP)
107
    return(expressionAsString)
108
# End spo_expression_as_string.
109

    
110
def spo_norm(poly, p=2):
111
    """
112
    Behaves more or less (no infinity defined) as the norm for the
113
    univariate polynomials.
114
    Quoting the Sage documentation:
115
    Definition: For integer p, the p-norm of a polynomial is the pth root of 
116
    the sum of the pth powers of the absolute values of the coefficients of 
117
    the polynomial.
118
    
119
    """
120
    # TODO: check the arguments (for p see below)..
121
    norm = 0
122
    # For infinity norm.
123
    if p == Infinity:
124
        for coefficient in poly.coefficients():
125
            coefficientAbs = coefficient.abs()
126
            if coefficientAbs > norm:
127
                norm = coefficientAbs
128
        return norm
129
    # TODO: check here the value of p
130
    # For 1 norm.
131
    if p == 1:
132
        for coefficient in poly.coefficients():
133
            norm +=  coefficient.abs()
134
        return norm
135
    # For other norms
136
    for coefficient in poly.coefficients():
137
        norm +=  (coefficient^p).abs()
138
    return pow(norm, 1/p)
139
# end spo_norm
140

    
141
def spo_polynomial_to_proto_matrix(p, pRing, alpha, N, columnsWidth=0):
142
    """
143
    From a (bivariate) polynomial and some other parameters build a proto 
144
    matrix (an array of "rows") to be converted into a "true" matrix and 
145
    eventually by reduced by fpLLL.
146
    The matrix is such as those found in Boneh-Durphee and Stehl?.
147
    
148
    Parameters
149
    ----------
150
    p: the (bivariate) polynomial;
151
    pRing: the ring over which p is defined;
152
    alpha:
153
    N:
154
    columsWidth: if == 0, no information is displayed, otherwise data is 
155
                 printed in colums of columnsWitdth width.
156
    """
157
    knownMonomials = []
158
    protoMatrixRows = []
159
    polynomialsList = []
160
    pVariables = p.variables()
161
    iVariable = pVariables[0]
162
    tVariable = pVariables[1]
163
    polynomialAtPower = pRing(1)
164
    currentPolynomial = pRing(1)
165
    pIdegree = p.degree(pVariables[0])
166
    pTdegree = p.degree(pVariables[1])
167
    currentIdegree = currentPolynomial.degree(iVariable)
168
    nAtPower = N^alpha
169
    nAtAlpha = nAtPower
170
    polExpStr = ""
171
    # We work from p^0 * N^alpha to p^alpha * N^0
172
    for pPower in xrange(0, alpha + 1):
173
        # pPower == 0 is a special case. We introduce all the monomials but one
174
        # in i and those in t necessary to be able to introduce
175
        # p. We arbitrary choose to introduce the highest degree monomial in i
176
        # with p. We also introduce all the mixed i^k * t^l monomials with
177
        # k < p.degree(i) and l <= p.degree(t).
178
        # Mixed terms introduction is necessary here before we start "i shifts"
179
        # in the next iteration.
180
        if pPower == 0:
181
            # Notice that i^pIdegree is excluded as the bound of the xrange is
182
            # pIdegree
183
            for iPower in xrange(0, pIdegree): 
184
                for tPower in xrange(0, pTdegree + 1):
185
                    if columnsWidth != 0:
186
                        polExpStr = spo_expression_as_string(iPower, 
187
                                                             tPower, 
188
                                                             pPower, 
189
                                                             alpha)
190
                        print "->", polExpStr
191
                    currentExpression = iVariable^iPower * \
192
                                        tVariable^tPower * nAtAlpha
193
                    # polynomialAtPower == 1 here. Next line should be commented
194
                    # out but it does not work! Some conversion problem?
195
                    currentPolynomial = pRing(currentExpression)
196
                    polynomialsList.append(polExpStr) 
197
                    pMonomials = currentPolynomial.monomials()
198
                    pCoefficients = currentPolynomial.coefficients()
199
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
200
                                                            pCoefficients, 
201
                                                            knownMonomials, 
202
                                                            protoMatrixRows, 
203
                                                            columnsWidth)
204
                # End tPower.
205
            # End for iPower.
206
        else: # pPower > 0: (p^1..p^alpha)
207
            # This where we introduce the p^pPower * N^(alpha-pPower)
208
            # polynomial.
209
            # This step could technically be fused as the first iteration
210
            # of the next loop (with iPower starting at 0).
211
            # We set it apart for clarity.
212
            if columnsWidth != 0:
213
                polExpStr = spo_expression_as_string(0, 0, pPower, alpha)
214
                print "->", polExpStr
215
            currentPolynomial = polynomialAtPower * nAtPower
216
            polynomialsList.append(polExpStr) 
217
            pMonomials = currentPolynomial.monomials()
218
            pCoefficients = currentPolynomial.coefficients()
219
            spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
220
                                                    pCoefficients, 
221
                                                    knownMonomials, 
222
                                                    protoMatrixRows, 
223
                                                    columnsWidth)
224
            
225
            # The i^iPower * p^pPower polynomials: they add i^k monomials to  
226
            # p^pPower up to k < pIdegree * pPower. This only introduces i^k 
227
            # monomials since mixed terms (that were introduced at a previous
228
            # stage) are only shifted to already existing 
229
            # ones. p^pPower is "shifted" to higher degrees in i as far as 
230
            # possible, one step short of the degree in i of p^(pPower+1) .
231
            # These "pure" i^k monomials can only show up with i multiplications.
232
            for iPower in xrange(1, pIdegree):
233
                if columnsWidth != 0:
234
                    polExpStr = spo_expression_as_string(iPower, \
235
                                                         0,      \
236
                                                         pPower, \
237
                                                         alpha)
238
                    print "->", polExpStr
239
                currentExpression = i^iPower * nAtPower
240
                currentPolynomial = pRing(currentExpression) * polynomialAtPower
241
                polynomialsList.append(polExpStr) 
242
                pMonomials = currentPolynomial.monomials()
243
                pCoefficients = currentPolynomial.coefficients()
244
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,      \
245
                                                        pCoefficients,   \
246
                                                        knownMonomials,  \
247
                                                        protoMatrixRows, \
248
                                                        columnsWidth)
249
            # End for iPower
250
            # We want now to introduce a t * p^pPower polynomial. But before
251
            # that we must introduce some mixed monomials.
252
            # This loop is no triggered before pPower == 2.
253
            # It introduces a first set of high i degree mixed monomials.
254
            for iPower in xrange(1, pPower):
255
                tPower = pPower - iPower + 1
256
                if columnsWidth != 0:
257
                    polExpStr = spo_expression_as_string(iPower * pIdegree, 
258
                                                         tPower, 
259
                                                         0,  
260
                                                         alpha)
261
                    print "->", polExpStr
262
                currentExpression = i^(iPower * pIdegree) * t^tPower * nAtAlpha
263
                currentPolynomial = pRing(currentExpression)
264
                polynomialsList.append(polExpStr) 
265
                pMonomials = currentPolynomial.monomials()
266
                pCoefficients = currentPolynomial.coefficients()
267
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
268
                                                        pCoefficients, 
269
                                                        knownMonomials, 
270
                                                        protoMatrixRows, 
271
                                                        columnsWidth)
272
            # End for iPower
273
            #
274
            # This is the mixed monomials main loop. It introduces:
275
            # - the missing mixed monomials needed before the 
276
            #   t^l * p^pPower * N^(alpha-pPower) polynomial;
277
            # - the t^l * p^pPower * N^(alpha-pPower) itself;
278
            # - for each of i^k * t^l * p^pPower * N^(alpha-pPower) polynomials:
279
            #   - the the missing mixed monomials needed  polynomials,
280
            #   - the i^k * t^l * p^pPower * N^(alpha-pPower) itself.
281
            # The t^l * p^pPower * N^(alpha-pPower) is introduced when
282
            # 
283
            for iShift in xrange(0, pIdegree):
284
                # When pTdegree == 1, the following loop only introduces 
285
                # a single new monomial.
286
                #print "++++++++++"
287
                for outerTpower in xrange(1, pTdegree + 1):
288
                    # First one high i degree mixed monomial.
289
                    iPower = iShift + pPower * pIdegree
290
                    if columnsWidth != 0:
291
                        polExpStr = spo_expression_as_string(iPower, 
292
                                                             outerTpower,
293
                                                             0,
294
                                                             alpha)
295
                        print "->", polExpStr
296
                    currentExpression = i^iPower * t^outerTpower * nAtAlpha
297
                    currentPolynomial = pRing(currentExpression)
298
                    polynomialsList.append(polExpStr) 
299
                    pMonomials = currentPolynomial.monomials()
300
                    pCoefficients = currentPolynomial.coefficients()
301
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
302
                                                            pCoefficients, 
303
                                                            knownMonomials, 
304
                                                            protoMatrixRows, 
305
                                                            columnsWidth)
306
                    #print "+++++"
307
                    # At iShift == 0, the following innerTpower loop adds  
308
                    # duplicate monomials, since no extra i^l * t^k is needed 
309
                    # before introducing the  
310
                    # i^iShift * t^outerPpower * p^pPower * N^(alpha-pPower) 
311
                    # polynomial.
312
                    # It introduces smaller i degree monomials than the
313
                    # one(s) added previously (no pPower multiplication).
314
                    # Here the exponent of t decreases as that of i increases.
315
                    # This conditional is not entered before pPower == 1.
316
                    # The innerTpower loop does not produce anything before
317
                    # pPower == 2. We keep it anyway for other configuration of
318
                    # p.
319
                    if iShift > 0:
320
                        iPower = pIdegree + iShift
321
                        for innerTpower in xrange(pPower, 1, -1):
322
                            if columnsWidth != 0:
323
                                polExpStr = spo_expression_as_string(iPower, 
324
                                                                     innerTpower,
325
                                                                     0,
326
                                                                     alpha) 
327
                            currentExpression = \
328
                                    i^(iPower) * t^(innerTpower) * nAtAlpha
329
                            currentPolynomial = pRing(currentExpression)
330
                            polynomialsList.append(polExpStr) 
331
                            pMonomials = currentPolynomial.monomials()
332
                            pCoefficients = currentPolynomial.coefficients()
333
                            spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
334
                                                                pCoefficients, 
335
                                                                knownMonomials, 
336
                                                                protoMatrixRows, 
337
                                                                columnsWidth)
338
                            iPower += pIdegree
339
                        # End for innerTpower
340
                    # End of if iShift > 0
341
                    # When iShift == 0, just after each of the  
342
                    # p^pPower * N^(alpha-pPower) polynomials has 
343
                    # been introduced (followed by a string of 
344
                    # i^k * p^pPower * N^(alpha-pPower) polynomials) a
345
                    # t^l *  p^pPower * N^(alpha-pPower) is introduced here.
346
                    # 
347
                    # Eventually, the following section introduces the 
348
                    # i^iShift * t^outerTpower * p^iPower * N^(alpha-iPower) 
349
                    # polynomials.
350
                    if columnsWidth != 0:
351
                        polExpStr = spo_expression_as_string(iShift, 
352
                                                             outerTpower,
353
                                                             pPower,
354
                                                             alpha)
355
                        print "->", polExpStr
356
                    currentExpression = i^iShift * t^outerTpower * nAtPower
357
                    currentPolynomial = pRing(currentExpression) * polynomialAtPower
358
                    polynomialsList.append(polExpStr) 
359
                    pMonomials = currentPolynomial.monomials()
360
                    pCoefficients = currentPolynomial.coefficients()
361
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials, 
362
                                                            pCoefficients, 
363
                                                            knownMonomials, 
364
                                                            protoMatrixRows, 
365
                                                            columnsWidth)
366
                # End for outerTpower 
367
                #print "++++++++++"
368
            # End for iShift
369
        polynomialAtPower *= p  
370
        nAtPower /= N
371
    # End for pPower loop
372
    return ((protoMatrixRows, knownMonomials, polynomialsList))
373
# End spo_polynomial_to_proto_matrix
374

    
375
def spo_proto_to_column_matrix(protoMatrixColumns, \
376
                               knownMonomialsList, \
377
                               boundVar1, \
378
                               boundVar2):
379
    """
380
    Create a column (each row holds the coefficients of one monomial) matrix.
381
    protoMatrixRows.
382
    
383
    Parameters
384
    ----------
385
    protoMatrixColumns: a list of coefficient lists.
386
    """
387
    numColumns = len(protoMatrixColumns)
388
    if numColumns == 0:
389
        return None
390
    # The last column holds has the maximum length. 
391
    numRows = len(protoMatrixColumns[numColumns-1])
392
    if numColumns == 0:
393
        return None
394
    baseMatrix = matrix(ZZ, numRows, numColumns)
395
    for colIndex in xrange(0, numColumns):
396
        for rowIndex in xrange(0, len(protoMatrixColumns[colIndex])):
397
            if protoMatrixColumns[colIndex][rowIndex] != 0:
398
                baseMatrix[rowIndex, colIndex] = \
399
                    protoMatrixColumns[colIndex][rowIndex] * \
400
                    knownMonomialsList[rowIndex](boundVar1, boundVar2)
401
    return baseMatrix
402
# End spo_proto_to_column_matrix.
403
#
404
def spo_proto_to_row_matrix(protoMatrixRows, \
405
                            knownMonomialsList, \
406
                            boundVar1, \
407
                            boundVar2):
408
    """
409
    Create a row (each column holds the evaluation one monomial at boundVar1 and
410
    boundVar2 values) matrix.
411
    
412
    Parameters
413
    ----------
414
    protoMatrixRows: a list of coefficient lists.
415
    """
416
    numRows = len(protoMatrixRows)
417
    if numRows == 0:
418
        return None
419
    # The last row is the longest one.
420
    numColumns = len(protoMatrixRows[numRows-1])
421
    if numColumns == 0:
422
        return None
423
    baseMatrix = matrix(ZZ, numRows, numColumns)
424
    for rowIndex in xrange(0, numRows):
425
        for colIndex in xrange(0, len(protoMatrixRows[rowIndex])):
426
            if protoMatrixRows[rowIndex][colIndex] !=  0:
427
                baseMatrix[rowIndex, colIndex] = \
428
                    protoMatrixRows[rowIndex][colIndex] * \
429
                    knownMonomialsList[colIndex](boundVar1,boundVar2)
430
            #print rowIndex, colIndex,
431
            #print protoMatrixRows[rowIndex][colIndex],
432
            #print knownMonomialsList[colIndex](boundVar1,boundVar2)
433
    return baseMatrix
434
# End spo_proto_to_row_matrix.
435
#
436
print "\t...sagePolynomialOperations loaded"