gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:57:00 24 4
gpt4 key购买 nike

我在 Python 中有两个字符串,

A m * B s / (A m + C m)

C m * B s / (C m + A m)

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

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

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

是否有 python 方法来测试这种等价性?它应该适用于任意表达式。

最佳答案

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

这个想法是,如果两个表达式在排列下是等价的,则携带一个到另一个的排列必须将第一个字符串中的第 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

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

关于python - 在 Python 中测试数学表达式的等价性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6154954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com