Path: blob/master/sage/geometry/polyhedron/cdd_file_format.py
4079 views
"""1Generate cdd ``.ext`` / ``.ine`` file format2"""34########################################################################5# Copyright (C) 2008 Marshall Hampton <[email protected]>6# Copyright (C) 2011 Volker Braun <[email protected]>7#8# Distributed under the terms of the GNU General Public License (GPL)9#10# http://www.gnu.org/licenses/11########################################################################121314from misc import _set_to_None_if_empty, _common_length_of, _to_space_separated_string15161718#########################################################################19def cdd_Vrepresentation(cdd_type, vertices, rays, lines):20r"""21Return a string containing the V-representation in cddlib's ext format.2223NOTE:2425If there is no vertex given, then the origin will be implicitly26added. You cannot write the empty V-representation (which cdd27would refuse to process).2829EXAMPLES::3031sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Vrepresentation32sage: print cdd_Vrepresentation('rational', [[0,0]], [[1,0]], [[0,1]])33V-representation34linearity 1 135begin363 3 rational370 0 1380 1 0391 0 040end41"""42vertices = _set_to_None_if_empty(vertices)43rays = _set_to_None_if_empty(rays)44lines = _set_to_None_if_empty(lines)4546num, ambient_dim = _common_length_of(vertices, rays, lines)4748# cdd implicitly assumes that the origin is a vertex if none is given49if vertices==None:50vertices = [[0]*ambient_dim]51num += 15253s = 'V-representation\n'54if lines!=None:55n = len(lines)56s += "linearity " + repr(n) + ' '57s += _to_space_separated_string(range(1,n+1)) + '\n'58s += 'begin\n'59s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n'60if lines!=None:61for l in lines:62s += ' 0 ' + _to_space_separated_string(l) + '\n'63if rays!=None:64for r in rays:65s += ' 0 ' + _to_space_separated_string(r) + '\n'66if vertices!=None:67for v in vertices:68s += ' 1 ' + _to_space_separated_string(v) + '\n'69s += 'end\n'70return s7172#########################################################################73def cdd_Hrepresentation(cdd_type, ieqs, eqns):74r"""75Return a string containing the H-representation in cddlib's ine format.7677EXAMPLES::7879sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Hrepresentation80sage: cdd_Hrepresentation('rational', None, [[0,1]])81'H-representation\nlinearity 1 1\nbegin\n 1 2 rational\n 0 1\nend\n'82"""83ieqs = _set_to_None_if_empty(ieqs)84eqns = _set_to_None_if_empty(eqns)8586num, ambient_dim = _common_length_of(ieqs, eqns)87ambient_dim -= 18889s = 'H-representation\n'90if eqns!=None:91assert len(eqns)>092n = len(eqns)93s += "linearity " + repr(n) + ' '94s += _to_space_separated_string(range(1,n+1)) + '\n'95s += 'begin\n'96s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n'97if eqns!=None:98for e in eqns:99s += ' ' + _to_space_separated_string(e) + '\n'100if ieqs!=None:101for i in ieqs:102s += ' ' + _to_space_separated_string(i) + '\n'103s += 'end\n'104return s105106107