0

SymPy is giving me complex solutions, even when I constrain my variables to be real, as in this answer.

Here's an example finding the intersection of a unit sphere and the plane X=1. There should be exactly 1 real solution, (1,0,0).

>>> import sympy
>>> x,y,z = sympy.symbols('x,y,z', real=True)
>>> list(sympy.solve([x**2 + y**2 + z**2 - 1, x - 1], [x,y,z]))
[(1, -I*z, z), (1, I*z, z)]

These are correct, but not real.

I can't use solveset(…, domain=S.Reals) because solveset doesn't seem to support systems of equations.

ppm
  • 178
  • 8

1 Answers1

2

This seems to be open issue of sympy and known problem: https://github.com/sympy/sympy/issues/9973

You could probably hack a function that checks whether there is a real solution using a heuristic - for each complex part, it will solve the equation setting the complex part to 0). This might lead to branching and a lot of work if the solutions are not simple though.

Vladimír Kunc
  • 379
  • 1
  • 4