python - SymPy 中的相同表达式不是 ==
问题描述
我在 SymPy 中有两个结构相同的表达式,以不同的方式创建:
import sympy as sp
f = sp.Symbol('f')
x = sp.Symbol('x')
by_hand = sp.Function(f)(x)
from_string = sp.sympify('f(x)')
尽管在构造上是相同的表达,但两者不是==
:
by_hand == from_string
# False
事实上,SymPy 甚至不知道它们在符号上是相等的:
sp.simplify(by_hand - from_string)
# ()−()
两者具有相同srepr
的 s:
(sp.srepr(by_hand), sp.srepr(from_string))
# ("Function('f')(Symbol('x'))", "Function('f')(Symbol('x'))")
sp.srepr(by_hand) == sp.srepr(from_string)
# True
我知道通过字符串的往返对于在 SymPy 中维护身份并不一定可靠,但即使使用locals
参数作为修复也不能解决这个问题:
by_hand == sp.sympify('f(x)', locals={'f': f}) == by_hand
# False
不得不使用locals
已经有些痛苦了,因为我正在尝试对一个通过独立构造它们来构造一堆符号表达式的例程进行单元测试。
解决方案
有几种方法可以解决这个问题。
修复 1
通过locals={'f': sp.Function(f)}
而不是locals={'f': f}
.
by_hand = sp.Function(f)(x)
from_string = sp.sympify('f(x)')
# by_hand == sp.sympify('f(x)', locals={'f': f}) # Change this
by_hand == sp.sympify('f(x)', locals={'f': sp.Function(f)}) # True # to this
你定义了by_hand = sp.Function(f)(x)
,所以Function('f')
在sp.srepr(by_hand)
是sp.Function(f)
,或sp.Function(sp.Symbol('f'))
。
另一方面,Function('f')
insp.srepr(from_string)
是sp.Function('f')
。
(这导致我们修复 2。)
不同之处在于sp.Function(sp.Symbol('f'))
继承了 的默认假设{'commutative': True}
。
(这导致我们修复 3。)sp.Symbol('f')
修复 2
这是干净的修复,可能是您想要的。
# f = sp.Symbol('f') # Change this
f = sp.Function('f') # to this
x = sp.Symbol('x')
# by_hand = sp.Function(f)(x) # Change this
by_hand = f(x) # to this
from_string = sp.sympify('f(x)')
by_hand == from_string # True
修复 3
这个 hack 演示了{'commutative': True}
Fix 1 中确定的继承假设。
by_hand = sp.Function(f)(x)
del by_hand._kwargs['commutative'] # Add this
# del by_hand._assumptions['commutative'] # Add this, optional
from_string = sp.sympify('f(x)')
by_hand == from_string # True
推荐阅读
- quickbooks - Quickbooks Webconnector 在每次导入前延迟 10 分钟以上
- node.js - How to retrieve a newly created google sheet using nodejs and google sheets api
- iis - IIS - 发布 - 错误 405 方法不允许
- node.js - 在 Jenkins 管道中合并多个笑话覆盖率报告
- python - 403 客户端错误:禁止访问 url:https://api.twitter.com/oauth/request_token - 无法使用 Twitter 登录
- c++ - 如何将对象函数的实例传递给另一个函数?
- batch-file - 有没有办法编写批处理文件以使用设置变量重命名文件?
- swift - 选择可重复使用的表格视图单元格时索引超出范围
- c# - 实体框架数据库优先方法 - 如何制作异步方法?
- sql - 如何删除sqlite中的第n行表?