# return ``0`` when ``K`` has no rays.
v = V(sum(scaled_gens))
return v
+
+
+def positive_operators(K):
+ r"""
+ Compute generators of the cone of positive operators on this cone.
+
+ OUTPUT:
+
+ A list of `n`-by-``n`` matrices where ``n == K.lattice_dim()``.
+ Each matrix ``P`` in the list should have the property that ``P*x``
+ is an element of ``K`` whenever ``x`` is an element of
+ ``K``. Moreover, any nonnegative linear combination of these
+ matrices shares the same property.
+
+ EXAMPLES:
+
+ The trivial cone in a trivial space has no positive operators::
+
+ sage: K = Cone([], ToricLattice(0))
+ sage: positive_operators(K)
+ []
+
+ Positive operators on the nonnegative orthant are nonnegative matrices::
+
+ sage: K = Cone([(1,)])
+ sage: positive_operators(K)
+ [[1]]
+
+ sage: K = Cone([(1,0),(0,1)])
+ sage: positive_operators(K)
+ [
+ [1 0] [0 1] [0 0] [0 0]
+ [0 0], [0 0], [1 0], [0 1]
+ ]
+
+ Every operator is positive on the ambient vector space::
+
+ sage: K = Cone([(1,),(-1,)])
+ sage: K.is_full_space()
+ True
+ sage: positive_operators(K)
+ [[1], [-1]]
+
+ sage: K = Cone([(1,0),(-1,0),(0,1),(0,-1)])
+ sage: K.is_full_space()
+ True
+ sage: positive_operators(K)
+ [
+ [1 0] [-1 0] [0 1] [ 0 -1] [0 0] [ 0 0] [0 0] [ 0 0]
+ [0 0], [ 0 0], [0 0], [ 0 0], [1 0], [-1 0], [0 1], [ 0 -1]
+ ]
+
+ TESTS:
+
+ A positive operator on a cone should send its generators into the cone::
+
+ sage: K = random_cone(max_ambient_dim = 6)
+ sage: pi_of_k = positive_operators(K)
+ sage: all([K.contains(p*x) for p in pi_of_k for x in K.rays()])
+ True
+
+ """
+ V = K.lattice().vector_space()
+
+ # Sage doesn't think matrices are vectors, so we have to convert
+ # our matrices to vectors explicitly before we can figure out how
+ # many are linearly-indepenedent.
+ #
+ # The space W has the same base ring as V, but dimension
+ # dim(V)^2. So it has the same dimension as the space of linear
+ # transformations on V. In other words, it's just the right size
+ # to create an isomorphism between it and our matrices.
+ W = VectorSpace(V.base_ring(), V.dimension()**2)
+
+ G1 = [ V(x) for x in K.rays() ]
+ G2 = [ V(s) for s in K.dual().rays() ]
+
+ tensor_products = [ s.tensor_product(x) for x in G1 for s in G2 ]
+
+ # Turn our matrices into long vectors...
+ vectors = [ W(m.list()) for m in tensor_products ]
+
+ # Create the *dual* cone of the positive operators, expressed as
+ # long vectors..
+ L = ToricLattice(W.dimension())
+ pi_dual = Cone(vectors, lattice=L)
+
+ # Now compute the desired cone from its dual...
+ pi_cone = pi_dual.dual()
+
+ # And finally convert its rays back to matrix representations.
+ M = MatrixSpace(V.base_ring(), V.dimension())
+
+ return [ M(v.list()) for v in pi_cone.rays() ]