Statistiques
| Révision :

root / pobysoPythonSage / src / sageSLZ / sagePolynomialOperations.sage @ 75

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