Reputation: 1363
I am trying to solve large systems of arbitrary equations (for a process flow sheeting application with physical component models written in plain math).
I am confused by the behavior of sympy's solve_poly_system function when dealing with non-integer numbers and can't figure it out through the documentation. This simple code snippet:
sympy.solve_poly_system([x-5,y-x])
gives me [(5,5)] as I would expect. When I change the number to a float however:
sympy.solve_poly_system([x-5.1,y-x])
I get the error: raise DomainError("can't compute a Groebner basis over %s" % opt.domain) sympy.polys.polyerrors.DomainError: can't compute a Groebner basis over RR
The documentation mentions that sympy prefers using its own variable types so I have tried using 'sympify' to convert my input functions to something more palatable to sympy with no luck. Any advice on how to solve this? Am I just using the wrong tool?
Note: Obviously this is a trivial example, I'm just trying to illustrate what I've distilled as the essential problem.
I'm using python 2.7 on Windows 7.
Upvotes: 3
Views: 275
Reputation: 91550
Try using SymPy 0.7.4 (just released a few days ago). This works now.
In [37]: solve_poly_system([x-5,y-x])
Out[37]: [(5, 5)]
In [38]: solve_poly_system([x-5.1,y-x])
Out[38]: [(5.1, 5.1)]
To clear up some confusion from the question:
It is true that SymPy uses its own types, but for the most part, you don't ever need to worry about this. When you evaluate x - 5.1
, the __sub__
method is called on x
and 5.1
(a Symbol
), which immediately converts 5.1
to Float(5.1)
using sympify
. The only time you ever need to worry about this is when you do an operation where both operands are not SymPy types, and the SymPy behavior is different than what the built-in behavior would be (e.g., you don't need to worry about 1 - 4
vs. sympify(1) - sympify(4)
). The only instance of this is int/int
, like 1/4
. Python will evaluated this to 0
(or 0.25
in Python 3), whereas SymPy would evaluate it to Rational(1, 4)
. So in that case, you need to sympify one of the arguments to get a rational, like S(1)/4
(S
is shorthand for sympify
).
The DomainError thing was referring to the internal representation of polynomials used by the solvers. It was never intended to be seen by users (this was a bug). In particular, this error had to do with the fact that certain algorithms were not implemented for floating point numbers (as opposed to rational numbers).
If you still see the error with more nontrivial examples, please report it as a bug in the SymPy issue tracker.
Upvotes: 2