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