]> gitweb.michael.orlitzky.com - sage.d.git/blobdiff - mjo/cone/rearrangement.py
cone/permutation_invariant.py: use xrange in two places.
[sage.d.git] / mjo / cone / rearrangement.py
index 5da9b284ab66d7ca816167e2729a31eabbc22700..b2ccfcb58f8aa58b52a8d724ca42725593ed7463 100644 (file)
@@ -1,12 +1,4 @@
-# Sage doesn't load ~/.sage/init.sage during testing (sage -t), so we
-# have to explicitly mangle our sitedir here so that "mjo.cone"
-# resolves.
-from os.path import abspath
-from site import addsitedir
-addsitedir(abspath('../../'))
-
 from sage.all import *
-from mjo.cone.cone import lyapunov_rank
 
 def rearrangement_cone(p,n):
     r"""
@@ -36,6 +28,10 @@ def rearrangement_cone(p,n):
     A polyhedral closed convex cone object representing a rearrangement
     cone of order ``p`` in ``n`` dimensions.
 
+    SETUP::
+
+        sage: from mjo.cone.rearrangement import rearrangement_cone
+
     EXAMPLES:
 
     The rearrangement cones of order one are nonnegative orthants::
@@ -56,21 +52,21 @@ def rearrangement_cone(p,n):
 
     All rearrangement cones are proper::
 
-        sage: all([ rearrangement_cone(p,n).is_proper()
-        ....:               for n in range(10)
-        ....:               for p in range(n) ])
+        sage: all( rearrangement_cone(p,n).is_proper()
+        ....:              for n in range(10)
+        ....:              for p in range(n) )
         True
 
     The Lyapunov rank of the rearrangement cone of order ``p`` in ``n``
     dimensions is ``n`` for ``p == 1`` or ``p == n`` and one otherwise::
 
-        sage: all([ lyapunov_rank(rearrangement_cone(p,n)) == n
-        ....:               for n in range(2, 10)
-        ....:               for p in [1, n-1] ])
+        sage: all( rearrangement_cone(p,n).lyapunov_rank() == n
+        ....:              for n in range(2, 10)
+        ....:              for p in [1, n-1] )
         True
-        sage: all([ lyapunov_rank(rearrangement_cone(p,n)) == 1
-        ....:               for n in range(3, 10)
-        ....:               for p in range(2, n-1) ])
+        sage: all( rearrangement_cone(p,n).lyapunov_rank() == 1
+        ....:              for n in range(3, 10)
+        ....:              for p in range(2, n-1) )
         True
 
     TESTS:
@@ -81,7 +77,7 @@ def rearrangement_cone(p,n):
         sage: p = ZZ.random_element(1,n)
         sage: K = rearrangement_cone(p,n)
         sage: P = SymmetricGroup(n).random_element().matrix()
-        sage: all([ K.contains(P*r) for r in K.rays() ])
+        sage: all( K.contains(P*r) for r in K )
         True
 
     """
@@ -94,3 +90,58 @@ def rearrangement_cone(p,n):
     V = VectorSpace(QQ, n)
     G = V.basis() + [ d(j) for j in range(n) ]
     return Cone(G)
+
+
+def has_rearrangement_property(v, p):
+    r"""
+    Test if the vector ``v`` has the "rearrangement property."
+
+    The rearrangement cone of order ``p`` in `n` dimensions has its
+    members vectors of length `n`. The "rearrangement property,"
+    satisfied by its elements, is to have its smallest ``p`` components
+    sum to a nonnegative number.
+
+    We believe that we have a description of the extreme vectors of the
+    rearrangement cone: see ``rearrangement_cone()``. This function is
+    used to test that conic combinations of those extreme vectors are in
+    fact elements of the rearrangement cone. We can't test all conic
+    combinations, obviously, but we can test a random one.
+
+    To become more sure of the result, generate a bunch of vectors with
+    ``random_element()`` and test them with this function.
+
+    INPUT:
+
+    - ``v`` -- An element of a cone suspected of being the rearrangement
+               cone of order ``p``.
+
+    - ``p`` -- The suspected order of the rearrangement cone.
+
+    OUTPUT:
+
+    If ``v`` has the rearrangement property (that is, if its smallest ``p``
+    components sum to a nonnegative number), ``True`` is returned. Otherwise
+    ``False`` is returned.
+
+    SETUP::
+
+        sage: from mjo.cone.rearrangement import (has_rearrangement_property,
+        ....:                                     rearrangement_cone)
+
+    EXAMPLES:
+
+    Every element of a rearrangement cone should have the property::
+
+        sage: set_random_seed()
+        sage: all( has_rearrangement_property(
+        ....:        rearrangement_cone(p,n).random_element(),
+        ....:        p
+        ....:      )
+        ....:      for n in range(2, 10)
+        ....:      for p in range(1, n-1)
+        ....: )
+        True
+
+    """
+    components = sorted(v)[0:p]
+    return sum(components) >= 0