sympy's plot_parametric says TypeError: can't convert complex to float
sympy's plot_parametric says TypeError: can't convert complex to float
我想画一条实际上是S形的曲线:
- 画一个圆圈
- 将半圆的下半部分移动整个直径距离
但代码显示 TypeError: can't convert complex to float
。复数在哪里?如何解决?谢谢
import sympy as sp
from sympy.plotting import *
u = sp.Symbol('u', real=True)
R0 = 2
boundLower = 0
boundUpper = 2 * sp.pi
x_u = sp.Piecewise(
(R0*sp.cos(u)+R0, sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.cos(u)+3*R0, sp.And(boundUpper/2 < u, u <= boundUpper)),
)
y_u = sp.Piecewise(
(R0*sp.sin(u), sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.sin(u), sp.And(boundUpper/2 < u, u <= boundUpper)),
)
plot_parametric(x_u, y_u, (u, boundLower, boundUpper))
SymPy 的 plot
涉及一些容易出错的操作,旨在提高绘图的性能;它们涉及 complex
数据类型,当涉及不等式时有时会导致错误。在这种情况下减少不等式的数量会有所帮助:
x_u = sp.Piecewise(
(R0*sp.cos(u)+R0, u <= boundUpper/2),
(R0*sp.cos(u)+3*R0, u <= boundUpper),
)
y_u = sp.Piecewise(
(R0*sp.sin(u), u <= boundUpper/2),
(R0*sp.sin(u), u <= boundUpper),
)
plot_parametric(x_u, y_u, (u, boundLower, boundUpper))
以上是 Piecewise 通常在 SymPy 中的呈现方式:条件按给定的顺序求值,第一个为 True 的结果导致对相应表达式的求值。 (u <= boundUpper
可以用 True
代替。) 结果:
这仍然不理想。当然,从 0 到 4 的水平线不应该存在 - 这是绘图的产物。此外,此代码显示
UserWarning: The evaluation of the expression is problematic. We are trying a failback method that may still work. Please report this as a bug.
我建议在绘图时避免使用 Piecewise。而是使用 extend
组合片段中的图,如下所示。
x_u1 = R0*sp.cos(u)+R0
x_u2 = R0*sp.cos(u)+3*R0
y_u1 = R0*sp.sin(u)
y_u2 = R0*sp.sin(u)
p = plot_parametric(x_u1, y_u1, (u, boundLower, boundUpper/2), show=False)
p.extend(plot_parametric(x_u2, y_u2, (u, boundUpper/2, boundUpper), show=False))
p.show()
输出(无警告,无伪影)。
我想画一条实际上是S形的曲线:
- 画一个圆圈
- 将半圆的下半部分移动整个直径距离
但代码显示 TypeError: can't convert complex to float
。复数在哪里?如何解决?谢谢
import sympy as sp
from sympy.plotting import *
u = sp.Symbol('u', real=True)
R0 = 2
boundLower = 0
boundUpper = 2 * sp.pi
x_u = sp.Piecewise(
(R0*sp.cos(u)+R0, sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.cos(u)+3*R0, sp.And(boundUpper/2 < u, u <= boundUpper)),
)
y_u = sp.Piecewise(
(R0*sp.sin(u), sp.And(boundLower <= u, u <= boundUpper/2)),
(R0*sp.sin(u), sp.And(boundUpper/2 < u, u <= boundUpper)),
)
plot_parametric(x_u, y_u, (u, boundLower, boundUpper))
SymPy 的 plot
涉及一些容易出错的操作,旨在提高绘图的性能;它们涉及 complex
数据类型,当涉及不等式时有时会导致错误。在这种情况下减少不等式的数量会有所帮助:
x_u = sp.Piecewise(
(R0*sp.cos(u)+R0, u <= boundUpper/2),
(R0*sp.cos(u)+3*R0, u <= boundUpper),
)
y_u = sp.Piecewise(
(R0*sp.sin(u), u <= boundUpper/2),
(R0*sp.sin(u), u <= boundUpper),
)
plot_parametric(x_u, y_u, (u, boundLower, boundUpper))
以上是 Piecewise 通常在 SymPy 中的呈现方式:条件按给定的顺序求值,第一个为 True 的结果导致对相应表达式的求值。 (u <= boundUpper
可以用 True
代替。) 结果:
这仍然不理想。当然,从 0 到 4 的水平线不应该存在 - 这是绘图的产物。此外,此代码显示
UserWarning: The evaluation of the expression is problematic. We are trying a failback method that may still work. Please report this as a bug.
我建议在绘图时避免使用 Piecewise。而是使用 extend
组合片段中的图,如下所示。
x_u1 = R0*sp.cos(u)+R0
x_u2 = R0*sp.cos(u)+3*R0
y_u1 = R0*sp.sin(u)
y_u2 = R0*sp.sin(u)
p = plot_parametric(x_u1, y_u1, (u, boundLower, boundUpper/2), show=False)
p.extend(plot_parametric(x_u2, y_u2, (u, boundUpper/2, boundUpper), show=False))
p.show()
输出(无警告,无伪影)。