root / pobysoPythonSage / src / sageSLZ / sagePolynomialOperations.sage @ 76
Historique | Voir | Annoter | Télécharger (7,61 ko)
1 | 74 | storres | load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage" |
---|---|---|---|
2 | 74 | storres | |
3 | 74 | storres | def spo_polynomial_to_matrix(p, pRing, alpha, N, columnWidth=0): |
4 | 74 | storres | """ |
5 | 74 | storres | From a (bivariate) polynomial and some other parameters build a matrix |
6 | 74 | storres | to be reduced by fpLLL. |
7 | 74 | storres | The matrix is such as those found in Boneh-Durphy and Stehlé. |
8 | 74 | storres | |
9 | 74 | storres | p: the (bivariate) polynomial |
10 | 74 | storres | alpha: |
11 | 74 | storres | N: |
12 | 74 | storres | |
13 | 74 | storres | """ |
14 | 74 | storres | pVariables = p.variables() |
15 | 74 | storres | iVariable = pVariables[0] |
16 | 76 | storres | tVariable = pVariables[1] |
17 | 74 | storres | polynomialAtPower = P(1) |
18 | 74 | storres | currentPolynomial = P(1) |
19 | 74 | storres | pIdegree = p.degree(pVariables[0]) |
20 | 74 | storres | pTdegree = p.degree(pVariables[1]) |
21 | 74 | storres | currentIdegree = currentPolynomial.degree(i) |
22 | 74 | storres | nAtPower = N^alpha |
23 | 74 | storres | # We work from p^0 * N^alpha to p^alpha * N^0 |
24 | 74 | storres | for pPower in xrange(0, alpha + 1): |
25 | 76 | storres | # pPower == 0 is a special case. We introduce all the monomials but one |
26 | 76 | storres | # in, those in t necessary to be able to introduce |
27 | 76 | storres | # p. We arbitrary choose to introduce the highest degree monomial in i |
28 | 76 | storres | # with p. We also introduce all the mixed i^k * t^l monomials with |
29 | 76 | storres | # k < p.degree(i) and l <= p.degree(t) |
30 | 74 | storres | if pPower == 0: |
31 | 74 | storres | # iter1: power of i |
32 | 74 | storres | # Notice how i^pIdegree is excluded. |
33 | 74 | storres | for iPower in xrange(0, pIdegree): |
34 | 74 | storres | # iter5: power of t. |
35 | 74 | storres | for tPower in xrange(0, pTdegree + 1): |
36 | 74 | storres | if columnWidth != 0: |
37 | 76 | storres | print "->", spo_expression_as_string(iPower, |
38 | 76 | storres | tPower, |
39 | 76 | storres | pPower, |
40 | 74 | storres | alpha) |
41 | 74 | storres | currentExpression = iVariable^iPower * \ |
42 | 74 | storres | tVariable^tPower * nAtPower |
43 | 74 | storres | # polynomialAtPower == 1 here. Next line should be commented out but it does not work! |
44 | 74 | storres | # Some convertion problem? |
45 | 74 | storres | currentPolynomial = pRing(currentExpression) * \ |
46 | 74 | storres | polynomialAtPower |
47 | 74 | storres | pMonomials = currentPolynomial.monomials() |
48 | 74 | storres | pCoefficients = currentPolynomial.coefficients() |
49 | 76 | storres | spo_add_polynomial_coeffs_to_matrix(pMonomials, pCoefficients, knownMonomials, protoMatrixRows, monomialLengthChars) |
50 | 74 | storres | # End iter5. |
51 | 74 | storres | # End for iter1. |
52 | 74 | storres | |
53 | 74 | storres | else: # Next runs (p^1..p^alpha) |
54 | 74 | storres | pass |
55 | 74 | storres | |
56 | 74 | storres | # End spo_polynomial_to_matrix |
57 | 74 | storres | |
58 | 74 | storres | def spo_add_polynomial_coeffs_to_matrix(pMonomials, |
59 | 74 | storres | pCoefficients, |
60 | 74 | storres | knownMonomials, |
61 | 74 | storres | protoMatrixRows, |
62 | 74 | storres | columnWidth=0): |
63 | 74 | storres | """ |
64 | 74 | storres | For a given polynomial (under the form of monomials and coefficents lists), |
65 | 74 | storres | add the coefficients of the protoMatrix (a list of proto rows). |
66 | 74 | storres | Coefficients are added to the protoMatrix row in the order imposed by the |
67 | 74 | storres | monomials discovery list (the knownMonomials list) built as construction |
68 | 74 | storres | goes on. |
69 | 74 | storres | As a bonus data can be printed out for a visual check. |
70 | 74 | storres | pMonomials : the list of the monomials coming form some polynomial; |
71 | 74 | storres | pCoefficients : the list of the corresponding coefficients to add to |
72 | 74 | storres | the protoMatrix in the exact same order as the monomials; |
73 | 74 | storres | knownMonomials : the list of the already knonw monomials; |
74 | 74 | storres | protoMatrixRows: a list of lists, each one holding the coefficients of the |
75 | 74 | storres | monomials |
76 | 74 | storres | columnWith : the width, in characters, of the displayed column ; if 0, |
77 | 74 | storres | do display anything. |
78 | 74 | storres | """ |
79 | 74 | storres | # We have started with the smaller degrees in the first variable. |
80 | 74 | storres | pMonomials.reverse() |
81 | 74 | storres | pCoefficients.reverse() |
82 | 74 | storres | # New empty proto matrix row. |
83 | 74 | storres | protoMatrixRowCoefficients = [] |
84 | 74 | storres | # We work according to the order of the already known monomials |
85 | 74 | storres | # No known monomials yet: add the pMonomials to knownMonomials |
86 | 74 | storres | # and add the coefficients to the proto matrix row. |
87 | 74 | storres | if len(knownMonomials) == 0: |
88 | 74 | storres | for pmIdx in xrange(0, len(pMonomials)): |
89 | 74 | storres | knownMonomials.append(pMonomials[pmIdx]) |
90 | 74 | storres | protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
91 | 74 | storres | if columnWidth != 0: |
92 | 74 | storres | monomialAsString = str(pCoefficients[pmIdx]) + " " + \ |
93 | 74 | storres | str(pMonomials[pmIdx]) |
94 | 74 | storres | print monomialAsString, " " * \ |
95 | 74 | storres | (columnWidth - len(monomialAsString)), |
96 | 74 | storres | # There are some known monomials. We search for them in pMonomials and |
97 | 74 | storres | # add their coefficients to the proto matrix row. |
98 | 74 | storres | else: |
99 | 74 | storres | for knownMonomialIndex in xrange(0,len(knownMonomials)): |
100 | 74 | storres | # We lazily use an exception here since pMonomials.index() function |
101 | 74 | storres | # may fail throwing the ValueError exception. |
102 | 74 | storres | try: |
103 | 74 | storres | indexInPmonomials = \ |
104 | 74 | storres | pMonomials.index(knownMonomials[knownMonomialIndex]) |
105 | 74 | storres | if columnWidth != 0: |
106 | 74 | storres | monomialAsString = str(pCoefficients[indexInPmonomials]) + \ |
107 | 74 | storres | " " + str(knownMonomials[knownMonomialIndex]) |
108 | 74 | storres | print monomialAsString, " " * \ |
109 | 74 | storres | (columnWidth - len(monomialAsString)), |
110 | 74 | storres | # Add the coefficient to the proto matrix row and delete the \ |
111 | 74 | storres | # known monomial from the current pMonomial list |
112 | 74 | storres | #(and the corresponding coefficient as well). |
113 | 74 | storres | protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials]) |
114 | 74 | storres | del pMonomials[indexInPmonomials] |
115 | 74 | storres | del pCoefficients[indexInPmonomials] |
116 | 74 | storres | # The knownMonomials element is not in pMonomials |
117 | 74 | storres | except ValueError: |
118 | 74 | storres | protoMatrixRowCoefficients.append(0) |
119 | 74 | storres | if columnWidth != 0: |
120 | 74 | storres | monomialAsString = "0" + " "+ \ |
121 | 74 | storres | str(knownMonomials[knownMonomialIndex]) |
122 | 74 | storres | print monomialAsString, " " * \ |
123 | 74 | storres | (columnWidth - len(monomialAsString)), |
124 | 74 | storres | # End for knownMonomialKey loop. |
125 | 74 | storres | # We now append the remaining monomials of pMonomials to knownMonomials |
126 | 74 | storres | # and the corresponding coefficients to proto matrix row. |
127 | 74 | storres | for pmIdx in xrange(0, len(pMonomials)): |
128 | 74 | storres | knownMonomials.append(pMonomials[pmIdx]) |
129 | 74 | storres | protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
130 | 74 | storres | if columnWidth != 0: |
131 | 74 | storres | monomialAsString = str(pCoefficients[pmIdx]) + " " \ |
132 | 74 | storres | + str(pMonomials[pmIdx]) |
133 | 74 | storres | print monomialAsString, " " * \ |
134 | 74 | storres | (columnWidth - len(monomialAsString)), |
135 | 74 | storres | # End for pmIdx loop. |
136 | 74 | storres | # Add the new list row elements to the proto matrix. |
137 | 74 | storres | protoMatrixRows.append(protoMatrixRowCoefficients) |
138 | 74 | storres | if columnWidth != 0: |
139 | 74 | storres | |
140 | 74 | storres | # End spo_add_polynomial_coeffs_to_matrix |
141 | 75 | storres | |
142 | 75 | storres | def spo_expression_as_string(powI, powT, powP, alpha): |
143 | 76 | storres | """ |
144 | 76 | storres | Computes a string version of the i^k + t^l + p^m + N^n expression for |
145 | 76 | storres | output. |
146 | 76 | storres | """ |
147 | 75 | storres | expressionAsString ="" |
148 | 75 | storres | if powI != 0: |
149 | 75 | storres | expressionAsString += "i^" + str(powI) |
150 | 75 | storres | if powT != 0: |
151 | 75 | storres | if len(expressionAsString) != 0: |
152 | 75 | storres | expressionAsString += " * " |
153 | 75 | storres | expressionAsString += "t^" + str(powT) |
154 | 75 | storres | if powP != 0: |
155 | 75 | storres | if len(expressionAsString) != 0: |
156 | 75 | storres | expressionAsString += " * " |
157 | 75 | storres | expressionAsString += "p^" + str(powP) |
158 | 75 | storres | if (alpha - powP) != 0 : |
159 | 75 | storres | if len(expressionAsString) != 0: |
160 | 75 | storres | expressionAsString += " * " |
161 | 75 | storres | expressionAsString += "N^" + str(alpha - powP) |
162 | 75 | storres | return(expressionAsString) |
163 | 75 | storres | # End spo_expression_as_string. |