为什么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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...