Exemple #1
0
def common_terms(pair):
#    print "common_terms: finding common terms of " + str(pair)
    if Rule.all_are(pair, Const):
        return const_common_terms(pair)
    if Rule.all_are(pair, Var):
        if pair[0] == pair[1]:
            return [pair[0]]
        else:
            return []
    if Rule.all_are(pair, Atom):
        return []
    if Rule.all_are(pair, Multiply):
        return mult_common_terms(pair)
    if Rule.is_a(pair[0], Multiply) and Rule.is_a(pair[1], Atom):
        return mult_atom_common_terms(pair)
    if Rule.is_a(pair[0], Atom) and Rule.is_a(pair[1], Multiply):
        return mult_atom_common_terms((pair[1], pair[0]))
    if Rule.all_are(pair, Power):
        return pow_common_terms(pair)
    if Rule.is_a(pair[0], Power) and Rule.is_a(pair[1], Atom):
        return pow_atom_common_terms(pair)
    if Rule.is_a(pair[0], Atom) and Rule.is_a(pair[1], Power):
        return pow_atom_common_terms((pair[1], pair[0]))
    if Rule.is_a(pair[0], Multiply) and Rule.is_a(pair[1], Power):
        return mult_pow_common_terms(pair)
    if Rule.is_a(pair[0], Power) and Rule.is_a(pair[1], Multiply):
        return mult_pow_common_terms((pair[1], pair[0]))
    return []
Exemple #2
0
def pow_common_terms(pair):
#    print "pow_common_terms: finding common terms of " + str(pair)
    if not Rule.all_equal([term.base() for term in pair]):
        return []
    if Rule.all_are([term.exponent() for term in pair], Var):
        return []
    if Rule.all_are([term.exponent() for term in pair], Const):
        if Rule.all_are([term.exponent().value() for term in pair], int):
            v1 = pair[0].exponent().value()
            v2 = pair[1].exponent().value()
            return [Power(pair[0].base(), Const(min(v1, v2)))]
    common_in_exp = common_terms((pair[0].exponent(), pair[1].exponent()))
    if len(common_in_exp) > 0:
        if len(common_in_exp) > 1:
            mults = Multiply(*common_in_exp)
        else:
            mults = common_in_exp[0]
        return [Power(pair[0].base(), mults)]
    return []