Statistiques
| Révision :

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

Historique | Voir | Annoter | Télécharger (20,31 ko)

1 74 storres
load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"
2 87 storres
print "sagePolynomialOperations loading..."
3 83 storres
def spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
4 83 storres
                                            pCoefficients,
5 83 storres
                                            knownMonomials,
6 83 storres
                                            protoMatrixRows,
7 83 storres
                                            columnsWidth=0):
8 80 storres
    """
9 80 storres
    For a given polynomial (under the form of monomials and coefficents lists),
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 80 storres
    pMonomials     : the list of the monomials coming form some polynomial;
16 80 storres
    pCoefficients  : the list of the corresponding coefficients to add to
17 80 storres
                     the protoMatrix in the exact same order as the monomials;
18 80 storres
    knownMonomials : the list of the already knonw monomials;
19 80 storres
    protoMatrixRows: a list of lists, each one holding the coefficients of the
20 80 storres
                     monomials
21 80 storres
    columnWith     : the width, in characters, of the displayed column ; if 0,
22 80 storres
                     do not display anything.
23 80 storres
    """
24 80 storres
    # We have started with the smaller degrees in the first variable.
25 80 storres
    pMonomials.reverse()
26 80 storres
    pCoefficients.reverse()
27 80 storres
    # New empty proto matrix row.
28 80 storres
    protoMatrixRowCoefficients = []
29 80 storres
    # We work according to the order of the already known monomials
30 80 storres
    # No known monomials yet: add the pMonomials to knownMonomials
31 80 storres
    # and add the coefficients to the proto matrix row.
32 80 storres
    if len(knownMonomials) == 0:
33 80 storres
        for pmIdx in xrange(0, len(pMonomials)):
34 80 storres
            knownMonomials.append(pMonomials[pmIdx])
35 80 storres
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
36 80 storres
            if columnsWidth != 0:
37 80 storres
                monomialAsString = str(pCoefficients[pmIdx]) + " " + \
38 80 storres
                                   str(pMonomials[pmIdx])
39 80 storres
                print monomialAsString, " " * \
40 80 storres
                      (columnsWidth - len(monomialAsString)),
41 80 storres
    # There are some known monomials. We search for them in pMonomials and
42 80 storres
    # add their coefficients to the proto matrix row.
43 80 storres
    else:
44 80 storres
        for knownMonomialIndex in xrange(0,len(knownMonomials)):
45 80 storres
            # We lazily use an exception here since pMonomials.index() function
46 80 storres
            # may fail throwing the ValueError exception.
47 80 storres
            try:
48 80 storres
                indexInPmonomials = \
49 80 storres
                    pMonomials.index(knownMonomials[knownMonomialIndex])
50 80 storres
                if columnsWidth != 0:
51 80 storres
                    monomialAsString = str(pCoefficients[indexInPmonomials]) + \
52 80 storres
                        " " + str(knownMonomials[knownMonomialIndex])
53 80 storres
                    print monomialAsString, " " * \
54 80 storres
                        (columnsWidth - len(monomialAsString)),
55 80 storres
                # Add the coefficient to the proto matrix row and delete the \
56 80 storres
                # known monomial from the current pMonomial list
57 80 storres
                #(and the corresponding coefficient as well).
58 80 storres
                protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials])
59 80 storres
                del pMonomials[indexInPmonomials]
60 80 storres
                del pCoefficients[indexInPmonomials]
61 80 storres
            # The knownMonomials element is not in pMonomials
62 80 storres
            except ValueError:
63 80 storres
                protoMatrixRowCoefficients.append(0)
64 80 storres
                if columnsWidth != 0:
65 80 storres
                    monomialAsString = "0" + " "+ \
66 80 storres
                        str(knownMonomials[knownMonomialIndex])
67 80 storres
                    print monomialAsString, " " * \
68 80 storres
                        (columnsWidth - len(monomialAsString)),
69 80 storres
        # End for knownMonomialKey loop.
70 80 storres
        # We now append the remaining monomials of pMonomials to knownMonomials
71 80 storres
        # and the corresponding coefficients to proto matrix row.
72 80 storres
        for pmIdx in xrange(0, len(pMonomials)):
73 80 storres
            knownMonomials.append(pMonomials[pmIdx])
74 80 storres
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
75 80 storres
            if columnsWidth != 0:
76 80 storres
                monomialAsString = str(pCoefficients[pmIdx]) + " " \
77 80 storres
                    + str(pMonomials[pmIdx])
78 80 storres
                print monomialAsString, " " * \
79 80 storres
                    (columnsWidth - len(monomialAsString)),
80 80 storres
        # End for pmIdx loop.
81 80 storres
    # Add the new list row elements to the proto matrix.
82 80 storres
    protoMatrixRows.append(protoMatrixRowCoefficients)
83 80 storres
    if columnsWidth != 0:
84 80 storres
        print
85 83 storres
# End spo_add_polynomial_coeffs_to_matrix_row
86 80 storres
87 80 storres
def spo_expression_as_string(powI, powT, powP, alpha):
88 80 storres
    """
89 80 storres
    Computes a string version of the i^k + t^l + p^m + N^n expression for
90 80 storres
    output.
91 80 storres
    """
92 80 storres
    expressionAsString =""
93 80 storres
    if powI != 0:
94 80 storres
        expressionAsString += "i^" + str(powI)
95 80 storres
    if powT != 0:
96 80 storres
        if len(expressionAsString) != 0:
97 80 storres
            expressionAsString += " * "
98 80 storres
        expressionAsString += "t^" + str(powT)
99 80 storres
    if powP != 0:
100 80 storres
        if len(expressionAsString) != 0:
101 80 storres
            expressionAsString += " * "
102 80 storres
        expressionAsString += "p^" + str(powP)
103 80 storres
    if (alpha - powP) != 0 :
104 80 storres
        if len(expressionAsString) != 0:
105 80 storres
            expressionAsString += " * "
106 80 storres
        expressionAsString += "N^" + str(alpha - powP)
107 80 storres
    return(expressionAsString)
108 80 storres
# End spo_expression_as_string.
109 80 storres
110 87 storres
def spo_norm(poly, p=2):
111 81 storres
    """
112 81 storres
    Behaves more or less (no infinity defined) as the norm for the
113 81 storres
    univariate polynomials.
114 81 storres
    Quoting the Sage documentation:
115 81 storres
    Definition: For integer p, the p-norm of a polynomial is the pth root of
116 81 storres
    the sum of the pth powers of the absolute values of the coefficients of
117 81 storres
    the polynomial.
118 87 storres
119 81 storres
    """
120 87 storres
    # TODO: check the arguments (for p see below)..
121 81 storres
    norm = 0
122 87 storres
    # For infinity norm.
123 87 storres
    if p == Infinity:
124 87 storres
        for coefficient in poly.coefficients():
125 87 storres
            coefficientAbs = coefficient.abs()
126 87 storres
            if coefficientAbs > norm:
127 87 storres
                norm = coefficientAbs
128 87 storres
        return norm
129 87 storres
    # TODO: check here the value of p
130 87 storres
    # For 1 norm.
131 87 storres
    if p == 1:
132 87 storres
        for coefficient in poly.coefficients():
133 87 storres
            norm +=  coefficient.abs()
134 87 storres
        return norm
135 87 storres
    # For other norms
136 81 storres
    for coefficient in poly.coefficients():
137 87 storres
        norm +=  (coefficient^p).abs()
138 87 storres
    return pow(norm, 1/p)
139 81 storres
# end spo_norm
140 81 storres
141 83 storres
def spo_polynomial_to_proto_matrix(p, pRing, alpha, N, columnsWidth=0):
142 74 storres
    """
143 83 storres
    From a (bivariate) polynomial and some other parameters build a proto
144 87 storres
    matrix (an array of "rows") to be converted into a "true" matrix and
145 83 storres
    eventually by reduced by fpLLL.
146 80 storres
    The matrix is such as those found in Boneh-Durphee and Stehl?.
147 74 storres
148 83 storres
    Parameters
149 83 storres
    ----------
150 87 storres
    p: the (bivariate) polynomial;
151 87 storres
    pRing: the ring over which p is defined;
152 74 storres
    alpha:
153 74 storres
    N:
154 83 storres
    columsWidth: if == 0, no information is displayed, otherwise data is
155 83 storres
                 printed in colums of columnsWitdth width.
156 74 storres
    """
157 77 storres
    knownMonomials = []
158 77 storres
    protoMatrixRows = []
159 74 storres
    pVariables = p.variables()
160 74 storres
    iVariable = pVariables[0]
161 76 storres
    tVariable = pVariables[1]
162 87 storres
    polynomialAtPower = pRing(1)
163 87 storres
    currentPolynomial = pRing(1)
164 74 storres
    pIdegree = p.degree(pVariables[0])
165 74 storres
    pTdegree = p.degree(pVariables[1])
166 87 storres
    currentIdegree = currentPolynomial.degree(iVariable)
167 74 storres
    nAtPower = N^alpha
168 74 storres
    # We work from p^0 * N^alpha to p^alpha * N^0
169 74 storres
    for pPower in xrange(0, alpha + 1):
170 76 storres
        # pPower == 0 is a special case. We introduce all the monomials but one
171 78 storres
        # in i and those in t necessary to be able to introduce
172 76 storres
        # p. We arbitrary choose to introduce the highest degree monomial in i
173 76 storres
        # with p. We also introduce all the mixed i^k * t^l monomials with
174 77 storres
        # k < p.degree(i) and l <= p.degree(t).
175 78 storres
        # Mixed terms introduction is necessary here before we start "i shifts"
176 78 storres
        # in the next iteration.
177 74 storres
        if pPower == 0:
178 78 storres
            # Notice that i^pIdegree is excluded as the bound of the xrange is
179 78 storres
            # pIdegree
180 74 storres
            for iPower in xrange(0, pIdegree):
181 74 storres
                for tPower in xrange(0, pTdegree + 1):
182 77 storres
                    if columnsWidth != 0:
183 76 storres
                        print "->", spo_expression_as_string(iPower,
184 76 storres
                                                             tPower,
185 76 storres
                                                             pPower,
186 74 storres
                                                             alpha)
187 74 storres
                    currentExpression = iVariable^iPower * \
188 74 storres
                                        tVariable^tPower * nAtPower
189 78 storres
                    # polynomialAtPower == 1 here. Next line should be commented
190 78 storres
                    # out but it does not work! Some conversion problem?
191 74 storres
                    currentPolynomial = pRing(currentExpression) * \
192 74 storres
                                        polynomialAtPower
193 74 storres
                    pMonomials = currentPolynomial.monomials()
194 74 storres
                    pCoefficients = currentPolynomial.coefficients()
195 83 storres
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
196 83 storres
                                                            pCoefficients,
197 83 storres
                                                            knownMonomials,
198 83 storres
                                                            protoMatrixRows,
199 83 storres
                                                            columnsWidth)
200 78 storres
                # End tPower.
201 78 storres
            # End for iPower.
202 77 storres
        else: # pPower > 0: (p^1..p^alpha)
203 78 storres
            # This where we introduce the p^pPower * N^(alpha-pPower)
204 77 storres
            # polynomial.
205 77 storres
            # This step could technically be fused as the first iteration
206 77 storres
            # of the next loop (with iPower starting at 0).
207 77 storres
            # We set it apart for clarity.
208 77 storres
            if columnsWidth != 0:
209 77 storres
                print "->", spo_expression_as_string(0, 0, pPower, alpha)
210 77 storres
            currentPolynomial = polynomialAtPower * nAtPower
211 77 storres
            pMonomials = currentPolynomial.monomials()
212 77 storres
            pCoefficients = currentPolynomial.coefficients()
213 83 storres
            spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
214 83 storres
                                                    pCoefficients,
215 83 storres
                                                    knownMonomials,
216 83 storres
                                                    protoMatrixRows,
217 83 storres
                                                    columnsWidth)
218 77 storres
219 77 storres
            # The i^iPower * p^pPower polynomials: they add i^k monomials to
220 77 storres
            # p^pPower up to k < pIdegree * pPower. This only introduces i^k
221 77 storres
            # monomials since mixed terms (that were introduced at a previous
222 77 storres
            # stage) are only shifted to already existing
223 77 storres
            # ones. p^pPower is "shifted" to higher degrees in i as far as
224 77 storres
            # possible, one step short of the degree in i of p^(pPower+1) .
225 77 storres
            # These "pure" i^k monomials can only show up with i multiplications.
226 77 storres
            for iPower in xrange(1, pIdegree):
227 87 storres
                if columnsWidth != 0:
228 87 storres
                    print "->", spo_expression_as_string(iPower, \
229 87 storres
                                                         0,      \
230 87 storres
                                                         pPower, \
231 87 storres
                                                         alpha)
232 77 storres
                currentExpression = i^iPower * nAtPower
233 87 storres
                currentPolynomial = pRing(currentExpression) * polynomialAtPower
234 77 storres
                pMonomials = currentPolynomial.monomials()
235 77 storres
                pCoefficients = currentPolynomial.coefficients()
236 87 storres
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,      \
237 87 storres
                                                        pCoefficients,   \
238 87 storres
                                                        knownMonomials,  \
239 87 storres
                                                        protoMatrixRows, \
240 83 storres
                                                        columnsWidth)
241 77 storres
            # End for iPower
242 77 storres
            # We want now to introduce a t * p^pPower polynomial. But before
243 77 storres
            # that we must introduce some mixed monomials.
244 77 storres
            # This loop is no triggered before pPower == 2.
245 78 storres
            # It introduces a first set of high i degree mixed monomials.
246 77 storres
            for iPower in xrange(1, pPower):
247 77 storres
                tPower = pPower - iPower + 1
248 77 storres
                if columnsWidth != 0:
249 77 storres
                    print "->", spo_expression_as_string(iPower * pIdegree,
250 77 storres
                                                         tPower,
251 77 storres
                                                         0,
252 77 storres
                                                         alpha)
253 77 storres
                currentExpression = i^(iPower * pIdegree) * t^tPower * nAtPower
254 87 storres
                currentPolynomial = pRing(currentExpression)
255 77 storres
                pMonomials = currentPolynomial.monomials()
256 77 storres
                pCoefficients = currentPolynomial.coefficients()
257 83 storres
                spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
258 83 storres
                                                        pCoefficients,
259 83 storres
                                                        knownMonomials,
260 83 storres
                                                        protoMatrixRows,
261 83 storres
                                                        columnsWidth)
262 77 storres
            # End for iPower
263 78 storres
            #
264 78 storres
            # This is the mixed monomials main loop. It introduces:
265 77 storres
            # - the missing mixed monomials needed before the
266 78 storres
            #   t^l * p^pPower * N^(alpha-pPower) polynomial;
267 78 storres
            # - the t^l * p^pPower * N^(alpha-pPower) itself;
268 78 storres
            # - for each of i^k * t^l * p^pPower * N^(alpha-pPower) polynomials:
269 78 storres
            #   - the the missing mixed monomials needed  polynomials,
270 78 storres
            #   - the i^k * t^l * p^pPower * N^(alpha-pPower) itself.
271 78 storres
            # The t^l * p^pPower * N^(alpha-pPower) is introduced when
272 78 storres
            #
273 77 storres
            for iShift in xrange(0, pIdegree):
274 77 storres
                # When pTdegree == 1, the following loop only introduces
275 77 storres
                # a single new monomial.
276 77 storres
                #print "++++++++++"
277 77 storres
                for outerTpower in xrange(1, pTdegree + 1):
278 77 storres
                    # First one high i degree mixed monomial.
279 77 storres
                    iPower = iShift + pPower * pIdegree
280 77 storres
                    if columnsWidth != 0:
281 77 storres
                        print "->", spo_expression_as_string(iPower,
282 77 storres
                                                             outerTpower,
283 77 storres
                                                             0,
284 77 storres
                                                             alpha)
285 77 storres
                    currentExpression = i^iPower * t^outerTpower * nAtPower
286 87 storres
                    currentPolynomial = pRing(currentExpression)
287 77 storres
                    pMonomials = currentPolynomial.monomials()
288 77 storres
                    pCoefficients = currentPolynomial.coefficients()
289 83 storres
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
290 83 storres
                                                            pCoefficients,
291 83 storres
                                                            knownMonomials,
292 83 storres
                                                            protoMatrixRows,
293 83 storres
                                                            columnsWidth)
294 77 storres
                    #print "+++++"
295 78 storres
                    # At iShift == 0, the following innerTpower loop adds
296 78 storres
                    # duplicate monomials, since no extra i^l * t^k is needed
297 78 storres
                    # before introducing the
298 77 storres
                    # i^iShift * t^outerPpower * p^pPower * N^(alpha-pPower)
299 77 storres
                    # polynomial.
300 77 storres
                    # It introduces smaller i degree monomials than the
301 77 storres
                    # one(s) added previously (no pPower multiplication).
302 77 storres
                    # Here the exponent of t decreases as that of i increases.
303 78 storres
                    # This conditional is not entered before pPower == 1.
304 78 storres
                    # The innerTpower loop does not produce anything before
305 78 storres
                    # pPower == 2. We keep it anyway for other configuration of
306 78 storres
                    # p.
307 77 storres
                    if iShift > 0:
308 77 storres
                        iPower = pIdegree + iShift
309 77 storres
                        for innerTpower in xrange(pPower, 1, -1):
310 77 storres
                            if columnsWidth != 0:
311 77 storres
                                print "->", spo_expression_as_string(iPower,
312 77 storres
                                                                     innerTpower,
313 77 storres
                                                                     0,
314 77 storres
                                                                     alpha)
315 77 storres
                            currentExpression = \
316 77 storres
                                    i^(iPower) * t^(innerTpower) * nAtPower
317 87 storres
                            currentPolynomial = pRing(currentExpression)
318 77 storres
                            pMonomials = currentPolynomial.monomials()
319 77 storres
                            pCoefficients = currentPolynomial.coefficients()
320 83 storres
                            spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
321 77 storres
                                                                pCoefficients,
322 77 storres
                                                                knownMonomials,
323 77 storres
                                                                protoMatrixRows,
324 77 storres
                                                                columnsWidth)
325 77 storres
                            iPower += pIdegree
326 77 storres
                        # End for innerTpower
327 77 storres
                    # End of if iShift > 0
328 78 storres
                    # When iShift == 0, just after each of the
329 78 storres
                    # p^pPower * N^(alpha-pPower) polynomials has
330 78 storres
                    # been introduced (followed by a string of
331 78 storres
                    # i^k * p^pPower * N^(alpha-pPower) polynomials) a
332 78 storres
                    # t^l *  p^pPower * N^(alpha-pPower) is introduced here.
333 78 storres
                    #
334 77 storres
                    # Eventually, the following section introduces the
335 77 storres
                    # i^iShift * t^outerTpower * p^iPower * N^(alpha-iPower)
336 77 storres
                    # polynomials.
337 77 storres
                    if columnsWidth != 0:
338 77 storres
                        print "->", spo_expression_as_string(iShift,
339 77 storres
                                                             outerTpower,
340 77 storres
                                                             pPower,
341 77 storres
                                                             alpha)
342 77 storres
                    currentExpression = i^iShift * t^outerTpower * nAtPower
343 87 storres
                    currentPolynomial = pRing(currentExpression) * polynomialAtPower
344 77 storres
                    pMonomials = currentPolynomial.monomials()
345 77 storres
                    pCoefficients = currentPolynomial.coefficients()
346 83 storres
                    spo_add_polynomial_coeffs_to_matrix_row(pMonomials,
347 83 storres
                                                            pCoefficients,
348 83 storres
                                                            knownMonomials,
349 83 storres
                                                            protoMatrixRows,
350 83 storres
                                                            columnsWidth)
351 77 storres
                # End for outerTpower
352 77 storres
                #print "++++++++++"
353 77 storres
            # End for iShift
354 77 storres
        polynomialAtPower *= p
355 77 storres
        nAtPower /= N
356 77 storres
    # End for pPower loop
357 87 storres
    return ((protoMatrixRows, knownMonomials))
358 83 storres
# End spo_polynomial_to_proto_matrix
359 81 storres
360 87 storres
def spo_proto_to_column_matrix(protoMatrixColumns):
361 83 storres
    """
362 87 storres
    Create a column (each row holds the coefficients of one monomial) matrix.
363 83 storres
    protoMatrixRows.
364 83 storres
365 83 storres
    Parameters
366 83 storres
    ----------
367 87 storres
    protoMatrixColumns: a list of coefficient lists.
368 83 storres
    """
369 87 storres
    numColumns = len(protoMatrixColumns)
370 87 storres
    if numColumns == 0:
371 83 storres
        return None
372 87 storres
    # The last column holds has the maximum length.
373 87 storres
    numRows = len(protoMatrixColumns[numColumns-1])
374 83 storres
    if numColumns == 0:
375 83 storres
        return None
376 83 storres
    baseMatrix = matrix(ZZ, numRows, numColumns)
377 87 storres
    for colIndex in xrange(0, numColumns):
378 87 storres
        for rowIndex in xrange(0, len(protoMatrixColumns[colIndex])):
379 87 storres
            baseMatrix[rowIndex, colIndex] = \
380 87 storres
            protoMatrixColumns[colIndex][rowIndex]
381 83 storres
    return baseMatrix
382 83 storres
# End spo_proto_to_column_matrix.
383 83 storres
#
384 83 storres
def spo_proto_to_row_matrix(protoMatrixRows):
385 83 storres
    """
386 87 storres
    Create a row (each column holds the coefficients of one monomial) matrix.
387 83 storres
    protoMatrixRows.
388 83 storres
389 83 storres
    Parameters
390 83 storres
    ----------
391 83 storres
    protoMatrixRows: a list of coefficient lists.
392 83 storres
    """
393 83 storres
    numRows = len(protoMatrixRows)
394 83 storres
    if numRows == 0:
395 83 storres
        return None
396 83 storres
    numColumns = len(protoMatrixRows[numRows-1])
397 83 storres
    if numColumns == 0:
398 83 storres
        return None
399 83 storres
    baseMatrix = matrix(ZZ, numRows, numColumns)
400 83 storres
    for rowIndex in xrange(0, numRows):
401 83 storres
        for colIndex in xrange(0, len(protoMatrixRows[rowIndex])):
402 83 storres
            baseMatrix[rowIndex, colIndex] = protoMatrixRows[rowIndex][colIndex]
403 83 storres
    return baseMatrix
404 83 storres
# End spo_proto_to_row_matrix.
405 83 storres
#
406 87 storres
print "\t...sagePolynomialOperations loaded"