I want to solve a system of inequalities A x <= b, namely to visualize the set of solutions of this system. Are there any ways to do this in Python? The solutions I've found using the scipy library give only one vertex.
A = np.array([[-1, 1],
[0, 1],
[0.5, 1],
[1.5, 1],
[-1, 0],
[0, -1]])
b = np.array([1, 2, 3, 6, 0, 0])
It seems that fillplots is a superset of what you need. That should handle linear inequations very easily.
Update
I was thinking about this again, and I thought I would try to see what can be done without
fillplots
, just using standard libraries such asscipy
andnumpy
.In such a system of inequalities, each equation defines a half-space. The system is the intersection of all these half-spaces and is a convex set.
Finding the vertices of that set (for example, to plot them) is called the Vertex enumeration problem. Fortunately, there are powerful algorithms to manipulate convex hulls, compute half-space intersections (and do many other wonderful things) in n dimensions. An example implementation is the Qhull library.
Even more fortunate for us, we can access aspects of that library directly via
scipy.spacial
, specifically:HalfspaceIntersection
andConvexHull
.In the following:
interior_point
, needed byHalfspaceIntersection
.Inf
,nan
in the result) when the convex set is open, we augment the original systemAx <= b
with constraints that define a bounding box (to be supplied by the caller, and also used as plotting boundaries).HalfspaceIntersection
, and in 2D the vertices of the hull are guaranteed to be in counterclockwise order).Here we go:
Tests
(Your original system):
A modified system that results in an open set: