-
Notifications
You must be signed in to change notification settings - Fork 0
/
vert2lcon.m
210 lines (154 loc) · 4.57 KB
/
vert2lcon.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
function [A,b,Aeq,beq]=vert2lcon(V,tol)
%An extension of Michael Kleder's vert2con function, used for finding the
%linear constraints defining a polyhedron in R^n given its vertices. This
%wrapper extends the capabilities of vert2con to also handle cases where the
%polyhedron is not solid in R^n, i.e., where the polyhedron is defined by
%both equality and inequality constraints.
%
%SYNTAX:
%
% [A,b,Aeq,beq]=vert2lcon(V,TOL)
%
%The rows of the N x n matrix V are a series of N vertices of a polyhedron
%in R^n. TOL is a rank-estimation tolerance (Default = 1e-10).
%
%Any point x inside the polyhedron will/must satisfy
%
% A*x <= b
% Aeq*x = beq
%
%up to machine precision issues.
%
%
%EXAMPLE:
%
%Consider V=eye(3) corresponding to the 3D region defined
%by x+y+z=1, x>=0, y>=0, z>=0.
%
%
%>> [A,b,Aeq,beq]=vert2lcon(eye(3))
%
% A =
%
% 1.0000 1.0000 -2.0000
% -2.0000 1.0000 1.0000
% 1.0000 -2.0000 1.0000
%
%
% b =
%
% 1.0000
% 1.0000
% 1.0000
%
%
% Aeq =
%
% 1.0000 1.0000 1.0000
%
%
% beq =
%
% 1
%%initial stuff
[M,N]=size(V);
if M==1
A=[];b=[];
Aeq=eye(N); beq=V(:);
return
end
if nargin<2, tol=1e-10; end
p=V(1,:).';
X=bsxfun(@minus,V.',p);
%In the following, we need Q to be full column rank
%and we prefer E compact.
if M>N %X is wide
[Q, R, E] = qr(X,0); %economy-QR ensures that E is compact.
%Q automatically full column rank since X wide
else%X is tall, hence non-solid polytope
[Q, R, P]=qr(X); %non-economy-QR so that Q is full-column rank.
[~,E]=max(P); %No way to get E compact. This is the alternative.
clear P
end
diagr = abs(diag(R));
if nnz(diagr)
%Rank estimation
r = find(diagr >= tol*diagr(1), 1, 'last'); %rank estimation
iE=1:length(E);
iE(E)=iE;
Rsub=R(1:r,iE).';
if r>1
[A,b]=vert2con(Rsub);
elseif r==1
A=[1;-1];
b=[max(Rsub);-min(Rsub)];
end
A=A*Q(:,1:r).';
b=bsxfun(@plus,b,A*p);
if r<N
Aeq=Q(:,r+1:end).';
beq=Aeq*p;
else
Aeq=[];
beq=[];
end
else %Rank=0. All points are identical
A=[]; b=[];
Aeq=eye(N);
beq=p;
end
ibeq=abs(beq);
ibeq(~beq)=1;
Aeq=bsxfun(@rdivide,Aeq,ibeq);
beq=beq./ibeq;
function [A,b] = vert2con(V)
% VERT2CON - convert a set of points to the set of inequality constraints
% which most tightly contain the points; i.e., create
% constraints to bound the convex hull of the given points
%
% [A,b] = vert2con(V)
%
% V = a set of points, each ROW of which is one point
% A,b = a set of constraints such that A*x <= b defines
% the region of space enclosing the convex hull of
% the given points
%
% For n dimensions:
% V = p x n matrix (p vertices, n dimensions)
% A = m x n matrix (m constraints, n dimensions)
% b = m x 1 vector (m constraints)
%
% NOTES: (1) In higher dimensions, duplicate constraints can
% appear. This program detects duplicates at up to 6
% digits of precision, then returns the unique constraints.
% (2) See companion function CON2VERT.
% (3) ver 1.0: initial version, June 2005.
% (4) ver 1.1: enhanced redundancy checks, July 2005
% (5) Written by Michael Kleder,
%
%Modified by Matt Jacobson - March 29,2011
%
k = convhulln(V);
c = mean(V(unique(k),:));
% V=V-repmat(c,[size(V,1) 1]);
% A = NaN*zeros(size(k,1),size(V,2));
%modified the above 2 lines according to various suggestions
%in the Newsgroup
V = bsxfun(@minus,V,c);
A = nan(size(k,1),size(V,2));
rc=0;
for ix = 1:size(k,1)
F = V(k(ix,:),:);
if rank(F,1e-5) == size(F,1)
rc=rc+1;
A(rc,:)=F\ones(size(F,1),1);
end
end
A=A(1:rc,:);
b=ones(size(A,1),1);
b=b+A*c';
% eliminate duplicate constraints:
[null,I]=unique(num2str([A b],6),'rows');
A=A(I,:); % rounding is NOT done for actual returned results
b=b(I);
return