我是一个完全有同情心的初学者,所以我可能会忽略一些基本的东西。我想旋转我的坐标系,这样我就可以在标准位置构建双曲线,然后将其转换为任意情况。首先,我建立了我的等式:
> x, y, a, b, theta = symbols('x y a b theta')
> f = Eq(x ** 2/a ** 2 - y ** 2/b ** 2, 1)
> f1 = f.subs({a: 5, b: 10})
> f1
> x**2/25 - y**2/100 == 1
接下来我想要旋转它,我尝试使用sub:
> f1.subs({x: x*cos(theta) - y*sin(theta), y: x*sin(theta) + y*cos(theta)})
> -(x*sin(theta) + y*cos(theta))**2/100 + (x*cos(theta) - (x*sin(theta) + y*cos(theta))*sin(theta))**2/25 == 1
但这并不起作用,因为显然x的替换是在y的替换之前,并且替换的x的值已经更新。必须有某种方法来做这种替换,对吗?
还是有一个比同意更好的工具吗?一旦我得到我的双曲线,我就会想找到不同的点之间的交点。
感谢您的任何建议。
答案 0 :(得分:3)
一个简单的解决方案是使用临时符号:
x_temp, y_temp = symbols('x_temp y_temp')
f1.subs({x: x_temp*cos(theta) - y_temp*sin(theta), y: x_temp*sin(theta) + y_temp*cos(theta)}).subs({x_temp: x, y_temp: y})
> -(x*sin(theta) + y*cos(theta))**2/100 + (x*cos(theta) - y*sin(theta))**2/25 == 1
I think sympy can do what you want. There is a polysys
modules in sympy.solvers
:
"""
Solve a system of polynomial equations.
Examples
========
>>> from sympy import solve_poly_system
>>> from sympy.abc import x, y
>>> solve_poly_system([x*y - 2*y, 2*y**2 - x**2], x, y)
[(0, 0), (2, -sqrt(2)), (2, sqrt(2))]
"""
答案 1 :(得分:1)
您可以在 .subs()方法中设置 simultaneous = True 以强制SymPy一次替换所有变量(实际上,SymPy在内部创建临时变量,继续替换,然后恢复旧的变量。)
In [13]: f1.subs({x: x*cos(theta) - y*sin(theta),
y: x*sin(theta) + y*cos(theta)}, simultaneous=True)
Out[13]:
2 2
(x⋅sin(θ) + y⋅cos(θ)) (x⋅cos(θ) - y⋅sin(θ))
- ────────────────────── + ────────────────────── = 1
100 25
否则使用 .xreplace(...)而不是 .subs(...)
In [11]: f1.xreplace({x: x*cos(theta) - y*sin(theta),
y: x*sin(theta) + y*cos(theta)})
Out[11]:
2 2
(x⋅sin(θ) + y⋅cos(θ)) (x⋅cos(θ) - y⋅sin(θ))
- ────────────────────── + ────────────────────── = 1
100 25