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_polytopes.h"123Obj REAL_CREATE_POLYTOPE_BY_POINTS( Polymake_Data* data, Obj polytope ){45#ifdef MORE_TESTS6if( ! IS_PLIST( polytope ) ){7ErrorMayQuit( "not a plain list", 0, 0);8return NULL;9}10#endif1112int len = LEN_PLIST( polytope );13Obj akt = ELM_PLIST( polytope, 1 );14Obj elem;1516#ifdef MORE_TESTS17if( !IS_PLIST( akt ) ){18ErrorMayQuit( "not a plain list", 0, 0);19return NULL;20}21#endif2223int len_elem = LEN_PLIST( akt );24data->main_polymake_session->set_application("polytope");2526pm::Integer* ratarray;27ratarray = new pm::Integer[(len)*(len_elem+1)];2829for(int i=1;i<=len;i++){30akt = ELM_PLIST( polytope, i );31#ifdef MORE_TESTS32if( !IS_PLIST( akt ) ){33delete [] ratarray;34ErrorMayQuit( "not a plain list", 0, 0);35return NULL;36}37if( LEN_PLIST( akt ) != len_elem ){38delete [] ratarray;39ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);40return NULL;41}42#endif4344ratarray[(i-1)*(len_elem+1)] = 1;45for(int j = 1; j <= len_elem; j++){46elem = ELM_PLIST( akt, j);4748#ifdef MORE_TESTS49if( ! IS_INTOBJ( elem ) ){50delete [] ratarray;51ErrorMayQuit( "some entries are not integers", 0, 0);52return NULL;53}54#endif5556ratarray[(i-1)*(len_elem+1)+j] = INT_INTOBJ( elem );57}5859}6061pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem+1,ratarray);62delete [] ratarray;63perlobj* p = new perlobj("LatticePolytope");64p->take("POINTS") << *matr;65delete matr;66elem = NewPolymakeExternalObject( T_POLYMAKE_EXTERNAL_POLYTOPE );67POLYMAKEOBJ_SET_PERLOBJ( elem, p );68return elem;69}70717273Obj REAL_VERTICES_OF_POLYTOPE( Polymake_Data* data, Obj polytope){7475#ifdef MORE_TESTS76if(! IS_POLYMAKE_POLYTOPE(polytope) ){77ErrorMayQuit(" parameter is not a polytope.",0,0);78return NULL;79}80#endif8182perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );83data->main_polymake_session->set_application_of(*polyobj);84pm::Matrix<pm::Rational> matr;85try{86pm::Matrix<pm::Rational> matr_temp = polyobj->give("VERTICES");87matr = matr_temp;88}8990POLYMAKE_GAP_CATCH9192UInt l = 10;93Obj RETLI = NEW_PLIST( T_PLIST , l );94SET_LEN_PLIST(RETLI, l );95UInt k = 0;96Obj LIZeil;97UInt matr_cols = matr.cols() - 1;98for(int i = 0;i<matr.rows();i++){99if( matr(i,0) == 1 ){100if( ++k > l){101GROW_PLIST(RETLI,l*=2);102SET_LEN_PLIST(RETLI, l );103}104LIZeil = NEW_PLIST( T_PLIST, matr.cols()-1);105SET_LEN_PLIST( LIZeil , matr_cols );106for(int j = 1;j<matr.cols();j++){107SET_ELM_PLIST(LIZeil,j,INTOBJ_INT((matr(i,j)).to_int()));108}109SET_ELM_PLIST(RETLI,k,LIZeil);110CHANGED_BAG(RETLI);111}112}113SHRINK_PLIST(RETLI,k);114SET_LEN_PLIST(RETLI, k );115return RETLI;116117}118119120121Obj REAL_LATTICE_POINTS_OF_POLYTOPE( Polymake_Data* data, Obj polytope){122123#ifdef MORE_TESTS124if(! IS_POLYMAKE_POLYTOPE(polytope) ){125ErrorMayQuit(" parameter is not a polytope.",0,0);126return NULL;127}128#endif129130perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );131data->main_polymake_session->set_application_of(*polyobj);132pm::Matrix<pm::Rational> matr;133try{134pm::Array<pm::Matrix<pm::Rational> > matr_temp = polyobj->give("LATTICE_POINTS_GENERATORS");135matr = matr_temp[ 1 ];136}137138POLYMAKE_GAP_CATCH139140Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());141UInt matr_rows = matr.rows();142SET_LEN_PLIST( RETLI , matr_rows );143Obj LIZeil;144UInt matr_cols = matr.cols() - 1;145for(int i = 0;i<matr.rows();i++){146LIZeil = NEW_PLIST( T_PLIST, matr.cols()-1);147SET_LEN_PLIST( LIZeil , matr_cols );148for(int j = 1;j<matr.cols();j++){149SET_ELM_PLIST(LIZeil,j,INTOBJ_INT((matr(i,j)).to_int()));150}151SET_ELM_PLIST(RETLI,i+1,LIZeil);152CHANGED_BAG(RETLI);153}154return RETLI;155156}157158159160Obj REAL_CREATE_POLYTOPE_BY_INEQUALITIES( Polymake_Data* data, Obj polytope){161162#ifdef MORE_TESTS163if( ! IS_PLIST( polytope ) ){164ErrorMayQuit( "not a plain list", 0, 0);165return NULL;166}167#endif168169int len = LEN_PLIST( polytope );170Obj akt = ELM_PLIST( polytope, 1 );171Obj elem;172173#ifdef MORE_TESTS174if( !IS_PLIST( akt ) ){175ErrorMayQuit( "not a plain list", 0, 0);176return NULL;177}178#endif179180int len_elem = LEN_PLIST( akt );181data->main_polymake_session->set_application("polytope");182183pm::Integer* ratarray;184ratarray = new pm::Integer[(len)*(len_elem)];185186for(int i=0;i<len;i++){187akt = ELM_PLIST( polytope, i+1 );188#ifdef MORE_TESTS189if( !IS_PLIST( akt ) ){190delete [] ratarray;191ErrorMayQuit( "not a plain list", 0, 0);192return NULL;193}194if( LEN_PLIST( akt ) != len_elem ){195delete [] ratarray;196ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);197return NULL;198}199#endif200for(int j = 0; j < len_elem; j++){201elem = ELM_PLIST( akt, j+1);202#ifdef MORE_TESTS203if( ! IS_INTOBJ( elem ) ){204delete [] ratarray;205ErrorMayQuit( "some entries are not integers", 0, 0);206return NULL;207}208#endif209ratarray[i*(len_elem)+j] = INT_INTOBJ( elem );210}211212}213214pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);215delete [] ratarray;216perlobj* p = new perlobj("LatticePolytope"); //Maybe Name the Polytope by the Number217p->take("INEQUALITIES") << *matr;218delete matr;219elem = NewPolymakeExternalObject( T_POLYMAKE_EXTERNAL_POLYTOPE );220POLYMAKEOBJ_SET_PERLOBJ( elem, p );221return elem;222}223224225226Obj REAL_FACET_INEQUALITIES_OF_POLYTOPE( Polymake_Data* data, Obj polytope){227228#ifdef MORE_TESTS229if(! IS_POLYMAKE_POLYTOPE(polytope) ){230ErrorMayQuit(" parameter is not a polytope.",0,0);231return NULL;232}233#endif234235perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );236data->main_polymake_session->set_application_of(*polyobj);237pm::Matrix<pm::Rational> matr;238try{239pm::Matrix<pm::Rational> matr_temp = polyobj->give("FACETS");240matr = matr_temp;241}242243POLYMAKE_GAP_CATCH244245Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());246UInt matr_rows = matr.rows();247SET_LEN_PLIST( RETLI , matr_rows );248Obj LIZeil;249UInt matr_cols = matr.cols();250for(int i = 0;i<matr.rows();i++){251LIZeil = NEW_PLIST( T_PLIST, matr.cols() );252SET_LEN_PLIST( LIZeil , matr_cols );253for(int j = 0;j<matr.cols();j++){254SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT((matr(i,j)).to_int()));255}256SET_ELM_PLIST(RETLI,i+1,LIZeil);257CHANGED_BAG(RETLI);258}259return RETLI;260261}262263264Obj REAL_EQUALITIES_OF_POLYTOPE( Polymake_Data* data, Obj polytope){265266#ifdef MORE_TESTS267if(! IS_POLYMAKE_POLYTOPE(polytope) ){268ErrorMayQuit(" parameter is not a polytope.",0,0);269return NULL;270}271#endif272273perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );274data->main_polymake_session->set_application_of(*polyobj);275pm::Matrix<pm::Rational> matr;276try{277pm::Matrix<pm::Rational> matr_temp = polyobj->give("AFFINE_HULL");278matr = matr_temp;279}280281POLYMAKE_GAP_CATCH282283Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());284UInt matr_rows = matr.rows();285SET_LEN_PLIST( RETLI , matr_rows );286Obj LIZeil;287UInt matr_cols = matr.cols();288for(int i = 0;i<matr.rows();i++){289LIZeil = NEW_PLIST( T_PLIST, matr.cols() );290SET_LEN_PLIST( LIZeil , matr_cols );291for(int j = 0;j<matr.cols();j++){292SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT((matr(i,j)).to_int()));293}294SET_ELM_PLIST(RETLI,i+1,LIZeil);295CHANGED_BAG(RETLI);296}297return RETLI;298299}300301302Obj REAL_INTERIOR_LATTICE_POINTS( Polymake_Data* data, Obj polytope){303304#ifdef MORE_TESTS305if(! IS_POLYMAKE_POLYTOPE(polytope) ){306ErrorMayQuit(" parameter is not a polytope.",0,0);307return NULL;308}309#endif310311perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );312data->main_polymake_session->set_application_of(*polyobj);313pm::Matrix<pm::Rational> matr;314try{315pm::Matrix<pm::Rational> matr_temp = polyobj->give("INTERIOR_LATTICE_POINTS");316matr = matr_temp;317}318319POLYMAKE_GAP_CATCH320321Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());322UInt matr_rows = matr.rows();323SET_LEN_PLIST( RETLI , matr_rows );324Obj LIZeil;325UInt matr_cols = matr.cols() - 1;326for(int i = 0;i<matr.rows();i++){327LIZeil = NEW_PLIST( T_PLIST, matr.cols()-1);328SET_LEN_PLIST( LIZeil , matr_cols );329for(int j = 1;j<matr.cols();j++){330SET_ELM_PLIST(LIZeil,j,INTOBJ_INT((matr(i,j)).to_int()));331}332SET_ELM_PLIST(RETLI,i+1,LIZeil);333CHANGED_BAG(RETLI);334}335return RETLI;336337}338339340341Obj REAL_CREATE_POLYTOPE_BY_HOMOGENEOUS_POINTS( Polymake_Data* data, Obj points ){342343if( ! IS_PLIST( points ) ){344ErrorMayQuit( "not a plain list", 0, 0);345return NULL;346}347348int len = LEN_PLIST( points );349Obj akt = ELM_PLIST( points, 1 );350Obj elem;351352#ifdef MORE_TESTS353if( !IS_PLIST( akt ) ){354ErrorMayQuit( "first ray is not a plain list", 0, 0);355return NULL;356}357#endif358359int len_elem = LEN_PLIST( akt );360data->main_polymake_session->set_application("polytope");361362pm::Integer* ratarray;363ratarray = new pm::Integer[(len)*(len_elem)];364365for(int i=0;i<len;i++){366akt = ELM_PLIST( points, i+1 );367#ifdef MORE_TESTS368if( !IS_PLIST( akt ) ){369delete [] ratarray;370ErrorMayQuit( "one ray is not a plain list", 0, 0);371return NULL;372}373if( LEN_PLIST( akt ) != len_elem ){374delete [] ratarray;375ErrorMayQuit( "raygenerators are not of the same lenght", 0, 0);376return NULL;377}378#endif379for(int j = 0; j < len_elem; j++){380elem = ELM_PLIST( akt, j+1);381#ifdef MORE_TESTS382if( ! IS_INTOBJ( elem) ){383delete [] ratarray;384ErrorMayQuit( "some entries are not integers", 0, 0);385return NULL;386}387#endif388ratarray[ ( i * len_elem ) + j] = INT_INTOBJ( elem );389}390391}392393pm::Matrix<pm::Integer>* matr = new pm::Matrix<pm::Integer>(len,len_elem,ratarray);394delete [] ratarray;395perlobj* p = new perlobj("Polytope<Rational>");396p->take("POINTS") << *matr;397delete matr;398elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_POLYTOPE);399400POLYMAKEOBJ_SET_PERLOBJ(elem, p);401402return elem;403}404405406407Obj REAL_HOMOGENEOUS_POINTS_OF_POLYTOPE( Polymake_Data* data, Obj polytope){408409#ifdef MORE_TESTS410if( ( ! IS_POLYMAKE_POLYTOPE(polytope) ) ){411ErrorMayQuit(" parameter is not a polytope.",0,0);412return NULL;413}414#endif415416perlobj* coneobj = PERLOBJ_POLYMAKEOBJ( polytope );417data->main_polymake_session->set_application_of(*coneobj);418pm::Matrix<pm::Rational> matr;419try{420pm::Matrix<pm::Rational> matr_temp = coneobj->give("VERTICES");421matr = matr_temp;422}423424POLYMAKE_GAP_CATCH425426Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());427UInt matr_rows = matr.rows();428SET_LEN_PLIST( RETLI , matr_rows );429Obj LIZeil;430polymake::common::primitive( matr );431UInt matr_cols = matr.cols();432for(int i = 0;i<matr.rows();i++){433LIZeil = NEW_PLIST( T_PLIST, matr.cols());434SET_LEN_PLIST( LIZeil , matr_cols );435for(int j = 0;j<matr.cols();j++){436SET_ELM_PLIST(LIZeil,j+1,INTOBJ_INT((matr(i,j)).to_int()));437}438SET_ELM_PLIST(RETLI,i+1,LIZeil);439CHANGED_BAG(RETLI);440}441return RETLI;442443}444445446447Obj REAL_TAIL_CONE_OF_POLYTOPE( Polymake_Data* data, Obj polytope){448449#ifdef MORE_TESTS450if(! IS_POLYMAKE_POLYTOPE(polytope) ){451ErrorMayQuit(" parameter is not a polytope.",0,0);452return NULL;453}454#endif455456perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );457data->main_polymake_session->set_application_of(*polyobj);458pm::Matrix<pm::Rational> matr;459try{460pm::Matrix<pm::Rational> matr_temp = polyobj->give("VERTICES");461matr = matr_temp;462}463464POLYMAKE_GAP_CATCH465466UInt l = 10;467Obj RETLI = NEW_PLIST( T_PLIST , l );468SET_LEN_PLIST(RETLI, l );469Obj LIZeil;470UInt k = 0;471UInt matr_cols = matr.cols() - 1;472for(int i = 0;i<matr.rows();i++){473if( matr(i,0)==0 ){474if(++k>l){475GROW_PLIST(RETLI,l*=2);476SET_LEN_PLIST(RETLI, l );477}478LIZeil = NEW_PLIST( T_PLIST, matr.cols()-1);479SET_LEN_PLIST( LIZeil , matr_cols );480for(int j = 1;j<matr.cols();j++){481SET_ELM_PLIST(LIZeil,j, INTOBJ_INT( (matr(i,j)).to_int() ) );482}483SET_ELM_PLIST(RETLI,k,LIZeil);484CHANGED_BAG(RETLI);485}486}487SHRINK_PLIST(RETLI,k);488SET_LEN_PLIST(RETLI, k );489return RETLI;490491}492493494Obj REAL_MINKOWSKI_SUM( Polymake_Data* data, Obj polytope1, Obj polytope2 ){495496#ifdef MORE_TESTS497if( (!IS_POLYMAKE_POLYTOPE(polytope1)) || (!IS_POLYMAKE_POLYTOPE(polytope2)) ){498ErrorMayQuit("one parameter is not a polytope.",0,0);499return NULL;500}501#endif502perlobj* poly1 = PERLOBJ_POLYMAKEOBJ( polytope1 );503perlobj* poly2 = PERLOBJ_POLYMAKEOBJ( polytope2 );504data->main_polymake_session->set_application_of(*poly1);505506perlobj sum;507try{508CallPolymakeFunction("minkowski_sum",*poly1,*poly2) >> sum;509}510511POLYMAKE_GAP_CATCH512513perlobj* sumpointer = new perlobj(sum);514Obj elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_POLYTOPE);515POLYMAKEOBJ_SET_PERLOBJ(elem, sumpointer);516return elem;517}518519520521Obj REAL_MINKOWSKI_SUM_WITH_COEFFICIENTS( Polymake_Data* data, Obj fact1, Obj polytope1, Obj fact2, Obj polytope2 ){522523#ifdef MORE_TESTS524if( (!IS_POLYMAKE_POLYTOPE(polytope1)) || (!IS_POLYMAKE_POLYTOPE(polytope2)) ){525ErrorMayQuit("one parameter is not a polytope.",0,0);526return NULL;527}528#endif529530#ifdef MORE_TESTS531if( (!IS_INTOBJ(fact1)) || (!IS_INTOBJ(fact2)) ){532ErrorMayQuit("one parameter is not an integer.",0,0);533return NULL;534}535#endif536537perlobj* poly1 = PERLOBJ_POLYMAKEOBJ( polytope1 );538perlobj* poly2 = PERLOBJ_POLYMAKEOBJ( polytope2 );539data->main_polymake_session->set_application_of(*poly1);540541perlobj sum;542try{543CallPolymakeFunction("minkowski_sum",INT_INTOBJ(fact1),*poly1,INT_INTOBJ(fact2),*poly2) >> sum;544}545546POLYMAKE_GAP_CATCH547548perlobj* sumpointer = new perlobj(sum);549Obj elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_POLYTOPE);550POLYMAKEOBJ_SET_PERLOBJ(elem, sumpointer);551return elem;552}553554555Obj REAL_LATTICE_POINTS_GENERATORS( Polymake_Data* data, Obj polytope ){556#ifdef MORE_TESTS557if(! IS_POLYMAKE_POLYTOPE(polytope) ){558ErrorMayQuit(" parameter is not a polytope.",0,0);559return NULL;560}561#endif562563perlobj* polyobj = PERLOBJ_POLYMAKEOBJ( polytope );564data->main_polymake_session->set_application_of(*polyobj);565pm::Array<pm::Matrix<pm::Rational> > array;566try{567pm::Array<pm::Matrix<pm::Rational> > matr_temp = polyobj->give("LATTICE_POINTS_GENERATORS");568array = matr_temp;569}570571POLYMAKE_GAP_CATCH572573pm::Matrix<pm::Rational> matr = array[ 0 ];574575for(int i=0;i<matr.rows();i++){576for(int j=0;j<matr.cols();j++){577}578}579580Obj RET_ARRAY = NEW_PLIST( T_PLIST, 3 );581SET_LEN_PLIST( RET_ARRAY, (UInt)3 );582for( int index_of_array=0;index_of_array<3;index_of_array++ ){583pm::Matrix<pm::Rational> matr = array[index_of_array];584Obj RETLI = NEW_PLIST( T_PLIST , matr.rows());585UInt matr_rows = matr.rows();586SET_LEN_PLIST( RETLI , matr_rows );587Obj LIZeil;588UInt matr_cols = matr.cols() - 1;589for(int i = 0;i<matr.rows();i++){590LIZeil = NEW_PLIST( T_PLIST, matr.cols()-1);591SET_LEN_PLIST( LIZeil , matr_cols );592for(int j = 1;j<matr.cols();j++){593SET_ELM_PLIST(LIZeil,j,INTOBJ_INT((matr(i,j)).to_int()));594}595SET_ELM_PLIST(RETLI,i+1,LIZeil);596CHANGED_BAG(RETLI);597}598SET_ELM_PLIST( RET_ARRAY, index_of_array + 1, RETLI );599CHANGED_BAG( RET_ARRAY );600}601return RET_ARRAY;602603}604605606Obj REAL_INTERSECTION_OF_POLYTOPES( Polymake_Data* data, Obj cone1, Obj cone2){607608#ifdef MORE_TESTS609if(! IS_POLYMAKE_POLYTOPE(cone1) || ! IS_POLYMAKE_POLYTOPE(cone2) ){610ErrorMayQuit(" parameter is not a cone.",0,0);611return NULL;612}613#endif614615perlobj* coneobj1 = PERLOBJ_POLYMAKEOBJ( cone1 );616perlobj* coneobj2 = PERLOBJ_POLYMAKEOBJ( cone2 );617618data->main_polymake_session->set_application_of( *coneobj1 );619620perlobj intersec;621622CallPolymakeFunction( "intersection", *coneobj1, *coneobj2 ) >> intersec;623624perlobj* returnobj = new perlobj(intersec);625626Obj elem = NewPolymakeExternalObject(T_POLYMAKE_EXTERNAL_POLYTOPE);627628POLYMAKEOBJ_SET_PERLOBJ( elem, returnobj );629630return elem;631632}633634