]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/cone/rearrangement.py
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('../../'))
9 from mjo
.cone
.cone
import random_element
11 def rearrangement_cone(p
,n
):
13 Return the rearrangement cone of order ``p`` in ``n`` dimensions.
15 The rearrangement cone in ``n`` dimensions has as its elements
16 vectors of length ``n``. For inclusion in the cone, the smallest
17 ``p`` components of a vector must sum to a nonnegative number.
19 For example, the rearrangement cone of order ``p == 1`` has its
20 single smallest component nonnegative. This implies that all
21 components are nonnegative, and that therefore the rearrangement
22 cone of order one is the nonnegative orthant.
24 When ``p == n``, the sum of all components of a vector must be
25 nonnegative for inclusion in the cone. That is, the cone is a
26 half-space in ``n`` dimensions.
30 - ``p`` -- The number of components to "rearrange."
32 - ``n`` -- The dimension of the ambient space for the resulting cone.
36 A polyhedral closed convex cone object representing a rearrangement
37 cone of order ``p`` in ``n`` dimensions.
41 The rearrangement cones of order one are nonnegative orthants::
43 sage: rearrangement_cone(1,1) == Cone([(1,)])
45 sage: rearrangement_cone(1,2) == Cone([(0,1),(1,0)])
47 sage: rearrangement_cone(1,3) == Cone([(0,0,1),(0,1,0),(1,0,0)])
50 When ``p == n``, the resulting cone will be a half-space, so we
51 expect its lineality to be one less than ``n`` because it will
52 contain a hyperplane but is not the entire space::
54 sage: rearrangement_cone(5,5).lineality()
57 All rearrangement cones are proper::
59 sage: all([ rearrangement_cone(p,n).is_proper()
60 ....: for n in range(10)
61 ....: for p in range(n) ])
64 The Lyapunov rank of the rearrangement cone of order ``p`` in ``n``
65 dimensions is ``n`` for ``p == 1`` or ``p == n`` and one otherwise::
67 sage: all([ rearrangement_cone(p,n).lyapunov_rank() == n
68 ....: for n in range(2, 10)
69 ....: for p in [1, n-1] ])
71 sage: all([ rearrangement_cone(p,n).lyapunov_rank() == 1
72 ....: for n in range(3, 10)
73 ....: for p in range(2, n-1) ])
78 The rearrangement cone is permutation-invariant::
80 sage: n = ZZ.random_element(2,10).abs()
81 sage: p = ZZ.random_element(1,n)
82 sage: K = rearrangement_cone(p,n)
83 sage: P = SymmetricGroup(n).random_element().matrix()
84 sage: all([ K.contains(P*r) for r in K.rays() ])
90 v
= [1]*n
# Create the list of all ones...
91 v
[j
] = 1 - p
# Now "fix" the ``j``th entry.
94 V
= VectorSpace(QQ
, n
)
95 G
= V
.basis() + [ d(j
) for j
in range(n
) ]
99 def has_rearrangement_property(v
, p
):
101 Test if the vector ``v`` has the "rearrangement property."
103 The rearrangement cone of order ``p`` in `n` dimensions has its
104 members vectors of length `n`. The "rearrangement property,"
105 satisfied by its elements, is to have its smallest ``p`` components
106 sum to a nonnegative number.
108 We believe that we have a description of the extreme vectors of the
109 rearrangement cone: see ``rearrangement_cone()``. This function is
110 used to test that conic combinations of those extreme vectors are in
111 fact elements of the rearrangement cone. We can't test all conic
112 combinations, obviously, but we can test a random one.
114 To become more sure of the result, generate a bunch of vectors with
115 ``random_element()`` and test them with this function.
119 - ``v`` -- An element of a cone suspected of being the rearrangement
122 - ``p`` -- The suspected order of the rearrangement cone.
126 If ``v`` has the rearrangement property (that is, if its smallest ``p``
127 components sum to a nonnegative number), ``True`` is returned. Otherwise
128 ``False`` is returned.
132 Every element of a rearrangement cone should have the property::
134 sage: for n in range(2,10):
135 ....: for p in range(1, n-1):
136 ....: K = rearrangement_cone(p,n)
137 ....: v = random_element(K)
138 ....: if not has_rearrangement_property(v,p): print v
141 components
= sorted(v
)[0:p
]
142 return sum(components
) >= 0