gpt4 book ai didi

python - 伪环中 Sympy 中的自定义简化规则

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:56 24 4
gpt4 key购买 nike

我正在使用 Sympy 来表示自定义交换伪环的元素。我想让 Sympy 了解我域中的一些特定简化规则,例如

x*x = x
n*x = x

有了这些规则,我希望 x*y*x + y*x 之类的东西在 x*y 中得到简化。有没有办法在 Sympy 中做到这一点?你推荐其他库来做吗? (我只使用了expandsimplify、相等性测试、subsparse_expr)

最佳答案

经过几次实验,我最好的猜测是根据常量的扩展路径使用常量的自定义替换(参见 Sympy doc on Epath)

formula = expand(simplify(EPath("/*/*/Integer").apply(formula, lambda x: Integer(1))))
formula = expand(simplify(EPath("/*/Integer").apply(formula, lambda x: Integer(1))))
formula = expand(simplify(EPath("/Integer").apply(formula, lambda x: Integer(1))))

这三行基本上完成了所需的简化(例如 x*y*x + y*x -> x*y)。

可能有更优雅的解决方案,但我发布了这个答案,因为它可以帮助其他人。

关于python - 伪环中 Sympy 中的自定义简化规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260111/

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