I'm trying to solve a system of equations using sympy
.
from sympy import *
def get_angles(a, b, c, d):
theta, phi, lamb = symbols('\\theta \\phi \\lambda', real=True)
a_eq = Eq(cos(theta / 2), a)
b_eq = Eq(exp(I * phi) * sin(theta / 2), b)
c_eq = Eq(-exp(I * lamb) * sin(theta / 2), c)
d_eq = Eq(exp(I * (phi + lamb)) * cos(theta / 2), d)
# theta_constr1 = Eq(theta >= 0)
# theta_constr2 = Eq(theta <= pi)
# phi_constr1 = Eq(phi >= 0)
# phi_constr2 = Eq(phi < 2 * pi)
res = solve([
a_eq, b_eq, c_eq, d_eq,
#theta_constr1, theta_constr2, phi_constr1, phi_constr2,
],
theta,
phi,
lamb,
check=False,
dict=True)
return res
The function returns the right results as it is, but it doesn't work if I try to put the constraint on the angles inside the system of equations (the commented parts). Is there any way to have them?
At the moment, I'm using a simple solution to overcome this limitation: I pass the result of the previous function to the following one to filter out unwanted results
def _final_constraint(result):
res = []
for sol in result:
to_add = True
for k, v in sol.items():
if str(k) == '\\theta' and (v < 0 or v > pi):
to_add = False
break
elif str(k) == '\\phi' and (v < 0 or v >= 2 * pi):
to_add = False
break
if to_add:
res.append(simplify(sol))
return res