Пример #1
0
    def __init__(self, monomials = ()):

        # combine monomials with the same variables and exponents
        # and bring them into canonical order

        assert isinstance(monomials, tuple)

        # create for each monomial a dictionary
        # with key being the variables and exponents
        # and value being the coefficient

        listOfVarsCoeffDicts = [
            { monomial.getVars() : monomial.getCoefficient() }
            for monomial in monomials]

        # combine the dictionaries using sum
        combinedVarsCoeffDict = basicAlgorithms.combineDicts(
            listOfVarsCoeffDicts,
            _operatorTypePolicy)

        # turn dictionary into a list of pairs (vars, coefficient)
        # in canonical order
        orderedTupleOfVarsCoeffPairs = (
            basicAlgorithms.dictToOrderedTupleOfPairs(combinedVarsCoeffDict))

        # turn pairs into monomials, skip trivial monomials
        combinedMonomials = [
            Monomial(coefficient, vars)
            for vars, coefficient in orderedTupleOfVarsCoeffPairs
            if _coefficientIsNonTrivial(coefficient)]

        # convert to tuple
        self._monomials = tuple(combinedMonomials)
Пример #2
0
    def __init__(self, coefficient, vars):
        """
        >>> M = Monomial(2, (('a', 2), ('b', 3)))
        >>> M
        Monomial(2, (('a', 2), ('b', 3)))
        >>> str(M)
        '2 * a^2 * b^3'
        """

        self._coefficient = coefficient

        if isinstance(vars, dict):
            self._vars = basicAlgorithms.dictToOrderedTupleOfPairs(vars)
        else:
            assert isinstance(vars, tuple)
            for var, expo in vars:
                assert isinstance(var, str)
                assert isinstance(expo, int)
                assert expo > 0
            self._vars = vars