load "/home/storres/recherche/arithmetique/pobysoPythonSage/src/sageSLZ/sageMatrixOperations.sage"

def spo_polynomial_to_matrix(p, pRing, alpha, N, columnWidth=0):
    """
    From a (bivariate) polynomial and some other parameters build a matrix
    to be reduced by fpLLL.
    The matrix is such as those found in Boneh-Durphy and Stehlé.
    
    p: the (bivariate) polynomial
    alpha:
    N:
    
    """
    pVariables = p.variables()
    iVariable = pVariables[0]
    tVaraible = pVariables[1]
    polynomialAtPower = P(1)
    currentPolynomial = P(1)
    pIdegree = p.degree(pVariables[0])
    pTdegree = p.degree(pVariables[1])
    currentIdegree = currentPolynomial.degree(i)
    nAtPower = N^alpha
    # We work from p^0 * N^alpha to p^alpha * N^0
    for pPower in xrange(0, alpha + 1):
        # pPower == 0 is a special case. We introduce all the monomials in
        # i, those in t and the mixed one necessary to be able to introduce
        # p and only a one monomial (our (arbitrary) choice is, at this point,
        # to add i to it's maximum power in p.
        if pPower == 0:
            # iter1: power of i
            # Notice how i^pIdegree is excluded.
            for iPower in xrange(0, pIdegree): 
                # iter5: power of t.
                for tPower in xrange(0, pTdegree + 1):
                    if columnWidth != 0:
                        print "->", spo_expression_as_string(iter1, 
                                                             iter5, 
                                                             iter0, 
                                                             alpha)
                    currentExpression = iVariable^iPower * \
                                        tVariable^tPower * nAtPower
                    # polynomialAtPower == 1 here. Next line should be commented out but it does not work!
                    # Some convertion problem?
                    currentPolynomial = pRing(currentExpression) * \
                                        polynomialAtPower 
                    pMonomials = currentPolynomial.monomials()
                    pCoefficients = currentPolynomial.coefficients()
                    add_polynomial_coeffs_to_matrix(pMonomials, pCoefficients, knownMonomials, protoMatrixRows, monomialLengthChars)
                # End iter5.
            # End for iter1.
            
    else: # Next runs (p^1..p^alpha)
        pass
    
# End spo_polynomial_to_matrix

def spo_add_polynomial_coeffs_to_matrix(pMonomials, 
                                        pCoefficients, 
                                        knownMonomials, 
                                        protoMatrixRows, 
                                        columnWidth=0):
    """
    For a given polynomial (under the form of monomials and coefficents lists),
    add the coefficients of the protoMatrix (a list of proto rows).
    Coefficients are added to the protoMatrix row in the order imposed by the 
    monomials discovery list (the knownMonomials list) built as construction
    goes on. 
    As a bonus data can be printed out for a visual check.
    pMonomials     : the list of the monomials coming form some polynomial;
    pCoefficients  : the list of the corresponding coefficients to add to
                     the protoMatrix in the exact same order as the monomials;
    knownMonomials : the list of the already knonw monomials;
    protoMatrixRows: a list of lists, each one holding the coefficients of the 
                     monomials
    columnWith     : the width, in characters, of the displayed column ; if 0, 
                     do display anything.
    """
    # We have started with the smaller degrees in the first variable.
    pMonomials.reverse()
    pCoefficients.reverse()
    # New empty proto matrix row.
    protoMatrixRowCoefficients = []
    # We work according to the order of the already known monomials
    # No known monomials yet: add the pMonomials to knownMonomials 
    # and add the coefficients to the proto matrix row.
    if len(knownMonomials) == 0: 
        for pmIdx in xrange(0, len(pMonomials)):
            knownMonomials.append(pMonomials[pmIdx])
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
            if columnWidth != 0:
                monomialAsString = str(pCoefficients[pmIdx]) + " " + \
                                   str(pMonomials[pmIdx])
                print monomialAsString, " " * \
                      (columnWidth - len(monomialAsString)),
    # There are some known monomials. We search for them in pMonomials and 
    # add their coefficients to the proto matrix row.
    else: 
        for knownMonomialIndex in xrange(0,len(knownMonomials)):
            # We lazily use an exception here since pMonomials.index() function
            # may fail throwing the ValueError exception.
            try:
                indexInPmonomials = \
                    pMonomials.index(knownMonomials[knownMonomialIndex])
                if columnWidth != 0:
                    monomialAsString = str(pCoefficients[indexInPmonomials]) + \
                        " " + str(knownMonomials[knownMonomialIndex])
                    print monomialAsString, " " * \
                        (columnWidth - len(monomialAsString)),
                # Add the coefficient to the proto matrix row and delete the \
                # known monomial from the current pMonomial list 
                #(and the corresponding coefficient as well).
                protoMatrixRowCoefficients.append(pCoefficients[indexInPmonomials])
                del pMonomials[indexInPmonomials]
                del pCoefficients[indexInPmonomials]
            # The knownMonomials element is not in pMonomials
            except ValueError:   
                protoMatrixRowCoefficients.append(0)
                if columnWidth != 0:
                    monomialAsString = "0" + " "+ \
                        str(knownMonomials[knownMonomialIndex])
                    print monomialAsString, " " * \
                        (columnWidth - len(monomialAsString)),
        # End for knownMonomialKey loop. 
        # We now append the remaining monomials of pMonomials to knownMonomials 
        # and the corresponding coefficients to proto matrix row.
        for pmIdx in xrange(0, len(pMonomials)):
            knownMonomials.append(pMonomials[pmIdx])
            protoMatrixRowCoefficients.append(pCoefficients[pmIdx])
            if columnWidth != 0:
                monomialAsString = str(pCoefficients[pmIdx]) + " " \
                    + str(pMonomials[pmIdx])
                print monomialAsString, " " * \
                    (columnWidth - len(monomialAsString)),
        # End for pmIdx loop.
    # Add the new list row elements to the proto matrix.
    protoMatrixRows.append(protoMatrixRowCoefficients)
    if columnWidth != 0:
        print    
# End spo_add_polynomial_coeffs_to_matrix
