Révision 74

pobysoPythonSage/src/sageSLZ/sagePolynomialOperations.sage (revision 74)
1
load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"
2

  
3
def spo_polynomial_to_matrix(p, pRing, alpha, N, columnWidth=0):
4
    """
5
    From a (bivariate) polynomial and some other parameters build a matrix
6
    to be reduced by fpLLL.
7
    The matrix is such as those found in Boneh-Durphy and Stehlé.
8
    
9
    p: the (bivariate) polynomial
10
    alpha:
11
    N:
12
    
13
    """
14
    pVariables = p.variables()
15
    iVariable = pVariables[0]
16
    tVaraible = pVariables[1]
17
    polynomialAtPower = P(1)
18
    currentPolynomial = P(1)
19
    pIdegree = p.degree(pVariables[0])
20
    pTdegree = p.degree(pVariables[1])
21
    currentIdegree = currentPolynomial.degree(i)
22
    nAtPower = N^alpha
23
    # We work from p^0 * N^alpha to p^alpha * N^0
24
    for pPower in xrange(0, alpha + 1):
25
        # pPower == 0 is a special case. We introduce all the monomials in
26
        # i, those in t and the mixed one necessary to be able to introduce
27
        # p and only a one monomial (our (arbitrary) choice is, at this point,
28
        # to add i to it's maximum power in p.
29
        if pPower == 0:
30
            # iter1: power of i
31
            # Notice how i^pIdegree is excluded.
32
            for iPower in xrange(0, pIdegree): 
33
                # iter5: power of t.
34
                for tPower in xrange(0, pTdegree + 1):
35
                    if columnWidth != 0:
36
                        print "->", spo_expression_as_string(iter1, 
37
                                                             iter5, 
38
                                                             iter0, 
39
                                                             alpha)
40
                    currentExpression = iVariable^iPower * \
41
                                        tVariable^tPower * nAtPower
42
                    # polynomialAtPower == 1 here. Next line should be commented out but it does not work!
43
                    # Some convertion problem?
44
                    currentPolynomial = pRing(currentExpression) * \
45
                                        polynomialAtPower 
46
                    pMonomials = currentPolynomial.monomials()
47
                    pCoefficients = currentPolynomial.coefficients()
48
                    add_polynomial_coeffs_to_matrix(pMonomials, pCoefficients, knownMonomials, protoMatrixRows, monomialLengthChars)
49
                # End iter5.
50
            # End for iter1.
51
            
52
    else: # Next runs (p^1..p^alpha)
53
        pass
54
    
55
# End spo_polynomial_to_matrix
56

  
57
def spo_add_polynomial_coeffs_to_matrix(pMonomials, 
58
                                        pCoefficients, 
59
                                        knownMonomials, 
60
                                        protoMatrixRows, 
61
                                        columnWidth=0):
62
    """
63
    For a given polynomial (under the form of monomials and coefficents lists),
64
    add the coefficients of the protoMatrix (a list of proto rows).
65
    Coefficients are added to the protoMatrix row in the order imposed by the 
66
    monomials discovery list (the knownMonomials list) built as construction
67
    goes on. 
68
    As a bonus data can be printed out for a visual check.
69
    pMonomials     : the list of the monomials coming form some polynomial;
70
    pCoefficients  : the list of the corresponding coefficients to add to
71
                     the protoMatrix in the exact same order as the monomials;
72
    knownMonomials : the list of the already knonw monomials;
73
    protoMatrixRows: a list of lists, each one holding the coefficients of the 
74
                     monomials
75
    columnWith     : the width, in characters, of the displayed column ; if 0, 
76
                     do display anything.
77
    """
78
    # We have started with the smaller degrees in the first variable.
79
    pMonomials.reverse()
80
    pCoefficients.reverse()
81
    # New empty proto matrix row.
82
    protoMatrixRowCoefficients = []
83
    # We work according to the order of the already known monomials
84
    # No known monomials yet: add the pMonomials to knownMonomials 
85
    # and add the coefficients to the proto matrix row.
86
    if len(knownMonomials) == 0: 
87
        for pmIdx in xrange(0, len(pMonomials)):
88
            knownMonomials.append(pMonomials[pmIdx])
89
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
90
            if columnWidth != 0:
91
                monomialAsString = str(pCoefficients[pmIdx]) + " " + \
92
                                   str(pMonomials[pmIdx])
93
                print monomialAsString, " " * \
94
                      (columnWidth - len(monomialAsString)),
95
    # There are some known monomials. We search for them in pMonomials and 
96
    # add their coefficients to the proto matrix row.
97
    else: 
98
        for knownMonomialIndex in xrange(0,len(knownMonomials)):
99
            # We lazily use an exception here since pMonomials.index() function
100
            # may fail throwing the ValueError exception.
101
            try:
102
                indexInPmonomials = \
103
                    pMonomials.index(knownMonomials[knownMonomialIndex])
104
                if columnWidth != 0:
105
                    monomialAsString = str(pCoefficients[indexInPmonomials]) + \
106
                        " " + str(knownMonomials[knownMonomialIndex])
107
                    print monomialAsString, " " * \
108
                        (columnWidth - len(monomialAsString)),
109
                # Add the coefficient to the proto matrix row and delete the \
110
                # known monomial from the current pMonomial list 
111
                #(and the corresponding coefficient as well).
112
                protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials])
113
                del pMonomials[indexInPmonomials]
114
                del pCoefficients[indexInPmonomials]
115
            # The knownMonomials element is not in pMonomials
116
            except ValueError:   
117
                protoMatrixRowCoefficients.append(0)
118
                if columnWidth != 0:
119
                    monomialAsString = "0" + " "+ \
120
                        str(knownMonomials[knownMonomialIndex])
121
                    print monomialAsString, " " * \
122
                        (columnWidth - len(monomialAsString)),
123
        # End for knownMonomialKey loop. 
124
        # We now append the remaining monomials of pMonomials to knownMonomials 
125
        # and the corresponding coefficients to proto matrix row.
126
        for pmIdx in xrange(0, len(pMonomials)):
127
            knownMonomials.append(pMonomials[pmIdx])
128
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
129
            if columnWidth != 0:
130
                monomialAsString = str(pCoefficients[pmIdx]) + " " \
131
                    + str(pMonomials[pmIdx])
132
                print monomialAsString, " " * \
133
                    (columnWidth - len(monomialAsString)),
134
        # End for pmIdx loop.
135
    # Add the new list row elements to the proto matrix.
136
    protoMatrixRows.append(protoMatrixRowCoefficients)
137
    if columnWidth != 0:
138
        print    
139
# End spo_add_polynomial_coeffs_to_matrix
pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage (revision 74)
1
def is_lower_triangular_matrix(mat):
2
    """
3
    Check that the matrix is lower triangular.
4
    """
5
    dimensions = mat.dimensions()
6
    # Must be a bidimensional matrix.
7
    if len(dimensions) != 2:
8
        return False
9
    # Must be square.
10
    if dimensions[0] != dimensions[1]:
11
        return False
12
    # A 1x1 matrix is lower triangular.
13
    if dimensions[0] == 1:
14
        return True
15
    for rowIndex in xrange(0, dimensions[0]):
16
        for colIndex in xrange(rowIndex + 1, dimensions[1]):
17
            if mat[rowIndex, colIndex] != 0:
18
                print mat.rows()[rowIndex]
19
                return False
20
    return True
21
# End is_lower_triangular_matrix
22

  
23
def is_diagonal_complete_matrix(mat):
24
    """
25
    Check that all the element on the diagonal are not 0.
26
    """
27
    dimensions = mat.dimensions()
28
    # Must be a bidimensional matrix.
29
    if len(dimensions) != 2:
30
        return False
31
    # Must be square.
32
    if dimensions[0] != dimensions[1]:
33
        return False
34
    # A 1x1 matrix is diagonal complete if it's single element is not 0.
35
    if dimensions[0] == 1:
36
        if mat[0, 0] != 0:
37
            return True
38
        else:
39
            return False
40
    # End if
41
    for rowIndex in xrange(0, dimensions[0]):
42
        if mat[rowIndex, rowIndex] == 0:
43
            print mat.rows()[rowIndex], rowIndex
44
            return False
45
    return True
46
# End is_diagonal_complete_matrix

Formats disponibles : Unified diff