简体   繁体   English

Sympy 简化被破坏(平方根)?

[英]Sympy simplification is broken (square roots)?

I tried this in python shell我在 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)那么如何让 numpy 给我一个更简化的结果(它并不总是一个 int 所以我不能使用 evalf 或 N)

There are 2 things missing here:这里缺少两件事:

  1. You need to explicitly tell sympy to simplify the expression if it's something complex.如果表达式很复杂,您需要明确告诉 sympy 以simplify表达式。
  2. You should use Rational whenever possible, to avoid numerical inaccuraties of floating point arithmetic您应该尽可能使用Rational ,以避免浮点运算的数值不准确

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM