source: git/gfanlib/gfanlib_zcone.h @ def863

spielwiese
Last change on this file since def863 was def863, checked in by Frank Seelisch <seelisch@…>, 13 years ago
included Anders Jensens gfan lib git-svn-id: file:///usr/local/Singular/svn/trunk@13558 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.4 KB
Line 
1/*
2 * lib_cone.h
3 *
4 *  Created on: Sep 28, 2010
5 *      Author: anders
6 */
7
8#ifndef LIB_CONE_H_
9#define LIB_CONE_H_
10
11#include "gfanlib_matrix.h"
12
13namespace gfan{
14
15
16/**
17A PolyhedralCone is represented by linear inequalities and equations. The inequalities are non-strict
18and stored as the rows of a matrix and the equations are stored as rows of a second matrix.
19
20A cone can be in one of the four states:
210) Nothing has been done to remove redundancies. This is the initial state.
221) A basis for the true, implied equations space has been computed. This means that
23   the implied equations have been computed. In particular the dimension of the cone is known.
242) Redundant inequalities have been computed and have been eliminated.
25   This means that the true set of facets is known - one for each element in halfSpaces.
263) The inequalities and equations from 2) have been transformed into a canonical form. Besides having
27   a unique representation for the cone this also allows comparisons between cones with operator<().
28
29Since moving for one state to the next is expensive, the user of the PolyhedralCone can specify flags
30at the construction of the cone informing about which things are known.
31
32PCP_impliedEquationsKnown means that the given set of equations generate the space of implied equations.
33PCP_facetsKnown means that each inequalities describe define a (different) facet of the cone.
34
35Each cone has the additional information: multiplicity and linear forms.
36The multiplicity is an integer whose default value is one. It can be set by the user.
37When a cone is projected, it can happen that the multiplicity changes according to a lattice index.
38The linear forms are stored in a matrix linearForms, whose width equals the dimension of the ambient space.
39The idea is that a collection of cones in this way can represent a piecewise linear function (a tropical rational function).
40
41Caching:
42When new properties are computed by changing state the information is stored in the object by updating equations and inequalities.
43When some other properties are computed, such as rays the result is cached in the object. Each cached property has a corresponding flag telling if a cached value has been stored.
44These methods
45for these properties are considered const. Caching only works for extreme rays at the moment.
46
47Notice:
48The lineality space of a cone C is C\cap(-C).
49A cone is ray if its dimension is 1+the dimension of the its lineality space.
50
51Should the user of this class know about the states?
52
53need to think about this...
54Always putting the cone in state 1 after something has changed helps a lot.
55Then all operations can be performed except comparing and getting facets with
56out taking the cone to a special state.
57
58
59Things to change:
60- Thomas wants operations where the natural description is the dual to be fast.
61  One way to achieve this is as Frank suggests to have a state -1, in which only
62  the generator description is known. These should be stored in the cache. If it
63  is required to move to state 0, then the inequality description is computed.
64  This sounds like a reasonable solution, but of course, what we are really storing is the dual.
65
66 - Basically all data in the object should be mutable, while almost every method should be const.
67
68 - A method should set the cone in a given state if required. The reason for this is that
69   it will be difficult for the user to figure out which state is required and therefore
70   will tend to call canonicalize when not needed.
71
72 - Cache should be added for more properties.
73
74Optimization:
75- When inequalities can be represented in 32 bit some optimizations can be done.
76
77More things to consider:
78- Does it make sense to do dimension reduction when lineality space / linear span has been
79  computed?
80
81
82When calling generated by rays, two flags should be passed.
83
84 */
85
86enum PolyhedralConePreassumptions{
87  PCP_none=0,
88  PCP_impliedEquationsKnown=1,
89  PCP_facetsKnown=2
90};
91
92class ZCone
93{
94  int preassumptions;
95  mutable int state;
96  int n;
97  Integer multiplicity;
98  ZMatrix linearForms;
99  mutable ZMatrix inequalities;
100  mutable ZMatrix equations;
101  mutable ZMatrix cachedExtremeRays;
102/**
103 * If this bool is true it means that cachedExtremeRays contains the extreme rays as found by extremeRays().
104 */
105  mutable bool haveExtremeRaysBeenCached;
106  void ensureStateAsMinimum(int s)const;
107
108  bool isInStateMinimum(int s)const;
109  int getState()const;
110public:
111   /**
112    * Constructs a polyhedral cone with specified equations and ineqalities. They are read off as rows
113    * of the matrices. For efficiency it is possible to specifying a PolyhedralConePreassumptions flag
114    * which tells what is known about the description already.
115    */
116     ZCone(ZMatrix const &inequalities_, ZMatrix const &equations_, int preassumptions_=PCP_none);
117
118     /**
119      * Get the multiplicity of the cone.
120      */
121     Integer getMultiplicity()const;
122     /**
123      * Set the multiplicity of the cone.
124      */
125     void setMultiplicity(Integer const &m);
126     /**
127      * Returns the matrix of linear forms stored in the cone object.
128      */
129     ZMatrix getLinearForms()const;
130     /**
131      * Store a matrix of linear forms in the cone object.
132      */
133     void setLinearForms(ZMatrix const &linearForms_);
134
135     /**
136      * Get the inequalities in the description of the cone.
137      */
138     ZMatrix getInequalities()const;
139     /**
140      * Get the equations in the description of the cone.
141      */
142     ZMatrix getEquations()const;
143     /**
144      * Compute generators of the span of the cone. They are stored as rows of the returned matrix.
145      */
146     ZMatrix generatorsOfSpan()const;
147     /**
148      * Compute generators of the lineality space of the cone. They are stored as rows of the returned matrix.
149      */
150     ZMatrix generatorsOfLinealitySpace()const;
151     /**
152      * Returns true iff it is known that every inequalities in the description defines a different facets of the cone.
153      */
154     bool areFacetsKnown()const{return (state>=2)||(preassumptions&PCP_facetsKnown);}
155     /**
156      * Returns true iff it is known that the set of equations span the space of implied equations of the description.
157      */
158     bool areImpliedEquationsKnown()const{return (state>=1)||(preassumptions&PCP_impliedEquationsKnown);}
159
160     /**
161      * Takes the cone to a canonical form. After taking cones to canonical form, two cones are the same
162      * if and only if their matrices of equations and inequalities are the same.
163      */
164     void canonicalize();
165     /**
166      * Computes and returns the facet inequalities of the cone.
167      */
168     ZMatrix getFacets()const;
169     /**
170      * After this function has been called all inequalities describe different facets of the cone.
171      */
172     void findFacets();
173     /**
174      * The set of linear forms vanishing on the cone is a subspace. This routine returns a basis
175      * of this subspace as the rows of a matrix.
176      */
177     ZMatrix getImpliedEquations()const;
178     /**
179      * After this function has been called a minimal set of implied equations for the cone is known and is
180      * returned when calling getEquations(). The returned equations form a basis of the space of implied
181      * equations.
182      */
183     void findImpliedEquations();
184
185     /**
186      * Constructor for polyhedral cone with no inequalities or equations. Tthat is, the full space of some dimension.
187      */
188     ZCone(int ambientDimension=0);
189
190     /**
191      * Computes are relative interior point of the cone.
192      */
193     ZVector getRelativeInteriorPoint()const;
194  /**
195     Assuming that this cone C is in state at least 3 (why not 2?), this routine returns a relative interior point v(C) of C with the following properties:
196     1) v is a function, that is v(C) is found deterministically
197     2) for any angle preserving, lattice preserving and lineality space preserving transformation T of R^n we have that v(T(C))=T(v(C)). This makes it easy to check if two cones in the same fan are equal up to symmetry. Here preserving the lineality space L just means T(L)=L.
198  */
199     ZVector getUniquePoint()const;
200  /**
201   * Takes a list of possible extreme rays and add up those actually contained in the cone.
202   */
203     ZVector getUniquePointFromExtremeRays(ZMatrix const &extremeRays)const;
204     /**
205      * Returns the dimension of the ambient space.
206      */
207     int ambientDimension()const;
208     /**
209      * Returns the dimension of the cone.
210      */
211     int dimension()const;
212     /**
213      * Returns (ambient dimension)-(dimension).
214      */
215     int codimension()const;
216     /**
217      * Returns the dimension of the lineality space of the cone.
218      */
219     int dimensionOfLinealitySpace()const;
220     /**
221      * Returns true iff the cone is the origin.
222      */
223     bool isOrigin()const;
224     /**
225      * Returns true iff the cone is the full space.
226      */
227     bool isFullSpace()const;
228
229     /**
230      * Returns the intersection of cone a and b as a cone object.
231      */
232     friend ZCone intersection(const ZCone &a, const ZCone &b);
233     /**
234      * Returns the Cartesian product of the two cones a and b.
235      */
236     friend ZCone product(const ZCone &a, const ZCone &b);
237     /**
238      * Returns the positive orthant of some dimension as a polyhedral cone.
239      */
240     static ZCone positiveOrthant(int dimension);
241     /**
242      * Returns the cone which is the sum of row span of linealitySpace and
243      * the non-negative span of the rows of generators.
244      */
245     static ZCone givenByRays(ZMatrix const &generators, ZMatrix const &linealitySpace);
246
247     /**
248      * To use the comparison operator< the cones must have been canonicalized.
249      */
250     friend bool operator<(ZCone const &a, ZCone const &b);
251     /**
252      * To use the comparison operator!= the cones must have been canonicalized.
253      */
254     friend bool operator!=(ZCone const &a, ZCone const &b);
255
256     /**
257      * Returns true iff the cone contains a positive vector.
258      */
259     bool containsPositiveVector()const;
260     /**
261      * Returns true iff the cone contains the specified vector v.
262      */
263     bool contains(ZVector const &v)const;
264     /**
265      * Returns true iff the cone contains all rows of the matrix l.
266      */
267     bool containsRowsOf(ZMatrix const &l)const;
268     /**
269      * Returns true iff c is contained in the cone.
270      */
271     bool contains(ZCone const &c)const;
272     /**
273      * Returns true iff the PolyhedralCone contains v in its relative interior. False otherwise. The cone must be in state at least 1.
274      */
275     bool containsRelatively(ZVector const &v)const;
276     /*
277      * Returns true iff the cone is simplicial. That is, iff the dimension of the cone equals the number of facets.
278      */
279     bool isSimplicial()const;
280
281     //PolyhedralCone permuted(IntegerVector const &v)const;
282
283     /**
284      * Returns the lineality space of the cone as a polyhedral cone.
285      */
286     ZCone linealitySpace()const;
287
288     /**
289      * Returns the dual cone of the cone.
290      */
291     ZCone dualCone()const;
292     /**
293      * Return -C, where C is the cone.
294      */
295     ZCone negated()const;
296     /**
297      * Compute the extreme rays of the cone, and return generators of these as the rows of a matrix.
298      * The returned extreme rays are represented by vectors which are orthogonal to the lineality
299      * space and which are primitive primitive.
300      * This makes them unique and invariant under lattice and angle preserving linear transformations
301      * in the sense that a transformed cone would give the same set of extreme rays except the
302      * extreme rays have been transformed.
303      * If generators for the lineality space are known, they can be supplied. This can
304      * speed up computations a lot.
305      */
306    ZMatrix extremeRays(ZMatrix const *generatorsOfLinealitySpace=0)const;
307    /**
308       The cone defines two lattices, namely Z^n intersected with the
309       span of the cone and Z^n intersected with the lineality space of
310       the cone. Clearly the second is contained in the
311       first. Furthermore, the second is a saturated lattice of the
312       first. The quotient is torsion-free - hence a lattice. Generators
313       of this lattice as vectors in the span of the cone are computed
314       by this routine. The implied equations must be known when this
315       function is called - if not the routine asserts.
316     */
317    ZMatrix quotientLatticeBasis()const;
318    /**
319       For a ray (dim=linealitydim +1)
320       the quotent lattice described in quotientLatticeBasis() is
321       isomorphic to Z. In fact the ray intersected with Z^n modulo the
322       lineality space intersected with Z^n is a semigroup generated by
323       just one element. This routine computes that element as an
324       integer vector in the cone. Asserts if the cone is not a ray.
325       Asserts if the implied equations have not been computed.
326     */
327    ZVector semiGroupGeneratorOfRay()const;
328
329    /**
330       Computes the link of the face containing v in its relative
331       interior.
332     */
333    ZCone link(ZVector const &w)const;
334
335    /**
336       Tests if f is a face of the cone.
337     */
338  //  bool hasFace(ZCone const &f)const; TODO: make implementation of this
339  /**
340   Computes the face of the cone containing v in its relative interior.
341   The vector MUST be contained in the cone.
342   */
343    ZCone faceContaining(ZVector const &v)const;
344    /**
345     * Computes the projection of the cone to the first newn coordinates.
346     * The ambient space of the returned cone has dimension newn.
347     */
348   // PolyhedralCone projection(int newn)const;
349    friend std::ostream &operator<<(std::ostream &f, ZCone const &c);
350};
351
352};
353
354
355
356
357#endif /* LIB_CONE_H_ */
Note: See TracBrowser for help on using the repository browser.