]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/cone/cone.py
3a1e190cb2ebe41f57810f04726f8123294c55cd
1 # Sage doesn't load ~/.sage/init.sage during testing (sage -t), so we
2 # have to explicitly mangle our sitedir here so that "mjo.cone"
4 from os
.path
import abspath
5 from site
import addsitedir
6 addsitedir(abspath('../../'))
11 def project_span(K
, K2
= None):
13 Return a "copy" of ``K`` embeded in a lower-dimensional space.
15 By default, we will project ``K`` into the subspace spanned by its
16 rays. However, if ``K2`` is not ``None``, we will project into the
17 space spanned by the rays of ``K2`` instead.
21 sage: K = Cone([(1,0,0), (0,1,0)])
23 2-d cone in 2-d lattice N
24 sage: project_span(K).rays()
29 sage: K = Cone([(1,0,0), (0,1,0)])
30 sage: K2 = Cone([(0,1)])
31 sage: project_span(K, K2).rays()
36 # Allow us to use a second cone to generate the subspace into
37 # which we're "projecting."
41 # Use these to generate the new cone.
42 cs1
= K
.rays().matrix().columns()
44 # And use these to figure out which indices to drop.
45 cs2
= K2
.rays().matrix().columns()
49 for idx
in range(0, len(cs2
)):
50 if cs2
[idx
].is_zero():
53 solid_cols
= [ cs1
[idx
] for idx
in range(0,len(cs1
))
54 if not idx
in perp_idxs
55 and not idx
>= len(cs2
) ]
57 m
= matrix(solid_cols
)
58 L
= ToricLattice(len(m
.rows()))
59 J
= Cone(m
.transpose(), lattice
=L
)
63 def discrete_complementarity_set(K
):
65 Compute the discrete complementarity set of this cone.
67 The complementarity set of this cone is the set of all orthogonal
68 pairs `(x,s)` such that `x` is in this cone, and `s` is in its
69 dual. The discrete complementarity set restricts `x` and `s` to be
70 generators of their respective cones.
74 A list of pairs `(x,s)` such that,
76 * `x` is in this cone.
77 * `x` is a generator of this cone.
78 * `s` is in this cone's dual.
79 * `s` is a generator of this cone's dual.
80 * `x` and `s` are orthogonal.
84 The discrete complementarity set of the nonnegative orthant consists
85 of pairs of standard basis vectors::
87 sage: K = Cone([(1,0),(0,1)])
88 sage: discrete_complementarity_set(K)
89 [((1, 0), (0, 1)), ((0, 1), (1, 0))]
91 If the cone consists of a single ray, the second components of the
92 discrete complementarity set should generate the orthogonal
93 complement of that ray::
95 sage: K = Cone([(1,0)])
96 sage: discrete_complementarity_set(K)
97 [((1, 0), (0, 1)), ((1, 0), (0, -1))]
98 sage: K = Cone([(1,0,0)])
99 sage: discrete_complementarity_set(K)
100 [((1, 0, 0), (0, 1, 0)),
101 ((1, 0, 0), (0, -1, 0)),
102 ((1, 0, 0), (0, 0, 1)),
103 ((1, 0, 0), (0, 0, -1))]
105 When the cone is the entire space, its dual is the trivial cone, so
106 the discrete complementarity set is empty::
108 sage: K = Cone([(1,0),(-1,0),(0,1),(0,-1)])
109 sage: discrete_complementarity_set(K)
114 The complementarity set of the dual can be obtained by switching the
115 components of the complementarity set of the original cone::
117 sage: K1 = random_cone(max_dim=10, max_rays=10)
119 sage: expected = [(x,s) for (s,x) in discrete_complementarity_set(K2)]
120 sage: actual = discrete_complementarity_set(K1)
121 sage: actual == expected
125 V
= K
.lattice().vector_space()
127 # Convert the rays to vectors so that we can compute inner
129 xs
= [V(x
) for x
in K
.rays()]
130 ss
= [V(s
) for s
in K
.dual().rays()]
132 return [(x
,s
) for x
in xs
for s
in ss
if x
.inner_product(s
) == 0]
137 Compute the space `\mathbf{LL}` of all Lyapunov-like transformations
142 A list of matrices forming a basis for the space of all
143 Lyapunov-like transformations on the given cone.
147 The trivial cone has no Lyapunov-like transformations::
149 sage: L = ToricLattice(0)
150 sage: K = Cone([], lattice=L)
154 The Lyapunov-like transformations on the nonnegative orthant are
155 simply diagonal matrices::
157 sage: K = Cone([(1,)])
161 sage: K = Cone([(1,0),(0,1)])
168 sage: K = Cone([(1,0,0),(0,1,0),(0,0,1)])
171 [1 0 0] [0 0 0] [0 0 0]
172 [0 0 0] [0 1 0] [0 0 0]
173 [0 0 0], [0 0 0], [0 0 1]
176 Only the identity matrix is Lyapunov-like on the `L^{3}_{1}` and
177 `L^{3}_{\infty}` cones [Rudolf et al.]_::
179 sage: L31 = Cone([(1,0,1), (0,-1,1), (-1,0,1), (0,1,1)])
187 sage: L3infty = Cone([(0,1,1), (1,0,1), (0,-1,1), (-1,0,1)])
197 The inner product `\left< L\left(x\right), s \right>` is zero for
198 every pair `\left( x,s \right)` in the discrete complementarity set
201 sage: K = random_cone(max_dim=8, max_rays=10)
202 sage: C_of_K = discrete_complementarity_set(K)
203 sage: l = [ (L*x).inner_product(s) for (x,s) in C_of_K for L in LL(K) ]
204 sage: sum(map(abs, l))
207 Try the formula in my paper::
209 sage: K = random_cone(max_dim=15, max_rays=25)
210 sage: actual = lyapunov_rank(K)
211 sage: K_S = project_span(K)
212 sage: J_T1 = project_span(K, K_S.dual())
213 sage: J_T2 = project_span(K_S.dual()).dual()
214 sage: J_T2 = Cone(J_T2.rays(), lattice=J_T1.lattice())
218 sage: l = K.linear_subspace().dimension()
219 sage: codim = K.lattice_dim() - K.dim()
220 sage: expected = lyapunov_rank(J_T) + K.dim()*(l + codim) + codim**2
221 sage: actual == expected
225 V
= K
.lattice().vector_space()
227 C_of_K
= discrete_complementarity_set(K
)
229 tensor_products
= [s
.tensor_product(x
) for (x
,s
) in C_of_K
]
231 # Sage doesn't think matrices are vectors, so we have to convert
232 # our matrices to vectors explicitly before we can figure out how
233 # many are linearly-indepenedent.
235 # The space W has the same base ring as V, but dimension
236 # dim(V)^2. So it has the same dimension as the space of linear
237 # transformations on V. In other words, it's just the right size
238 # to create an isomorphism between it and our matrices.
239 W
= VectorSpace(V
.base_ring(), V
.dimension()**2)
241 # Turn our matrices into long vectors...
242 vectors
= [ W(m
.list()) for m
in tensor_products
]
244 # Vector space representation of Lyapunov-like matrices
245 # (i.e. vec(L) where L is Luapunov-like).
246 LL_vector
= W
.span(vectors
).complement()
248 # Now construct an ambient MatrixSpace in which to stick our
250 M
= MatrixSpace(V
.base_ring(), V
.dimension())
252 matrix_basis
= [ M(v
.list()) for v
in LL_vector
.basis() ]
258 def lyapunov_rank(K
):
260 Compute the Lyapunov (or bilinearity) rank of this cone.
262 The Lyapunov rank of a cone can be thought of in (mainly) two ways:
264 1. The dimension of the Lie algebra of the automorphism group of the
267 2. The dimension of the linear space of all Lyapunov-like
268 transformations on the cone.
272 A closed, convex polyhedral cone.
276 An integer representing the Lyapunov rank of the cone. If the
277 dimension of the ambient vector space is `n`, then the Lyapunov rank
278 will be between `1` and `n` inclusive; however a rank of `n-1` is
279 not possible (see the first reference).
283 In the references, the cones are always assumed to be proper. We
284 do not impose this restriction.
292 The codimension formula from the second reference is used. We find
293 all pairs `(x,s)` in the complementarity set of `K` such that `x`
294 and `s` are rays of our cone. It is known that these vectors are
295 sufficient to apply the codimension formula. Once we have all such
296 pairs, we "brute force" the codimension formula by finding all
297 linearly-independent `xs^{T}`.
301 .. [Gowda/Tao] M.S. Gowda and J. Tao. On the bilinearity rank of a proper
302 cone and Lyapunov-like transformations, Mathematical Programming, 147
305 .. [Rudolf et al.] G. Rudolf, N. Noyan, D. Papp, and F. Alizadeh, Bilinear
306 optimality constraints for the cone of positive polynomials,
307 Mathematical Programming, Series B, 129 (2011) 5-31.
311 The nonnegative orthant in `\mathbb{R}^{n}` always has rank `n`
314 sage: positives = Cone([(1,)])
315 sage: lyapunov_rank(positives)
317 sage: quadrant = Cone([(1,0), (0,1)])
318 sage: lyapunov_rank(quadrant)
320 sage: octant = Cone([(1,0,0), (0,1,0), (0,0,1)])
321 sage: lyapunov_rank(octant)
324 The `L^{3}_{1}` cone is known to have a Lyapunov rank of one
327 sage: L31 = Cone([(1,0,1), (0,-1,1), (-1,0,1), (0,1,1)])
328 sage: lyapunov_rank(L31)
331 Likewise for the `L^{3}_{\infty}` cone [Rudolf et al.]_::
333 sage: L3infty = Cone([(0,1,1), (1,0,1), (0,-1,1), (-1,0,1)])
334 sage: lyapunov_rank(L3infty)
337 The Lyapunov rank should be additive on a product of cones
340 sage: L31 = Cone([(1,0,1), (0,-1,1), (-1,0,1), (0,1,1)])
341 sage: octant = Cone([(1,0,0), (0,1,0), (0,0,1)])
342 sage: K = L31.cartesian_product(octant)
343 sage: lyapunov_rank(K) == lyapunov_rank(L31) + lyapunov_rank(octant)
346 Two isomorphic cones should have the same Lyapunov rank [Rudolf et al.]_.
347 The cone ``K`` in the following example is isomorphic to the nonnegative
348 octant in `\mathbb{R}^{3}`::
350 sage: K = Cone([(1,2,3), (-1,1,0), (1,0,6)])
351 sage: lyapunov_rank(K)
354 The dual cone `K^{*}` of ``K`` should have the same Lyapunov rank as ``K``
355 itself [Rudolf et al.]_::
357 sage: K = Cone([(2,2,4), (-1,9,0), (2,0,6)])
358 sage: lyapunov_rank(K) == lyapunov_rank(K.dual())
363 The Lyapunov rank should be additive on a product of cones
366 sage: K1 = random_cone(max_dim=10, max_rays=10)
367 sage: K2 = random_cone(max_dim=10, max_rays=10)
368 sage: K = K1.cartesian_product(K2)
369 sage: lyapunov_rank(K) == lyapunov_rank(K1) + lyapunov_rank(K2)
372 The dual cone `K^{*}` of ``K`` should have the same Lyapunov rank as ``K``
373 itself [Rudolf et al.]_::
375 sage: K = random_cone(max_dim=10, max_rays=10)
376 sage: lyapunov_rank(K) == lyapunov_rank(K.dual())
379 The Lyapunov rank of a proper polyhedral cone in `n` dimensions can
380 be any number between `1` and `n` inclusive, excluding `n-1`
381 [Gowda/Tao]_. By accident, the `n-1` restriction will hold for the
382 trivial cone in a trivial space as well. However, in zero dimensions,
383 the Lyapunov rank of the trivial cone will be zero::
385 sage: K = random_cone(max_dim=10, strictly_convex=True, solid=True)
386 sage: b = lyapunov_rank(K)
387 sage: n = K.lattice_dim()
388 sage: (n == 0 or 1 <= b) and b <= n