root / pobysoPythonSage / src / sageSLZ / sagePolynomialOperations.sage @ 155
Historique | Voir | Annoter | Télécharger (47,46 ko)
1 | 74 | storres | load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage" |
---|---|---|---|
2 | 87 | storres | print "sagePolynomialOperations loading..." |
3 | 106 | storres | def spo_add_polynomial_coeffs_to_matrix_row(poly, |
4 | 83 | storres | knownMonomials, |
5 | 83 | storres | protoMatrixRows, |
6 | 83 | storres | columnsWidth=0): |
7 | 80 | storres | """ |
8 | 106 | storres | For a given polynomial , |
9 | 80 | storres | add the coefficients of the protoMatrix (a list of proto matrix rows). |
10 | 80 | storres | Coefficients are added to the protoMatrix row in the order imposed by the |
11 | 80 | storres | monomials discovery list (the knownMonomials list) built as construction |
12 | 80 | storres | goes on. |
13 | 83 | storres | As a bonus, data can be printed out for a visual check. |
14 | 106 | storres | poly : the polynomial; in argument; |
15 | 106 | storres | knownMonomials : the list of the already known monomials; will determine |
16 | 106 | storres | the order of the coefficients appending to a row; in-out |
17 | 106 | storres | argument (new monomials may be discovered and then |
18 | 106 | storres | appended the the knowMonomials list); |
19 | 80 | storres | protoMatrixRows: a list of lists, each one holding the coefficients of the |
20 | 106 | storres | monomials of a polynomial; in-out argument: a new row is |
21 | 106 | storres | added at each call; |
22 | 80 | storres | columnWith : the width, in characters, of the displayed column ; if 0, |
23 | 106 | storres | do not display anything; in argument. |
24 | 80 | storres | """ |
25 | 106 | storres | pMonomials = poly.monomials() |
26 | 106 | storres | pCoefficients = poly.coefficients() |
27 | 80 | storres | # We have started with the smaller degrees in the first variable. |
28 | 80 | storres | pMonomials.reverse() |
29 | 80 | storres | pCoefficients.reverse() |
30 | 80 | storres | # New empty proto matrix row. |
31 | 80 | storres | protoMatrixRowCoefficients = [] |
32 | 80 | storres | # We work according to the order of the already known monomials |
33 | 80 | storres | # No known monomials yet: add the pMonomials to knownMonomials |
34 | 80 | storres | # and add the coefficients to the proto matrix row. |
35 | 80 | storres | if len(knownMonomials) == 0: |
36 | 80 | storres | for pmIdx in xrange(0, len(pMonomials)): |
37 | 80 | storres | knownMonomials.append(pMonomials[pmIdx]) |
38 | 80 | storres | protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
39 | 80 | storres | if columnsWidth != 0: |
40 | 80 | storres | monomialAsString = str(pCoefficients[pmIdx]) + " " + \ |
41 | 80 | storres | str(pMonomials[pmIdx]) |
42 | 80 | storres | print monomialAsString, " " * \ |
43 | 80 | storres | (columnsWidth - len(monomialAsString)), |
44 | 80 | storres | # There are some known monomials. We search for them in pMonomials and |
45 | 80 | storres | # add their coefficients to the proto matrix row. |
46 | 80 | storres | else: |
47 | 80 | storres | for knownMonomialIndex in xrange(0,len(knownMonomials)): |
48 | 80 | storres | # We lazily use an exception here since pMonomials.index() function |
49 | 80 | storres | # may fail throwing the ValueError exception. |
50 | 80 | storres | try: |
51 | 80 | storres | indexInPmonomials = \ |
52 | 80 | storres | pMonomials.index(knownMonomials[knownMonomialIndex]) |
53 | 80 | storres | if columnsWidth != 0: |
54 | 80 | storres | monomialAsString = str(pCoefficients[indexInPmonomials]) + \ |
55 | 80 | storres | " " + str(knownMonomials[knownMonomialIndex]) |
56 | 80 | storres | print monomialAsString, " " * \ |
57 | 80 | storres | (columnsWidth - len(monomialAsString)), |
58 | 155 | storres | # Add the coefficient to the proto matrix row and delete the |
59 | 80 | storres | # known monomial from the current pMonomial list |
60 | 80 | storres | #(and the corresponding coefficient as well). |
61 | 80 | storres | protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials]) |
62 | 80 | storres | del pMonomials[indexInPmonomials] |
63 | 80 | storres | del pCoefficients[indexInPmonomials] |
64 | 80 | storres | # The knownMonomials element is not in pMonomials |
65 | 80 | storres | except ValueError: |
66 | 80 | storres | protoMatrixRowCoefficients.append(0) |
67 | 80 | storres | if columnsWidth != 0: |
68 | 80 | storres | monomialAsString = "0" + " "+ \ |
69 | 80 | storres | str(knownMonomials[knownMonomialIndex]) |
70 | 80 | storres | print monomialAsString, " " * \ |
71 | 80 | storres | (columnsWidth - len(monomialAsString)), |
72 | 80 | storres | # End for knownMonomialKey loop. |
73 | 80 | storres | # We now append the remaining monomials of pMonomials to knownMonomials |
74 | 80 | storres | # and the corresponding coefficients to proto matrix row. |
75 | 80 | storres | for pmIdx in xrange(0, len(pMonomials)): |
76 | 80 | storres | knownMonomials.append(pMonomials[pmIdx]) |
77 | 80 | storres | protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
78 | 80 | storres | if columnsWidth != 0: |
79 | 80 | storres | monomialAsString = str(pCoefficients[pmIdx]) + " " \ |
80 | 80 | storres | + str(pMonomials[pmIdx]) |
81 | 80 | storres | print monomialAsString, " " * \ |
82 | 80 | storres | (columnsWidth - len(monomialAsString)), |
83 | 80 | storres | # End for pmIdx loop. |
84 | 80 | storres | # Add the new list row elements to the proto matrix. |
85 | 80 | storres | protoMatrixRows.append(protoMatrixRowCoefficients) |
86 | 80 | storres | if columnsWidth != 0: |
87 | 80 | storres | |
88 | 83 | storres | # End spo_add_polynomial_coeffs_to_matrix_row |
89 | 80 | storres | |
90 | 109 | storres | def spo_get_coefficient_for_monomial(monomialsList, coefficientsList, monomial): |
91 | 109 | storres | """ |
92 | 109 | storres | Get, for a polynomial, the coefficient for a given monomial. |
93 | 109 | storres | The polynomial is given as two lists (monomials and coefficients as |
94 | 109 | storres | return by the respective methods ; indexes of the two lists must match). |
95 | 109 | storres | If the monomial is not found, 0 is returned. |
96 | 109 | storres | """ |
97 | 109 | storres | monomialIndex = 0 |
98 | 109 | storres | for mono in monomialsList: |
99 | 109 | storres | if mono == monomial: |
100 | 109 | storres | return coefficientsList[monomialIndex] |
101 | 109 | storres | monomialIndex += 1 |
102 | 109 | storres | return 0 |
103 | 109 | storres | # End spo_get_coefficient_for_monomial. |
104 | 109 | storres | |
105 | 109 | storres | |
106 | 111 | storres | def spo_expression_as_string(powI, boundI, powT, boundT, powP, powN): |
107 | 80 | storres | """ |
108 | 80 | storres | Computes a string version of the i^k + t^l + p^m + N^n expression for |
109 | 80 | storres | output. |
110 | 80 | storres | """ |
111 | 80 | storres | expressionAsString ="" |
112 | 80 | storres | if powI != 0: |
113 | 111 | storres | expressionAsString += str(iBound^powI) + " i^" + str(powI) |
114 | 80 | storres | if powT != 0: |
115 | 80 | storres | if len(expressionAsString) != 0: |
116 | 80 | storres | expressionAsString += " * " |
117 | 111 | storres | expressionAsString += str(tBound^powT) + " t^" + str(powT) |
118 | 80 | storres | if powP != 0: |
119 | 80 | storres | if len(expressionAsString) != 0: |
120 | 80 | storres | expressionAsString += " * " |
121 | 80 | storres | expressionAsString += "p^" + str(powP) |
122 | 105 | storres | if (powN) != 0 : |
123 | 80 | storres | if len(expressionAsString) != 0: |
124 | 80 | storres | expressionAsString += " * " |
125 | 105 | storres | expressionAsString += "N^" + str(powN) |
126 | 80 | storres | return(expressionAsString) |
127 | 80 | storres | # End spo_expression_as_string. |
128 | 80 | storres | |
129 | 87 | storres | def spo_norm(poly, p=2): |
130 | 81 | storres | """ |
131 | 81 | storres | Behaves more or less (no infinity defined) as the norm for the |
132 | 81 | storres | univariate polynomials. |
133 | 107 | storres | Quoting Sage documentation: |
134 | 107 | storres | "Definition: For integer p, the p-norm of a polynomial is the pth root of |
135 | 81 | storres | the sum of the pth powers of the absolute values of the coefficients of |
136 | 107 | storres | the polynomial." |
137 | 87 | storres | |
138 | 81 | storres | """ |
139 | 87 | storres | # TODO: check the arguments (for p see below).. |
140 | 81 | storres | norm = 0 |
141 | 87 | storres | # For infinity norm. |
142 | 87 | storres | if p == Infinity: |
143 | 87 | storres | for coefficient in poly.coefficients(): |
144 | 87 | storres | coefficientAbs = coefficient.abs() |
145 | 87 | storres | if coefficientAbs > norm: |
146 | 87 | storres | norm = coefficientAbs |
147 | 87 | storres | return norm |
148 | 87 | storres | # TODO: check here the value of p |
149 | 107 | storres | # p must be a positive integer >= 1. |
150 | 107 | storres | if p < 1 or (not p in ZZ): |
151 | 94 | storres | return None |
152 | 87 | storres | # For 1 norm. |
153 | 87 | storres | if p == 1: |
154 | 87 | storres | for coefficient in poly.coefficients(): |
155 | 87 | storres | norm += coefficient.abs() |
156 | 87 | storres | return norm |
157 | 87 | storres | # For other norms |
158 | 81 | storres | for coefficient in poly.coefficients(): |
159 | 103 | storres | norm += coefficient.abs()^p |
160 | 87 | storres | return pow(norm, 1/p) |
161 | 81 | storres | # end spo_norm |
162 | 81 | storres | |
163 | 100 | storres | def spo_polynomial_to_proto_matrix(p, alpha, N, columnsWidth=0): |
164 | 74 | storres | """ |
165 | 83 | storres | From a (bivariate) polynomial and some other parameters build a proto |
166 | 87 | storres | matrix (an array of "rows") to be converted into a "true" matrix and |
167 | 83 | storres | eventually by reduced by fpLLL. |
168 | 102 | storres | The matrix is such as those found in Boneh-Durphee and Stehlé. |
169 | 74 | storres | |
170 | 83 | storres | Parameters |
171 | 83 | storres | ---------- |
172 | 87 | storres | p: the (bivariate) polynomial; |
173 | 87 | storres | pRing: the ring over which p is defined; |
174 | 74 | storres | alpha: |
175 | 74 | storres | N: |
176 | 83 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
177 | 83 | storres | printed in colums of columnsWitdth width. |
178 | 74 | storres | """ |
179 | 100 | storres | pRing = p.parent() |
180 | 77 | storres | knownMonomials = [] |
181 | 77 | storres | protoMatrixRows = [] |
182 | 92 | storres | polynomialsList = [] |
183 | 74 | storres | pVariables = p.variables() |
184 | 123 | storres | #print "In spo...", p, p.variables() |
185 | 74 | storres | iVariable = pVariables[0] |
186 | 76 | storres | tVariable = pVariables[1] |
187 | 87 | storres | polynomialAtPower = pRing(1) |
188 | 87 | storres | currentPolynomial = pRing(1) |
189 | 74 | storres | pIdegree = p.degree(pVariables[0]) |
190 | 74 | storres | pTdegree = p.degree(pVariables[1]) |
191 | 87 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
192 | 105 | storres | nAtAlpha = N^alpha |
193 | 105 | storres | nAtPower = nAtAlpha |
194 | 92 | storres | polExpStr = "" |
195 | 74 | storres | # We work from p^0 * N^alpha to p^alpha * N^0 |
196 | 74 | storres | for pPower in xrange(0, alpha + 1): |
197 | 76 | storres | # pPower == 0 is a special case. We introduce all the monomials but one |
198 | 78 | storres | # in i and those in t necessary to be able to introduce |
199 | 76 | storres | # p. We arbitrary choose to introduce the highest degree monomial in i |
200 | 76 | storres | # with p. We also introduce all the mixed i^k * t^l monomials with |
201 | 77 | storres | # k < p.degree(i) and l <= p.degree(t). |
202 | 78 | storres | # Mixed terms introduction is necessary here before we start "i shifts" |
203 | 78 | storres | # in the next iteration. |
204 | 74 | storres | if pPower == 0: |
205 | 78 | storres | # Notice that i^pIdegree is excluded as the bound of the xrange is |
206 | 78 | storres | # pIdegree |
207 | 74 | storres | for iPower in xrange(0, pIdegree): |
208 | 74 | storres | for tPower in xrange(0, pTdegree + 1): |
209 | 77 | storres | if columnsWidth != 0: |
210 | 92 | storres | polExpStr = spo_expression_as_string(iPower, |
211 | 76 | storres | tPower, |
212 | 76 | storres | pPower, |
213 | 105 | storres | alpha-pPower) |
214 | 92 | storres | print "->", polExpStr |
215 | 74 | storres | currentExpression = iVariable^iPower * \ |
216 | 91 | storres | tVariable^tPower * nAtAlpha |
217 | 78 | storres | # polynomialAtPower == 1 here. Next line should be commented |
218 | 78 | storres | # out but it does not work! Some conversion problem? |
219 | 91 | storres | currentPolynomial = pRing(currentExpression) |
220 | 106 | storres | polynomialsList.append(currentPolynomial) |
221 | 74 | storres | pMonomials = currentPolynomial.monomials() |
222 | 74 | storres | pCoefficients = currentPolynomial.coefficients() |
223 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
224 | 83 | storres | pCoefficients, |
225 | 83 | storres | knownMonomials, |
226 | 83 | storres | protoMatrixRows, |
227 | 83 | storres | columnsWidth) |
228 | 78 | storres | # End tPower. |
229 | 78 | storres | # End for iPower. |
230 | 77 | storres | else: # pPower > 0: (p^1..p^alpha) |
231 | 78 | storres | # This where we introduce the p^pPower * N^(alpha-pPower) |
232 | 77 | storres | # polynomial. |
233 | 77 | storres | # This step could technically be fused as the first iteration |
234 | 77 | storres | # of the next loop (with iPower starting at 0). |
235 | 77 | storres | # We set it apart for clarity. |
236 | 77 | storres | if columnsWidth != 0: |
237 | 105 | storres | polExpStr = spo_expression_as_string(0, 0, pPower, alpha-pPower) |
238 | 92 | storres | print "->", polExpStr |
239 | 77 | storres | currentPolynomial = polynomialAtPower * nAtPower |
240 | 106 | storres | polynomialsList.append(currentPolynomial) |
241 | 77 | storres | pMonomials = currentPolynomial.monomials() |
242 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
243 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
244 | 83 | storres | pCoefficients, |
245 | 83 | storres | knownMonomials, |
246 | 83 | storres | protoMatrixRows, |
247 | 83 | storres | columnsWidth) |
248 | 77 | storres | |
249 | 77 | storres | # The i^iPower * p^pPower polynomials: they add i^k monomials to |
250 | 77 | storres | # p^pPower up to k < pIdegree * pPower. This only introduces i^k |
251 | 77 | storres | # monomials since mixed terms (that were introduced at a previous |
252 | 77 | storres | # stage) are only shifted to already existing |
253 | 77 | storres | # ones. p^pPower is "shifted" to higher degrees in i as far as |
254 | 77 | storres | # possible, one step short of the degree in i of p^(pPower+1) . |
255 | 77 | storres | # These "pure" i^k monomials can only show up with i multiplications. |
256 | 77 | storres | for iPower in xrange(1, pIdegree): |
257 | 87 | storres | if columnsWidth != 0: |
258 | 92 | storres | polExpStr = spo_expression_as_string(iPower, \ |
259 | 87 | storres | 0, \ |
260 | 87 | storres | pPower, \ |
261 | 87 | storres | alpha) |
262 | 92 | storres | print "->", polExpStr |
263 | 77 | storres | currentExpression = i^iPower * nAtPower |
264 | 87 | storres | currentPolynomial = pRing(currentExpression) * polynomialAtPower |
265 | 106 | storres | polynomialsList.append(currentPolynomial) |
266 | 77 | storres | pMonomials = currentPolynomial.monomials() |
267 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
268 | 87 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, \ |
269 | 87 | storres | pCoefficients, \ |
270 | 87 | storres | knownMonomials, \ |
271 | 87 | storres | protoMatrixRows, \ |
272 | 83 | storres | columnsWidth) |
273 | 77 | storres | # End for iPower |
274 | 77 | storres | # We want now to introduce a t * p^pPower polynomial. But before |
275 | 77 | storres | # that we must introduce some mixed monomials. |
276 | 77 | storres | # This loop is no triggered before pPower == 2. |
277 | 78 | storres | # It introduces a first set of high i degree mixed monomials. |
278 | 77 | storres | for iPower in xrange(1, pPower): |
279 | 77 | storres | tPower = pPower - iPower + 1 |
280 | 77 | storres | if columnsWidth != 0: |
281 | 92 | storres | polExpStr = spo_expression_as_string(iPower * pIdegree, |
282 | 77 | storres | tPower, |
283 | 77 | storres | 0, |
284 | 77 | storres | alpha) |
285 | 92 | storres | print "->", polExpStr |
286 | 91 | storres | currentExpression = i^(iPower * pIdegree) * t^tPower * nAtAlpha |
287 | 87 | storres | currentPolynomial = pRing(currentExpression) |
288 | 106 | storres | polynomialsList.append(currentPolynomial) |
289 | 77 | storres | pMonomials = currentPolynomial.monomials() |
290 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
291 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
292 | 83 | storres | pCoefficients, |
293 | 83 | storres | knownMonomials, |
294 | 83 | storres | protoMatrixRows, |
295 | 83 | storres | columnsWidth) |
296 | 77 | storres | # End for iPower |
297 | 78 | storres | # |
298 | 78 | storres | # This is the mixed monomials main loop. It introduces: |
299 | 77 | storres | # - the missing mixed monomials needed before the |
300 | 78 | storres | # t^l * p^pPower * N^(alpha-pPower) polynomial; |
301 | 78 | storres | # - the t^l * p^pPower * N^(alpha-pPower) itself; |
302 | 78 | storres | # - for each of i^k * t^l * p^pPower * N^(alpha-pPower) polynomials: |
303 | 78 | storres | # - the the missing mixed monomials needed polynomials, |
304 | 78 | storres | # - the i^k * t^l * p^pPower * N^(alpha-pPower) itself. |
305 | 78 | storres | # The t^l * p^pPower * N^(alpha-pPower) is introduced when |
306 | 78 | storres | # |
307 | 77 | storres | for iShift in xrange(0, pIdegree): |
308 | 77 | storres | # When pTdegree == 1, the following loop only introduces |
309 | 77 | storres | # a single new monomial. |
310 | 77 | storres | #print "++++++++++" |
311 | 77 | storres | for outerTpower in xrange(1, pTdegree + 1): |
312 | 77 | storres | # First one high i degree mixed monomial. |
313 | 77 | storres | iPower = iShift + pPower * pIdegree |
314 | 77 | storres | if columnsWidth != 0: |
315 | 92 | storres | polExpStr = spo_expression_as_string(iPower, |
316 | 77 | storres | outerTpower, |
317 | 77 | storres | 0, |
318 | 77 | storres | alpha) |
319 | 92 | storres | print "->", polExpStr |
320 | 91 | storres | currentExpression = i^iPower * t^outerTpower * nAtAlpha |
321 | 87 | storres | currentPolynomial = pRing(currentExpression) |
322 | 106 | storres | polynomialsList.append(currentPolynomial) |
323 | 77 | storres | pMonomials = currentPolynomial.monomials() |
324 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
325 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
326 | 83 | storres | pCoefficients, |
327 | 83 | storres | knownMonomials, |
328 | 83 | storres | protoMatrixRows, |
329 | 83 | storres | columnsWidth) |
330 | 77 | storres | #print "+++++" |
331 | 78 | storres | # At iShift == 0, the following innerTpower loop adds |
332 | 78 | storres | # duplicate monomials, since no extra i^l * t^k is needed |
333 | 78 | storres | # before introducing the |
334 | 77 | storres | # i^iShift * t^outerPpower * p^pPower * N^(alpha-pPower) |
335 | 77 | storres | # polynomial. |
336 | 77 | storres | # It introduces smaller i degree monomials than the |
337 | 77 | storres | # one(s) added previously (no pPower multiplication). |
338 | 77 | storres | # Here the exponent of t decreases as that of i increases. |
339 | 78 | storres | # This conditional is not entered before pPower == 1. |
340 | 78 | storres | # The innerTpower loop does not produce anything before |
341 | 78 | storres | # pPower == 2. We keep it anyway for other configuration of |
342 | 78 | storres | # p. |
343 | 77 | storres | if iShift > 0: |
344 | 77 | storres | iPower = pIdegree + iShift |
345 | 77 | storres | for innerTpower in xrange(pPower, 1, -1): |
346 | 77 | storres | if columnsWidth != 0: |
347 | 92 | storres | polExpStr = spo_expression_as_string(iPower, |
348 | 77 | storres | innerTpower, |
349 | 77 | storres | 0, |
350 | 77 | storres | alpha) |
351 | 77 | storres | currentExpression = \ |
352 | 91 | storres | i^(iPower) * t^(innerTpower) * nAtAlpha |
353 | 87 | storres | currentPolynomial = pRing(currentExpression) |
354 | 106 | storres | polynomialsList.append(currentPolynomial) |
355 | 77 | storres | pMonomials = currentPolynomial.monomials() |
356 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
357 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
358 | 77 | storres | pCoefficients, |
359 | 77 | storres | knownMonomials, |
360 | 77 | storres | protoMatrixRows, |
361 | 77 | storres | columnsWidth) |
362 | 77 | storres | iPower += pIdegree |
363 | 77 | storres | # End for innerTpower |
364 | 77 | storres | # End of if iShift > 0 |
365 | 78 | storres | # When iShift == 0, just after each of the |
366 | 78 | storres | # p^pPower * N^(alpha-pPower) polynomials has |
367 | 78 | storres | # been introduced (followed by a string of |
368 | 78 | storres | # i^k * p^pPower * N^(alpha-pPower) polynomials) a |
369 | 78 | storres | # t^l * p^pPower * N^(alpha-pPower) is introduced here. |
370 | 78 | storres | # |
371 | 77 | storres | # Eventually, the following section introduces the |
372 | 105 | storres | # i^iShift * t^outerTpower * p^iPower * N^(alpha-pPower) |
373 | 77 | storres | # polynomials. |
374 | 77 | storres | if columnsWidth != 0: |
375 | 92 | storres | polExpStr = spo_expression_as_string(iShift, |
376 | 77 | storres | outerTpower, |
377 | 77 | storres | pPower, |
378 | 105 | storres | alpha-pPower) |
379 | 92 | storres | print "->", polExpStr |
380 | 77 | storres | currentExpression = i^iShift * t^outerTpower * nAtPower |
381 | 105 | storres | currentPolynomial = pRing(currentExpression) * \ |
382 | 105 | storres | polynomialAtPower |
383 | 106 | storres | polynomialsList.append(currentPolynomial) |
384 | 77 | storres | pMonomials = currentPolynomial.monomials() |
385 | 77 | storres | pCoefficients = currentPolynomial.coefficients() |
386 | 83 | storres | spo_add_polynomial_coeffs_to_matrix_row(pMonomials, |
387 | 83 | storres | pCoefficients, |
388 | 83 | storres | knownMonomials, |
389 | 83 | storres | protoMatrixRows, |
390 | 83 | storres | columnsWidth) |
391 | 77 | storres | # End for outerTpower |
392 | 77 | storres | #print "++++++++++" |
393 | 77 | storres | # End for iShift |
394 | 77 | storres | polynomialAtPower *= p |
395 | 77 | storres | nAtPower /= N |
396 | 77 | storres | # End for pPower loop |
397 | 92 | storres | return ((protoMatrixRows, knownMonomials, polynomialsList)) |
398 | 83 | storres | # End spo_polynomial_to_proto_matrix |
399 | 81 | storres | |
400 | 111 | storres | def spo_polynomial_to_polynomials_list_2(p, alpha, N, iBound, tBound, |
401 | 111 | storres | columnsWidth=0): |
402 | 105 | storres | """ |
403 | 112 | storres | Badly out of sync code: check with versions 3 or 4. |
404 | 112 | storres | |
405 | 106 | storres | From p, alpha, N build a list of polynomials... |
406 | 106 | storres | TODO: clean up the comments below! |
407 | 106 | storres | |
408 | 105 | storres | From a (bivariate) polynomial and some other parameters build a proto |
409 | 105 | storres | matrix (an array of "rows") to be converted into a "true" matrix and |
410 | 105 | storres | eventually by reduced by fpLLL. |
411 | 105 | storres | The matrix is based on a list of polynomials that are built in a way |
412 | 105 | storres | that one and only monomial is added at each new polynomial. Among the many |
413 | 105 | storres | possible ways to build this list we pick one strongly dependent on the |
414 | 105 | storres | structure of the polynomial and of the problem. |
415 | 105 | storres | We consider here the polynomials of the form: |
416 | 105 | storres | a_k*i^k + a_(k-1)*i^(k-1) + ... + a_1*i + a_0 - t |
417 | 105 | storres | The values of i and t are bounded and we eventually look for (i_0,t_0) |
418 | 105 | storres | pairs such that: |
419 | 105 | storres | a_k*i_0^k + a_(k-1)*i_0^(k-1) + ... + a_1*i_0 + a_0 = t_0 |
420 | 105 | storres | Hence, departing from the procedure in described in Boneh-Durfee, we will |
421 | 105 | storres | not use "t-shifts" but only "i-shifts". |
422 | 105 | storres | |
423 | 105 | storres | Parameters |
424 | 105 | storres | ---------- |
425 | 105 | storres | p: the (bivariate) polynomial; |
426 | 105 | storres | pRing: the ring over which p is defined; |
427 | 105 | storres | alpha: |
428 | 105 | storres | N: |
429 | 105 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
430 | 105 | storres | printed in colums of columnsWitdth width. |
431 | 105 | storres | """ |
432 | 105 | storres | pRing = p.parent() |
433 | 105 | storres | polynomialsList = [] |
434 | 105 | storres | pVariables = p.variables() |
435 | 105 | storres | iVariable = pVariables[0] |
436 | 105 | storres | tVariable = pVariables[1] |
437 | 105 | storres | polynomialAtPower = pRing(1) |
438 | 105 | storres | currentPolynomial = pRing(1) |
439 | 105 | storres | pIdegree = p.degree(iVariable) |
440 | 105 | storres | pTdegree = p.degree(tVariable) |
441 | 105 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
442 | 105 | storres | nAtAlpha = N^alpha |
443 | 105 | storres | nAtPower = nAtAlpha |
444 | 105 | storres | polExpStr = "" |
445 | 105 | storres | # We work from p^0 * N^alpha to p^alpha * N^0 |
446 | 105 | storres | for pPower in xrange(0, alpha + 1): |
447 | 105 | storres | # pPower == 0 is a special case. We introduce all the monomials in i |
448 | 105 | storres | # up to i^pIdegree. |
449 | 105 | storres | if pPower == 0: |
450 | 105 | storres | # Notice who iPower runs up to i^pIdegree. |
451 | 105 | storres | for iPower in xrange(0, pIdegree + 1): |
452 | 105 | storres | # No t power is taken into account as we limit our selves to |
453 | 105 | storres | # degree 1 in t and make no "t-shifts". |
454 | 105 | storres | if columnsWidth != 0: |
455 | 111 | storres | polExpStr = spo_expression_as_string(iPower, |
456 | 111 | storres | iBound, |
457 | 105 | storres | 0, |
458 | 111 | storres | tBound, |
459 | 105 | storres | 0, |
460 | 105 | storres | alpha) |
461 | 105 | storres | print "->", polExpStr |
462 | 111 | storres | currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
463 | 105 | storres | # polynomialAtPower == 1 here. Next line should be commented |
464 | 105 | storres | # out but it does not work! Some conversion problem? |
465 | 105 | storres | currentPolynomial = pRing(currentExpression) |
466 | 105 | storres | polynomialsList.append(currentPolynomial) |
467 | 105 | storres | # End for iPower. |
468 | 105 | storres | else: # pPower > 0: (p^1..p^alpha) |
469 | 105 | storres | # This where we introduce the p^pPower * N^(alpha-pPower) |
470 | 105 | storres | # polynomial. This is also where the t^pPower monomials shows up for |
471 | 105 | storres | # the first time. |
472 | 105 | storres | if columnsWidth != 0: |
473 | 111 | storres | polExpStr = spo_expression_as_string(0, iBound, 0, tBound, \ |
474 | 111 | storres | pPower, alpha-pPower) |
475 | 105 | storres | print "->", polExpStr |
476 | 105 | storres | currentPolynomial = polynomialAtPower * nAtPower |
477 | 105 | storres | polynomialsList.append(currentPolynomial) |
478 | 106 | storres | # Exit when pPower == alpha |
479 | 106 | storres | if pPower == alpha: |
480 | 110 | storres | return polynomialsList |
481 | 105 | storres | # This is where the "i-shifts" take place. Mixed terms, i^k * t^l |
482 | 105 | storres | # (that were introduced at a previous |
483 | 105 | storres | # stage or are introduced now) are only shifted to already existing |
484 | 105 | storres | # ones with the notable exception of i^iPower * t^pPower, which |
485 | 105 | storres | # must be manually introduced. |
486 | 105 | storres | # p^pPower is "shifted" to higher degrees in i as far as |
487 | 105 | storres | # possible, up to of the degree in i of p^(pPower+1). |
488 | 105 | storres | # These "pure" i^k monomials can only show up with i multiplications. |
489 | 105 | storres | for iPower in xrange(1, pIdegree + 1): |
490 | 105 | storres | # The i^iPower * t^pPower monomial. Notice the alpha exponent |
491 | 105 | storres | # for N. |
492 | 105 | storres | internalIpower = iPower |
493 | 105 | storres | for tPower in xrange(pPower,0,-1): |
494 | 105 | storres | if columnsWidth != 0: |
495 | 111 | storres | polExpStr = spo_expression_as_string(internalIpower, |
496 | 111 | storres | iBound, |
497 | 111 | storres | tPower, |
498 | 111 | storres | tBound, |
499 | 111 | storres | 0, |
500 | 105 | storres | alpha) |
501 | 105 | storres | print "->", polExpStr |
502 | 111 | storres | currentExpression = i^internalIpower * t^tPower * \ |
503 | 111 | storres | nAtAlpha * iBound^internalIpower * \ |
504 | 111 | storres | tBound^tPower |
505 | 111 | storres | |
506 | 105 | storres | currentPolynomial = pRing(currentExpression) |
507 | 105 | storres | polynomialsList.append(currentPolynomial) |
508 | 105 | storres | internalIpower += pIdegree |
509 | 105 | storres | # End for tPower |
510 | 105 | storres | # The i^iPower * p^pPower * N^(alpha-pPower) i-shift. |
511 | 105 | storres | if columnsWidth != 0: |
512 | 111 | storres | polExpStr = spo_expression_as_string(iPower, |
513 | 111 | storres | iBound, |
514 | 111 | storres | 0, |
515 | 111 | storres | tBound, |
516 | 111 | storres | pPower, |
517 | 105 | storres | alpha-pPower) |
518 | 105 | storres | print "->", polExpStr |
519 | 111 | storres | currentExpression = i^iPower * nAtPower * iBound^iPower |
520 | 105 | storres | currentPolynomial = pRing(currentExpression) * polynomialAtPower |
521 | 105 | storres | polynomialsList.append(currentPolynomial) |
522 | 105 | storres | # End for iPower |
523 | 105 | storres | polynomialAtPower *= p |
524 | 105 | storres | nAtPower /= N |
525 | 105 | storres | # End for pPower loop |
526 | 109 | storres | return polynomialsList |
527 | 105 | storres | # End spo_polynomial_to_proto_matrix_2 |
528 | 105 | storres | |
529 | 111 | storres | def spo_polynomial_to_polynomials_list_3(p, alpha, N, iBound, tBound, |
530 | 109 | storres | columnsWidth=0): |
531 | 108 | storres | """ |
532 | 108 | storres | From p, alpha, N build a list of polynomials... |
533 | 108 | storres | TODO: more in depth rationale... |
534 | 108 | storres | |
535 | 108 | storres | Our goal is to introduce each monomial with the smallest coefficient. |
536 | 108 | storres | |
537 | 108 | storres | |
538 | 108 | storres | |
539 | 108 | storres | Parameters |
540 | 108 | storres | ---------- |
541 | 108 | storres | p: the (bivariate) polynomial; |
542 | 108 | storres | pRing: the ring over which p is defined; |
543 | 108 | storres | alpha: |
544 | 108 | storres | N: |
545 | 108 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
546 | 108 | storres | printed in colums of columnsWitdth width. |
547 | 108 | storres | """ |
548 | 108 | storres | pRing = p.parent() |
549 | 108 | storres | polynomialsList = [] |
550 | 108 | storres | pVariables = p.variables() |
551 | 108 | storres | iVariable = pVariables[0] |
552 | 108 | storres | tVariable = pVariables[1] |
553 | 108 | storres | polynomialAtPower = pRing(1) |
554 | 108 | storres | currentPolynomial = pRing(1) |
555 | 108 | storres | pIdegree = p.degree(iVariable) |
556 | 108 | storres | pTdegree = p.degree(tVariable) |
557 | 108 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
558 | 108 | storres | nAtAlpha = N^alpha |
559 | 108 | storres | nAtPower = nAtAlpha |
560 | 108 | storres | polExpStr = "" |
561 | 108 | storres | # We work from p^0 * N^alpha to p^alpha * N^0 |
562 | 108 | storres | for pPower in xrange(0, alpha + 1): |
563 | 108 | storres | # pPower == 0 is a special case. We introduce all the monomials in i |
564 | 108 | storres | # up to i^pIdegree. |
565 | 108 | storres | if pPower == 0: |
566 | 108 | storres | # Notice who iPower runs up to i^pIdegree. |
567 | 108 | storres | for iPower in xrange(0, pIdegree + 1): |
568 | 108 | storres | # No t power is taken into account as we limit our selves to |
569 | 108 | storres | # degree 1 in t and make no "t-shifts". |
570 | 108 | storres | if columnsWidth != 0: |
571 | 108 | storres | polExpStr = spo_expression_as_string(iPower, |
572 | 111 | storres | iBound, |
573 | 108 | storres | 0, |
574 | 111 | storres | tBound, |
575 | 108 | storres | 0, |
576 | 108 | storres | alpha) |
577 | 108 | storres | print "->", polExpStr |
578 | 111 | storres | currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
579 | 108 | storres | # polynomialAtPower == 1 here. Next line should be commented |
580 | 108 | storres | # out but it does not work! Some conversion problem? |
581 | 108 | storres | currentPolynomial = pRing(currentExpression) |
582 | 108 | storres | polynomialsList.append(currentPolynomial) |
583 | 108 | storres | # End for iPower. |
584 | 108 | storres | else: # pPower > 0: (p^1..p^alpha) |
585 | 108 | storres | # This where we introduce the p^pPower * N^(alpha-pPower) |
586 | 108 | storres | # polynomial. This is also where the t^pPower monomials shows up for |
587 | 108 | storres | # the first time. It app |
588 | 108 | storres | if columnsWidth != 0: |
589 | 111 | storres | polExpStr = spo_expression_as_string(0, iBound, |
590 | 111 | storres | 0, tBound, |
591 | 111 | storres | pPower, alpha-pPower) |
592 | 108 | storres | print "->", polExpStr |
593 | 108 | storres | currentPolynomial = polynomialAtPower * nAtPower |
594 | 108 | storres | polynomialsList.append(currentPolynomial) |
595 | 108 | storres | # Exit when pPower == alpha |
596 | 108 | storres | if pPower == alpha: |
597 | 111 | storres | return polynomialsList |
598 | 108 | storres | # This is where the "i-shifts" take place. Mixed terms, i^k * t^l |
599 | 108 | storres | # (that were introduced at a previous |
600 | 108 | storres | # stage or are introduced now) are only shifted to already existing |
601 | 108 | storres | # ones with the notable exception of i^iPower * t^pPower, which |
602 | 108 | storres | # must be manually introduced. |
603 | 108 | storres | # p^pPower is "shifted" to higher degrees in i as far as |
604 | 108 | storres | # possible, up to of the degree in i of p^(pPower+1). |
605 | 108 | storres | # These "pure" i^k monomials can only show up with i multiplications. |
606 | 108 | storres | for iPower in xrange(1, pIdegree + 1): |
607 | 108 | storres | # The i^iPower * t^pPower monomial. Notice the alpha exponent |
608 | 108 | storres | # for N. |
609 | 108 | storres | internalIpower = iPower |
610 | 108 | storres | for tPower in xrange(pPower,0,-1): |
611 | 108 | storres | if columnsWidth != 0: |
612 | 111 | storres | polExpStr = spo_expression_as_string(internalIpower, |
613 | 111 | storres | iBound, |
614 | 111 | storres | tPower, |
615 | 111 | storres | tBound, |
616 | 111 | storres | 0, |
617 | 108 | storres | alpha) |
618 | 108 | storres | print "->", polExpStr |
619 | 111 | storres | currentExpression = i^internalIpower * t^tPower * nAtAlpha * \ |
620 | 111 | storres | iBound^internalIpower * tBound^tPower |
621 | 108 | storres | currentPolynomial = pRing(currentExpression) |
622 | 108 | storres | polynomialsList.append(currentPolynomial) |
623 | 108 | storres | internalIpower += pIdegree |
624 | 108 | storres | # End for tPower |
625 | 109 | storres | # Here we have to choose between a |
626 | 109 | storres | # i^iPower * p^pPower * N^(alpha-pPower) i-shift and |
627 | 111 | storres | # i^iPower * i^(d_i(p) * pPower) * N^alpha, depending on which |
628 | 109 | storres | # coefficient is smallest. |
629 | 109 | storres | IcurrentExponent = iPower + \ |
630 | 111 | storres | (pPower * polynomialAtPower.degree(iVariable)) |
631 | 111 | storres | currentMonomial = pRing(iVariable^IcurrentExponent) |
632 | 111 | storres | currentPolynomial = pRing(iVariable^iPower * nAtPower * \ |
633 | 111 | storres | iBound^iPower) * \ |
634 | 111 | storres | polynomialAtPower |
635 | 109 | storres | currMonomials = currentPolynomial.monomials() |
636 | 109 | storres | currCoefficients = currentPolynomial.coefficients() |
637 | 109 | storres | currentCoefficient = spo_get_coefficient_for_monomial( \ |
638 | 109 | storres | currMonomials, |
639 | 109 | storres | currCoefficients, |
640 | 109 | storres | currentMonomial) |
641 | 111 | storres | print "Current coefficient:", currentCoefficient |
642 | 111 | storres | alterCoefficient = iBound^IcurrentExponent * nAtAlpha |
643 | 111 | storres | print "N^alpha * ibound^", IcurrentExponent, ":", \ |
644 | 111 | storres | alterCoefficient |
645 | 111 | storres | if currentCoefficient > alterCoefficient : |
646 | 109 | storres | if columnsWidth != 0: |
647 | 111 | storres | polExpStr = spo_expression_as_string(IcurrentExponent, |
648 | 111 | storres | iBound, |
649 | 111 | storres | 0, |
650 | 111 | storres | tBound, |
651 | 111 | storres | 0, |
652 | 109 | storres | alpha) |
653 | 111 | storres | print "->", polExpStr |
654 | 111 | storres | polynomialsList.append(currentMonomial * \ |
655 | 111 | storres | alterCoefficient) |
656 | 109 | storres | else: |
657 | 109 | storres | if columnsWidth != 0: |
658 | 111 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
659 | 111 | storres | 0, tBound, |
660 | 111 | storres | pPower, |
661 | 109 | storres | alpha-pPower) |
662 | 111 | storres | print "->", polExpStr |
663 | 109 | storres | polynomialsList.append(currentPolynomial) |
664 | 108 | storres | # End for iPower |
665 | 108 | storres | polynomialAtPower *= p |
666 | 108 | storres | nAtPower /= N |
667 | 108 | storres | # End for pPower loop |
668 | 109 | storres | return polynomialsList |
669 | 108 | storres | # End spo_polynomial_to_proto_matrix_3 |
670 | 108 | storres | |
671 | 111 | storres | def spo_polynomial_to_polynomials_list_4(p, alpha, N, iBound, tBound, |
672 | 111 | storres | columnsWidth=0): |
673 | 83 | storres | """ |
674 | 111 | storres | From p, alpha, N build a list of polynomials... |
675 | 111 | storres | TODO: more in depth rationale... |
676 | 83 | storres | |
677 | 111 | storres | Our goal is to introduce each monomial with the smallest coefficient. |
678 | 111 | storres | |
679 | 111 | storres | |
680 | 111 | storres | |
681 | 83 | storres | Parameters |
682 | 83 | storres | ---------- |
683 | 111 | storres | p: the (bivariate) polynomial; |
684 | 111 | storres | pRing: the ring over which p is defined; |
685 | 111 | storres | alpha: |
686 | 111 | storres | N: |
687 | 111 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
688 | 111 | storres | printed in colums of columnsWitdth width. |
689 | 111 | storres | """ |
690 | 111 | storres | pRing = p.parent() |
691 | 111 | storres | polynomialsList = [] |
692 | 111 | storres | pVariables = p.variables() |
693 | 111 | storres | iVariable = pVariables[0] |
694 | 111 | storres | tVariable = pVariables[1] |
695 | 111 | storres | polynomialAtPower = copy(p) |
696 | 111 | storres | currentPolynomial = pRing(1) |
697 | 111 | storres | pIdegree = p.degree(iVariable) |
698 | 111 | storres | pTdegree = p.degree(tVariable) |
699 | 111 | storres | maxIdegree = pIdegree * alpha |
700 | 111 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
701 | 111 | storres | nAtAlpha = N^alpha |
702 | 111 | storres | nAtPower = nAtAlpha |
703 | 111 | storres | polExpStr = "" |
704 | 111 | storres | # We first introduce all the monomials in i alone multiplied by N^alpha. |
705 | 111 | storres | for iPower in xrange(0, maxIdegree + 1): |
706 | 111 | storres | if columnsWidth !=0: |
707 | 111 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
708 | 111 | storres | 0, tBound, |
709 | 111 | storres | 0, alpha) |
710 | 111 | storres | print "->", polExpStr |
711 | 111 | storres | currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
712 | 111 | storres | currentPolynomial = pRing(currentExpression) |
713 | 111 | storres | polynomialsList.append(currentPolynomial) |
714 | 111 | storres | # End for iPower |
715 | 111 | storres | # We work from p^1 * N^alpha-1 to p^alpha * N^0 |
716 | 111 | storres | for pPower in xrange(1, alpha + 1): |
717 | 111 | storres | # First of all the p^pPower * N^(alpha-pPower) polynomial. |
718 | 111 | storres | nAtPower /= N |
719 | 111 | storres | if columnsWidth !=0: |
720 | 111 | storres | polExpStr = spo_expression_as_string(0, iBound, |
721 | 111 | storres | 0, tBound, |
722 | 111 | storres | pPower, alpha-pPower) |
723 | 111 | storres | print "->", polExpStr |
724 | 111 | storres | currentPolynomial = polynomialAtPower * nAtPower |
725 | 111 | storres | polynomialsList.append(currentPolynomial) |
726 | 111 | storres | # Exit when pPower == alpha |
727 | 111 | storres | if pPower == alpha: |
728 | 111 | storres | return polynomialsList |
729 | 111 | storres | # We now introduce the mixed i^k * t^l monomials by i^m * p^n * N^(alpha-n) |
730 | 111 | storres | for iPower in xrange(1, pIdegree + 1): |
731 | 111 | storres | if columnsWidth != 0: |
732 | 111 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
733 | 111 | storres | 0, tBound, |
734 | 111 | storres | pPower, alpha-pPower) |
735 | 111 | storres | print "->", polExpStr |
736 | 111 | storres | currentExpression = i^iPower * iBound^iPower * nAtPower |
737 | 111 | storres | currentPolynomial = pRing(currentExpression) * polynomialAtPower |
738 | 111 | storres | polynomialsList.append(currentPolynomial) |
739 | 111 | storres | # End for iPower |
740 | 111 | storres | polynomialAtPower *= p |
741 | 111 | storres | # End for pPower loop |
742 | 111 | storres | return polynomialsList |
743 | 111 | storres | # End spo_polynomial_to_proto_matrix_4 |
744 | 111 | storres | |
745 | 113 | storres | def spo_polynomial_to_polynomials_list_5(p, alpha, N, iBound, tBound, |
746 | 113 | storres | columnsWidth=0): |
747 | 113 | storres | """ |
748 | 113 | storres | From p, alpha, N build a list of polynomials use to create a base |
749 | 113 | storres | that will eventually be reduced with LLL. |
750 | 113 | storres | |
751 | 113 | storres | The bounds are computed for the coefficients that will be used to |
752 | 113 | storres | form the base. |
753 | 113 | storres | |
754 | 113 | storres | We try to introduce only one new monomial at a time, to obtain a |
755 | 113 | storres | triangular matrix (it is easy to compute the volume of the underlining |
756 | 113 | storres | latice if the matrix is triangular). |
757 | 113 | storres | |
758 | 113 | storres | There are many possibilities to introduce the monomials: our goal is also |
759 | 113 | storres | to introduce each of them on the diagonal with the smallest coefficient. |
760 | 113 | storres | |
761 | 113 | storres | The method depends on the structure of the polynomial. Here it is adapted |
762 | 113 | storres | to the a_n*i^n + ... + a_1 * i - t + b form. |
763 | 113 | storres | |
764 | 113 | storres | Parameters |
765 | 113 | storres | ---------- |
766 | 113 | storres | p: the (bivariate) polynomial; |
767 | 113 | storres | alpha: |
768 | 113 | storres | N: |
769 | 113 | storres | iBound: |
770 | 113 | storres | tBound: |
771 | 113 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
772 | 113 | storres | printed in colums of columnsWitdth width. |
773 | 113 | storres | """ |
774 | 113 | storres | pRing = p.parent() |
775 | 113 | storres | polynomialsList = [] |
776 | 113 | storres | pVariables = p.variables() |
777 | 113 | storres | iVariable = pVariables[0] |
778 | 113 | storres | tVariable = pVariables[1] |
779 | 113 | storres | polynomialAtPower = copy(p) |
780 | 113 | storres | currentPolynomial = pRing(1) |
781 | 113 | storres | pIdegree = p.degree(iVariable) |
782 | 113 | storres | pTdegree = p.degree(tVariable) |
783 | 113 | storres | maxIdegree = pIdegree * alpha |
784 | 113 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
785 | 113 | storres | nAtAlpha = N^alpha |
786 | 113 | storres | nAtPower = nAtAlpha |
787 | 113 | storres | polExpStr = "" |
788 | 113 | storres | # We first introduce all the monomials in i alone multiplied by N^alpha. |
789 | 113 | storres | for iPower in xrange(0, maxIdegree + 1): |
790 | 113 | storres | if columnsWidth !=0: |
791 | 113 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
792 | 113 | storres | 0, tBound, |
793 | 113 | storres | 0, alpha) |
794 | 113 | storres | print "->", polExpStr |
795 | 113 | storres | currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
796 | 113 | storres | currentPolynomial = pRing(currentExpression) |
797 | 113 | storres | polynomialsList.append(currentPolynomial) |
798 | 113 | storres | # End for iPower |
799 | 113 | storres | # We work from p^1 * N^alpha-1 to p^alpha * N^0 |
800 | 113 | storres | for pPower in xrange(1, alpha + 1): |
801 | 113 | storres | # First of all the p^pPower * N^(alpha-pPower) polynomial. |
802 | 113 | storres | nAtPower /= N |
803 | 113 | storres | if columnsWidth !=0: |
804 | 113 | storres | polExpStr = spo_expression_as_string(0, iBound, |
805 | 113 | storres | 0, tBound, |
806 | 113 | storres | pPower, alpha-pPower) |
807 | 113 | storres | print "->", polExpStr |
808 | 113 | storres | currentPolynomial = polynomialAtPower * nAtPower |
809 | 113 | storres | polynomialsList.append(currentPolynomial) |
810 | 113 | storres | # Exit when pPower == alpha |
811 | 113 | storres | if pPower == alpha: |
812 | 113 | storres | return polynomialsList |
813 | 113 | storres | for iPower in xrange(1, pIdegree + 1): |
814 | 113 | storres | iCurrentPower = pIdegree + iPower |
815 | 113 | storres | for tPower in xrange(pPower-1, 0, -1): |
816 | 114 | storres | #print "tPower:", tPower |
817 | 113 | storres | if columnsWidth != 0: |
818 | 113 | storres | polExpStr = spo_expression_as_string(iCurrentPower, iBound, |
819 | 113 | storres | tPower, tBound, |
820 | 113 | storres | 0, alpha) |
821 | 113 | storres | print "->", polExpStr |
822 | 113 | storres | currentExpression = i^iCurrentPower * iBound^iCurrentPower * t^tPower * tBound^tPower *nAtAlpha |
823 | 113 | storres | currentPolynomial = pRing(currentExpression) |
824 | 113 | storres | polynomialsList.append(currentPolynomial) |
825 | 113 | storres | iCurrentPower += pIdegree |
826 | 113 | storres | # End for tPower |
827 | 113 | storres | # We now introduce the mixed i^k * t^l monomials by i^m * p^n * N^(alpha-n) |
828 | 113 | storres | if columnsWidth != 0: |
829 | 113 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
830 | 113 | storres | 0, tBound, |
831 | 113 | storres | pPower, alpha-pPower) |
832 | 113 | storres | print "->", polExpStr |
833 | 113 | storres | currentExpression = i^iPower * iBound^iPower * nAtPower |
834 | 113 | storres | currentPolynomial = pRing(currentExpression) * polynomialAtPower |
835 | 113 | storres | polynomialsList.append(currentPolynomial) |
836 | 113 | storres | # End for iPower |
837 | 113 | storres | polynomialAtPower *= p |
838 | 113 | storres | # End for pPower loop |
839 | 113 | storres | return polynomialsList |
840 | 113 | storres | # End spo_polynomial_to_proto_matrix_5 |
841 | 113 | storres | |
842 | 155 | storres | def spo_polynomial_to_polynomials_list_6(p, alpha, N, iBound, tBound, |
843 | 155 | storres | columnsWidth=0): |
844 | 155 | storres | """ |
845 | 155 | storres | From p, alpha, N build a list of polynomials use to create a base |
846 | 155 | storres | that will eventually be reduced with LLL. |
847 | 155 | storres | |
848 | 155 | storres | The bounds are computed for the coefficients that will be used to |
849 | 155 | storres | form the base. |
850 | 155 | storres | |
851 | 155 | storres | We try to introduce only one new monomial at a time, whithout trying to |
852 | 155 | storres | obtain a triangular matrix. |
853 | 155 | storres | |
854 | 155 | storres | There are many possibilities to introduce the monomials: our goal is also |
855 | 155 | storres | to introduce each of them on the diagonal with the smallest coefficient. |
856 | 155 | storres | |
857 | 155 | storres | The method depends on the structure of the polynomial. Here it is adapted |
858 | 155 | storres | to the a_n*i^n + ... + a_1 * i - t + b form. |
859 | 155 | storres | |
860 | 155 | storres | Parameters |
861 | 155 | storres | ---------- |
862 | 155 | storres | p: the (bivariate) polynomial; |
863 | 155 | storres | alpha: |
864 | 155 | storres | N: |
865 | 155 | storres | iBound: |
866 | 155 | storres | tBound: |
867 | 155 | storres | columsWidth: if == 0, no information is displayed, otherwise data is |
868 | 155 | storres | printed in colums of columnsWitdth width. |
869 | 155 | storres | """ |
870 | 155 | storres | pRing = p.parent() |
871 | 155 | storres | polynomialsList = [] |
872 | 155 | storres | pVariables = p.variables() |
873 | 155 | storres | iVariable = pVariables[0] |
874 | 155 | storres | tVariable = pVariables[1] |
875 | 155 | storres | polynomialAtPower = copy(p) |
876 | 155 | storres | currentPolynomial = pRing(1) # Constant term. |
877 | 155 | storres | pIdegree = p.degree(iVariable) |
878 | 155 | storres | pTdegree = p.degree(tVariable) |
879 | 155 | storres | maxIdegree = pIdegree * alpha |
880 | 155 | storres | currentIdegree = currentPolynomial.degree(iVariable) |
881 | 155 | storres | nAtAlpha = N^alpha |
882 | 155 | storres | nAtPower = nAtAlpha |
883 | 155 | storres | polExpStr = "" |
884 | 155 | storres | # |
885 | 155 | storres | ## Shouldn't we start at tPower == 1 because polynomials without |
886 | 155 | storres | # t monomials are useless? |
887 | 155 | storres | for tPower in xrange(0, alpha+1): |
888 | 155 | storres | ## Start at iPower == 0 because here there are i monomials |
889 | 155 | storres | # in p even if iPower is zero. |
890 | 155 | storres | for iPower in xrange(0, alpha-tPower+1): |
891 | 155 | storres | if iPower + tPower <= alpha: |
892 | 155 | storres | print "iPower:", iPower, " tPower:", tPower |
893 | 155 | storres | q = pRing(iVariable * iBound)^iPower * ((p * N)^tPower) |
894 | 155 | storres | print q.monomials() |
895 | 155 | storres | polynomialsList.append(q) |
896 | 155 | storres | return polynomialsList |
897 | 155 | storres | |
898 | 155 | storres | """ |
899 | 155 | storres | # We first introduce all the monomials in i alone multiplied by N^alpha. |
900 | 155 | storres | for iPower in xrange(0, maxIdegree + 1): |
901 | 155 | storres | if columnsWidth !=0: |
902 | 155 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
903 | 155 | storres | 0, tBound, |
904 | 155 | storres | 0, alpha) |
905 | 155 | storres | print "->", polExpStr |
906 | 155 | storres | currentExpression = iVariable^iPower * nAtAlpha * iBound^iPower |
907 | 155 | storres | currentPolynomial = pRing(currentExpression) |
908 | 155 | storres | polynomialsList.append(currentPolynomial) |
909 | 155 | storres | # End for iPower |
910 | 155 | storres | # We work from p^1 * N^alpha-1 to p^alpha * N^0 |
911 | 155 | storres | for pPower in xrange(1, alpha + 1): |
912 | 155 | storres | # First of all the p^pPower * N^(alpha-pPower) polynomial. |
913 | 155 | storres | nAtPower /= N |
914 | 155 | storres | if columnsWidth !=0: |
915 | 155 | storres | polExpStr = spo_expression_as_string(0, iBound, |
916 | 155 | storres | 0, tBound, |
917 | 155 | storres | pPower, alpha-pPower) |
918 | 155 | storres | print "->", polExpStr |
919 | 155 | storres | currentPolynomial = polynomialAtPower * nAtPower |
920 | 155 | storres | polynomialsList.append(currentPolynomial) |
921 | 155 | storres | # Exit when pPower == alpha |
922 | 155 | storres | if pPower == alpha: |
923 | 155 | storres | return polynomialsList |
924 | 155 | storres | for iPower in xrange(1, pIdegree + 1): |
925 | 155 | storres | iCurrentPower = pIdegree + iPower |
926 | 155 | storres | for tPower in xrange(pPower-1, 0, -1): |
927 | 155 | storres | #print "tPower:", tPower |
928 | 155 | storres | if columnsWidth != 0: |
929 | 155 | storres | polExpStr = spo_expression_as_string(iCurrentPower, iBound, |
930 | 155 | storres | tPower, tBound, |
931 | 155 | storres | 0, alpha) |
932 | 155 | storres | print "->", polExpStr |
933 | 155 | storres | currentExpression = i^iCurrentPower * iBound^iCurrentPower * t^tPower * tBound^tPower *nAtAlpha |
934 | 155 | storres | currentPolynomial = pRing(currentExpression) |
935 | 155 | storres | polynomialsList.append(currentPolynomial) |
936 | 155 | storres | iCurrentPower += pIdegree |
937 | 155 | storres | # End for tPower |
938 | 155 | storres | # We now introduce the mixed i^k * t^l monomials by i^m * p^n * N^(alpha-n) |
939 | 155 | storres | if columnsWidth != 0: |
940 | 155 | storres | polExpStr = spo_expression_as_string(iPower, iBound, |
941 | 155 | storres | 0, tBound, |
942 | 155 | storres | pPower, alpha-pPower) |
943 | 155 | storres | print "->", polExpStr |
944 | 155 | storres | currentExpression = i^iPower * iBound^iPower * nAtPower |
945 | 155 | storres | currentPolynomial = pRing(currentExpression) * polynomialAtPower |
946 | 155 | storres | polynomialsList.append(currentPolynomial) |
947 | 155 | storres | # End for iPower |
948 | 155 | storres | polynomialAtPower *= p |
949 | 155 | storres | # End for pPower loop |
950 | 155 | storres | """ |
951 | 155 | storres | return polynomialsList |
952 | 155 | storres | # End spo_polynomial_to_proto_matrix_6 |
953 | 155 | storres | |
954 | 111 | storres | def spo_proto_to_column_matrix(protoMatrixColumns): |
955 | 111 | storres | """ |
956 | 111 | storres | Create a column (each row holds the coefficients for one monomial) matrix. |
957 | 111 | storres | |
958 | 111 | storres | Parameters |
959 | 111 | storres | ---------- |
960 | 87 | storres | protoMatrixColumns: a list of coefficient lists. |
961 | 83 | storres | """ |
962 | 87 | storres | numColumns = len(protoMatrixColumns) |
963 | 87 | storres | if numColumns == 0: |
964 | 83 | storres | return None |
965 | 87 | storres | # The last column holds has the maximum length. |
966 | 87 | storres | numRows = len(protoMatrixColumns[numColumns-1]) |
967 | 83 | storres | if numColumns == 0: |
968 | 83 | storres | return None |
969 | 83 | storres | baseMatrix = matrix(ZZ, numRows, numColumns) |
970 | 87 | storres | for colIndex in xrange(0, numColumns): |
971 | 87 | storres | for rowIndex in xrange(0, len(protoMatrixColumns[colIndex])): |
972 | 90 | storres | if protoMatrixColumns[colIndex][rowIndex] != 0: |
973 | 90 | storres | baseMatrix[rowIndex, colIndex] = \ |
974 | 111 | storres | protoMatrixColumns[colIndex][rowIndex] |
975 | 83 | storres | return baseMatrix |
976 | 83 | storres | # End spo_proto_to_column_matrix. |
977 | 83 | storres | # |
978 | 111 | storres | def spo_proto_to_row_matrix(protoMatrixRows): |
979 | 83 | storres | """ |
980 | 111 | storres | Create a row (each column holds the coefficients corresponding to one |
981 | 111 | storres | monomial) matrix from the protoMatrixRows list. |
982 | 83 | storres | |
983 | 83 | storres | Parameters |
984 | 83 | storres | ---------- |
985 | 83 | storres | protoMatrixRows: a list of coefficient lists. |
986 | 83 | storres | """ |
987 | 83 | storres | numRows = len(protoMatrixRows) |
988 | 83 | storres | if numRows == 0: |
989 | 83 | storres | return None |
990 | 91 | storres | # The last row is the longest one. |
991 | 83 | storres | numColumns = len(protoMatrixRows[numRows-1]) |
992 | 83 | storres | if numColumns == 0: |
993 | 83 | storres | return None |
994 | 83 | storres | baseMatrix = matrix(ZZ, numRows, numColumns) |
995 | 83 | storres | for rowIndex in xrange(0, numRows): |
996 | 83 | storres | for colIndex in xrange(0, len(protoMatrixRows[rowIndex])): |
997 | 89 | storres | if protoMatrixRows[rowIndex][colIndex] != 0: |
998 | 89 | storres | baseMatrix[rowIndex, colIndex] = \ |
999 | 111 | storres | protoMatrixRows[rowIndex][colIndex] |
1000 | 89 | storres | #print rowIndex, colIndex, |
1001 | 89 | storres | #print protoMatrixRows[rowIndex][colIndex], |
1002 | 89 | storres | #print knownMonomialsList[colIndex](boundVar1,boundVar2) |
1003 | 83 | storres | return baseMatrix |
1004 | 83 | storres | # End spo_proto_to_row_matrix. |
1005 | 83 | storres | # |
1006 | 87 | storres | print "\t...sagePolynomialOperations loaded" |