问题描述
Sympy 似乎无法简化涉及变量平方根的表达式:
在[28]中:a = x**2在[29]中:b = a**(1/2)在 [30] 中:b出[30]:0.5⎛ 2⎞⎝x ⎠在 [31] 中:b.simplify()出[31]:0.5⎛ 2⎞⎝x ⎠
我不认为它适用于 simplify
的其他变体,特别是我认为 b.powsimp()
应该可以工作.
在[32]中:b.powsimp()出[32]:0.5⎛ 2⎞⎝x ⎠
有谁知道为什么这不起作用,或者我做错了什么?
你的例子有两个问题.
第一个 sqrt(x**2)==x
仅用于 正 实数.
其次,对于 SymPy 1/2
和 0.5
不是一回事.第一个是 Rational
实例,第二个是 float
实例.
最后一个例子:
>>>x = Symbol('x', real=True)>>>(x**2)**(1./2)∣x∣**1.0>>>(x**2)**(S(1)/2) # S() 是 sympify() 的缩写∣x∣sympify
将 python 对象转换为更合适的 SymPy 对象.
Sympy does not seem to be able to simplify an expression where the square root of a square of a variable is involved:
In [28]: a = x**2
In [29]: b = a**(1/2)
In [30]: b
Out[30]:
0.5
⎛ 2⎞
⎝x ⎠
In [31]: b.simplify()
Out[31]:
0.5
⎛ 2⎞
⎝x ⎠
I do not get this to work with other variants of simplify
, in particular I would have thought that b.powsimp()
should work.
In [32]: b.powsimp()
Out[32]:
0.5
⎛ 2⎞
⎝x ⎠
Does anyone know why this does not work, or what I am doing wrong?
There are two problems with your example.
First sqrt(x**2)==x
only for positive real numbers.
Second, for SymPy 1/2
and 0.5
are not the same things. The first is a Rational
instance, the second is a float
instance.
Finally, an example:
>>> x = Symbol('x', real=True)
>>> (x**2)**(1./2)
∣x∣**1.0
>>> (x**2)**(S(1)/2) # S() is short for sympify()
∣x∣
sympify
transforms python objects to more adequate SymPy objects.
这篇关于Sympy:简化平方的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!