首页 > 解决方案 > 如何在分母中用平方根创建 sympy 分数?

问题描述

# From jupyter notebook
import sympy
sympy.init_printing()

x1 = sympy.Rational(1, 2)
x2 = sympy.sqrt(2)

# ERROR: can't do this..
x3 = sympy.Rational(1, sympy.sqrt(2))

# ERROR: can't do this either
x4 = sympy.Rational(1, x2)

如何在不使用浮点数的情况下将 sqrt 符号放入等式的分母中?

标签: pythonsympy

解决方案


当您需要 1 / sqrt(2) 时,请执行以下操作:

x1 = 1 / sympy.sqrt(2)

当您需要 1/2 时,请执行以下操作:

x1 = sympy.Rational(1,2)

不要这样做:

x1 = sympy.Rational(1, sympy.sqrt(2))

错误。


推荐阅读