I'm using the scipy.spatial.ConvexHull API for evaluating the convex hull of a set of points and it works well. Given the code below:
p1 = points[11]
hull = ConvexHull(points)
vertices = [points[i] for i in hull.vertices]
how can I evaluate the coefficients(weights) of the convex combination of vertices that equals to p1?
Thank you very much, Moshe
Note: if the number of vertices is higher than
d+1, wheredis the dimension, then the combination will not be unique. In the remainder of the answer I assume thatd=2for simplicity.Input:
v0 = (x0, y0), v1 = (x1, y1), ..., vn = (xn, yn);p1 = (x,y);Output:
a0, a1, ..., an;such that:
x = a0 * x0 + a1 * x1 + ... + an * xn;y = a0 * y0 + a1 * y1 + ... + an * yn;1 = a0 + a1 + ... + an;ai >= 0.This is a system of three linear equations in the unknown
(a0, a1, ..., an), along with n+1 linear inequations. We can solve this system using scipy's linear programming module scipy.optimize.linprogExample solution:
Important note: I began this answer by warning that if there are more than 3 vertices in the plane, or more than d+1 vertices in dimension d, then there will be more than 1 solution to our system of equations. The code above only returns one solution, meaning that an arbitrary choice was made. You have control over this arbitrary choice:
scipy.optimize.linprogis an optimisation tool; here it is minimizing the dot-product of the vectorcwe gave as parameter, and the solution vectors.x. Here I set all coefficients ofcto 1, meaninglinprogwill find a solution that minimizes the sum of coefficients in the solution; try supplying a different vectorcand you'll get a different solution.