Sympy 简化被打破(平方根)? [英] Sympy simplification is broken (square roots)?

查看:35
本文介绍了Sympy 简化被打破(平方根)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python shell 中试过这个

<预><代码>>>>从 sympy 导入 sqrt>>>sqrt((-9/10 + 6*sqrt(3)/5)**2 + (6/5 + 9*sqrt(3)/10)**2)sqrt((-0.9 + 6*sqrt(3)/5)**2 + (1.2 + 9*sqrt(3)/10)**2)

当在谷歌中输入时:

那么我如何让 numpy 给我一个更简化的结果(它不会总是一个 int 所以我不能使用 evalf 或 N)

解决方案

这里缺少两件事:

  1. 如果表达式很复杂,您需要明确告诉 sympy 简化表达式.
  2. 您应该尽可能使用 Rational,以避免浮点运算的数值不准确

总而言之:

>>>from sympy import Rational、simple、sqrt>>>简化(sqrt((-Rational(9, 10) + Rational(6,5)*sqrt(3))**2 + (Rational(6,5) + Rational(9,10)*sqrt(3))**2))3

I tried this in python shell

>>> from sympy import sqrt
>>> sqrt((-9/10 + 6*sqrt(3)/5)**2 + (6/5 + 9*sqrt(3)/10)**2)
sqrt((-0.9 + 6*sqrt(3)/5)**2 + (1.2 + 9*sqrt(3)/10)**2)

and when type it in google:

So how do I get numpy to give me a more simpilified result (it won't always be an int so I cant use evalf or N)

解决方案

There are 2 things missing here:

  1. You need to explicitly tell sympy to simplify the expression if it's something complex.
  2. You should use Rational whenever possible, to avoid numerical inaccuraties of floating point arithmetic

All in all:

>>> from sympy import Rational, simplify, sqrt
>>> simplify(sqrt((-Rational(9, 10) + Rational(6,5)*sqrt(3))**2 + (Rational(6,5) + Rational(9,10)*sqrt(3))**2))
3

这篇关于Sympy 简化被打破(平方根)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆