在Python中测试数学表达式的等价性

Mic*_*ert 8 python equality sympy mathematical-expressions

我在Python中有两个字符串,

A m * B s / (A m + C m)
Run Code Online (Sandbox Code Playgroud)

C m * B s / (C m + A m)
Run Code Online (Sandbox Code Playgroud)

它们都是无序集(A,C)和无序集(B)的等价函数.m和s表示可以在相同但不与另一个单元交换的单位.

到目前为止,我正在进行A,B和C的排列,并使用eval和SymPy的==运算符对它们进行测试.这有许多缺点:

  • 对于更复杂的表达式,我必须生成大量的排列(在我的例子中,8个嵌套for循环)
  • 我需要将A,B,C定义为符号,当我不知道我将拥有哪些参数时,这不是最佳的(因此我必须生成所有这些参数 - >非常低效并且弄乱我的变量名称空间)

有没有pythonian方法来测试这种等价?它应该是一个任意的表达式.

Amo*_*hua 3

这是基于我之前的答案的简化方法。

这个想法是,如果两个表达式在排列下是等价的,则携带一个到另一个的排列必须将第一个字符串中的第 i 个符号(按第一次出现的索引排序)映射到第二个字符串中的第 i 个符号(再次按索引排序)第一次出现)。该原理可用于构造排列,将其应用于第一个字符串,然后检查与第二个字符串是否相等 - 如果它们相等,则它们是等价的,否则它们不是。

这是一种可能的实现:

import re

# Unique-ify list, preserving order
def uniquify(l):
    return reduce(lambda s, e: s + ([] if e in s else [e]), l, [])

# Replace all keys in replacements with corresponding values in str
def replace_all(str, replacements):
    for old, new in replacements.iteritems():
        str = str.replace(old, new)
    return str

class Expression:
    units = ["m", "s"]

    def __init__(self, exp):
        self.exp = exp

    # Returns a list of symbols in the expression that are preceded
    # by the given unit, ordered by first appearance. Assumes the
    # symbol and unit are separated by a space. For example:
    # Expression("A m * B s / (A m + C m)").symbols_for_unit("m")
    # returns ['A', 'C']
    def symbols_for_unit(self, unit):
        sym_re = re.compile("(.) %s" % unit)
        symbols = sym_re.findall(self.exp)
        return uniquify(symbols)

    # Returns a string with all symbols that have units other than
    # unit "muted", that is replaced with the empty string. Example:
    # Expression("A m * B s / (A m + C m)").mute_symbols_for_other_units("m")
    # returns "A m *  s / (A m + C m)"
    def mute_symbols_for_other_units(self, unit):
        other_units = "".join(set(self.units) - set(unit))
        return re.sub("(.) ([%s])" % "".join(other_units), " \g<2>", self.exp)

    # Returns a string with all symbols that have the given unit
    # replaced with tokens of the form $0, $1, ..., by order of their
    # first appearance in the string, and all other symbols muted. 
    # For example:
    # Expression("A m * B s / (A m + C m)").canonical_form("m")
    # returns "$0 m *  s / ($0 m + $1 m)"
    def canonical_form(self, unit):
        symbols = self.symbols_for_unit(unit)
        muted_self = self.mute_symbols_for_other_units(unit)
        for i, sym in enumerate(symbols):
            muted_self = muted_self.replace("%s %s" % (sym, unit), "$%s %s" % (i, unit))
        return muted_self

    # Define a permutation, represented as a dictionary, according to
    # the following rule: replace $i with the ith distinct symbol
    # occurring in the expression with the given unit. For example:
    # Expression("C m * B s / (C m + A m)").permutation("m")
    # returns {'$0':'C', '$1':'A'}
    def permutation(self, unit):
        enum = enumerate(self.symbols_for_unit(unit))
        return dict(("$%s" % i, sym) for i, sym in enum)

    # Return a string produced from the expression by first converting it
    # into canonical form, and then performing the replacements defined
    # by the given permutation. For example:
    # Expression("A m * B s / (A m + C m)").permute("m", {"$0":"C", "$1":"A"})
    # returns "C m *  s / (C m + A m)"
    def permute(self, unit, permutation):
        new_exp = self.canonical_form(unit)
        return replace_all(new_exp, permutation) 

    # Test for equality under permutation and muting of all other symbols 
    # than the unit provided. 
    def eq_under_permutation(self, unit, other_exp):
        muted_self = self.mute_symbols_for_other_units(unit)        
        other_permuted_str = other_exp.permute(unit, self.permutation(unit))
        return muted_self == other_permuted_str    

    # Test for equality under permutation. This is done for each of
    # the possible units using eq_under_permutation
    def __eq__(self, other):
        return all([self.eq_under_permutation(unit, other) for unit in self.units])

e1 = Expression("A m * B s / (A m + C m)")
e2 = Expression("C m * B s / (C m + A m)")
e3 = Expression("A s * B s / (A m + C m)")

f1 = Expression("A s * (B s + D s) / (A m + C m)")
f2 = Expression("A s * (D s + B s) / (C m + A m)")
f3 = Expression("D s")

print "e1 == e2: ", e1 == e2 # True
print "e1 == e3: ", e1 == e3 # False
print "e2 == e3: ", e2 == e3 # False

print "f1 == f2: ", f1 == f2 # True
print "f1 == f3: ", f1 == f3 # False
Run Code Online (Sandbox Code Playgroud)

正如您所指出的,这会检查排列下的字符串等价性,而不考虑数学等价性,但这只是成功的一半。如果您有数学表达式的规范形式,则可以对两个规范形式的表达式使用此方法。也许 sympy 的Simplify之一可以解决这个问题。