]> gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/cone/cone.py
Add some Proposition 3 tests for positive operators.
[sage.d.git] / mjo / cone / cone.py
1 from sage.all import *
2
3 def is_lyapunov_like(L,K):
4 r"""
5 Determine whether or not ``L`` is Lyapunov-like on ``K``.
6
7 We say that ``L`` is Lyapunov-like on ``K`` if `\left\langle
8 L\left\lparenx\right\rparen,s\right\rangle = 0` for all pairs
9 `\left\langle x,s \right\rangle` in the complementarity set of
10 ``K``. It is known [Orlitzky]_ that this property need only be
11 checked for generators of ``K`` and its dual.
12
13 INPUT:
14
15 - ``L`` -- A linear transformation or matrix.
16
17 - ``K`` -- A polyhedral closed convex cone.
18
19 OUTPUT:
20
21 ``True`` if it can be proven that ``L`` is Lyapunov-like on ``K``,
22 and ``False`` otherwise.
23
24 .. WARNING::
25
26 If this function returns ``True``, then ``L`` is Lyapunov-like
27 on ``K``. However, if ``False`` is returned, that could mean one
28 of two things. The first is that ``L`` is definitely not
29 Lyapunov-like on ``K``. The second is more of an "I don't know"
30 answer, returned (for example) if we cannot prove that an inner
31 product is zero.
32
33 REFERENCES:
34
35 M. Orlitzky. The Lyapunov rank of an improper cone.
36 http://www.optimization-online.org/DB_HTML/2015/10/5135.html
37
38 EXAMPLES:
39
40 The identity is always Lyapunov-like in a nontrivial space::
41
42 sage: set_random_seed()
43 sage: K = random_cone(min_ambient_dim=1, max_ambient_dim=8)
44 sage: L = identity_matrix(K.lattice_dim())
45 sage: is_lyapunov_like(L,K)
46 True
47
48 As is the "zero" transformation::
49
50 sage: K = random_cone(min_ambient_dim=1, max_ambient_dim=8)
51 sage: R = K.lattice().vector_space().base_ring()
52 sage: L = zero_matrix(R, K.lattice_dim())
53 sage: is_lyapunov_like(L,K)
54 True
55
56 Everything in ``K.lyapunov_like_basis()`` should be Lyapunov-like
57 on ``K``::
58
59 sage: K = random_cone(min_ambient_dim=1, max_ambient_dim=6)
60 sage: all([ is_lyapunov_like(L,K) for L in K.lyapunov_like_basis() ])
61 True
62
63 """
64 return all([(L*x).inner_product(s) == 0
65 for (x,s) in K.discrete_complementarity_set()])
66
67
68 def motzkin_decomposition(K):
69 r"""
70 Return the pair of components in the Motzkin decomposition of this cone.
71
72 Every convex cone is the direct sum of a strictly convex cone and a
73 linear subspace [Stoer-Witzgall]_. Return a pair ``(P,S)`` of cones
74 such that ``P`` is strictly convex, ``S`` is a subspace, and ``K``
75 is the direct sum of ``P`` and ``S``.
76
77 OUTPUT:
78
79 An ordered pair ``(P,S)`` of closed convex polyhedral cones where
80 ``P`` is strictly convex, ``S`` is a subspace, and ``K`` is the
81 direct sum of ``P`` and ``S``.
82
83 REFERENCES:
84
85 .. [Stoer-Witzgall] J. Stoer and C. Witzgall. Convexity and
86 Optimization in Finite Dimensions I. Springer-Verlag, New
87 York, 1970.
88
89 EXAMPLES:
90
91 The nonnegative orthant is strictly convex, so it is its own
92 strictly convex component and its subspace component is trivial::
93
94 sage: K = Cone([(1,0,0),(0,1,0),(0,0,1)])
95 sage: (P,S) = motzkin_decomposition(K)
96 sage: K.is_equivalent(P)
97 True
98 sage: S.is_trivial()
99 True
100
101 Likewise, full spaces are their own subspace components::
102
103 sage: K = Cone([(1,0),(-1,0),(0,1),(0,-1)])
104 sage: K.is_full_space()
105 True
106 sage: (P,S) = motzkin_decomposition(K)
107 sage: K.is_equivalent(S)
108 True
109 sage: P.is_trivial()
110 True
111
112 TESTS:
113
114 A random point in the cone should belong to either the strictly
115 convex component or the subspace component. If the point is nonzero,
116 it cannot be in both::
117
118 sage: set_random_seed()
119 sage: K = random_cone(max_ambient_dim=8)
120 sage: (P,S) = motzkin_decomposition(K)
121 sage: x = K.random_element()
122 sage: P.contains(x) or S.contains(x)
123 True
124 sage: x.is_zero() or (P.contains(x) != S.contains(x))
125 True
126
127 The strictly convex component should always be strictly convex, and
128 the subspace component should always be a subspace::
129
130 sage: set_random_seed()
131 sage: K = random_cone(max_ambient_dim=8)
132 sage: (P,S) = motzkin_decomposition(K)
133 sage: P.is_strictly_convex()
134 True
135 sage: S.lineality() == S.dim()
136 True
137
138 The generators of the components are obtained from orthogonal
139 projections of the original generators [Stoer-Witzgall]_::
140
141 sage: set_random_seed()
142 sage: K = random_cone(max_ambient_dim=8)
143 sage: (P,S) = motzkin_decomposition(K)
144 sage: A = S.linear_subspace().complement().matrix()
145 sage: proj_S_perp = A.transpose() * (A*A.transpose()).inverse() * A
146 sage: expected_P = Cone([ proj_S_perp*g for g in K ], K.lattice())
147 sage: P.is_equivalent(expected_P)
148 True
149 sage: A = S.linear_subspace().matrix()
150 sage: proj_S = A.transpose() * (A*A.transpose()).inverse() * A
151 sage: expected_S = Cone([ proj_S*g for g in K ], K.lattice())
152 sage: S.is_equivalent(expected_S)
153 True
154 """
155 # The lines() method only returns one generator per line. For a true
156 # line, we also need a generator pointing in the opposite direction.
157 S_gens = [ direction*gen for direction in [1,-1] for gen in K.lines() ]
158 S = Cone(S_gens, K.lattice())
159
160 # Since ``S`` is a subspace, the rays of its dual generate its
161 # orthogonal complement.
162 S_perp = Cone(S.dual(), K.lattice())
163 P = K.intersection(S_perp)
164
165 return (P,S)
166
167
168 def positive_operator_gens(K):
169 r"""
170 Compute generators of the cone of positive operators on this cone.
171
172 OUTPUT:
173
174 A list of `n`-by-``n`` matrices where ``n == K.lattice_dim()``.
175 Each matrix ``P`` in the list should have the property that ``P*x``
176 is an element of ``K`` whenever ``x`` is an element of
177 ``K``. Moreover, any nonnegative linear combination of these
178 matrices shares the same property.
179
180 EXAMPLES:
181
182 The trivial cone in a trivial space has no positive operators::
183
184 sage: K = Cone([], ToricLattice(0))
185 sage: positive_operator_gens(K)
186 []
187
188 Positive operators on the nonnegative orthant are nonnegative matrices::
189
190 sage: K = Cone([(1,)])
191 sage: positive_operator_gens(K)
192 [[1]]
193
194 sage: K = Cone([(1,0),(0,1)])
195 sage: positive_operator_gens(K)
196 [
197 [1 0] [0 1] [0 0] [0 0]
198 [0 0], [0 0], [1 0], [0 1]
199 ]
200
201 Every operator is positive on the ambient vector space::
202
203 sage: K = Cone([(1,),(-1,)])
204 sage: K.is_full_space()
205 True
206 sage: positive_operator_gens(K)
207 [[1], [-1]]
208
209 sage: K = Cone([(1,0),(-1,0),(0,1),(0,-1)])
210 sage: K.is_full_space()
211 True
212 sage: positive_operator_gens(K)
213 [
214 [1 0] [-1 0] [0 1] [ 0 -1] [0 0] [ 0 0] [0 0] [ 0 0]
215 [0 0], [ 0 0], [0 0], [ 0 0], [1 0], [-1 0], [0 1], [ 0 -1]
216 ]
217
218 TESTS:
219
220 Each positive operator generator should send the generators of the
221 cone into the cone::
222
223 sage: set_random_seed()
224 sage: K = random_cone(max_ambient_dim=5)
225 sage: pi_of_K = positive_operator_gens(K)
226 sage: all([ K.contains(P*x) for P in pi_of_K for x in K ])
227 True
228
229 Each positive operator generator should send a random element of the
230 cone into the cone::
231
232 sage: set_random_seed()
233 sage: K = random_cone(max_ambient_dim=5)
234 sage: pi_of_K = positive_operator_gens(K)
235 sage: all([ K.contains(P*K.random_element()) for P in pi_of_K ])
236 True
237
238 A random element of the positive operator cone should send the
239 generators of the cone into the cone::
240
241 sage: set_random_seed()
242 sage: K = random_cone(max_ambient_dim=5)
243 sage: pi_of_K = positive_operator_gens(K)
244 sage: L = ToricLattice(K.lattice_dim()**2)
245 sage: pi_cone = Cone([ g.list() for g in pi_of_K ], lattice=L)
246 sage: P = matrix(K.lattice_dim(), pi_cone.random_element().list())
247 sage: all([ K.contains(P*x) for x in K ])
248 True
249
250 A random element of the positive operator cone should send a random
251 element of the cone into the cone::
252
253 sage: set_random_seed()
254 sage: K = random_cone(max_ambient_dim=5)
255 sage: pi_of_K = positive_operator_gens(K)
256 sage: L = ToricLattice(K.lattice_dim()**2)
257 sage: pi_cone = Cone([ g.list() for g in pi_of_K ], lattice=L)
258 sage: P = matrix(K.lattice_dim(), pi_cone.random_element().list())
259 sage: K.contains(P*K.random_element())
260 True
261
262 The dimension of the cone of positive operators is given by the
263 corollary in my paper::
264
265 sage: set_random_seed()
266 sage: K = random_cone(max_ambient_dim=5)
267 sage: n = K.lattice_dim()
268 sage: m = K.dim()
269 sage: l = K.lineality()
270 sage: pi_of_K = positive_operator_gens(K)
271 sage: L = ToricLattice(n**2)
272 sage: actual = Cone([p.list() for p in pi_of_K], lattice=L).dim()
273 sage: expected = n**2 - l*(m - l) - (n - m)*m
274 sage: actual == expected
275 True
276
277 The lineality of the cone of positive operators is given by the
278 corollary in my paper::
279
280 sage: set_random_seed()
281 sage: K = random_cone(max_ambient_dim=5)
282 sage: n = K.lattice_dim()
283 sage: pi_of_K = positive_operator_gens(K)
284 sage: L = ToricLattice(n**2)
285 sage: actual = Cone([p.list() for p in pi_of_K], lattice=L).lineality()
286 sage: expected = n**2 - K.dim()*K.dual().dim()
287 sage: actual == expected
288 True
289
290 The cone ``K`` is proper if and only if the cone of positive
291 operators on ``K`` is proper::
292
293 sage: set_random_seed()
294 sage: K = random_cone(max_ambient_dim=5)
295 sage: pi_of_K = positive_operator_gens(K)
296 sage: L = ToricLattice(K.lattice_dim()**2)
297 sage: pi_cone = Cone([p.list() for p in pi_of_K], lattice=L)
298 sage: K.is_proper() == pi_cone.is_proper()
299 True
300 """
301 # Matrices are not vectors in Sage, so we have to convert them
302 # to vectors explicitly before we can find a basis. We need these
303 # two values to construct the appropriate "long vector" space.
304 F = K.lattice().base_field()
305 n = K.lattice_dim()
306
307 tensor_products = [ s.tensor_product(x) for x in K for s in K.dual() ]
308
309 # Convert those tensor products to long vectors.
310 W = VectorSpace(F, n**2)
311 vectors = [ W(tp.list()) for tp in tensor_products ]
312
313 # Create the *dual* cone of the positive operators, expressed as
314 # long vectors..
315 pi_dual = Cone(vectors, ToricLattice(W.dimension()))
316
317 # Now compute the desired cone from its dual...
318 pi_cone = pi_dual.dual()
319
320 # And finally convert its rays back to matrix representations.
321 M = MatrixSpace(F, n)
322 return [ M(v.list()) for v in pi_cone.rays() ]
323
324
325 def Z_transformation_gens(K):
326 r"""
327 Compute generators of the cone of Z-transformations on this cone.
328
329 OUTPUT:
330
331 A list of `n`-by-``n`` matrices where ``n == K.lattice_dim()``.
332 Each matrix ``L`` in the list should have the property that
333 ``(L*x).inner_product(s) <= 0`` whenever ``(x,s)`` is an element the
334 discrete complementarity set of ``K``. Moreover, any nonnegative
335 linear combination of these matrices shares the same property.
336
337 EXAMPLES:
338
339 Z-transformations on the nonnegative orthant are just Z-matrices.
340 That is, matrices whose off-diagonal elements are nonnegative::
341
342 sage: K = Cone([(1,0),(0,1)])
343 sage: Z_transformation_gens(K)
344 [
345 [ 0 -1] [ 0 0] [-1 0] [1 0] [ 0 0] [0 0]
346 [ 0 0], [-1 0], [ 0 0], [0 0], [ 0 -1], [0 1]
347 ]
348 sage: K = Cone([(1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)])
349 sage: all([ z[i][j] <= 0 for z in Z_transformation_gens(K)
350 ....: for i in range(z.nrows())
351 ....: for j in range(z.ncols())
352 ....: if i != j ])
353 True
354
355 The trivial cone in a trivial space has no Z-transformations::
356
357 sage: K = Cone([], ToricLattice(0))
358 sage: Z_transformation_gens(K)
359 []
360
361 Z-transformations on a subspace are Lyapunov-like and vice-versa::
362
363 sage: K = Cone([(1,0),(-1,0),(0,1),(0,-1)])
364 sage: K.is_full_space()
365 True
366 sage: lls = span([ vector(l.list()) for l in K.lyapunov_like_basis() ])
367 sage: zs = span([ vector(z.list()) for z in Z_transformation_gens(K) ])
368 sage: zs == lls
369 True
370
371 TESTS:
372
373 The Z-property is possessed by every Z-transformation::
374
375 sage: set_random_seed()
376 sage: K = random_cone(max_ambient_dim=6)
377 sage: Z_of_K = Z_transformation_gens(K)
378 sage: dcs = K.discrete_complementarity_set()
379 sage: all([(z*x).inner_product(s) <= 0 for z in Z_of_K
380 ....: for (x,s) in dcs])
381 True
382
383 The lineality space of Z is LL::
384
385 sage: set_random_seed()
386 sage: K = random_cone(min_ambient_dim=1, max_ambient_dim=6)
387 sage: lls = span([ vector(l.list()) for l in K.lyapunov_like_basis() ])
388 sage: z_cone = Cone([ z.list() for z in Z_transformation_gens(K) ])
389 sage: z_cone.linear_subspace() == lls
390 True
391
392 And thus, the lineality of Z is the Lyapunov rank::
393
394 sage: set_random_seed()
395 sage: K = random_cone(max_ambient_dim=6)
396 sage: Z_of_K = Z_transformation_gens(K)
397 sage: L = ToricLattice(K.lattice_dim()**2)
398 sage: z_cone = Cone([ z.list() for z in Z_of_K ], lattice=L)
399 sage: z_cone.lineality() == K.lyapunov_rank()
400 True
401
402 The lineality spaces of pi-star and Z-star are equal:
403
404 sage: set_random_seed()
405 sage: K = random_cone(max_ambient_dim=5)
406 sage: pi_of_K = positive_operator_gens(K)
407 sage: Z_of_K = Z_transformation_gens(K)
408 sage: L = ToricLattice(K.lattice_dim()**2)
409 sage: pi_star = Cone([p.list() for p in pi_of_K], lattice=L).dual()
410 sage: z_star = Cone([ z.list() for z in Z_of_K], lattice=L).dual()
411 sage: pi_star.linear_subspace() == z_star.linear_subspace()
412 True
413 """
414 # Matrices are not vectors in Sage, so we have to convert them
415 # to vectors explicitly before we can find a basis. We need these
416 # two values to construct the appropriate "long vector" space.
417 F = K.lattice().base_field()
418 n = K.lattice_dim()
419
420 # These tensor products contain generators for the dual cone of
421 # the cross-positive transformations.
422 tensor_products = [ s.tensor_product(x)
423 for (x,s) in K.discrete_complementarity_set() ]
424
425 # Turn our matrices into long vectors...
426 W = VectorSpace(F, n**2)
427 vectors = [ W(m.list()) for m in tensor_products ]
428
429 # Create the *dual* cone of the cross-positive operators,
430 # expressed as long vectors..
431 Sigma_dual = Cone(vectors, lattice=ToricLattice(W.dimension()))
432
433 # Now compute the desired cone from its dual...
434 Sigma_cone = Sigma_dual.dual()
435
436 # And finally convert its rays back to matrix representations.
437 # But first, make them negative, so we get Z-transformations and
438 # not cross-positive ones.
439 M = MatrixSpace(F, n)
440 return [ -M(v.list()) for v in Sigma_cone.rays() ]
441
442
443 def Z_cone(K):
444 gens = Z_transformation_gens(K)
445 L = None
446 if len(gens) == 0:
447 L = ToricLattice(0)
448 return Cone([ g.list() for g in gens ], lattice=L)
449
450 def pi_cone(K):
451 gens = positive_operator_gens(K)
452 L = None
453 if len(gens) == 0:
454 L = ToricLattice(0)
455 return Cone([ g.list() for g in gens ], lattice=L)