+ # Matrices are not vectors in Sage, so we have to convert them
+ # to vectors explicitly before we can find a basis. We need these
+ # two values to construct the appropriate "long vector" space.
+ F = K.lattice().base_field()
+ n = K.lattice_dim()
+
+ tensor_products = [ s.tensor_product(x) for x in K for s in K.dual() ]
+
+ # Convert those tensor products to long vectors.
+ W = VectorSpace(F, n**2)
+ vectors = [ W(tp.list()) for tp in tensor_products ]
+
+ check = True
+ if K.is_solid() or K.is_strictly_convex():
+ # The lineality space of either ``K`` or ``K.dual()`` is
+ # trivial and it's easy to show that our generating set is
+ # minimal. I would love a proof that this works when ``K`` is
+ # neither pointed nor solid.
+ #
+ # Note that in that case we can get *duplicates*, since the
+ # tensor product of (x,s) is the same as that of (-x,-s).
+ check = False
+
+ # Create the dual cone of the positive operators, expressed as
+ # long vectors.
+ pi_dual = Cone(vectors, ToricLattice(W.dimension()), check=check)
+
+ # Now compute the desired cone from its dual...
+ pi_cone = pi_dual.dual()
+
+ # And finally convert its rays back to matrix representations.
+ M = MatrixSpace(F, n)
+ return [ M(v.list()) for v in pi_cone ]
+
+
+def Z_transformation_gens(K):
+ r"""
+ Compute generators of the cone of Z-transformations on this cone.
+
+ OUTPUT:
+
+ A list of `n`-by-``n`` matrices where ``n == K.lattice_dim()``.
+ Each matrix ``L`` in the list should have the property that
+ ``(L*x).inner_product(s) <= 0`` whenever ``(x,s)`` is an element the
+ discrete complementarity set of ``K``. Moreover, any nonnegative
+ linear combination of these matrices shares the same property.
+
+ EXAMPLES:
+
+ Z-transformations on the nonnegative orthant are just Z-matrices.
+ That is, matrices whose off-diagonal elements are nonnegative::
+
+ sage: K = Cone([(1,0),(0,1)])
+ sage: Z_transformation_gens(K)
+ [
+ [ 0 -1] [ 0 0] [-1 0] [1 0] [ 0 0] [0 0]
+ [ 0 0], [-1 0], [ 0 0], [0 0], [ 0 -1], [0 1]
+ ]
+ sage: K = Cone([(1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)])
+ sage: all([ z[i][j] <= 0 for z in Z_transformation_gens(K)
+ ....: for i in range(z.nrows())
+ ....: for j in range(z.ncols())
+ ....: if i != j ])
+ True
+
+ The trivial cone in a trivial space has no Z-transformations::
+
+ sage: K = Cone([], ToricLattice(0))
+ sage: Z_transformation_gens(K)
+ []
+
+ Every operator is a Z-transformation on the ambient vector space::