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: 418346#include "polymake_cone.h"123Obj REAL_CREATE_CONE_BY_RAYS( Polymake_Data* data, Obj rays ){45if( ! IS_PLIST( rays ) ){6ErrorMayQuit( "not a plain list", 0, 0);7return NULL;8}910int len = LEN_PLIST( rays );11Obj akt = ELM_PLIST( rays, 1 );12Obj elem;1314#ifdef MORE_TESTS15if( !IS_PLIST( akt ) ){16ErrorMayQuit( "first ray is not a plain list", 0, 0);17return NULL;18}19#endif2021int len_elem = LEN_PLIST( akt );22data->main_polymake_session->set_application("polytope");2324pm::Integer* ratarray = new pm::Integer[(len)*(len_elem)];2526for(int i=0;i<len;i++){27akt = ELM_PLIST( rays, i+1 );28#ifdef MORE_TESTS29if( !IS_PLIST( akt ) ){30delete [] ratarray;31ErrorMayQuit( "one ray is not a plain list", 0, 0);32return NULL;33}34if( LEN_PLIST( akt ) != len_elem ){35delete [] ratarray;36ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);37return NULL;38}39#endif40for(int j = 0; j < len_elem; j++){41elem = ELM_PLIST( akt, j+1);42#ifdef MORE_TESTS43if( ! IS_INTOBJ( elem) ){44delete [] ratarray;45ErrorMayQuit( "some entries are not integers", 0, 0);46return NULL;47}48#endif49ratarray[ ( i * len_elem ) + j] = static_cast<pm::Integer>(INT_INTOBJ( elem ));50}5152}5354pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);55delete [] ratarray;56perlobj* p = new perlobj("Cone");57p->take("INPUT_RAYS") << *matr;5859delete matr;6061elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_CONE);6263POLYMAKEOBJ_SET_PERLOBJ(elem, p);6465return elem;66}676869Obj REAL_CREATE_CONE_BY_RAYS_UNSAVE( Polymake_Data* data, Obj rays ){7071if( ! IS_PLIST( rays ) ){72ErrorMayQuit( "not a plain list", 0, 0);73return NULL;74}7576int len = LEN_PLIST( rays );77Obj akt = ELM_PLIST( rays, 1 );78Obj elem;7980#ifdef MORE_TESTS81if( !IS_PLIST( akt ) ){82ErrorMayQuit( "first ray is not a plain list", 0, 0);83return NULL;84}85#endif8687int len_elem = LEN_PLIST( akt );88data->main_polymake_session->set_application("polytope");8990pm::Integer* ratarray;91ratarray = new pm::Integer[(len)*(len_elem)];9293for(int i=0;i<len;i++){94akt = ELM_PLIST( rays, i+1 );95#ifdef MORE_TESTS96if( !IS_PLIST( akt ) ){97delete [] ratarray;98ErrorMayQuit( "one ray is not a plain list", 0, 0);99return NULL;100}101if( LEN_PLIST( akt ) != len_elem ){102delete [] ratarray;103ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);104return NULL;105}106#endif107for(int j = 0; j < len_elem; j++){108elem = ELM_PLIST( akt, j+1);109#ifdef MORE_TESTS110if( ! IS_INTOBJ( elem) ){111delete [] ratarray;112ErrorMayQuit( "some entries are not integers", 0, 0);113return NULL;114}115#endif116ratarray[ ( i * len_elem ) + j] = INT_INTOBJ( elem );117}118119}120121pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);122delete [] ratarray;123perlobj* p = new perlobj("Cone");124p->take("RAYS") << *matr;125delete matr;126elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_CONE);127128POLYMAKEOBJ_SET_PERLOBJ(elem, p);129130return elem;131}132133134Obj REAL_CREATE_CONE_BY_INEQUALITIES( Polymake_Data* data, Obj rays ){135136if( ! IS_PLIST( rays ) ){137ErrorMayQuit( "not a plain list 1", 0, 0);138return NULL;139}140141int len = LEN_PLIST( rays );142Obj akt = ELM_PLIST( rays, 1 );143Obj elem;144145#ifdef MORE_TESTS146if( !IS_PLIST( akt ) ){147ErrorMayQuit( "not a plain list 2", 0, 0);148return NULL;149}150#endif151152int len_elem = LEN_PLIST( akt );153data->main_polymake_session->set_application("polytope");154155pm::Integer* ratarray;156ratarray = new pm::Integer[(len)*(len_elem)];157158for(int i=0;i<len;i++){159akt = ELM_PLIST( rays, i+1 );160#ifdef MORE_TESTS161if( !IS_PLIST( akt ) ){162delete [] ratarray;163ErrorMayQuit( "not a plain list 3", 0, 0);164return NULL;165}166if( LEN_PLIST( akt ) != len_elem ){167delete [] ratarray;168ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);169return NULL;170}171#endif172for(int j = 0; j < len_elem; j++){173elem = ELM_PLIST( akt, j+1);174#ifdef MORE_TESTS175if( ! IS_INTOBJ( elem) ){176delete [] ratarray;177ErrorMayQuit( "some entries are not integers", 0, 0);178return NULL;179}180#endif181ratarray[ ( i * len_elem ) + j] = INT_INTOBJ( elem );182}183184}185186pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);187delete [] ratarray;188perlobj* p = new perlobj("Cone");189p->take("INEQUALITIES") << *matr;190//this needs to be fixed!!191delete matr;192elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_CONE);193POLYMAKEOBJ_SET_PERLOBJ(elem, p);194return elem;195}196197198Obj REAL_CREATE_DUAL_CONE_OF_CONE( Polymake_Data* data, Obj cone ){199200#ifdef MORE_TESTS201if(! IS_POLYMAKE_CONE(cone) ){202ErrorMayQuit(" parameter is not an integer.",0,0);203return NULL;204}205#endif206207perlobj* coneobj = PERLOBJ_POLYMAKEOBJ(cone);208209data->main_polymake_session->set_application_of(*coneobj);210pm::Matrix<pm::Rational> matr;211try212{213pm::Matrix<pm::Rational> matr_temp = coneobj->give("FACETS"); //That is the ugliest solution214matr = matr_temp;215}216217POLYMAKE_GAP_CATCH218219perlobj* p;220p = new perlobj("Cone<Rational>");221pm::Matrix<pm::Rational> matr2;222try223{224pm::Matrix<pm::Rational> matr2_temp = coneobj->give("LINEAR_SPAN");225matr2 = matr2_temp;226}227228POLYMAKE_GAP_CATCH229230pm::Matrix<pm::Rational>* matr3 = new pm::Matrix<pm::Rational>(matr.rows()+2*matr2.rows(), matr.cols());231for(int i = 0; i < matr.rows(); i++)232matr3->row(i) = matr.row(i);233for(int i = 0; i < matr2.rows(); i++)234matr3->row(matr.rows()+i) = matr2.row(i);235for(int i = 0; i < matr2.rows(); i++)236matr3->row(matr.rows()+matr2.rows()+i) = -(matr2.row(i));237p->take("INPUT_RAYS") << *matr3;238delete matr3;239240Obj elem = NewPolymakeExternalObject( T_POLYMAKE_EXTERNAL_CONE );241242POLYMAKEOBJ_SET_PERLOBJ( elem, p );243244return elem;245246}247248249// Obj REAL_GENERATING_RAYS_OF_CONE( Polymake_Data* data, Obj cone){250//251// #ifdef MORE_TESTS252// if( ( ! IS_POLYMAKE_CONE(cone) ) && ( ! IS_POLYMAKE_FAN(cone) ) ){253// ErrorMayQuit(" parameter is not a cone or fan.",0,0);254// return NULL;255// }256// #endif257//258// perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );259// data->main_polymake_session->set_application_of(*coneobj);260// pm::Matrix<pm::Rational> matr = coneobj->give("RAYS");261// Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());262// SET_LEN_PLIST( RETLI , matr.rows() );263// Obj LIZeil;264// pm::Rational nenner;265// pm::Rational dentemp;266// for(int i = 0;i<matr.rows();i++){267// LIZeil = NEW_PLIST( T_PLIST, matr.cols());268// SET_LEN_PLIST( LIZeil , matr.cols() );269// nenner = 1;270// for(int j = 0;j<matr.cols();j++){271// CallPolymakeFunction("denominator",matr(i,j)) >> dentemp;272// CallPolymakeFunction("lcm",nenner, dentemp ) >> nenner;273// }274// for(int j = 0;j<matr.cols();j++){275// SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT(matr(i,j)*nenner));276// }277// SET_ELM_PLIST(RETLI,i+1,LIZeil);278// CHANGED_BAG(RETLI);279// }280// return RETLI;281// }282283284Obj REAL_GENERATING_RAYS_OF_CONE( Polymake_Data* data, Obj cone){285286#ifdef MORE_TESTS287if(! IS_POLYMAKE_CONE(cone) ){288ErrorMayQuit(" parameter is not an integer.",0,0);289return NULL;290}291#endif292perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );293data->main_polymake_session->set_application_of(*coneobj);294pm::Matrix<pm::Rational> matr;295try{296pm::Matrix<pm::Rational> matr_temp = coneobj->give("RAYS");297matr = matr_temp;298}299300POLYMAKE_GAP_CATCH301302pm::Matrix<pm::Rational> matr2;303try{304pm::Matrix<pm::Rational> matr2_temp = coneobj->give("LINEALITY_SPACE");305matr2 = matr2_temp;306}307308POLYMAKE_GAP_CATCH309310Obj RETLI = NEW_PLIST( T_PLIST , matr.rows() + 2*matr2.rows());311UInt lenght_RETLI = matr.rows() + 2*matr2.rows();312SET_LEN_PLIST( RETLI , lenght_RETLI );313314polymake::common::primitive( matr );315polymake::common::primitive( matr2 );316317Obj LIZeil;318UInt matr_cols = matr.cols();319for(int i = 0;i<matr.rows();i++){320LIZeil = NEW_PLIST( T_PLIST, matr.cols());321SET_LEN_PLIST( LIZeil , matr_cols );322for(int j = 0;j<matr.cols();j++){323SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT( (matr(i,j)).to_int() ));324}325SET_ELM_PLIST(RETLI,i+1,LIZeil);326CHANGED_BAG(RETLI);327}328329UInt matr2_cols = matr2.cols();330331for(int i = 0;i<matr2.rows();i++){332LIZeil = NEW_PLIST( T_PLIST, matr2.cols());333SET_LEN_PLIST( LIZeil ,matr2_cols );334for(int j = 0;j<matr2.cols();j++){335SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT( (matr2(i,j)).to_int() ));336}337SET_ELM_PLIST(RETLI,matr.rows() + i +1,LIZeil);338CHANGED_BAG(RETLI);339}340341for(int i = 0;i<matr2.rows();i++){342LIZeil = NEW_PLIST( T_PLIST, matr2.cols());343SET_LEN_PLIST( LIZeil ,matr2_cols);344for(int j = 0;j<matr2.cols();j++){345SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT( (-matr2(i,j)).to_int() ));346}347SET_ELM_PLIST(RETLI,matr.rows() + matr2.rows() + i +1,LIZeil);348CHANGED_BAG(RETLI);349}350return RETLI;351}352353354Obj REAL_LINEALITY_SPACE_OF_CONE( Polymake_Data* data, Obj cone){355356#ifdef MORE_TESTS357if( ( ! IS_POLYMAKE_CONE(cone) ) ){358ErrorMayQuit(" parameter is not a cone or fan.",0,0);359return NULL;360}361#endif362363perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );364data->main_polymake_session->set_application_of(*coneobj);365pm::Matrix<pm::Rational> matr;366try367{368pm::Matrix<pm::Rational> matr_temp = coneobj->give("LINEALITY_SPACE");369matr = matr_temp;370}371372POLYMAKE_GAP_CATCH373374375polymake::common::primitive( matr );376377Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());378UInt matr_rows = matr.rows();379SET_LEN_PLIST( RETLI , matr_rows );380Obj LIZeil;381UInt matr_cols = matr.cols();382for(int i = 0;i<matr.rows();i++){383LIZeil = NEW_PLIST( T_PLIST, matr.cols());384SET_LEN_PLIST( LIZeil , matr_cols );385for(int j = 0;j<matr.cols();j++){386SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT( (matr(i,j)).to_int() ));387}388SET_ELM_PLIST(RETLI,i+1,LIZeil);389CHANGED_BAG(RETLI);390}391return RETLI;392}393394395Obj REAL_HILBERT_BASIS_OF_CONE( Polymake_Data* data, Obj cone){396397#ifdef MORE_TESTS398if(! IS_POLYMAKE_CONE(cone) ){399ErrorMayQuit(" parameter is not a cone.",0,0);400return NULL;401}402#endif403404perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );405data->main_polymake_session->set_application_of(*coneobj);406pm::Matrix<pm::Rational> matr;407try408{409pm::Array<pm::Matrix<pm::Rational> > matr_temp = coneobj->give("HILBERT_BASIS_GENERATORS");410matr = matr_temp[0];411}412413POLYMAKE_GAP_CATCH414415Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());416UInt matr_rows = matr.rows();417SET_LEN_PLIST( RETLI , matr_rows );418Obj LIZeil;419UInt matr_cols = matr.cols();420for(int i = 0;i<matr.rows();i++){421LIZeil = NEW_PLIST( T_PLIST, matr.cols());422SET_LEN_PLIST( LIZeil ,matr_cols);423for(int j = 0;j<matr.cols();j++){424SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT((matr(i,j)).to_int()));425}426SET_ELM_PLIST(RETLI,i+1,LIZeil);427}428return RETLI;429430}431432Obj REAL_RAYS_IN_FACETS( Polymake_Data* data, Obj cone){433434#ifdef MORE_TESTS435if( ( ! IS_POLYMAKE_CONE(cone) ) and ( ! IS_POLYMAKE_POLYTOPE( cone ) ) ){436ErrorMayQuit("parameter is not a cone.",0,0);437return NULL;438}439#endif440441perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );442data->main_polymake_session->set_application_of(*coneobj);443pm::IncidenceMatrix<pm::NonSymmetric> matr;444try445{446pm::IncidenceMatrix<pm::NonSymmetric> matr_temp = coneobj->give("RAYS_IN_FACETS");447matr = matr_temp;448}449450POLYMAKE_GAP_CATCH451452Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());453UInt matr_rows = matr.rows();454SET_LEN_PLIST( RETLI , matr_rows );455Obj LIZeil;456UInt matr_cols = matr.cols();457for(int i = 0;i<matr.rows();i++){458LIZeil = NEW_PLIST( T_PLIST, matr.cols());459SET_LEN_PLIST( LIZeil , matr_cols );460for(int j = 0;j<matr.cols();j++){461SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT(matr(i,j)));462}463SET_ELM_PLIST(RETLI,i+1,LIZeil);464CHANGED_BAG(RETLI);465}466return RETLI;467468}469470471Obj REAL_DEFINING_INEQUALITIES_OF_CONE( Polymake_Data* data, Obj cone){472473#ifdef MORE_TESTS474if(! IS_POLYMAKE_CONE(cone) ){475ErrorMayQuit("parameter is not a polymake cone",0,0);476return NULL;477}478#endif479480perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );481data->main_polymake_session->set_application_of(*coneobj);482pm::Matrix<pm::Integer> matr;483pm::Matrix<pm::Integer> matr2;484485try{486coneobj->give("FACETS") >> matr;487coneobj->give("LINEAR_SPAN") >> matr2;488}489490POLYMAKE_GAP_CATCH491492Obj RETLI = NEW_PLIST( T_PLIST , matr.rows() + 2*matr2.rows());493UInt lenght_RETLI = matr.rows() + 2*matr2.rows();494SET_LEN_PLIST( RETLI , lenght_RETLI );495Obj LIZeil;496497UInt matr_cols = matr.cols();498for(int i = 0;i<matr.rows();i++){499LIZeil = NEW_PLIST( T_PLIST, matr.cols());500SET_LEN_PLIST( LIZeil , matr_cols );501for(int j = 0;j<matr.cols();j++){502SET_ELM_PLIST(LIZeil, j+1, INTOBJ_INT( matr(i,j).to_int() ) );503CHANGED_BAG( LIZeil );504}505SET_ELM_PLIST(RETLI,i+1,LIZeil);506CHANGED_BAG(RETLI);507}508509UInt matr2_cols = matr2.cols();510511Obj LIZeil2;512513for(int i = 0;i<matr2.rows();i++){514LIZeil = NEW_PLIST( T_PLIST, matr2.cols());515LIZeil2 = NEW_PLIST( T_PLIST, matr2.cols() );516SET_LEN_PLIST( LIZeil , matr2_cols );517SET_LEN_PLIST( LIZeil2, matr2_cols );518for(int j = 0;j<matr2.cols();j++){519SET_ELM_PLIST( LIZeil, j+1, INTOBJ_INT( matr2(i,j).to_int() ));520CHANGED_BAG( LIZeil );521SET_ELM_PLIST( LIZeil2, j+1, INTOBJ_INT( -matr2(i,j).to_int() ) );522CHANGED_BAG( LIZeil2 );523524}525SET_ELM_PLIST(RETLI,matr.rows() + i +1 ,LIZeil);526CHANGED_BAG(RETLI);527SET_ELM_PLIST(RETLI, matr.rows() + matr2.rows() + i + 1, LIZeil2 );528CHANGED_BAG(RETLI);529}530return RETLI;531532}533534Obj REAL_INTERSECTION_OF_CONES( Polymake_Data* data, Obj cone1, Obj cone2){535536#ifdef MORE_TESTS537if(! IS_POLYMAKE_CONE(cone1) || ! IS_POLYMAKE_CONE(cone2) ){538ErrorMayQuit(" parameter is not a cone.",0,0);539return NULL;540}541#endif542543perlobj* coneobj1 = PERLOBJ_POLYMAKEOBJ( cone1 );544perlobj* coneobj2 = PERLOBJ_POLYMAKEOBJ( cone2 );545546data->main_polymake_session->set_application_of( *coneobj1 );547548perlobj intersec;549550CallPolymakeFunction( "intersection", *coneobj1, *coneobj2 ) >> intersec;551552perlobj* returnobj = new perlobj(intersec);553554Obj elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_CONE);555556POLYMAKEOBJ_SET_PERLOBJ( elem, returnobj );557558return elem;559560}561562Obj REAL_EQUALITIES_OF_CONE( Polymake_Data* data, Obj cone){563564#ifdef MORE_TESTS565if(! IS_POLYMAKE_CONE(cone) ){566ErrorMayQuit(" parameter is not an integer.",0,0);567return NULL;568}569#endif570perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( cone );571data->main_polymake_session->set_application_of(*coneobj);572pm::Matrix<pm::Rational> matr = coneobj->give("LINEAR_SPAN");573Obj RETLI = NEW_PLIST( T_PLIST , matr.rows() );574UInt matr_rows = matr.rows();575SET_LEN_PLIST( RETLI ,matr_rows);576Obj LIZeil;577polymake::common::primitive( matr );578UInt matr_cols = matr.cols();579for(int i = 0;i<matr.rows();i++){580LIZeil = NEW_PLIST( T_PLIST, matr.cols());581SET_LEN_PLIST( LIZeil , matr_cols );582for(int j = 0;j<matr.cols();j++){583SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT( (matr(i,j)).to_int() ));584}585SET_ELM_PLIST(RETLI,i+1,LIZeil);586CHANGED_BAG(RETLI);587}588return RETLI;589}590591Obj REAL_CREATE_CONE_BY_EQUALITIES_AND_INEQUALITIES( Polymake_Data* data, Obj eqs, Obj ineqs ){592593if( ! IS_PLIST( eqs ) || ! IS_PLIST( ineqs ) ){594ErrorMayQuit( "not a plain list", 0, 0);595return NULL;596}597598int numberofrays = LEN_PLIST( eqs );599Obj akt;600Obj elem;601Obj numb;602data->main_polymake_session->set_application("polytope");603int dimension = LEN_PLIST( ELM_PLIST( eqs, 1 ) );604pm::Integer* ratarray;605ratarray = new pm::Integer[ numberofrays*dimension ];606for(int i=0;i<numberofrays;i++){607akt = ELM_PLIST( eqs, i+1 );608#ifdef MORE_TESTS609if( !IS_PLIST( akt ) || LEN_PLIST( akt ) != dimension ){610delete [] ratarray;611ErrorMayQuit( "one ray is not a plain list", 0, 0);612return NULL;613}614#endif615for(int j = 0; j<dimension; j++){616numb = ELM_PLIST( akt, j+1 );617#ifdef MORE_TESTS618if( ! IS_INTOBJ( numb ) ){619delete [] ratarray;620ErrorMayQuit( "some entries are not integers", 0, 0);621return NULL;622}623#endif624ratarray[(i*dimension)+j] = INT_INTOBJ( numb );625}626}627int numberofcones = LEN_PLIST( ineqs );628pm::Integer* incMatr = new pm::Integer[numberofcones*dimension];629for(int i=0;i<numberofcones;i++){630akt = ELM_PLIST( ineqs, i+1 );631#ifdef MORE_TESTS632if( !IS_PLIST( akt ) || LEN_PLIST( akt ) != dimension ){633delete [] incMatr;634ErrorMayQuit( "one ray is not a plain list", 0, 0);635return NULL;636}637#endif638for(int j = 0; j<dimension; j++){639numb = ELM_PLIST( akt, j+1 );640#ifdef MORE_TESTS641if( ! IS_INTOBJ( numb ) ){642delete [] incMatr;643ErrorMayQuit( "some entries are not integers", 0, 0);644return NULL;645}646#endif647incMatr[(i*dimension)+j] = INT_INTOBJ( numb );648}649}650651pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(numberofrays,dimension,ratarray);652pm::Matrix<pm::Integer>* matr2 = new pm::Matrix<pm::Integer>(numberofcones,dimension,incMatr);653perlobj* q = new perlobj("Cone<Rational>");654q->take("EQUATIONS") << *matr;655q->take("INEQUALITIES") << *matr2;656elem = NewPolymakeExternalObject( T_POLYMAKE_EXTERNAL_CONE );657POLYMAKEOBJ_SET_PERLOBJ( elem, q);658delete [] ratarray;659delete [] incMatr;660delete matr;661delete matr2;662return elem;663}664665666