+ In fact [Orlitzky/Gowda]_, no closed convex polyhedral cone can have
+ Lyapunov rank `n-1` in `n` dimensions::
+
+ sage: set_random_seed()
+ sage: K = random_cone(max_ambient_dim=8)
+ sage: b = lyapunov_rank(K)
+ sage: n = K.lattice_dim()
+ sage: b == n-1
+ False
+
+ The calculation of the Lyapunov rank of an improper cone can be
+ reduced to that of a proper cone [Orlitzky/Gowda]_::
+
+ sage: set_random_seed()
+ sage: K = random_cone(max_ambient_dim=8)
+ sage: actual = lyapunov_rank(K)
+ sage: K_S = _restrict_to_space(K, K.span())
+ sage: K_SP = _restrict_to_space(K_S.dual(), K_S.dual().span()).dual()
+ sage: l = K.lineality()
+ sage: c = K.codim()
+ sage: expected = lyapunov_rank(K_SP) + K.dim()*(l + c) + c**2
+ sage: actual == expected
+ True
+
+ The Lyapunov rank of any cone is just the dimension of ``LL(K)``::
+
+ sage: set_random_seed()
+ sage: K = random_cone(max_ambient_dim=8)
+ sage: lyapunov_rank(K) == len(LL(K))
+ True
+
+ We can make an imperfect cone perfect by adding a slack variable
+ (a Theorem in [Orlitzky/Gowda]_)::
+
+ sage: set_random_seed()
+ sage: K = random_cone(max_ambient_dim=8,
+ ....: strictly_convex=True,
+ ....: solid=True)
+ sage: L = ToricLattice(K.lattice_dim() + 1)
+ sage: K = Cone([ r.list() + [0] for r in K.rays() ], lattice=L)
+ sage: lyapunov_rank(K) >= K.lattice_dim()
+ True
+
+ """
+ beta = 0
+
+ m = K.dim()
+ n = K.lattice_dim()
+ l = K.lineality()
+
+ if m < n:
+ # K is not solid, restrict to its span.
+ K = _restrict_to_space(K, K.span())
+
+ # Non-solid reduction lemma.
+ beta += (n - m)*n
+
+ if l > 0:
+ # K is not pointed, restrict to the span of its dual. Uses a
+ # proposition from our paper, i.e. this is equivalent to K =
+ # _rho(K.dual()).dual().
+ K = _restrict_to_space(K, K.dual().span())
+
+ # Non-pointed reduction lemma.
+ beta += l * m
+
+ beta += len(LL(K))
+ return beta
+
+
+
+def is_lyapunov_like(L,K):
+ r"""
+ Determine whether or not ``L`` is Lyapunov-like on ``K``.
+
+ We say that ``L`` is Lyapunov-like on ``K`` if `\left\langle
+ L\left\lparenx\right\rparen,s\right\rangle = 0` for all pairs
+ `\left\langle x,s \right\rangle` in the complementarity set of
+ ``K``. It is known [Orlitzky]_ that this property need only be
+ checked for generators of ``K`` and its dual.
+
+ INPUT:
+
+ - ``L`` -- A linear transformation or matrix.
+
+ - ``K`` -- A polyhedral closed convex cone.
+
+ OUTPUT:
+
+ ``True`` if it can be proven that ``L`` is Lyapunov-like on ``K``,
+ and ``False`` otherwise.
+
+ .. WARNING::
+
+ If this function returns ``True``, then ``L`` is Lyapunov-like
+ on ``K``. However, if ``False`` is returned, that could mean one
+ of two things. The first is that ``L`` is definitely not
+ Lyapunov-like on ``K``. The second is more of an "I don't know"
+ answer, returned (for example) if we cannot prove that an inner
+ product is zero.
+
+ REFERENCES:
+
+ .. [Orlitzky] M. Orlitzky. The Lyapunov rank of an
+ improper cone (preprint).
+
+ EXAMPLES:
+
+ The identity is always Lyapunov-like in a nontrivial space::
+
+ sage: set_random_seed()
+ sage: K = random_cone(min_ambient_dim = 1, max_rays = 8)
+ sage: L = identity_matrix(K.lattice_dim())
+ sage: is_lyapunov_like(L,K)
+ True
+
+ As is the "zero" transformation::
+
+ sage: K = random_cone(min_ambient_dim = 1, max_rays = 5)
+ sage: R = K.lattice().vector_space().base_ring()
+ sage: L = zero_matrix(R, K.lattice_dim())
+ sage: is_lyapunov_like(L,K)
+ True
+
+ Everything in ``LL(K)`` should be Lyapunov-like on ``K``::
+
+ sage: K = random_cone(min_ambient_dim = 1, max_rays = 5)
+ sage: all([is_lyapunov_like(L,K) for L in LL(K)])
+ True
+
+ """
+ return all([(L*x).inner_product(s) == 0
+ for (x,s) in discrete_complementarity_set(K)])
+
+
+def random_element(K):
+ r"""
+ Return a random element of ``K`` from its ambient vector space.
+
+ ALGORITHM:
+
+ The cone ``K`` is specified in terms of its generators, so that
+ ``K`` is equal to the convex conic combination of those generators.
+ To choose a random element of ``K``, we assign random nonnegative
+ coefficients to each generator of ``K`` and construct a new vector
+ from the scaled rays.
+
+ A vector, rather than a ray, is returned so that the element may
+ have non-integer coordinates. Thus the element may have an
+ arbitrarily small norm.
+
+ EXAMPLES:
+
+ A random element of the trivial cone is zero::
+
+ sage: set_random_seed()
+ sage: K = Cone([], ToricLattice(0))
+ sage: random_element(K)
+ ()
+ sage: K = Cone([(0,)])
+ sage: random_element(K)
+ (0)
+ sage: K = Cone([(0,0)])
+ sage: random_element(K)
+ (0, 0)
+ sage: K = Cone([(0,0,0)])
+ sage: random_element(K)
+ (0, 0, 0)
+
+ TESTS:
+
+ Any cone should contain an element of itself::
+
+ sage: set_random_seed()
+ sage: K = random_cone(max_rays = 8)
+ sage: K.contains(random_element(K))
+ True
+