Révision 80 pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage

sagePolynomialOperations.sage (revision 80)
1 1
load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"
2 2

  
3
def spo_add_polynomial_coeffs_to_matrix(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
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

  
3 110
def spo_polynomial_to_matrix(p, pRing, alpha, N, columnsWidth=0):
4 111
    """
5 112
    From a (bivariate) polynomial and some other parameters build a matrix
6 113
    to be reduced by fpLLL.
7
    The matrix is such as those found in Boneh-Durphy and Stehlé.
114
    The matrix is such as those found in Boneh-Durphee and Stehl?.
8 115
    
9 116
    p: the (bivariate) polynomial
10 117
    alpha:
......
209 316
    # End for pPower loop
210 317
    return protoMatrixRows
211 318
# End spo_polynomial_to_matrix
212

  
213
def spo_add_polynomial_coeffs_to_matrix(pMonomials, 
214
                                        pCoefficients, 
215
                                        knownMonomials, 
216
                                        protoMatrixRows, 
217
                                        columnsWidth=0):
218
    """
219
    For a given polynomial (under the form of monomials and coefficents lists),
220
    add the coefficients of the protoMatrix (a list of proto rows).
221
    Coefficients are added to the protoMatrix row in the order imposed by the 
222
    monomials discovery list (the knownMonomials list) built as construction
223
    goes on. 
224
    As a bonus data can be printed out for a visual check.
225
    pMonomials     : the list of the monomials coming form some polynomial;
226
    pCoefficients  : the list of the corresponding coefficients to add to
227
                     the protoMatrix in the exact same order as the monomials;
228
    knownMonomials : the list of the already knonw monomials;
229
    protoMatrixRows: a list of lists, each one holding the coefficients of the 
230
                     monomials
231
    columnWith     : the width, in characters, of the displayed column ; if 0, 
232
                     do not display anything.
233
    """
234
    # We have started with the smaller degrees in the first variable.
235
    pMonomials.reverse()
236
    pCoefficients.reverse()
237
    # New empty proto matrix row.
238
    protoMatrixRowCoefficients = []
239
    # We work according to the order of the already known monomials
240
    # No known monomials yet: add the pMonomials to knownMonomials 
241
    # and add the coefficients to the proto matrix row.
242
    if len(knownMonomials) == 0: 
243
        for pmIdx in xrange(0, len(pMonomials)):
244
            knownMonomials.append(pMonomials[pmIdx])
245
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
246
            if columnsWidth != 0:
247
                monomialAsString = str(pCoefficients[pmIdx]) + " " + \
248
                                   str(pMonomials[pmIdx])
249
                print monomialAsString, " " * \
250
                      (columnsWidth - len(monomialAsString)),
251
    # There are some known monomials. We search for them in pMonomials and 
252
    # add their coefficients to the proto matrix row.
253
    else: 
254
        for knownMonomialIndex in xrange(0,len(knownMonomials)):
255
            # We lazily use an exception here since pMonomials.index() function
256
            # may fail throwing the ValueError exception.
257
            try:
258
                indexInPmonomials = \
259
                    pMonomials.index(knownMonomials[knownMonomialIndex])
260
                if columnsWidth != 0:
261
                    monomialAsString = str(pCoefficients[indexInPmonomials]) + \
262
                        " " + str(knownMonomials[knownMonomialIndex])
263
                    print monomialAsString, " " * \
264
                        (columnsWidth - len(monomialAsString)),
265
                # Add the coefficient to the proto matrix row and delete the \
266
                # known monomial from the current pMonomial list 
267
                #(and the corresponding coefficient as well).
268
                protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials])
269
                del pMonomials[indexInPmonomials]
270
                del pCoefficients[indexInPmonomials]
271
            # The knownMonomials element is not in pMonomials
272
            except ValueError:   
273
                protoMatrixRowCoefficients.append(0)
274
                if columnsWidth != 0:
275
                    monomialAsString = "0" + " "+ \
276
                        str(knownMonomials[knownMonomialIndex])
277
                    print monomialAsString, " " * \
278
                        (columnsWidth - len(monomialAsString)),
279
        # End for knownMonomialKey loop. 
280
        # We now append the remaining monomials of pMonomials to knownMonomials 
281
        # and the corresponding coefficients to proto matrix row.
282
        for pmIdx in xrange(0, len(pMonomials)):
283
            knownMonomials.append(pMonomials[pmIdx])
284
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
285
            if columnsWidth != 0:
286
                monomialAsString = str(pCoefficients[pmIdx]) + " " \
287
                    + str(pMonomials[pmIdx])
288
                print monomialAsString, " " * \
289
                    (columnsWidth - len(monomialAsString)),
290
        # End for pmIdx loop.
291
    # Add the new list row elements to the proto matrix.
292
    protoMatrixRows.append(protoMatrixRowCoefficients)
293
    if columnsWidth != 0:
294
        print    
295
# End spo_add_polynomial_coeffs_to_matrix
296

  
297
def spo_expression_as_string(powI, powT, powP, alpha):
298
    """
299
    Computes a string version of the i^k + t^l + p^m + N^n expression for
300
    output.
301
    """
302
    expressionAsString =""
303
    if powI != 0:
304
        expressionAsString += "i^" + str(powI)
305
    if powT != 0:
306
        if len(expressionAsString) != 0:
307
            expressionAsString += " * "
308
        expressionAsString += "t^" + str(powT)
309
    if powP != 0:
310
        if len(expressionAsString) != 0:
311
            expressionAsString += " * "
312
        expressionAsString += "p^" + str(powP)
313
    if (alpha - powP) != 0 :
314
        if len(expressionAsString) != 0:
315
            expressionAsString += " * "
316
        expressionAsString += "N^" + str(alpha - powP)
317
    return(expressionAsString)
318
# End spo_expression_as_string.

Formats disponibles : Unified diff