1#  Copyright (c) 1997-2021
2#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
3#  Technische Universität Berlin, Germany
4#  https://polymake.org
5#
6#  This program is free software; you can redistribute it and/or modify it
7#  under the terms of the GNU General Public License as published by the
8#  Free Software Foundation; either version 2, or (at your option) any
9#  later version: http://www.gnu.org/licenses/gpl.txt.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#-------------------------------------------------------------------------------
16
17
18object Polytope {
19
20	# @category Geometry
21	# Dimension of the tropical projective space which contains the tropical polytope.
22	property PROJECTIVE_AMBIENT_DIM : Int;
23
24	# @category Geometry
25	# Input points in tropical homogeneous coordinates.
26	# This is the fixed system of generators with respect
27	# to which many combinatorial properties are expressed.
28	property POINTS : Matrix<TropicalNumber<Addition,Scalar>> {
29	   sub canonical { &canonicalize_to_leading_zero_and_check_columns; }
30	}
31
32        # permuting the [[POINTS]]
33	permutation PointsPerm : PermBase;
34
35	rule PointsPerm.PERMUTATION : PointsPerm.POINTS, POINTS {
36	   $this->PointsPerm->PERMUTATION = find_permutation($this->PointsPerm->POINTS, $this->POINTS)
37              // die "no permutation";
38	}
39
40	rule POINTS : PointsPerm.POINTS, PointsPerm.PERMUTATION {
41	   $this->POINTS = permuted_rows($this->PointsPerm->POINTS, $this->PointsPerm->PERMUTATION);
42	}
43	weight 1.10;
44
45	# @category Geometry
46	# Vertices of the tropical convex hull, a submatrix of [[POINTS]]
47	property VERTICES : Matrix<TropicalNumber<Addition,Scalar>> {
48           sub canonical { &canonicalize_to_leading_zero; }
49	}
50
51        # permuting the [[VERTICES]]
52	permutation VertexPerm : PermBase;
53
54	rule VertexPerm.PERMUTATION : VertexPerm.VERTICES, VERTICES {
55	   $this->VertexPerm->PERMUTATION = find_permutation($this->VertexPerm->VERTICES, $this->VERTICES)
56              // die "no permutation";
57	}
58
59	rule VERTICES : VertexPerm.VERTICES, VertexPerm.PERMUTATION {
60	   $this->VERTICES = permuted_rows($this->VertexPerm->VERTICES, $this->VertexPerm->PERMUTATION);
61	}
62	weight 1.10;
63
64
65        # @category Input property
66        # Inequalities giving rise to the polytope; redundancies are allowed.
67        # They must be encoded as a pair of matrices.
68        # The pair (A,B) encodes the inequality //A//x ~ //B//x,
69        # where ~ is <= for min and >= for max.
70        # All vectors in this section must be non-zero.
71        # Dual to [[POINTS]].
72        #
73        # Input section only.
74        property INEQUALITIES : Pair<Matrix<TropicalNumber<Addition,Scalar>>,Matrix<TropicalNumber<Addition,Scalar>>>;
75
76
77        # @category Geometry
78        # Some point belonging to the polyhedron.
79        property VALID_POINT : Vector<TropicalNumber<Addition,Scalar>> {
80            sub canonical { &canonicalize_to_leading_zero; }
81        }
82
83        # @category Geometry
84        # True if the polyhedron is not empty.
85        property FEASIBLE : Bool;
86
87
88	# @category Geometry
89	# Entries correspond to [[VERTICES]]. They describe for each vertex, what its row
90	# index in [[POINTS]] is.
91	property VERTICES_IN_POINTS : Array<Int>;
92
93	# @category Geometry
94	# Pseudovertices are the vertices of the type decomposition of the tropical torus induced by [[POINTS]].
95	# They are projections of the vertices of [[ENVELOPE]]. Note that each pseudovertex is given in tropical
96	# homogeneous coordinates with a leading 1 or 0, depending on whether it is a vertex or a ray.
97	property PSEUDOVERTICES : Matrix<Scalar> {
98		sub canonical { &canonicalize_vertices_to_leading_zero; }
99	}
100
101        # @category Geometry
102	# Subset of the [[PSEUDOVERTICES]] which are not contained in the tropical projective torus.
103
104	property FAR_PSEUDOVERTICES : Set;
105
106	# @category Combinatorics
107	# These are the maximal cells of the covector decomposition of the tropical torus with
108	# respect to [[POINTS]].
109	# Each row corresponds to a maximal cell, each column to an element of [[PSEUDOVERTICES]].
110
111
112
113
114	property MAXIMAL_COVECTOR_CELLS : IncidenceMatrix;
115
116	# @category Combinatorics
117	# This is the face lattice of the polyhedral complex, whose vertices are [[PSEUDOVERTICES]] and
118	# whose cells are the cells of the covector decomposition. For each face in this lattice, we save the following information:
119	# 1) What PSEUDOVERTICES make up this face, i.e. a Set<Int>
120	# 2) What is the covector of this face, i.e. an IncidenceMatrix (whose rows correspond to coordinates and
121	# whose columns to [[POINTS]]).
122	# NOTE: This lattice does not contain any far faces of the polyhedral cells, as they do not have well-defined covectors.
123	property TORUS_COVECTOR_DECOMPOSITION : CovectorLattice;
124
125	# @category Combinatorics
126	# This is a sublattice of [[TORUS_COVECTOR_DECOMPOSITION]], containing only the cells that belong to the tropical span
127	# of [[POINTS]].
128	property POLYTOPE_COVECTOR_DECOMPOSITION : CovectorLattice;
129
130	# @category Combinatorics
131	# This is a description of the tropical polytope as a polyhedral complex. Each
132	# row is a maximal cell of the covector subdivision of the tropical polytope. Indices refer to
133	# [[PSEUDOVERTICES]].
134	property POLYTOPE_MAXIMAL_COVECTOR_CELLS : IncidenceMatrix;
135
136	# @category Combinatorics
137	# The covectors of the maximal cells of the torus subdivision. Entries correspond
138	# to rows of [[MAXIMAL_COVECTOR_CELLS]].
139	property MAXIMAL_COVECTORS : Array<IncidenceMatrix>;
140
141	# @category Combinatorics
142	# The covectors of the maximal cells of the polytope subdivision. Entries correspond
143	# to rows of [[POLYTOPE_MAXIMAL_COVECTOR_CELLS]].
144	property POLYTOPE_MAXIMAL_COVECTORS : Array<IncidenceMatrix>;
145
146	# @category Visualization
147	# Unique names assigned to the [[VERTICES]].
148	# If specified, they are shown by visualization tools instead of vertex indices.
149	#property VERTEX_LABELS : Array<String>;
150
151	# @category Visualization
152	# Unique names assigned to the [[PSEUDOVERTICES]].
153	# Can be used as "NodeLabels" in [[VISUAL_PLANAR]].
154	#property PSEUDOVERTEX_LABELS : Array<String>;
155
156	# @category Geometry
157	# Tropical polytopes have a natural description as the complex of certain faces of their envelopes.
158	# This envelope depends on the choice of the [[POINTS]] that generate the tropical polytope.
159	property ENVELOPE : polytope::Polytope<Scalar>;
160
161	# @category Geometry
162	# This is the dome of the tropical hyperplane arrangement defined by the [[POINTS]].
163	# I.e. we take as function the (tropical) product of the tropical linear polynomials defined
164	# in the following manner: For each point (p_0,...,p_d) we get the linear polynomial
165	# sum_{i=1}^d (1/p_i) * x_i, where sum is the DUAL tropical addition and * and /  is regular
166	# addition and subtraction, respectively.
167	property DOME : polytope::Polytope<Scalar>;
168
169	# @category Combinatorics
170	# Types of [[PSEUDOVERTICES]] relative to [[POINTS]].
171	# Each type is encoded as an Incidence matrix, where rows correspond to coordinates and
172	# columns to [[POINTS]]. If the i-th row is a set S, that means that this pseudovertex is
173	# in the i-th sector of all points indexed by S.
174	# For bounded vertices, the type is computed as usual. For unbounded rays (i.e. starting with a 0), the type
175	# is computed as follows. Let g be a generator, with infinite entries at positions J and let the ray be
176	# e_J = sum_{j in J} +- e_j (the sign being the orientation of the addition).
177	# If J is contained in K, the ray is "contained" in all sectors of g.
178	# Otherwise, the ray is "contained" in the sectors indexed by g.
179	# NOTE: The latter is an artificial definition in the sense that it is not compatible with intersecting
180	# faces of the covector lattice. However, it is correct in the sense that faces spanned by a
181	# list of pseudovertices have as covector the intersection of the respective covectors.
182	property PSEUDOVERTEX_COVECTORS : Array<IncidenceMatrix>;
183
184	# @category Combinatorics
185	# Coarse types of [[PSEUDOVERTICES]] relative to [[POINTS]].
186	# Each row corresponds to a row of [[PSEUDOVERTICES]] and encodes at position i, how many [[POINTS]]
187	# contain that pseudovertex in the i-th sector.
188	property PSEUDOVERTEX_COARSE_COVECTORS : Matrix<Int>;
189
190	#FIXME: Properties for exterior description?
191
192	# @category Geometry
193	# Tropical halfspaces defining this tropical polytopes. Encoded as a pair of tropical matrices M,M' with the same
194	# dimensions. The row count of the matrices is the number of halfspaces. The column count is the
195	# number of (tropical homogeneous) coordinates of the polytope. A point p lies in the polytope described by these
196	# halfspaces, if and only M*p ~ M'*p, where ~ is >= for max and <= for min and all operations
197	# are tropical. In other words, p lies in the polytope, if and only if (M + M')*p = M*p.
198	#property HALF_SPACES : Pair<Matrix<TropicalNumber<Addition,Scalar>>, Matrix<TropicalNumber<Addition,Scalar>>>;
199
200	# @category Geometry
201	# This returns the subdivision of the tropical torus induced by [[POINTS]] as a
202	# polyhedral complex on a chosen affine chart
203	# @param Int chart Which coordinate to normalize to 0. This is 0 by default.
204	# @return fan::PolyhedralComplex
205	user_method torus_subdivision_as_complex(;$=0) {
206            my ($cone,$chart) = @_;
207            return new fan::PolyhedralComplex(VERTICES=>tdehomog($cone->PSEUDOVERTICES,$chart),
208                                              MAXIMAL_POLYTOPES=>$cone->MAXIMAL_COVECTOR_CELLS,
209                                              LINEALITY_SPACE=>[]);
210	}
211
212	# @category Geometry
213	# This returns the subdivision of the polytope induced by [[POINTS]] as a polyhedral
214	# complex on a chosen affine chart.
215	# @param Int chart Which coordinate to normalize to 0. This is 0 by default.
216	# @return fan::PolyhedralComplex
217	user_method polytope_subdivision_as_complex(;$=0) {
218            my ($cone,$chart) = @_;
219            my $n = $cone->PSEUDOVERTICES->rows();
220            my $finite_indices = sequence(0,$n) - $cone->FAR_PSEUDOVERTICES;
221            my $finite_pseudovertices = new Matrix($cone->PSEUDOVERTICES->minor($finite_indices,All));
222            return new fan::PolyhedralComplex(VERTICES=>tdehomog($finite_pseudovertices,$chart),
223                                              MAXIMAL_POLYTOPES=>$cone->POLYTOPE_MAXIMAL_COVECTOR_CELLS,
224                                              LINEALITY_SPACE=>[]);
225	}
226
227
228}
229
230# @category Conversion of tropical addition
231# This function takes a tropical polytope and returns a tropical polytope that uses
232# the opposite tropical addition. By default, the signs of the [[POINTS]] are inverted.
233# @param Polytope<Addition,Scalar> polytope
234# @param Bool strong_conversion This is optional and TRUE by default.
235# It indicates, whether the signs of the vertices should be inverted.
236# @return Polytope,
237user_function dual_addition_version<Addition,Scalar>(Polytope<Addition,Scalar>;$=1) {
238	return dual_addition_version_cone(@_);
239}
240
241# @category Other
242# This function takes a Matrix of tropical vectors in projective coordinates
243# (e.g. the [[POINTS]] of a [[Polytope]]) and a Matrix of Scalar vectors in extended tropical projective
244# coordinates (e.g. the [[PSEUDOVERTICES]] of a tropical [[Polytope]]).
245# It returns the set of row indices of the second matrix such that the corresponding row
246# starts with a 1 and the remaining vector occurs in the first matrix.
247# @param Matrix<TropicalNumber<Addition, Scalar>> points
248# @param Matrix<Scalar> pseudovertices
249# @return Set<Int>
250user_function points_in_pseudovertices<Addition,Scalar>(Matrix<TropicalNumber<Addition, Scalar>>, Matrix<Scalar>) {
251  my ($points, $pseudovertices)=@_;
252  $points = ones_vector<Scalar>($points->rows()) | new Matrix<Scalar>($points);
253  my $generators=new HashSet<Vector<Scalar>>(rows($points));
254
255  my $result = new Set<Int>();
256  for my $i (0..$pseudovertices->rows()-1) {
257    if (exists $generators->{$pseudovertices->row($i)}) { $result += $i; }
258  }
259  return $result;
260}
261
262
263# Local Variables:
264# mode: perl
265# cperl-indent-level: 3
266# indent-tabs-mode:nil
267# End:
268