root / pobysoPythonSage / src / sageSLZ / sagePolynomialOperations.sage @ 74
Historique | Voir | Annoter | Télécharger (6,79 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 |
tVaraible = 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 in |
| 26 |
# i, those in t and the mixed one necessary to be able to introduce |
| 27 |
# p and only a one monomial (our (arbitrary) choice is, at this point, |
| 28 |
# to add i to it's maximum power in p. |
| 29 |
if pPower == 0: |
| 30 |
# iter1: power of i |
| 31 |
# Notice how i^pIdegree is excluded. |
| 32 |
for iPower in xrange(0, pIdegree): |
| 33 |
# iter5: power of t. |
| 34 |
for tPower in xrange(0, pTdegree + 1): |
| 35 |
if columnWidth != 0: |
| 36 |
print "->", spo_expression_as_string(iter1, |
| 37 |
iter5, |
| 38 |
iter0, |
| 39 |
alpha) |
| 40 |
currentExpression = iVariable^iPower * \ |
| 41 |
tVariable^tPower * nAtPower |
| 42 |
# polynomialAtPower == 1 here. Next line should be commented out but it does not work! |
| 43 |
# Some convertion problem? |
| 44 |
currentPolynomial = pRing(currentExpression) * \ |
| 45 |
polynomialAtPower |
| 46 |
pMonomials = currentPolynomial.monomials() |
| 47 |
pCoefficients = currentPolynomial.coefficients() |
| 48 |
add_polynomial_coeffs_to_matrix(pMonomials, pCoefficients, knownMonomials, protoMatrixRows, monomialLengthChars) |
| 49 |
# End iter5. |
| 50 |
# End for iter1. |
| 51 |
|
| 52 |
else: # Next runs (p^1..p^alpha) |
| 53 |
pass |
| 54 |
|
| 55 |
# End spo_polynomial_to_matrix |
| 56 |
|
| 57 |
def spo_add_polynomial_coeffs_to_matrix(pMonomials, |
| 58 |
pCoefficients, |
| 59 |
knownMonomials, |
| 60 |
protoMatrixRows, |
| 61 |
columnWidth=0): |
| 62 |
""" |
| 63 |
For a given polynomial (under the form of monomials and coefficents lists), |
| 64 |
add the coefficients of the protoMatrix (a list of proto rows). |
| 65 |
Coefficients are added to the protoMatrix row in the order imposed by the |
| 66 |
monomials discovery list (the knownMonomials list) built as construction |
| 67 |
goes on. |
| 68 |
As a bonus data can be printed out for a visual check. |
| 69 |
pMonomials : the list of the monomials coming form some polynomial; |
| 70 |
pCoefficients : the list of the corresponding coefficients to add to |
| 71 |
the protoMatrix in the exact same order as the monomials; |
| 72 |
knownMonomials : the list of the already knonw monomials; |
| 73 |
protoMatrixRows: a list of lists, each one holding the coefficients of the |
| 74 |
monomials |
| 75 |
columnWith : the width, in characters, of the displayed column ; if 0, |
| 76 |
do display anything. |
| 77 |
""" |
| 78 |
# We have started with the smaller degrees in the first variable. |
| 79 |
pMonomials.reverse() |
| 80 |
pCoefficients.reverse() |
| 81 |
# New empty proto matrix row. |
| 82 |
protoMatrixRowCoefficients = [] |
| 83 |
# We work according to the order of the already known monomials |
| 84 |
# No known monomials yet: add the pMonomials to knownMonomials |
| 85 |
# and add the coefficients to the proto matrix row. |
| 86 |
if len(knownMonomials) == 0: |
| 87 |
for pmIdx in xrange(0, len(pMonomials)): |
| 88 |
knownMonomials.append(pMonomials[pmIdx]) |
| 89 |
protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
| 90 |
if columnWidth != 0: |
| 91 |
monomialAsString = str(pCoefficients[pmIdx]) + " " + \ |
| 92 |
str(pMonomials[pmIdx]) |
| 93 |
print monomialAsString, " " * \ |
| 94 |
(columnWidth - len(monomialAsString)), |
| 95 |
# There are some known monomials. We search for them in pMonomials and |
| 96 |
# add their coefficients to the proto matrix row. |
| 97 |
else: |
| 98 |
for knownMonomialIndex in xrange(0,len(knownMonomials)): |
| 99 |
# We lazily use an exception here since pMonomials.index() function |
| 100 |
# may fail throwing the ValueError exception. |
| 101 |
try: |
| 102 |
indexInPmonomials = \ |
| 103 |
pMonomials.index(knownMonomials[knownMonomialIndex]) |
| 104 |
if columnWidth != 0: |
| 105 |
monomialAsString = str(pCoefficients[indexInPmonomials]) + \ |
| 106 |
" " + str(knownMonomials[knownMonomialIndex]) |
| 107 |
print monomialAsString, " " * \ |
| 108 |
(columnWidth - len(monomialAsString)), |
| 109 |
# Add the coefficient to the proto matrix row and delete the \ |
| 110 |
# known monomial from the current pMonomial list |
| 111 |
#(and the corresponding coefficient as well). |
| 112 |
protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials]) |
| 113 |
del pMonomials[indexInPmonomials] |
| 114 |
del pCoefficients[indexInPmonomials] |
| 115 |
# The knownMonomials element is not in pMonomials |
| 116 |
except ValueError: |
| 117 |
protoMatrixRowCoefficients.append(0) |
| 118 |
if columnWidth != 0: |
| 119 |
monomialAsString = "0" + " "+ \ |
| 120 |
str(knownMonomials[knownMonomialIndex]) |
| 121 |
print monomialAsString, " " * \ |
| 122 |
(columnWidth - len(monomialAsString)), |
| 123 |
# End for knownMonomialKey loop. |
| 124 |
# We now append the remaining monomials of pMonomials to knownMonomials |
| 125 |
# and the corresponding coefficients to proto matrix row. |
| 126 |
for pmIdx in xrange(0, len(pMonomials)): |
| 127 |
knownMonomials.append(pMonomials[pmIdx]) |
| 128 |
protoMatrixRowCoefficients.append(pCoefficients[pmIdx]) |
| 129 |
if columnWidth != 0: |
| 130 |
monomialAsString = str(pCoefficients[pmIdx]) + " " \ |
| 131 |
+ str(pMonomials[pmIdx]) |
| 132 |
print monomialAsString, " " * \ |
| 133 |
(columnWidth - len(monomialAsString)), |
| 134 |
# End for pmIdx loop. |
| 135 |
# Add the new list row elements to the proto matrix. |
| 136 |
protoMatrixRows.append(protoMatrixRowCoefficients) |
| 137 |
if columnWidth != 0: |
| 138 |
|
| 139 |
# End spo_add_polynomial_coeffs_to_matrix |