Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (51,59 ko)

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