Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418384/*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//---------------------------------------------------------------------------2425#include <stdlib.h>26#include <vector>27#include <list>28#include <fstream>29#include <iostream>30#include <string>31#include <algorithm>3233#include "Qoutput.h"34#include "libQnormaliz/Qgeneral.h"35#include "libQnormaliz/Qmatrix.h"36#include "libQnormaliz/Qvector_operations.h"37#include "libQnormaliz/Qmap_operations.h"3839//---------------------------------------------------------------------------4041template<typename Number>42Output<Number>::Output(){43out=true;44inv=false;45ext=false;46esp=false;47typ=false;48egn=false;49gen=false;50cst=false;51tri=false;52tgn=false;53ht1=false;54dec=false;55lat=false;56mod=false;57msp=false;58lattice_ideal_input = false;59}6061//---------------------------------------------------------------------------6263template<typename Number>64void Output<Number>::set_lattice_ideal_input(bool value){65lattice_ideal_input=value;66}6768//---------------------------------------------------------------------------6970template<typename Number>71void Output<Number>::read() const{72cout<<"\nname="<<name<<"\n";73cout<<"\nout="<<out<<"\n";74cout<<"\ninv="<<inv<<"\n";75cout<<"\next="<<ext<<"\n";76cout<<"\nesp="<<esp<<"\n";77cout<<"\ntyp="<<typ<<"\n";78cout<<"\negn="<<egn<<"\n";79cout<<"\ngen="<<gen<<"\n";80cout<<"\ncst="<<cst<<"\n";81cout<<"\ntri="<<tri<<"\n";82cout<<"\ntgn="<<tgn<<"\n";83cout<<"\nht1="<<ht1<<"\n";84cout<<"\nResult is:\n";85Result->print();86}8788//---------------------------------------------------------------------------8990template<typename Number>91void Output<Number>::set_name(const string& n){92name=n;93}9495//---------------------------------------------------------------------------9697template<typename Number>98void Output<Number>::setCone(Cone<Number> & C) {99this->Result = &C;100dim = Result->getEmbeddingDim();101homogeneous = !Result->isInhomogeneous();102if (homogeneous) {103of_cone = "";104of_monoid = "";105of_polyhedron = "";106} else {107of_cone = " of recession cone";108of_monoid = " of recession monoid";109of_polyhedron = " of polyhedron (homogenized)";110}111}112113//---------------------------------------------------------------------------114115template<typename Number>116void Output<Number>::set_write_out(const bool& flag){117out=flag;118}119120//---------------------------------------------------------------------------121122template<typename Number>123void Output<Number>::set_write_inv(const bool& flag){124inv=flag;125}126127//---------------------------------------------------------------------------128129template<typename Number>130void Output<Number>::set_write_ext(const bool& flag){131ext=flag;132}133134//---------------------------------------------------------------------------135136template<typename Number>137void Output<Number>::set_write_esp(const bool& flag){138esp=flag;139}140141//---------------------------------------------------------------------------142143template<typename Number>144void Output<Number>::set_write_typ(const bool& flag){145typ=flag;146}147148//---------------------------------------------------------------------------149150template<typename Number>151void Output<Number>::set_write_egn(const bool& flag){152egn=flag;153}154155//---------------------------------------------------------------------------156157template<typename Number>158void Output<Number>::set_write_gen(const bool& flag){159gen=flag;160}161162//---------------------------------------------------------------------------163164template<typename Number>165void Output<Number>::set_write_cst(const bool& flag){166cst=flag;167}168169//---------------------------------------------------------------------------170171template<typename Number>172void Output<Number>::set_write_tri(const bool& flag) {173tri=flag;174}175176//---------------------------------------------------------------------------177178template<typename Number>179void Output<Number>::set_write_tgn(const bool& flag) {180tgn=flag;181}182183//---------------------------------------------------------------------------184185template<typename Number>186void Output<Number>::set_write_ht1(const bool& flag) {187ht1=flag;188}189190//---------------------------------------------------------------------------191192template<typename Number>193void Output<Number>::set_write_dec(const bool& flag) {194dec=flag;195}196197//---------------------------------------------------------------------------198199template<typename Number>200void Output<Number>::set_write_mod(const bool& flag) {201mod=flag;202}203204//---------------------------------------------------------------------------205206template<typename Number>207void Output<Number>::set_write_lat(const bool& flag) {208lat=flag;209}210211212//---------------------------------------------------------------------------213214template<typename Number>215void Output<Number>::set_write_msp(const bool& flag) {216msp=flag;217}218//---------------------------------------------------------------------------219220template<typename Number>221void Output<Number>::set_write_extra_files(){222out=true;223inv=true;224gen=true;225cst=true;226}227228//---------------------------------------------------------------------------229230template<typename Number>231void Output<Number>::set_write_all_files(){232out=true;233inv=true;234ext=true;235esp=true;236typ=true;237egn=true;238gen=true;239cst=true;240ht1=true;241lat=true;242mod=true;243msp=true;244}245246247248//---------------------------------------------------------------------------249250template<typename Number>251void Output<Number>::write_matrix_ext(const Matrix<Number>& M) const{252if (ext==true) {253M.print(name,"ext");254}255}256257//---------------------------------------------------------------------------258259template<typename Number>260void Output<Number>::write_matrix_mod(const Matrix<Number>& M) const{261if (mod==true) {262M.print(name,"mod");263}264}265266267//---------------------------------------------------------------------------268269template<typename Number>270void Output<Number>::write_matrix_lat(const Matrix<Number>& M) const{271if (ext==true) {272M.print(name,"lat");273}274}275276//---------------------------------------------------------------------------277278template<typename Number>279void Output<Number>::write_matrix_esp(const Matrix<Number>& M) const{280if (esp==true) {281M.print(name,"esp");282}283}284285//---------------------------------------------------------------------------286287template<typename Number>288void Output<Number>::write_matrix_typ(const Matrix<Number>& M) const{289if (typ==true) {290M.print(name,"typ");291}292}293294//---------------------------------------------------------------------------295296template<typename Number>297void Output<Number>::write_matrix_egn(const Matrix<Number>& M) const {298if (egn==true) {299M.print(name,"egn");300}301}302303//---------------------------------------------------------------------------304305template<typename Number>306void Output<Number>::write_matrix_gen(const Matrix<Number>& M) const {307if (gen==true) {308M.print(name,"gen");309}310}311//---------------------------------------------------------------------------312313template<typename Number>314void Output<Number>::write_matrix_msp(const Matrix<Number>& M) const {315if (msp==true) {316M.print(name,"msp");317}318}319320//---------------------------------------------------------------------------321322template<typename Number>323void Output<Number>::write_tri() const{324if (tri==true) {325string file_name = name+".tri";326ofstream out(file_name.c_str());327328const vector< pair<vector<libQnormaliz::key_t>,Number> >& Tri = Result->getTriangulation();329typename vector< pair<vector<libQnormaliz::key_t>,Number> >::const_iterator tit = Tri.begin();330const vector<vector<bool> >& Dec = Result->isComputed(ConeProperty::ConeDecomposition) ?331Result->getOpenFacets() : vector<vector<bool> >();332typename vector< vector<bool> >::const_iterator idd = Dec.begin();333334out << Tri.size() << endl;335size_t nr_extra_entries=1;336if (Result->isComputed(ConeProperty::ConeDecomposition))337nr_extra_entries+=Result->getSublattice().getRank();338out << Result->getSublattice().getRank()+nr_extra_entries << endl; //works also for empty list339340for(; tit != Tri.end(); ++tit) {341for (size_t i=0; i<tit->first.size(); i++) {342out << tit->first[i] +1 << " ";343}344out << " " << tit->second;345if(Result->isComputed(ConeProperty::ConeDecomposition)){346out << " ";347for (size_t i=0; i<tit->first.size(); i++) {348out << " " << (*idd)[i];349}350idd++;351}352out << endl;353}354if (Result->isTriangulationNested()) out << "nested" << endl;355else out << "plain" << endl;356if (Result->isTriangulationPartial()) out << "partial" << endl;357out.close();358}359}360361//---------------------------------------------------------------------------362363template<typename Number>364void Output<Number>::write_matrix_ht1(const Matrix<Number>& M) const{365if (ht1==true) {366M.print(name,"ht1");367}368}369370//---------------------------------------------------------------------------371372string is_maximal(long a, long b) {373return (a == b) ? " (maximal)" : "";374}375376//---------------------------------------------------------------------------377378template<typename Number>379void Output<Number>::write_inv_file() const{380if (inv==true) {//printing .inv file381382string name_open=name+".inv"; //preparing output files383const char* file=name_open.c_str();384ofstream inv(file);385386if (Result->isComputed(ConeProperty::VerticesOfPolyhedron)) {387inv << "integer number_vertices_polyhedron = "388<< Result->getNrVerticesOfPolyhedron() << endl;389}390if (Result->isComputed(ConeProperty::ExtremeRays)) {391size_t nr_ex_rays = Result->getNrExtremeRays();392inv<<"integer number_extreme_rays = "<<nr_ex_rays<<endl;393}394if (Result->isComputed(ConeProperty::MaximalSubspace)) {395size_t dim_max_subspace = Result->getDimMaximalSubspace();396inv<<"integer dim_max_subspace = "<<dim_max_subspace<<endl;397}398399inv << "integer embedding_dim = " << dim << endl;400if (!homogeneous){401if (Result->isComputed(ConeProperty::AffineDim))402inv << "integer affine_dim_polyhedron = " << Result->getAffineDim() << endl;403if (Result->isComputed(ConeProperty::RecessionRank))404inv << "integer recession_rank = " << Result->getRecessionRank() << endl;405}406if (Result->isComputed(ConeProperty::SupportHyperplanes)) {407inv<<"integer number_support_hyperplanes = "<<Result->getNrSupportHyperplanes()<<endl;408}409if (Result->isComputed(ConeProperty::TriangulationSize)) {410inv << "integer size_triangulation = " << Result->getTriangulationSize() << endl;411}412if (Result->isComputed(ConeProperty::TriangulationDetSum)) {413inv << "integer sum_dets = " << Result->getTriangulationDetSum() << endl;414}415416if (!Result->isComputed(ConeProperty::Dehomogenization)) {417inv << "boolean inhomogeneous = false" << endl;418}419else {420inv << "boolean inhomogeneous = true" << endl;421vector<Number> Linear_Form = Result->getDehomogenization();422inv << "vector " << Linear_Form.size()423<< " dehomogenization = " << Linear_Form;424}425426inv.close();427}428}429430431//---------------------------------------------------------------------------432433template<typename Number>434void Output<Number>::write_files() const {435vector<libQnormaliz::key_t> rees_ideal_key;436437if (esp && Result->isComputed(ConeProperty::SupportHyperplanes) && Result->isComputed(ConeProperty::Sublattice)) {438//write the suport hyperplanes of the full dimensional cone439const Sublattice_Representation<Number>& BasisChange = Result->getSublattice();440Matrix<Number> Support_Hyperplanes_Full_Cone = BasisChange.to_sublattice_dual(Result->getSupportHyperplanesMatrix());441// Support_Hyperplanes_Full_Cone.print(name,"esp");442string esp_string = name+".esp";443const char* esp_file = esp_string.c_str();444ofstream esp_out(esp_file);445Support_Hyperplanes_Full_Cone.print(esp_out);446esp_out << "inequalities" << endl;447if (Result->isComputed(ConeProperty::Grading)) {448esp_out << 1 << endl << Result->getRank() << endl;449}450if (Result->isComputed(ConeProperty::Dehomogenization)) {451esp_out << 1 << endl << Result->getRank() << endl;452esp_out << BasisChange.to_sublattice_dual(Result->getDehomogenization());453esp_out << "dehomogenization" << endl;454}455esp_out.close();456}457if (tgn && Result->isComputed(ConeProperty::Generators))458Result->getGeneratorsMatrix().print(name,"tgn");459if (tri && Result->isComputed(ConeProperty::Triangulation)) { //write triangulation460write_tri();461}462463if (out==true) { //printing .out file464string name_open=name+".out"; //preparing output files465const char* file=name_open.c_str();466ofstream out(file);467if(out.fail()){468errorOutput() << "Cannot write to output file." << endl;469exit(1);470}471472// write "header" of the .out file473size_t nr_orig_gens = 0;474if (lattice_ideal_input) {475nr_orig_gens = Result->getNrOriginalMonoidGenerators();476out << nr_orig_gens <<" original generators of the toric ring"<<endl;477}478479if (Result->isComputed(ConeProperty::VerticesOfPolyhedron)) {480out << Result->getNrVerticesOfPolyhedron() <<" vertices of polyhedron" << endl;481}482if (Result->isComputed(ConeProperty::ExtremeRays)) {483out << Result->getNrExtremeRays() <<" extreme rays" << of_cone << endl;484}485if (Result->isComputed(ConeProperty::SupportHyperplanes)) {486out << Result->getNrSupportHyperplanes() <<" support hyperplanes"487<< of_polyhedron << endl;488}489out<<endl;490491out << "embedding dimension = " << dim << endl;492if (homogeneous) {493if (Result->isComputed(ConeProperty::Sublattice)) {494auto rank = Result->getRank();495out << "rank = "<< rank << is_maximal(rank,dim) << endl;496}497} else { // now inhomogeneous case498if (Result->isComputed(ConeProperty::AffineDim))499out << "affine dimension of the polyhedron = "500<< Result->getAffineDim() << is_maximal(Result->getAffineDim(),dim-1) << endl;501if (Result->isComputed(ConeProperty::RecessionRank))502out << "rank of recession monoid = " << Result->getRecessionRank() << endl;503}504505if(Result->isComputed(ConeProperty::MaximalSubspace)){506size_t dim_max_subspace=Result->getDimMaximalSubspace();507if(dim_max_subspace>0)508out << "dimension of maximal subspace = " << dim_max_subspace << endl;509}510511out << endl;512if (Result->isComputed(ConeProperty::TriangulationSize)) {513out << "size of ";514if (Result->isTriangulationNested()) out << "nested ";515if (Result->isTriangulationPartial()) out << "partial ";516out << "triangulation = " << Result->getTriangulationSize() << endl;517}518if (Result->isComputed(ConeProperty::TriangulationDetSum)) {519out << "resulting sum of |det|s = " << Result->getTriangulationDetSum() << endl;520}521if (Result->isComputed(ConeProperty::TriangulationSize)) {522out << endl;523}524if ( Result->isComputed(ConeProperty::Dehomogenization) ) {525out << "dehomogenization:" << endl526<< Result->getDehomogenization() << endl;527}528529out << "***********************************************************************"530<< endl << endl;531532if (Result->isComputed(ConeProperty::VerticesOfPolyhedron)) {533out << Result->getNrVerticesOfPolyhedron() <<" vertices of polyhedron:" << endl;534Result->getVerticesOfPolyhedronMatrix().pretty_print(out);535out << endl;536}537if (Result->isComputed(ConeProperty::ExtremeRays)) {538out << Result->getNrExtremeRays() << " extreme rays" << of_cone << ":" << endl;539Result->getExtremeRaysMatrix().pretty_print(out);540out << endl;541if (ext) {542// for the .gen file we append the vertices of polyhedron if there are any543if (Result->isComputed(ConeProperty::VerticesOfPolyhedron)) {544Matrix<Number> Extreme_Rays(Result->getExtremeRaysMatrix());545Extreme_Rays.append(Result->getVerticesOfPolyhedronMatrix());546write_matrix_ext(Extreme_Rays);547} else {548write_matrix_ext(Result->getExtremeRaysMatrix());549}550}551}552553if(Result->isComputed(ConeProperty::MaximalSubspace) && Result->getDimMaximalSubspace()>0){554out << Result->getDimMaximalSubspace() <<" basis elements of maximal subspace:" << endl;555Result->getMaximalSubspaceMatrix().pretty_print(out);556out << endl;557if(msp)558write_matrix_msp(Result->getMaximalSubspaceMatrix());559}560561//write constrains (support hyperplanes, congruences, equations)562563if (Result->isComputed(ConeProperty::SupportHyperplanes)) {564const Matrix<Number>& Support_Hyperplanes = Result->getSupportHyperplanesMatrix();565out << Support_Hyperplanes.nr_of_rows() <<" support hyperplanes"566<< of_polyhedron << ":" << endl;567Support_Hyperplanes.pretty_print(out);568out << endl;569}570if (Result->isComputed(ConeProperty::Sublattice)) {571const Sublattice_Representation<Number>& BasisChange = Result->getSublattice();572//equations573const Matrix<Number>& Equations = BasisChange.getEquationsMatrix();574size_t nr_of_equ = Equations.nr_of_rows();575if (nr_of_equ > 0) {576out << nr_of_equ <<" equations:" <<endl;577Equations.pretty_print(out);578out << endl;579}580581//lattice582const Matrix<Number>& LatticeBasis = BasisChange.getEmbeddingMatrix();583size_t nr_of_latt = LatticeBasis.nr_of_rows();584if (nr_of_latt < dim) {585out << nr_of_latt <<" basis elements of lattice:" <<endl;586LatticeBasis.pretty_print(out);587out << endl;588}589if(lat)590write_matrix_lat(LatticeBasis);591592593if (cst && Result->isComputed(ConeProperty::SupportHyperplanes)) {594const Matrix<Number>& Support_Hyperplanes = Result->getSupportHyperplanesMatrix();595string cst_string = name+".cst";596const char* cst_file = cst_string.c_str();597ofstream cst_out(cst_file);598599Support_Hyperplanes.print(cst_out);600cst_out<<"inequalities"<<endl;601Equations.print(cst_out);602cst_out<<"equations"<<endl;603604if (Result->isComputed(ConeProperty::Dehomogenization)) {605cst_out << 1 << endl << dim << endl;606cst_out << Result->getDehomogenization();607cst_out << "dehomogenization" << endl;608}609cst_out.close();610}611}612613out.close();614}615616write_inv_file();617}618619620621