--- /dev/null
+# 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 *
+
+def random_permutation_invariant_cone(lattice=None,
+ min_ambient_dim=0,
+ max_ambient_dim=None,
+ min_rays=0,
+ max_rays=None,
+ strictly_convex=None,
+ solid=None):
+ r"""
+ Return a random permutation-invariant cone.
+
+ A cone ``K`` is said to be permutation-invariant if ``P(K) == K``
+ for all permutation matrices ``P``. To generate one, we can begin
+ with any cone, and construct the set of all permutations of its
+ generators. The resulting set will generate a permutation-invariant
+ cone by construction.
+
+ All of this function's parameters are passed to ``random_cone()``
+ function used to generate the initial cone whose generators we will
+ permute.
+ """
+ K = random_cone(lattice=lattice,
+ min_ambient_dim=min_ambient_dim,
+ max_ambient_dim=max_ambient_dim,
+ min_rays=min_rays,
+ max_rays=max_rays,
+ strictly_convex=strictly_convex,
+ solid=solid)
+
+ # We'll need this to turn rays into vectors so that we can permute
+ # them with permutation matrices.
+ V = K.lattice().vector_space()
+
+ # The set of all permutatin matrices on ``V``.
+ Sn = [ p.matrix() for p in SymmetricGroup(V.dimension()) ]
+
+ # Set of permuted generators
+ pgens = []
+
+ for g in K.rays():
+ for permutation in Sn:
+ pgens.append(permutation * V(g))
+
+ L = ToricLattice(V.dimension())
+ return Cone(pgens, lattice=L)
+
+
+def is_permutation_invariant(K):
+ r"""
+ Determine if ``K`` is permutation-invariant.
+
+ A cone is said to be permutation-invariant if ``P(K)`` is a subset
+ of ``K`` for every permutation matrix ``P``.
+
+ EXAMPLES:
+
+ sage: all([ is_permutation_invariant(rearrangement_cone(p,n))
+ ....: for n in range(3, 6)
+ ....: for p in range(1, n) ])
+ True
+
+ """
+ # We'll need this to turn rays into vectors so that we can permute
+ # them with permutation matrices.
+ V = K.lattice().vector_space()
+
+ # The set of all permutation matrices on ``V``.
+ Sn = [ p.matrix() for p in SymmetricGroup(V.dimension()) ]
+
+ for permutation in Sn:
+ L = ToricLattice(V.dimension())
+ permuted_gens = [ permutation * V(g) for g in K.rays() ]
+ for g in permuted_gens:
+ if not K.contains(g):
+ return False
+
+ return True