- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下 sympy 代码:
from sympy import Add
from sympy.abc import x
t1 = 2+2*x
t2 = x
myeq = sp.UnevaluatedExpr(Add(sp.UnevaluatedExpr(t1), sp.UnevaluatedExpr(t2), evaluate=False))
# BUG! Will print: x + 2*x + 2
# Yet it should print: 2+2*x+x
print(myeq)
此代码片段改编自this回答。那里的术语更简单,因此 Add
保留了顺序。但在这种情况下,如何让 Add
也保留顺序呢?
(备注:如果我们将术语更改为 t1=x
和 t2=x**2
我的方法是使用 sp.UnevaluatedExpr
有效,但没有这些术语的原始答案则无效。唉,对于我的具体情况,甚至使用 sp.UnevaluatedExpr
都不起作用。)
最佳答案
...但更多的是一个缺失的功能。所有这些都被记录下来。
这是 SymPy 的含义 unevaluated .
By unevaluated it is meant that the value inside of it will not interact with the expressions outside of it to give simplified outputs.
在您的示例中,术语 2*x
和 x
并未按预期进行简化。
您所看到的是 SymPy 没有保留您输入术语的顺序。这是documented under the expression tree section .
The arguments of the commutative operations
Add
andMul
are stored in an arbitrary (but consistent!) order, which is independent of the order inputted.
这应该不是问题,因为 Add
和 Mul
是可交换的。
尽管如此,如果由于某种原因您想由于乘法的不可交换性而保留输入的顺序,您可以这样做。
In SymPy, you can create noncommutative Symbols using
Symbol('A',
, and the order of multiplication for noncommutative Symbols is kept the same as the input)
commutative=False)
就目前而言,似乎不存在非交换加法。
关于python - 同情错误?即使使用 `evaluate=False` 方程也会自动处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866180/
来自 sympy solve() 的解决方案是否以某种方式排序?它是从最小到最大的解决方案吗? 如何强制解决方案的非负性? 在我的问题中,我需要独特的最小正解决方案。我感谢所有的帮助 最佳答案 它们不
我从 sympy python 库开始。 如果,我有这个表达式 from sympy.abc import a,b,c,p,q e = p * ( a + b ) + q * ( a + c ) 如何
当 Sympy 生成 C 代码时, 有没有办法对表达式中的 pow(或 powf)出现强制执行 CSE 优化? 例如,这个代码片段 c, s = symbols('c s') myexpr = c**
请考虑以下脚本;我们如何使用 python 计算 h 关于 f 的李导数? import sympy as sym x, y, L, u , v = sym.symbols('x y L u v')
我正在尝试求解这三个方程: a1 + a2 = b1 a2 + a3 = b2 b1 + b2 = c1 我为随机选择的三个变量生成值(不允许组合 b1、b2 和 c1),所以我可能有 a1 = 5、
我找不到让 SymPy 将 cos(a)*cos(b) 之类的乘积展开为角和的三角函数之和的方法。 from sympy import * init_printing() wrf,wlo,t = sy
lambdify Sympy 的函数只能将我们的表达式转换为 lambda 函数。 x, y = symbols('x y') expr = x + 2*y f = lambdify(x, expr)
from sympy import * x, alpha = symbols('x alpha') latex(alpha/x) Out[1]: '\\frac{\\alpha}{x}' 这在 lat
我对为什么以下代码的答案是 10 而不是 1 感到困惑。有人可以帮助我了解 lambdify 发生了什么或者是什么导致了错误答案吗? import sympy from sympy.utilities
基于我发现的示例 here ,我正在尝试从使用 sumpy.diag 创建的对角矩阵创建函数 myM = Matrix([ [x1, 4, 4], [4, x2, 4], [4, 4, x3]])
我是一名优秀的程序员,十分优秀!