为什么sympy.solve不返回任何解决方案?

问题描述

我正在尝试使用sympy.solve来解决以下方程式

import sympy as sp

theta = sp.symbols('theta')
x = 0

eq = sp.cos(theta)**2 - sp.sin(theta)**2 - sp.sin(theta)*sp.sqrt(sp.sin(theta)**2 + x) + sp.sin(theta)*sp.cos(theta)**2 / sp.sqrt(sp.sin(theta)**2 + x)

soln = sp.solve(eq,theta)

print(soln)

实际上,如果x = 0,则eq将为cos^2(theta) - sin^2(theta),解为pi / 4,3pi / 4,...。 但是,以上代码不返回任何数字,仅返回[]。 如果x不为0,则此代码有效。 为什么当x = 0时此代码返回[]

解决方法

不确定究竟为什么solve在这种情况下不起作用,但是solveset似乎更好。 docs建议假定存在要建立的域,使用solveset时默认为复数。

import sympy as sp

theta = sp.Symbol('theta',real=False)
x = 0.0

eq = sp.cos(theta)**2 - sp.sin(theta)**2 - sp.sin(theta)*sp.sqrt(sp.sin(theta)**2 + x) + sp.sin(theta)*sp.cos(theta)**2 / sp.sqrt(sp.sin(theta)**2 + x)

soln = sp.solveset(eq,theta)

结果soln

$ \ displaystyle \ left {2 n \ pi + \ frac {5 \ pi} {4}; |; n \ in \ mathbb {Z} \ right} \ cup \ left {2 n \ pi + \ frac {3 \ pi} {4}; |; n \ in \ mathbb {Z} \ right} \ cup \ left {2 n \ pi + \ frac {7 \ pi} {4}; |; n \ in \ mathbb {Z} \ right} \ cup \ left {2 n \ pi + \ frac {\ pi} {4}; |; n \ in \ mathbb {Z} \ right} $

enter image description here