Sympy:简化平方的平方根

编程入门 行业动态 更新时间:2024-10-18 22:27:23
本文介绍了Sympy:简化平方的平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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:简化平方的平方根

本文发布于:2023-11-28 21:08:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平方根   Sympy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!