Пример #1
0
def read_ascii_polynomial(istream, is_xxpp_format, order=None):
    """

    Read a polynomial from an input stream in ASCII format.

    @param istream: an iterable of input lines (e.g. file-like),

    @param is_xxpp_format: a boolean to specify whether the input file
    is in the old (Mathematica) format where all configuration
    coordinates come first, following by all the momenta, rather than
    the new even-odd format.

    @param order: reorder the degrees of freedom on the fly

    @return: polynomial.

    Note: this needs to be refactored; the xxpp logic belongs in a
    lie algebra of one sort or another.

    """
    line = istream.next()
    n_vars = int(line)
    assert n_vars >= 0
    line = istream.next()
    n_monomials = int(line)
    assert n_monomials >= 0
    p = Polynomial(n_vars)
    for i in xrange(n_monomials):
        line = istream.next()
        elts = line.split(' ')
        elts = elts[:n_vars] + [' '.join(elts[n_vars:])]
        assert len(elts) == n_vars + 1
        powers_list = [int(e) for e in elts[:-1]]
        if is_xxpp_format:
            powers_list = xxpp_to_xpxp(powers_list)
        if order != None:
            powers_list = reorder_dof(powers_list, order)
        powers = tuple(powers_list)
        coeff = complex(elts[-1])
        m = coeff * Polynomial.Monomial(powers)
        assert not p.has_term(Powers(powers))
        p += m
    return p
Пример #2
0
def read_ascii_polynomial(istream, is_xxpp_format, order=None):
    """

    Read a polynomial from an input stream in ASCII format.

    @param istream: an iterable of input lines (e.g. file-like),

    @param is_xxpp_format: a boolean to specify whether the input file
    is in the old (Mathematica) format where all configuration
    coordinates come first, following by all the momenta, rather than
    the new even-odd format.

    @param order: reorder the degrees of freedom on the fly

    @return: polynomial.

    Note: this needs to be refactored; the xxpp logic belongs in a
    lie algebra of one sort or another.

    """
    line = istream.next()
    n_vars = int(line)
    assert n_vars >= 0
    line = istream.next()
    n_monomials = int(line)
    assert n_monomials >= 0
    p = Polynomial(n_vars)
    for i in xrange(n_monomials):
        line = istream.next()
        elts = line.split(' ')
        elts = elts[:n_vars]+[' '.join(elts[n_vars:])]
        assert len(elts) == n_vars+1
        powers_list = [int(e) for e in elts[:-1]]
        if is_xxpp_format:
            powers_list = xxpp_to_xpxp(powers_list)
        if order != None:
            powers_list = reorder_dof(powers_list, order)
        powers = tuple(powers_list)
        coeff = complex(elts[-1])
        m = coeff*Polynomial.Monomial(powers)
        assert not p.has_term(Powers(powers))
        p += m
    return p