def con2vert(A, b):
    """
    Convert sets of constraints to a list of vertices (of the feasible region).
    If the shape is open, con2vert returns False for the closed property.
    """
    # Python implementation of con2vert.m by Michael Kleder (July 2005),
    #  available: http://www.mathworks.com/matlabcentral/fileexchange/7894
    #  -con2vert-constraints-to-vertices
    # Author: Michael Kelder (Original)
    #         Andre Campher (Python implementation)
    c = linalg.lstsq(mat(A), mat(b))[0]
    btmp = mat(b)-mat(A)*c
    D = mat(A)/matlib.repmat(btmp, 1, A.shape[1])

    fmatv = qhull(D, "Ft") #vertices on facets

    G  = zeros((fmatv.shape[0], D.shape[1]))
    for ix in range(0, fmatv.shape[0]):
        F = D[fmatv[ix, :], :].squeeze()
        G[ix, :] = linalg.lstsq(F, ones((F.shape[0], 1)))[0].transpose()

    V = G + matlib.repmat(c.transpose(), G.shape[0], 1)
    ux = uniqm(V)

    eps = 1e-13
    Av = dot(A, ux.T)
    bv = tile(b, (1, ux.shape[0]))
    closed = sciall(Av - bv <= eps)

    return ux, closed
 def allinside(self, conset2):
     """
     Determine if all vertices of self is within conset2. allvinside merely
     returns True/False whereas insidenorm returns a measure of 'inside-ness'
     better suited for optimisers.
     """
     # Inside check
     Av = dot(conset2.A, self.vert.T)
     bv = tile(conset2.b, (1, self.vert.shape[0]))
     eps = 1e-13
     intmpvals = Av - bv
     intmp = intmpvals <= eps
     allvinside = sciall(intmp)
     # Inside norm
     insidenorm = zeros((Av.shape[0], 1))
     for cons in range(Av.shape[0]):
         for verts in range(Av.shape[1]):
             dist = abs(intmpvals[cons, verts])/sqrt(sum(conset2.A[cons, :]**2))
             if not intmp[cons, verts]:  # outside
                 insidenorm[cons] = insidenorm[cons] - dist
     # Outside volume
     return allvinside, insidenorm#, outsidevol