GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/*1* Normaliz2* Copyright (C) 2007-2014 Winfried Bruns, Bogdan Ichim, Christof Soeger3* This program is free software: you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation, either version 3 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program. If not, see <http://www.gnu.org/licenses/>.15*16* As an exception, when this program is distributed through (i) the App Store17* by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or (iii) Google Play18* by Google Inc., then that store may impose any digital rights management,19* device limits and/or redistribution restrictions that are required by its20* terms of service.21*/2223#ifndef FULL_CONE_H24#define FULL_CONE_H2526#include <list>27#include <vector>28#include <deque>29//#include <set>30#include <boost/dynamic_bitset.hpp>3132#include "libnormaliz/libnormaliz.h"33#include "libnormaliz/cone_property.h"34#include "libnormaliz/matrix.h"35#include "libnormaliz/simplex.h"36#include "libnormaliz/cone_dual_mode.h"37#include "libnormaliz/HilbertSeries.h"38#include "libnormaliz/reduction.h"39// #include "libnormaliz/sublattice_representation.h"40#include "libnormaliz/offload_handler.h"4142namespace libnormaliz {43using std::list;44using std::vector;45using std::map;46using std::pair;47using boost::dynamic_bitset;4849template<typename Integer> class Cone;50template<typename Integer> class SimplexEvaluator;51template<typename Integer> class CandidateList;52template<typename Integer> class Candidate;53template<typename Integer> class Simplex;54template<typename Integer> class Collector;5556template<typename Integer>57class Full_Cone {5859friend class Cone<Integer>;60friend class SimplexEvaluator<Integer>;61friend class CandidateList<Integer>;62friend class Candidate<Integer>;63friend class Collector<Integer>;6465public:6667int omp_start_level; // records the omp_get_level() when the computation is started68// recorded at the start of the top cone constructor and the compute functions69// compute and dualize_cone70size_t dim;71size_t level0_dim; // dim of cone in level 0 of the inhomogeneous case72size_t module_rank; // rank of solution module over level 0 monoid in the inhomogeneous case73size_t nr_gen;74// size_t hyp_size; // not used at present75Integer index; // index of full lattice over lattice of generators7677bool verbose;7879bool pointed;80bool is_simplicial;81bool deg1_generated_computed;82bool deg1_generated;83bool deg1_extreme_rays;84bool deg1_triangulation;85bool deg1_hilbert_basis;86bool inhomogeneous;8788// control of what to compute89bool do_triangulation;90bool explicit_full_triang; // indicates whether full triangulation is asked for without default mode91bool explicit_h_vector; // to distinguish it from being set via default mode92bool do_partial_triangulation;93bool do_determinants;94bool do_multiplicity;95bool do_integrally_closed;96bool do_Hilbert_basis;97bool do_deg1_elements;98bool do_h_vector;99bool keep_triangulation;100bool do_Stanley_dec;101bool do_excluded_faces;102bool do_approximation;103bool do_default_mode;104bool do_bottom_dec;105bool suppress_bottom_dec;106bool keep_order;107bool do_class_group;108bool do_module_gens_intcl;109bool do_module_rank;110bool do_cone_dec;111bool stop_after_cone_dec;112bool do_hsop;113114bool do_extreme_rays;115bool do_pointed;116117// internal helper control variables118bool do_only_multiplicity;119bool do_only_mult_and_decomp;120bool do_evaluation;121bool do_all_hyperplanes; // controls whether all support hyperplanes must be computed122bool use_bottom_points;123ConeProperties is_Computed;124bool triangulation_is_nested;125bool triangulation_is_partial;126bool has_generator_with_common_divisor;127128// data of the cone (input or output)129vector<Integer> Truncation; //used in the inhomogeneous case to suppress vectors of level > 1130Integer TruncLevel; // used for approximation of simplicial cones131vector<Integer> Grading;132vector<Integer> Sorting;133mpq_class multiplicity;134Matrix<Integer> Generators;135Matrix<Integer> ExtStrahl;136vector<key_t> PermGens; // stores the permutation of the generators created by sorting137vector<bool> Extreme_Rays_Ind;138Matrix<Integer> Support_Hyperplanes;139Matrix<Integer> Subcone_Support_Hyperplanes; // used if *this computes elements in a subcone, for example in approximation140Matrix<Integer> Subcone_Equations;141vector<Integer> Subcone_Grading;142size_t nrSupport_Hyperplanes;143list<vector<Integer> > Hilbert_Basis;144vector<Integer> Witness; // for not integrally closed145Matrix<Integer> Basis_Max_Subspace; // a basis of the maximal linear subspace of the cone --- only used in connection with dual mode146list<vector<Integer> > ModuleGeneratorsOverOriginalMonoid;147CandidateList<Integer> OldCandidates,NewCandidates; // for the Hilbert basis148size_t CandidatesSize;149list<vector<Integer> > Deg1_Elements;150HilbertSeries Hilbert_Series;151vector<Integer> gen_degrees; // will contain the degrees of the generators152Integer shift; // needed in the inhomogeneous case to make degrees positive153vector<Integer> gen_levels; // will contain the levels of the generators (in the inhomogeneous case)154size_t TriangulationBufferSize; // number of elements in Triangulation, for efficiency155list< SHORTSIMPLEX<Integer> > Triangulation; // triangulation of cone156list< SHORTSIMPLEX<Integer> > TriangulationBuffer; // simplices to evaluate157list< SimplexEvaluator<Integer> > LargeSimplices; // Simplices for internal parallelization158Integer detSum; // sum of the determinants of the simplices159list< STANLEYDATA_int> StanleyDec; // Stanley decomposition160vector<Integer> ClassGroup; // the class group as a vector: ClassGroup[0]=its rank, then the orders of the finite cyclic summands161162Matrix<Integer> ProjToLevel0Quot; // projection matrix onto quotient modulo level 0 sublattice163164// privare data controlling the computations165vector<size_t> HypCounter; // counters used to give unique number to hyperplane166// must be defined thread wise to avoid critical167168vector<bool> in_triang; // intriang[i]==true means that Generators[i] has been actively inserted169vector<key_t> GensInCone; // lists the generators completely built in170size_t nrGensInCone; // their number171172struct FACETDATA {173vector<Integer> Hyp; // linear form of the hyperplane174boost::dynamic_bitset<> GenInHyp; // incidence hyperplane/generators175Integer ValNewGen; // value of linear form on the generator to be added176size_t BornAt; // number of generator (in order of insertion) at which this hyperplane was added,, counting from 0177size_t Ident; // unique number identifying the hyperplane (derived from HypCounter)178size_t Mother; // Ident of positive mother if known, 0 if unknown179bool is_positive_on_all_original_gens;180bool is_negative_on_some_original_gen;181bool simplicial; // indicates whether facet is simplicial182};183184list<FACETDATA> Facets; // contains the data for Fourier-Motzkin and extension of triangulation185size_t old_nr_supp_hyps; // must be remembered since Facets gets extended before the current generators is finished186187// data relating a pyramid to its ancestores188Full_Cone<Integer>* Top_Cone; // reference to cone on top level189vector<key_t> Top_Key; // indices of generators w.r.t Top_Cone190Full_Cone<Integer>* Mother; // reference to the mother of the pyramid191vector<key_t> Mother_Key; // indices of generators w.r.t Mother192size_t apex; // indicates which generator of mother cone is apex of pyramid193int pyr_level; // -1 for top cone, increased by 1 for each level of pyramids194195// control of pyramids, recusrion and parallelization196bool is_pyramid; // false for top cone197long last_to_be_inserted; // good to know in case of do_all_hyperplanes==false198bool recursion_allowed; // to allow or block recursive formation of pytamids199bool multithreaded_pyramid; // indicates that this cone is computed in parallel threads200bool tri_recursion; // true if we have gone to pyramids because of triangulation201202vector<size_t> Comparisons; // at index i we note the total number of comparisons203// of positive and negative hyperplanes needed for the first i generators204size_t nrTotalComparisons; // counts the comparisons in the current computation205206// storage for subpyramids207size_t store_level; // the level on which daughters will be stored208deque< list<vector<key_t> > > Pyramids; //storage for pyramids209deque<size_t> nrPyramids; // number of pyramids on the various levels210deque<bool> Pyramids_scrambled; // only used for mic211212// data that can be used to go out of build_cone and return later (not done at present)213// but also useful at other places214long nextGen; // the next generator to be processed215long lastGen; // the last generator processed216217// Helpers for triangulation and Fourier-Motzkin218vector<typename list < SHORTSIMPLEX<Integer> >::iterator> TriSectionFirst; // first simplex with lead vertex i219vector<typename list < SHORTSIMPLEX<Integer> >::iterator> TriSectionLast; // last simplex with lead vertex i220list<FACETDATA> LargeRecPyrs; // storage for large recusive pyramids given by basis of pyramid in mother cone221222list< SHORTSIMPLEX<Integer> > FreeSimpl; // list of short simplices already evaluated, kept for recycling223vector<list< SHORTSIMPLEX<Integer> > > FS; // the same per thread224vector< Matrix<Integer> > RankTest; // helper matrices for rank test225226// helpers for evaluation227vector< SimplexEvaluator<Integer> > SimplexEval; // one per thread228vector< Collector<Integer> > Results; // one per thread229vector<Integer> Order_Vector; // vector for the disjoint decomposition of the cone230#ifdef NMZ_MIC_OFFLOAD231MicOffloader<long long> mic_offloader;232#endif233void try_offload_loc(long place,size_t max_level);234235236// defining semiopen cones237Matrix<Integer> ExcludedFaces;238map<boost::dynamic_bitset<>, long> InExCollect;239240// statistics241size_t totalNrSimplices; // total number of simplices evaluated242size_t nrSimplicialPyr;243size_t totalNrPyr;244245bool use_existing_facets; // in order to avoid duplicate computation of already computed facets246size_t start_from;247248size_t AdjustedReductionBound;249250bool is_approximation;251bool is_global_approximation; // true if approximation is defined in Cone252253vector<vector<key_t>> approx_points_keys;254Matrix<Integer> OriginalGenerators;255256Integer VolumeBound; //used to stop computation of approximation if simplex of this has larger volume257258/* ---------------------------------------------------------------------------259* Private routines, used in the public routines260* ---------------------------------------------------------------------------261*/262void number_hyperplane(FACETDATA& hyp, const size_t born_at, const size_t mother);263bool is_hyperplane_included(FACETDATA& hyp);264void add_hyperplane(const size_t& new_generator, const FACETDATA & positive,const FACETDATA & negative,265list<FACETDATA>& NewHyps, bool known_to_be_simplicial);266void extend_triangulation(const size_t& new_generator);267void find_new_facets(const size_t& new_generator);268void process_pyramids(const size_t new_generator,const bool recursive);269void process_pyramid(const vector<key_t>& Pyramid_key,270const size_t new_generator, const size_t store_level, Integer height, const bool recursive,271typename list< FACETDATA >::iterator hyp, size_t start_level);272void select_supphyps_from(const list<FACETDATA>& NewFacets, const size_t new_generator,273const vector<key_t>& Pyramid_key);274bool check_pyr_buffer(const size_t level);275void evaluate_stored_pyramids(const size_t level);276void match_neg_hyp_with_pos_hyps(const FACETDATA& hyp, size_t new_generator,list<FACETDATA*>& PosHyps, boost::dynamic_bitset<>& Zero_P);277void collect_pos_supphyps(list<FACETDATA*>& PosHyps, boost::dynamic_bitset<>& Zero_P, size_t& nr_pos);278void evaluate_rec_pyramids(const size_t level);279void evaluate_large_rec_pyramids(size_t new_generator);280281void find_and_evaluate_start_simplex();282// Simplex<Integer> find_start_simplex() const;283vector<key_t> find_start_simplex() const;284void store_key(const vector<key_t>&, const Integer& height, const Integer& mother_vol,285list< SHORTSIMPLEX<Integer> >& Triangulation);286void find_bottom_facets();287vector<list<vector<Integer>>> latt_approx(); // makes a cone over a lattice polytope approximating "this"288void convert_polyhedron_to_polytope();289void compute_elements_via_approx(list<vector<Integer> >& elements_from_approx); // uses the approximation290void compute_deg1_elements_via_approx_global(); // deg 1 elements from the approximation291void compute_deg1_elements_via_projection_simplicial(const vector<key_t>& key); // for a simplicial subcone by projecion292void compute_sub_div_elements(const Matrix<Integer>& gens,list<vector<Integer> >& sub_div_elements,293bool best_point=false); //computes subdividing elements via approximation294void select_deg1_elements(const Full_Cone& C);295// void select_Hilbert_Basis(const Full_Cone& C); //experimental, unused296297void build_top_cone();298void build_cone();299void get_supphyps_from_copy(bool from_scratch); // if evealuation starts before support hyperplanes are fully computed300void update_reducers(bool forced=false); // update list of reducers after evaluation of simplices301302303bool is_reducible(list<vector<Integer> *> & Irred, const vector<Integer> & new_element);304void global_reduction();305306vector<Integer> compute_degree_function() const;307308Matrix<Integer> select_matrix_from_list(const list<vector<Integer> >& S,vector<size_t>& selection);309310bool contains(const vector<Integer>& v);311bool subcone_contains(const vector<Integer>& v);312bool contains(const Full_Cone& C);313void extreme_rays_and_deg1_check();314void find_grading();315void find_grading_inhom();316void check_given_grading();317void disable_grading_dep_comp();318void set_degrees();319void set_levels(); // for truncation in the inhomogeneous case320void find_module_rank(); // finds the module rank in the inhom case321void find_module_rank_from_HB();322void find_module_rank_from_proj(); // used if Hilbert basis is not computed323void find_level0_dim(); // ditto for the level 0 dimension324void sort_gens_by_degree(bool triangulate);325// void compute_support_hyperplanes(bool do_extreme_rays=false);326bool check_evaluation_buffer();327bool check_evaluation_buffer_size();328void prepare_old_candidates_and_support_hyperplanes();329void evaluate_triangulation();330void evaluate_large_simplices();331void evaluate_large_simplex(size_t j, size_t lss);332void transfer_triangulation_to_top();333void primal_algorithm();334void primal_algorithm_initialize();335void primal_algorithm_finalize();336void primal_algorithm_set_computed();337void make_module_gens();338void make_module_gens_and_extract_HB();339void remove_duplicate_ori_gens_from_HB();340void compute_class_group();341void compose_perm_gens(const vector<key_t>& perm);342void check_grading_after_dual_mode();343344void minimize_support_hyperplanes();345void compute_extreme_rays(bool use_facets=false);346void compute_extreme_rays_compare(bool use_facets);347void compute_extreme_rays_rank(bool use_facets);348void select_deg1_elements();349350void check_pointed();351void deg1_check();352void check_deg1_extreme_rays();353void check_deg1_hilbert_basis();354355void compute_multiplicity();356357void minimize_excluded_faces();358void prepare_inclusion_exclusion();359360void do_vars_check(bool with_default);361void reset_tasks();362void addMult(Integer& volume, const vector<key_t>& key, const int& tn); // multiplicity sum over thread tn363364void check_simpliciality_hyperplane(const FACETDATA& hyp) const;365void set_simplicial(FACETDATA& hyp);366367368void compute_hsop();369void heights(list<vector<key_t>>& facet_keys,list<pair<boost::dynamic_bitset<>,size_t>> faces, size_t index,vector<size_t>& ideal_heights, size_t max_dim);370371void start_message();372void end_message();373374void set_zero_cone();375376377#ifdef NMZ_MIC_OFFLOAD378void try_offload(size_t max_level);379#else380void try_offload(size_t max_level) {};381#endif382383384/*---------------------------------------------------------------------------385* Constructors386*---------------------------------------------------------------------------387*/388Full_Cone(const Matrix<Integer>& M, bool do_make_prime=true); //main constructor389Full_Cone(Cone_Dual_Mode<Integer> &C); // removes data from the argument!390Full_Cone(Full_Cone<Integer>& C, const vector<key_t>& Key); // for pyramids391392/*---------------------------------------------------------------------------393* Data access394*---------------------------------------------------------------------------395*/396void print() const; //to be modified, just for tests397size_t getDimension() const;398size_t getNrGenerators() const;399bool isPointed() const;400bool isDeg1ExtremeRays() const;401bool isDeg1HilbertBasis() const;402vector<Integer> getGrading() const;403mpq_class getMultiplicity() const;404Integer getShift()const;405size_t getModuleRank()const;406const Matrix<Integer>& getGenerators() const;407vector<bool> getExtremeRays() const;408Matrix<Integer> getSupportHyperplanes() const;409Matrix<Integer> getHilbertBasis() const;410Matrix<Integer> getModuleGeneratorsOverOriginalMonoid()const;411Matrix<Integer> getDeg1Elements() const;412vector<Integer> getHVector() const;413Matrix<Integer> getExcludedFaces()const;414415bool isComputed(ConeProperty::Enum prop) const;416417418/*---------------------------------------------------------------------------419* Computation Methods420*---------------------------------------------------------------------------421*/422void dualize_cone(bool print_message=true);423void support_hyperplanes();424425void compute();426427/* adds generators, they have to lie inside the existing cone */428void add_generators(const Matrix<Integer>& new_points);429430void dual_mode();431432void error_msg(string s) const;433};434//class end *****************************************************************435//---------------------------------------------------------------------------436437}438439//---------------------------------------------------------------------------440#endif441//---------------------------------------------------------------------------442443444445