source: git/Singular/dyn_modules/polymake/polymake_wrapper.cc @ af7978

spielwiese
Last change on this file since af7978 was af7978, checked in by Hans Schoenemann <hannes@…>, 9 years ago
renamed fVector from polymake.so to fVectorP
  • Property mode set to 100644
File size: 46.7 KB
Line 
1#include <kernel/mod2.h>
2
3#ifdef HAVE_POLYMAKE
4
5#include <Singular/dyn_modules/gfanlib/bbcone.h>
6#include <Singular/dyn_modules/gfanlib/bbfan.h>
7#include <Singular/dyn_modules/gfanlib/bbpolytope.h>
8
9#include <Singular/blackbox.h>
10#include <Singular/ipshell.h>
11#include <Singular/subexpr.h>
12#include <Singular/mod_lib.h>
13
14#include <polymake_conversion.h>
15#include <polymake_documentation.h>
16#include <polymake/Graph.h>
17
18polymake::Main* init_polymake=NULL;
19
20static BOOLEAN bbpolytope_Op2(int op, leftv res, leftv i1, leftv i2)
21{
22  gfan::ZCone* zp = (gfan::ZCone*) i1->Data();
23  switch(op)
24  {
25    case '+':
26    {
27      if (i2->Typ()==polytopeID || i2->Typ()==coneID)
28      {
29        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
30        gfan::ZCone* ms;
31        try
32        {
33          polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
34          polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
35          polymake::perl::Object pms;
36          CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
37          ms = PmPolytope2ZPolytope(&pms);
38          delete pp;
39          delete pq;
40        }
41        catch (const std::exception& ex)
42        {
43          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
44          return TRUE;
45        }
46        res->rtyp = polytopeID;
47        res->data = (void*) ms;
48        return FALSE;
49      }
50      return blackboxDefaultOp2(op,res,i1,i2);
51    }
52    case '*':
53    {
54      if (i2->Typ()==INT_CMD)
55      {
56        int s = (int)(long) i2->Data();
57        gfan::ZMatrix zm = zp->extremeRays();
58        for (int i=0; i<zm.getHeight(); i++)
59          for (int j=1; j<zm.getWidth(); j++)
60            zm[i][j] *= s;
61        gfan::ZCone* zs = new gfan::ZCone();
62        *zs = gfan::ZCone::givenByRays(zm,gfan::ZMatrix(0, zm.getWidth()));
63        res->rtyp = polytopeID;
64        res->data = (void*) zs;
65        return FALSE;
66      }
67      return blackboxDefaultOp2(op,res,i1,i2);
68    }
69    case '&':
70    {
71      if (i2->Typ()==polytopeID)
72      {
73        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
74        int d1 = zp->ambientDimension();
75        int d2 = zq->ambientDimension();
76        if (d1 != d2)
77        {
78          Werror("mismatching ambient dimensions");
79          return TRUE;
80        }
81        gfan::ZCone* zs = new gfan::ZCone();
82        *zs = gfan::intersection(*zp, *zq);
83        zs->canonicalize();
84        res->rtyp = polytopeID;
85        res->data = (void*) zs;
86        return FALSE;
87      }
88      return blackboxDefaultOp2(op,res,i1,i2);
89    }
90    case '|':
91    {
92      if(i2->Typ()==polytopeID)
93      {
94        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
95        int d1 = zp->ambientDimension();
96        int d2 = zq->ambientDimension();
97        if (d1 != d2)
98        {
99          Werror("mismatching ambient dimensions");
100          return TRUE;
101        }
102        gfan::ZMatrix rays = zp->extremeRays();
103        rays.append(zq->extremeRays());
104        gfan::ZMatrix lineality = zp->generatorsOfLinealitySpace();
105        lineality.append(zq->generatorsOfLinealitySpace());
106        gfan::ZCone* zs = new gfan::ZCone();
107        *zs = gfan::ZCone::givenByRays(rays,lineality);
108        zs->canonicalize();
109        res->rtyp = polytopeID;
110        res->data = (void*) zs;
111        return FALSE;
112      }
113    return blackboxDefaultOp2(op,res,i1,i2);
114    }
115    case EQUAL_EQUAL:
116    {
117      if(i2->Typ()==polytopeID)
118      {
119        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
120        zp->canonicalize();
121        zq->canonicalize();
122        bool b = !((*zp)!=(*zq));
123        res->rtyp = INT_CMD;
124        res->data = (char*) (long) b;
125        return FALSE;
126      }
127      return blackboxDefaultOp2(op,res,i1,i2);
128    }
129    default:
130      return blackboxDefaultOp2(op,res,i1,i2);
131  }
132  return blackboxDefaultOp2(op,res,i1,i2);
133}
134
135
136/* Functions for using Polymake in Singular */
137
138// BOOLEAN cube(leftv res, leftv args)
139// {
140//   leftv u = args;
141//   if ((u !=NULL) && (u->Typ() == INT_CMD))
142//     {
143//       int ambientDim = (int)(long)u->Data();
144//       if (ambientDim < 0)
145//      {
146//           Werror("expected non-negative ambient dim but got %d", ambientDim);
147//        return TRUE;
148//      }
149//       gfan::ZMatrix zm(ambientDim*2,ambientDim+1);
150//       int j=1;
151//       for (int i=0; i<ambientDim*2; i=i+2)
152//         {
153//           zm[i][0] = 1;
154//           zm[i][j] = 1;
155//           zm[i+1][0] = 1;
156//           zm[i+1][j] = -1;
157//           j = j+1;
158//         }
159//       gfan::ZCone* zc = new gfan::ZCone(zm, gfan::ZMatrix(0, zm.getWidth()));
160//       res->rtyp = coneID;
161//       res->data = (char *)zc;
162//       return FALSE;
163//     }
164//   WerrorS("cube: unexpected parameters");
165//   return TRUE;
166// }
167
168// BOOLEAN cross(leftv res, leftv args)
169// {
170//   leftv u = args;
171//   if ((u !=NULL) && (u->Typ() == INT_CMD))
172//     {
173//       int ambientDim = (int)(long)u->Data();
174//       if (ambientDim < 0)
175//      {
176//           Werror("expected non-negative ambient dim but got %d", ambientDim);
177//        return TRUE;
178//      }
179//       gfan::ZMatrix zm(ambientDim*2,ambientDim+1);
180//       int j=1;
181//       for (int i=0; i<ambientDim*2; i=i+2)
182//         {
183//           zm[i][0] = 1;
184//           zm[i][j] = 1;
185//           zm[i+1][0] = 1;
186//           zm[i+1][j] = -1;
187//           j = j+1;
188//         }
189//       gfan::ZCone* zc = new gfan::ZCone();
190//       *zc = gfan::ZCone::givenByRays(zm, gfan::ZMatrix(0, zm.getWidth()));
191//       res->rtyp = coneID;
192//       res->data = (char *)zc;
193//       return FALSE;
194//     }
195//   WerrorS("cross: unexpected parameters");
196//   return TRUE;
197// }
198
199
200BOOLEAN PMisLatticePolytope(leftv res, leftv args)
201{
202  leftv u = args;
203  if ((u != NULL) && (u->Typ() == polytopeID))
204  {
205    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
206    bool b;
207    try
208    {
209      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
210      b = p->give("Lattice");
211      delete p;
212    }
213    catch (const std::exception& ex)
214    {
215      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
216      return TRUE;
217    }
218    res->rtyp = INT_CMD;
219    res->data = (char*) (long) b;
220    return FALSE;
221  }
222  WerrorS("isLatticePolytope: unexpected parameters");
223  return TRUE;
224}
225
226
227BOOLEAN PMisBounded(leftv res, leftv args)
228{
229  leftv u = args;
230  if ((u != NULL) && (u->Typ() == polytopeID))
231  {
232    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
233    bool b;
234    try
235    {
236      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
237      b = p->give("BOUNDED");
238      delete p;
239    }
240    catch (const std::exception& ex)
241    {
242      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
243      return TRUE;
244    }
245    res->rtyp = INT_CMD;
246    res->data = (char*) (long) b;
247    return FALSE;
248  }
249  WerrorS("isBounded: unexpected parameters");
250  return TRUE;
251}
252
253
254BOOLEAN PMisReflexive(leftv res, leftv args)
255{
256  leftv u = args;
257  if ((u != NULL) && (u->Typ() == polytopeID))
258  {
259    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
260    bool b;
261    try
262    {
263      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
264      b = p->give("REFLEXIVE");
265      delete p;
266    }
267    catch (const std::exception& ex)
268    {
269      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
270      return TRUE;
271    }
272    res->rtyp = INT_CMD;
273    res->data = (char*) (long) b;
274    return FALSE;
275  }
276  WerrorS("isReflexive: unexpected parameters");
277  return TRUE;
278}
279
280
281BOOLEAN PMisGorenstein(leftv res, leftv args)
282{
283  leftv u = args;
284  if ((u != NULL) && (u->Typ() == polytopeID))
285  {
286    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
287    bool b;
288    try
289    {
290      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
291      b = p->give("GORENSTEIN");
292      delete p;
293    }
294    catch (const std::exception& ex)
295    {
296      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
297      return TRUE;
298    }
299    res->rtyp = INT_CMD;
300    res->data = (char*) (long) b;
301    return FALSE;
302  }
303  WerrorS("isGorenstein: unexpected parameters");
304  return TRUE;
305}
306
307
308BOOLEAN PMgorensteinIndex(leftv res, leftv args)
309{
310  leftv u = args;
311  if ((u != NULL) && (u->Typ() == polytopeID))
312  {
313    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
314    int gi;
315    bool ok = true;
316    try
317    {
318      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
319      bool b = p->give("GORENSTEIN");
320      if (b)
321      {
322        polymake::Integer pgi = p->give("GORENSTEIN_INDEX");
323        gi = PmInteger2Int(pgi,ok);
324        delete p;
325      }
326      else
327      {
328        delete p;
329        WerrorS("gorensteinIndex: input polytope not gorenstein");
330        return TRUE;
331      }
332    }
333    catch (const std::exception& ex)
334    {
335      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
336      return TRUE;
337    }
338    if (!ok)
339    {
340      WerrorS("overflow while converting polymake::Integer to int");
341      return TRUE;
342    }
343    res->rtyp = INT_CMD;
344    res->data = (char*) (long) gi;
345    return FALSE;
346  }
347  WerrorS("gorensteinIndex: unexpected parameters");
348  return TRUE;
349}
350
351
352BOOLEAN PMgorensteinVector(leftv res, leftv args)
353{
354  leftv u = args;
355  if ((u != NULL) && (u->Typ() == polytopeID))
356  {
357    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
358    intvec* gv;
359    bool ok = true;
360    try
361    {
362      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
363      bool b = p->give("GORENSTEIN");
364      if (b)
365      {
366        polymake::Vector<polymake::Integer> pgv = p->give("GORENSTEIN_VECTOR");
367        gv = PmVectorInteger2Intvec(&pgv,ok);
368        delete p;
369      }
370      else
371      {
372        delete p;
373        WerrorS("gorensteinVector: input polytope not gorenstein");
374        return TRUE;
375      }
376    }
377    catch (const std::exception& ex)
378    {
379      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
380      return TRUE;
381    }
382    if (!ok)
383    {
384      WerrorS("gorensteinVector: overflow in PmVectorInteger2Intvec");
385      return TRUE;
386    }
387    res->rtyp = INTVEC_CMD;
388    res->data = (char*) gv;
389    return FALSE;
390  }
391  WerrorS("gorensteinVector: unexpected parameters");
392  return TRUE;
393}
394
395
396BOOLEAN PMisCanonical(leftv res, leftv args)
397{
398  leftv u = args;
399  if ((u != NULL) && (u->Typ() == polytopeID))
400  {
401    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
402    bool b;
403    try
404    {
405      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
406      b = p->give("CANONICAL");
407      delete p;
408    }
409    catch (const std::exception& ex)
410    {
411      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
412      return TRUE;
413    }
414    res->rtyp = INT_CMD;
415    res->data = (char*) (long) b;
416    return FALSE;
417  }
418  WerrorS("isCanonical: unexpected parameters");
419  return TRUE;
420}
421
422
423BOOLEAN PMisTerminal(leftv res, leftv args)
424{
425  leftv u = args;
426  if ((u != NULL) && (u->Typ() == polytopeID))
427  {
428    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
429    bool b;
430    try
431    {
432      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
433      b = p->give("TERMINAL");
434      delete p;
435    }
436    catch (const std::exception& ex)
437    {
438      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
439      return TRUE;
440    }
441    res->rtyp = INT_CMD;
442    res->data = (char*) (long) b;
443    return FALSE;
444  }
445  WerrorS("isTerminal: unexpected parameters");
446  return TRUE;
447}
448
449
450BOOLEAN PMisLatticeEmpty(leftv res, leftv args)
451{
452  leftv u = args;
453  if ((u != NULL) && (u->Typ() == polytopeID))
454  {
455    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
456    bool b;
457    try
458    {
459      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
460      b = p->give("LATTICE_EMPTY");
461      delete p;
462    }
463    catch (const std::exception& ex)
464    {
465      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
466      return TRUE;
467    }
468    res->rtyp = INT_CMD;
469    res->data = (char*) (long) b;
470    return FALSE;
471  }
472  WerrorS("isLatticeEmpty: unexpected parameters");
473  return TRUE;
474}
475
476
477BOOLEAN PMlatticeVolume(leftv res, leftv args)
478{
479  leftv u = args;
480  if ((u != NULL) && (u->Typ() == polytopeID))
481  {
482    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
483    int lv;
484    bool ok = true;
485    try
486    {
487      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
488      polymake::Integer plv = p->give("LATTICE_VOLUME");
489      delete p;
490      lv = PmInteger2Int(plv,ok);
491    }
492    catch (const std::exception& ex)
493    {
494      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
495      return TRUE;
496    }
497    if (!ok)
498    {
499      WerrorS("overflow while converting polymake::Integer to int");
500      return TRUE;
501    }
502    res->rtyp = INT_CMD;
503    res->data = (char*) (long) lv;
504    return FALSE;
505  }
506  WerrorS("latticeVolume: unexpected parameters");
507  return TRUE;
508}
509
510
511BOOLEAN PMlatticeDegree(leftv res, leftv args)
512{
513  leftv u = args;
514  if ((u != NULL) && (u->Typ() == polytopeID))
515  {
516    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
517    int ld;
518    bool ok = true;
519    try
520    {
521      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
522      polymake::Integer pld = p->give("LATTICE_DEGREE");
523      delete p;
524      ld = PmInteger2Int(pld,ok);
525    }
526    catch (const std::exception& ex)
527    {
528      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
529      return TRUE;
530    }
531    if (!ok)
532    {
533      WerrorS("overflow while converting polymake::Integer to int");
534      return TRUE;
535    }
536    res->rtyp = INT_CMD;
537    res->data = (char*) (long) ld;
538    return FALSE;
539  }
540  WerrorS("latticeDegree: unexpected parameters");
541  return TRUE;
542}
543
544
545BOOLEAN PMlatticeCodegree(leftv res, leftv args)
546{
547  leftv u = args;
548  if ((u != NULL) && (u->Typ() == polytopeID))
549  {
550    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
551    int lc;
552    bool ok = true;
553    try
554    {
555      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
556      polymake::Integer plc = p->give("LATTICE_CODEGREE");
557      delete p;
558      lc = PmInteger2Int(plc,ok);
559    }
560    catch (const std::exception& ex)
561    {
562      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
563      return TRUE;
564    }
565    if (!ok)
566    {
567      WerrorS("overflow while converting polymake::Integer to int");
568      return TRUE;
569    }
570    res->rtyp = INT_CMD;
571    res->data = (char*) (long) lc;
572    return FALSE;
573  }
574  WerrorS("latticeCodegree: unexpected parameters");
575  return TRUE;
576}
577
578
579BOOLEAN PMehrhartPolynomialCoeff(leftv res, leftv args)
580{
581  leftv u = args;
582  if ((u != NULL) && (u->Typ() == polytopeID))
583  {
584    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
585    intvec* ec;
586    bool ok = true;
587    try
588    {
589      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
590      polymake::Vector<polymake::Integer> pec = p->give("EHRHART_POLYNOMIAL_COEFF");
591      delete p;
592      ec = PmVectorInteger2Intvec(&pec,ok);
593    }
594    catch (const std::exception& ex)
595    {
596      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
597      return TRUE;
598    }
599    if (!ok)
600    {
601      WerrorS("ehrhartPolynomialCoeff: overflow in PmVectorInteger2Intvec");
602      return TRUE;
603    }
604    res->rtyp = INTVEC_CMD;
605    res->data = (char*) ec;
606    return FALSE;
607  }
608  WerrorS("ehrhartPolynomialCoeff: unexpected parameters");
609  return TRUE;
610}
611
612
613BOOLEAN PMfVector(leftv res, leftv args)
614{
615  leftv u = args;
616  if ((u != NULL) && (u->Typ() == polytopeID))
617  {
618    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
619    intvec* hv;
620    bool ok = true;
621    try
622    {
623      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
624      polymake::Vector<polymake::Integer> phv = p->give("F_VECTOR");
625      delete p;
626      hv = PmVectorInteger2Intvec(&phv,ok);
627    }
628    catch (const std::exception& ex)
629    {
630      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
631      return TRUE;
632    }
633    if (!ok)
634    {
635      WerrorS("fVectorP: overflow in PmVectorInteger2Intvec");
636      return TRUE;
637    }
638    res->rtyp = INTVEC_CMD;
639    res->data = (char*) hv;
640    return FALSE;
641  }
642  WerrorS("fVectorP: unexpected parameters");
643  return TRUE;
644}
645
646
647BOOLEAN PMhVector(leftv res, leftv args)
648{
649  leftv u = args;
650  if ((u != NULL) && (u->Typ() == polytopeID))
651  {
652    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
653    intvec* hv;
654    bool ok = true;
655    try
656    {
657      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
658      polymake::Vector<polymake::Integer> phv = p->give("H_VECTOR");
659      delete p;
660      hv = PmVectorInteger2Intvec(&phv,ok);
661    }
662    catch (const std::exception& ex)
663    {
664      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
665      return TRUE;
666    }
667    if (!ok)
668    {
669      WerrorS("hVector: overflow in PmVectorInteger2Intvec");
670      return TRUE;
671    }
672    res->rtyp = INTVEC_CMD;
673    res->data = (char*) hv;
674    return FALSE;
675  }
676  WerrorS("hVector: unexpected parameters");
677  return TRUE;
678}
679
680
681BOOLEAN PMhStarVector(leftv res, leftv args)
682{
683  leftv u = args;
684  if ((u != NULL) && (u->Typ() == polytopeID))
685  {
686    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
687    intvec* hv;
688    bool ok = true;
689    try
690    {
691      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
692      polymake::Vector<polymake::Integer> phv = p->give("H_STAR_VECTOR");
693      delete p;
694      hv = PmVectorInteger2Intvec(&phv,ok);
695    }
696    catch (const std::exception& ex)
697    {
698      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
699      return TRUE;
700    }
701    if (!ok)
702    {
703      WerrorS("hStarVector: overflow in PmVectorInteger2Intvec");
704      return TRUE;
705    }
706    res->rtyp = INTVEC_CMD;
707    res->data = (char*) hv;
708    return FALSE;
709  }
710  WerrorS("hStarVector: unexpected parameters");
711  return TRUE;
712}
713
714
715BOOLEAN PMisNormal(leftv res, leftv args)
716{
717  leftv u = args;
718  if ((u != NULL) && (u->Typ() == polytopeID))
719  {
720    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
721    bool b;
722    try
723    {
724      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
725      b = p->give("NORMAL");
726      delete p;
727    }
728    catch (const std::exception& ex)
729    {
730      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
731      return TRUE;
732    }
733    res->rtyp = INT_CMD;
734    res->data = (char*) (long) b;
735    return FALSE;
736  }
737  WerrorS("isNormal: unexpected parameters");
738  return TRUE;
739}
740
741
742BOOLEAN PMfacetWidths(leftv res, leftv args)
743{
744  leftv u = args;
745  if ((u != NULL) && (u->Typ() == polytopeID))
746  {
747    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
748    intvec* fw;
749    bool ok = true;
750    try
751    {
752      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
753      polymake::Vector<polymake::Integer> pfw = p->give("FACET_WIDTHS");
754      delete p;
755      fw = PmVectorInteger2Intvec(&pfw,ok);
756    }
757    catch (const std::exception& ex)
758    {
759      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
760      return TRUE;
761    }
762    if (!ok)
763    {
764      WerrorS("facetWidths: overflow in PmVectorInteger2Intvec");
765      return TRUE;
766    }
767    res->rtyp = INTVEC_CMD;
768    res->data = (char*) fw;
769    return FALSE;
770  }
771  WerrorS("facetWidths: unexpected parameters");
772  return TRUE;
773}
774
775
776BOOLEAN PMfacetWidth(leftv res, leftv args)
777{
778  leftv u = args;
779  if ((u != NULL) && (u->Typ() == polytopeID))
780  {
781    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
782    int fw;
783    bool ok = true;
784    try
785    {
786      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
787      polymake::Integer pfw = p->give("FACET_WIDTH");
788      delete p;
789      fw = PmInteger2Int(pfw,ok);
790    }
791    catch (const std::exception& ex)
792    {
793      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
794      return TRUE;
795    }
796    if (!ok)
797    {
798      WerrorS("overflow while converting polymake::Integer to int");
799      return TRUE;
800    }
801    res->rtyp = INT_CMD;
802    res->data = (char*) (long) fw;
803    return FALSE;
804  }
805  WerrorS("facetWidth: unexpected parameters");
806  return TRUE;
807}
808
809
810BOOLEAN PMfacetVertexLatticeDistances(leftv res, leftv args)
811{
812  leftv u = args;
813  if ((u != NULL) && (u->Typ() == polytopeID))
814  {
815    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
816    intvec* ld;
817    bool ok=true;
818    try
819    {
820      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
821      polymake::Matrix<polymake::Integer> pld = p->give("FACET_VERTEX_LATTICE_DISTANCES");
822      delete p;
823      ld = PmMatrixInteger2Intvec(&pld,ok);
824    }
825    catch (const std::exception& ex)
826    {
827      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
828      return TRUE;
829    }
830    if (!ok)
831    {
832      WerrorS("overflow while converting polymake::Integer to int");
833      return TRUE;
834    }
835    res->rtyp = INTMAT_CMD;
836    res->data = (char*) ld;
837    return FALSE;
838  }
839  WerrorS("facetVertexLatticeDistances: unexpected parameters");
840  return TRUE;
841}
842
843
844BOOLEAN PMisCompressed(leftv res, leftv args)
845{
846  leftv u = args;
847  if ((u != NULL) && (u->Typ() == polytopeID))
848  {
849    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
850    bool b;
851    try
852    {
853      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
854      b = p->give("COMPRESSED");
855      delete p;
856    }
857    catch (const std::exception& ex)
858    {
859      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
860      return TRUE;
861    }
862    res->rtyp = INT_CMD;
863    res->data = (char*) (long) b;
864    return FALSE;
865  }
866  WerrorS("isCompressed: unexpected parameters");
867  return TRUE;
868}
869
870
871BOOLEAN PMisSmooth(leftv res, leftv args)
872{
873  leftv u = args;
874  if ((u != NULL) && (u->Typ() == coneID))
875  {
876    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
877    bool b;
878    try
879    {
880      polymake::perl::Object* p = ZCone2PmCone(zc);
881      b = p->give("SMOOTH_CONE");
882      delete p;
883    }
884    catch (const std::exception& ex)
885    {
886      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
887      return TRUE;
888    }
889    res->rtyp = INT_CMD;
890    res->data = (char*) (long) b;
891    return FALSE;
892  }
893  if ((u != NULL) && (u->Typ() == polytopeID))
894  {
895    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
896    bool b;
897    try
898    {
899      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
900      b = p->give("SMOOTH");
901      delete p;
902    }
903    catch (const std::exception& ex)
904    {
905      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
906      return TRUE;
907    }
908    res->rtyp = INT_CMD;
909    res->data = (char*) (long) b;
910    return FALSE;
911  }
912  if ((u != NULL) && (u->Typ() == fanID))
913  {
914    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
915    bool b;
916    try
917    {
918      polymake::perl::Object* p = ZFan2PmFan(zf);
919      b = p->give("SMOOTH_FAN");
920      delete p;
921    }
922    catch (const std::exception& ex)
923    {
924      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
925      return TRUE;
926    }
927    res->rtyp = INT_CMD;
928    res->data = (char*) (long) b;
929    return FALSE;
930  }
931  WerrorS("isSmooth: unexpected parameters");
932  return TRUE;
933}
934
935
936BOOLEAN PMisVeryAmple(leftv res, leftv args)
937{
938  leftv u = args;
939  if ((u != NULL) && (u->Typ() == polytopeID))
940  {
941    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
942    bool b;
943    try
944    {
945      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
946      b = p->give("VERY_AMPLE");
947      delete p;
948    }
949    catch (const std::exception& ex)
950    {
951      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
952      return TRUE;
953    }
954    res->rtyp = INT_CMD;
955    res->data = (char*) (long) b;
956    return FALSE;
957  }
958  WerrorS("isVeryAmple: unexpected parameters");
959  return TRUE;
960}
961
962
963BOOLEAN PMlatticePoints(leftv res, leftv args)
964{
965  leftv u = args;
966  if ((u != NULL) && (u->Typ() == polytopeID))
967  {
968    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
969    intvec* iv;
970    bool ok = true;
971    try
972    {
973      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
974      polymake::Matrix<polymake::Integer> lp = p->give("LATTICE_POINTS");
975      delete p;
976      iv = PmMatrixInteger2Intvec(&lp,ok);
977    }
978    catch (const std::exception& ex)
979    {
980      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
981      return TRUE;
982    }
983    if (!ok)
984    {
985      WerrorS("overflow while converting polymake::Integer to int");
986      return TRUE;
987    }
988    res->rtyp = INTMAT_CMD;
989    res->data = (char*) iv;
990    return FALSE;
991  }
992  WerrorS("LatticePoints: unexpected parameters");
993  return TRUE;
994}
995
996
997BOOLEAN PMnLatticePoints(leftv res, leftv args)
998{
999  leftv u = args;
1000  if ((u != NULL) && (u->Typ() == polytopeID))
1001  {
1002    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1003    int n;
1004    bool ok = true;
1005    try
1006    {
1007      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1008      polymake::Integer nlp = p->give("N_LATTICE_POINTS");
1009      delete p;
1010      n = PmInteger2Int(nlp,ok);
1011    }
1012    catch (const std::exception& ex)
1013    {
1014      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1015      return TRUE;
1016    }
1017    if (!ok)
1018    {
1019      WerrorS("overflow while converting polymake::Integer to int");
1020      return TRUE;
1021    }
1022    res->rtyp = INT_CMD;
1023    res->data = (char*) (long) n;
1024    return FALSE;
1025  }
1026  WerrorS("nLatticePoints: unexpected parameters");
1027  return TRUE;
1028}
1029
1030
1031BOOLEAN PMinteriorLatticePoints(leftv res, leftv args)
1032{
1033  leftv u = args;
1034  if ((u != NULL) && (u->Typ() == polytopeID))
1035  {
1036    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1037    intvec* iv;
1038    bool ok = true;
1039    try
1040    {
1041      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1042      polymake::Matrix<polymake::Integer> lp = p->give("INTERIOR_LATTICE_POINTS");
1043      delete p;
1044      iv = PmMatrixInteger2Intvec(&lp,ok);
1045    }
1046    catch (const std::exception& ex)
1047    {
1048      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1049      return TRUE;
1050    }
1051    if (!ok)
1052    {
1053      WerrorS("overflow while converting polymake::Integer to int");
1054      return TRUE;
1055    }
1056    res->rtyp = INTMAT_CMD;
1057    res->data = (char*) iv;
1058    return FALSE;
1059  }
1060  WerrorS("interiorLatticePoints: unexpected parameters");
1061  return TRUE;
1062}
1063
1064
1065BOOLEAN PMnInteriorLatticePoints(leftv res, leftv args)
1066{
1067  leftv u = args;
1068  if ((u != NULL) && (u->Typ() == polytopeID))
1069  {
1070    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1071    int n;
1072    bool ok = true;
1073    try
1074    {
1075      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1076      polymake::Integer nlp = p->give("N_INTERIOR_LATTICE_POINTS");
1077      delete p;
1078      n = PmInteger2Int(nlp,ok);
1079    }
1080    catch (const std::exception& ex)
1081    {
1082      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1083      return TRUE;
1084    }
1085    if (!ok)
1086    {
1087      WerrorS("overflow while converting polymake::Integer to int");
1088      return TRUE;
1089    }
1090    res->rtyp = INT_CMD;
1091    res->data = (char*) (long) n;
1092    return FALSE;
1093  }
1094  WerrorS("nInteriorLatticePoints: unexpected parameters");
1095  return TRUE;
1096}
1097
1098
1099BOOLEAN PMboundaryLatticePoints(leftv res, leftv args)
1100{
1101  leftv u = args;
1102  if ((u != NULL) && (u->Typ() == polytopeID))
1103  {
1104    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1105    intvec* iv;
1106    bool ok = true;
1107    try
1108    {
1109      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1110      polymake::Matrix<polymake::Integer> lp = p->give("BOUNDARY_LATTICE_POINTS");
1111      delete p;
1112      iv = PmMatrixInteger2Intvec(&lp,ok);
1113    }
1114    catch (const std::exception& ex)
1115    {
1116      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1117      return TRUE;
1118    }
1119    if (!ok)
1120    {
1121      WerrorS("overflow while converting polymake::Integer to int");
1122      return TRUE;
1123    }
1124    res->rtyp = INTMAT_CMD;
1125    res->data = (char*) iv;
1126    return FALSE;
1127  }
1128  WerrorS("boundaryLatticePoints: unexpected parameters");
1129  return TRUE;
1130}
1131
1132
1133BOOLEAN PMnBoundaryLatticePoints(leftv res, leftv args)
1134{
1135  leftv u = args;
1136  if ((u != NULL) && (u->Typ() == polytopeID))
1137  {
1138    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1139    int n;
1140    bool ok = true;
1141    try
1142    {
1143      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1144      polymake::Integer nlp = p->give("N_BOUNDARY_LATTICE_POINTS");
1145      delete p;
1146      n = PmInteger2Int(nlp,ok);
1147    }
1148    catch (const std::exception& ex)
1149    {
1150      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1151      return TRUE;
1152    }
1153    if (!ok)
1154    {
1155      WerrorS("overflow while converting polymake::Integer to int");
1156      return TRUE;
1157    }
1158    res->rtyp = INT_CMD;
1159    res->data = (char*) (long) n;
1160    return FALSE;
1161  }
1162  WerrorS("nBoundaryLatticePoints: unexpected parameters");
1163  return TRUE;
1164}
1165
1166
1167BOOLEAN PMhilbertBasis(leftv res, leftv args)
1168{
1169  leftv u = args;
1170  if ((u != NULL) && (u->Typ() == coneID))
1171  {
1172    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1173    intvec* iv;
1174    bool ok = true;
1175    try
1176    {
1177      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1178      polymake::Matrix<polymake::Integer> lp = p->give("HILBERT_BASIS");
1179      delete p;
1180      iv = PmMatrixInteger2Intvec(&lp,ok);
1181    }
1182    catch (const std::exception& ex)
1183    {
1184      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1185      return TRUE;
1186    }
1187    if (!ok)
1188    {
1189      WerrorS("overflow while converting polymake::Integer to int");
1190      return TRUE;
1191    }
1192    res->rtyp = INTMAT_CMD;
1193    res->data = (char*) iv;
1194    return FALSE;
1195  }
1196  WerrorS("hilbertBasis: unexpected parameters");
1197  return TRUE;
1198}
1199
1200
1201BOOLEAN PMnHilbertBasis(leftv res, leftv args)
1202{
1203  leftv u = args;
1204  if ((u != NULL) && (u->Typ() == coneID))
1205  {
1206    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1207    int n;
1208    bool ok = true;
1209    try
1210    {
1211      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1212      polymake::Integer nlp = p->give("N_HILBERT_BASIS");
1213      delete p;
1214      n = PmInteger2Int(nlp,ok);
1215    }
1216    catch (const std::exception& ex)
1217    {
1218      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1219      return TRUE;
1220    }
1221    if (!ok)
1222    {
1223      WerrorS("overflow while converting polymake::Integer to int");
1224      return TRUE;
1225    }
1226    res->rtyp = INT_CMD;
1227    res->data = (char*) (long) n;
1228    return FALSE;
1229  }
1230  WerrorS("nHilbertBasis: unexpected parameters");
1231  return TRUE;
1232}
1233
1234
1235BOOLEAN PMminkowskiSum(leftv res, leftv args)
1236{
1237  leftv u = args;
1238  if ((u != NULL) && (u->Typ() == polytopeID))
1239  {
1240    leftv v = u->next;
1241    if ((v != NULL) && (v->Typ() == polytopeID))
1242    {
1243      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1244      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1245      gfan::ZCone* ms;
1246      try
1247      {
1248        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1249        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1250        polymake::perl::Object pms;
1251        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1252        delete pp;
1253        delete pq;
1254        ms = PmPolytope2ZPolytope(&pms);
1255      }
1256      catch (const std::exception& ex)
1257      {
1258        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1259        return TRUE;
1260      }
1261      res->rtyp = polytopeID;
1262      res->data = (char*) ms;
1263      return FALSE;
1264    }
1265    if ((v != NULL) && (v->Typ() == coneID))
1266    {
1267      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1268      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
1269      gfan::ZCone* zq = new gfan::ZCone(liftUp(*zc));
1270      gfan::ZCone* ms;
1271      try
1272      {
1273        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1274        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1275        polymake::perl::Object pms;
1276        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1277        delete pp;
1278        delete pq;
1279        ms = PmPolytope2ZPolytope(&pms);
1280      }
1281      catch (const std::exception& ex)
1282      {
1283        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1284        delete zq;
1285        return TRUE;
1286      }
1287      res->rtyp = polytopeID;
1288      res->data = (char*) ms;
1289      delete zq;
1290      return FALSE;
1291    }
1292  }
1293  if ((u != NULL) && (u->Typ() == coneID))
1294  {
1295    leftv v = u->next;
1296    if ((v != NULL) && (v->Typ() == polytopeID))
1297    {
1298      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1299      gfan::ZCone* zp = new gfan::ZCone(liftUp(*zc));
1300      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1301      gfan::ZCone* ms;
1302      try
1303      {
1304        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1305        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1306        polymake::perl::Object pms;
1307        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1308        delete pp;
1309        delete pq;
1310        ms = PmPolytope2ZPolytope(&pms);
1311      }
1312      catch (const std::exception& ex)
1313      {
1314        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1315        delete zp;
1316        return TRUE;
1317      }
1318      res->rtyp = polytopeID;
1319      res->data = (char*) ms;
1320      delete zp;
1321      return FALSE;
1322    }
1323    if ((v != NULL) && (v->Typ() == coneID))
1324    {
1325      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1326      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1327      gfan::ZCone* ms;
1328      try
1329      {
1330        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1331        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1332        polymake::perl::Object pms;
1333        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1334        delete pp;
1335        delete pq;
1336        ms = PmPolytope2ZPolytope(&pms);
1337      }
1338      catch (const std::exception& ex)
1339      {
1340        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1341        return TRUE;
1342      }
1343      res->rtyp = coneID;
1344      res->data = (char*) ms;
1345      return FALSE;
1346    }
1347  }
1348  WerrorS("minkowskiSum: unexpected parameters");
1349  return TRUE;
1350}
1351
1352
1353polymake::Matrix<polymake::Integer> verticesOf(const polymake::perl::Object* p,
1354                                               const polymake::Set<polymake::Integer>* s)
1355{
1356  polymake::Matrix<polymake::Integer> allrays = p->give("VERTICES");
1357  polymake::Matrix<polymake::Integer> wantedrays;
1358  bool ok = true;
1359  for(polymake::Entire<polymake::Set<polymake::Integer> >::const_iterator i=polymake::entire(*s); !i.at_end(); i++)
1360  {
1361    wantedrays = wantedrays / allrays.row(PmInteger2Int(*i,ok));
1362  }
1363  if (!ok)
1364  {
1365    WerrorS("overflow while converting polymake::Integer to int in raysOf");
1366  }
1367  return wantedrays;
1368}
1369
1370
1371BOOLEAN PMmaximalFace(leftv res, leftv args)
1372{
1373  leftv u = args;
1374  if ((u != NULL) && (u->Typ() == polytopeID))
1375  {
1376    leftv v = u->next;
1377    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1378    {
1379      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1380      intvec* iv = (intvec*) v->Data();
1381      intvec* maxface;
1382      bool ok = true;
1383      try
1384      {
1385        polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1386        polymake::perl::Object o("LinearProgram<Rational>");
1387        o.take("LINEAR_OBJECTIVE") << Intvec2PmVectorInteger(iv);
1388        p->take("LP") << o;
1389        polymake::Set<polymake::Integer> mf = p->give("LP.MAXIMAL_FACE");
1390        polymake::Matrix<polymake::Integer> vertices = verticesOf(p,&mf);
1391        delete p;
1392        maxface = new intvec(PmMatrixInteger2Intvec(&vertices,ok));
1393      }
1394      catch (const std::exception& ex)
1395      {
1396        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1397        return TRUE;
1398      }
1399      if (!ok)
1400      {
1401        WerrorS("overflow while converting polymake::Integer to int");
1402        return TRUE;
1403      }
1404      res->rtyp = INTVEC_CMD;
1405      res->data = (char*) maxface;
1406      return FALSE;
1407    }
1408  }
1409  WerrorS("maximalFace: unexpected parameters");
1410  return TRUE;
1411}
1412
1413
1414BOOLEAN PMminimalFace(leftv res, leftv args)
1415{
1416  leftv u = args;
1417  if ((u != NULL) && (u->Typ() == polytopeID))
1418  {
1419    leftv v = u->next;
1420    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1421    {
1422      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1423      intvec* iv = (intvec*) v->Data();
1424      intvec* minface;
1425      bool ok = true;
1426      try
1427      {
1428        polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1429        polymake::perl::Object o("LinearProgram<Rational>");
1430        o.take("LINEAR_OBJECTIVE") << Intvec2PmVectorInteger(iv);
1431        p->take("LP") << o;
1432        polymake::Set<polymake::Integer> mf = p->give("LP.MINIMAL_FACE");
1433        polymake::Matrix<polymake::Integer> vertices = verticesOf(p,&mf);
1434        delete p;
1435        minface = new intvec(PmMatrixInteger2Intvec(&vertices,ok));
1436      }
1437      catch (const std::exception& ex)
1438      {
1439        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1440        return TRUE;
1441      }
1442      if (!ok)
1443      {
1444        WerrorS("overflow while converting polymake::Integer to int");
1445        return TRUE;
1446      }
1447      res->rtyp = INTVEC_CMD;
1448      res->data = (char*) minface;
1449      return FALSE;
1450    }
1451  }
1452  WerrorS("minimalFace: unexpected parameters");
1453  return TRUE;
1454}
1455
1456
1457BOOLEAN PMmaximalValue(leftv res, leftv args)
1458{
1459  leftv u = args;
1460  if ((u != NULL) && (u->Typ() == polytopeID))
1461  {
1462    leftv v = u->next;
1463    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1464    {
1465      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1466      intvec* iv = (intvec*) v->Data();
1467      if (iv->rows()==zp->ambientDimension())
1468      {
1469        int m;
1470        bool ok = true;
1471        try
1472        {
1473          polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1474          polymake::Vector<polymake::Integer> lo = Intvec2PmVectorInteger(iv);
1475          polymake::perl::Object o("LinearProgram<Rational>");
1476          o.take("LINEAR_OBJECTIVE") << lo;
1477          p->take("LP") << o;
1478          polymake::Integer mv = p->give("LP.MAXIMAL_VALUE");
1479          delete p;
1480          m = PmInteger2Int(mv,ok);
1481        }
1482        catch (const std::exception& ex)
1483        {
1484          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1485          return TRUE;
1486        }
1487        if (!ok)
1488        {
1489          WerrorS("overflow while converting polymake::Integer to int");
1490          return TRUE;
1491        }
1492        res->rtyp = INT_CMD;
1493        res->data = (char*) (long) m;
1494        return FALSE;
1495      }
1496    }
1497    WerrorS("maximalValue: vector is of wrong size");
1498    return TRUE;
1499  }
1500  WerrorS("maximalValue: unexpected parameters");
1501  return TRUE;
1502}
1503
1504BOOLEAN PMminimalValue(leftv res, leftv args)
1505{
1506  leftv u = args;
1507  if ((u != NULL) && (u->Typ() == polytopeID))
1508  {
1509    leftv v = u->next;
1510    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1511    {
1512      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1513      intvec* iv = (intvec*) v->Data();
1514      if (iv->rows()==zp->ambientDimension())
1515      {
1516        int m;
1517        bool ok = true;
1518        try
1519        {
1520          polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1521          polymake::Vector<polymake::Integer> lo = Intvec2PmVectorInteger(iv);
1522          polymake::perl::Object o("LinearProgram<Rational>");
1523          o.take("LINEAR_OBJECTIVE") << lo;
1524          p->take("LP") << o;
1525          polymake::Integer mv = p->give("LP.MINIMAL_VALUE");
1526          delete p;
1527          m = PmInteger2Int(mv,ok);
1528        }
1529        catch (const std::exception& ex)
1530        {
1531          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1532          return TRUE;
1533        }
1534        if (!ok)
1535        {
1536          WerrorS("overflow while converting polymake::Integer to int");
1537          return TRUE;
1538        }
1539        res->rtyp = INT_CMD;
1540        res->data = (char*) (long) m;
1541        return FALSE;
1542      }
1543    }
1544    WerrorS("minimalValue: vector is of wrong size");
1545    return TRUE;
1546  }
1547  WerrorS("minimalValue: unexpected parameters");
1548  return TRUE;
1549}
1550
1551
1552BOOLEAN visual(leftv res, leftv args)
1553{
1554  leftv u = args;
1555  if ((u != NULL) && (u->Typ() == polytopeID))
1556  {
1557    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1558    try
1559    {
1560      polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1561      VoidCallPolymakeFunction("jreality",pp->CallPolymakeMethod("VISUAL"));
1562      delete pp;
1563    }
1564    catch (const std::exception& ex)
1565    {
1566      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1567      return TRUE;
1568    }
1569    res->rtyp = NONE;
1570    res->data = NULL;
1571    return FALSE;
1572  }
1573  if ((u != NULL) && (u->Typ() == fanID))
1574  {
1575    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
1576    try
1577    {
1578      polymake::perl::Object* pf=ZFan2PmFan(zf);
1579      VoidCallPolymakeFunction("jreality",pf->CallPolymakeMethod("VISUAL"));
1580    }
1581    catch (const std::exception& ex)
1582    {
1583      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1584      return TRUE;
1585    }
1586    res->rtyp = NONE;
1587    res->data = NULL;
1588    return FALSE;
1589  }
1590  WerrorS("visual: unexpected parameters");
1591  return TRUE;
1592}
1593
1594BOOLEAN normalFan(leftv res, leftv args)
1595{
1596  leftv u = args;
1597  if ((u != NULL) && (u->Typ() == polytopeID))
1598  {
1599    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1600    gfan::ZFan* zf = new gfan::ZFan(0);
1601    try
1602    {
1603      polymake::perl::Object* p=ZPolytope2PmPolytope(zp);
1604      polymake::perl::Object pf;
1605      CallPolymakeFunction("normal_fan", *p) >> pf;
1606      delete p;
1607      zf = PmFan2ZFan(&pf);
1608    }
1609    catch (const std::exception& ex)
1610    {
1611      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1612      return TRUE;
1613    }
1614    res->rtyp = fanID;
1615    res->data = (char*) zf;
1616    return FALSE;
1617  }
1618  WerrorS("normalFan: unexpected parameters");
1619  return TRUE;
1620}
1621
1622BOOLEAN PMconeViaRays(leftv res, leftv args)
1623{
1624  leftv u = args;
1625  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
1626  {
1627    polymake::perl::Object pc("Cone<Rational>");
1628    intvec* hlines = (intvec*) u->Data(); // these will are half lines in the cone
1629    polymake::Matrix<polymake::Integer> pmhlines = Intvec2PmMatrixInteger(hlines);
1630    pc.take("INPUT_RAYS") << pmhlines;
1631
1632    leftv v = u->next;
1633    if ((v != NULL) && (v->Typ() == INTMAT_CMD))
1634    {
1635      intvec* lines = (intvec*) v->Data(); // these will be lines in the cone
1636      polymake::Matrix<polymake::Integer> pmlines = Intvec2PmMatrixInteger(lines);
1637      pc.take("INPUT_LINEALITY") << pmlines;
1638
1639      // leftv w = v->next;
1640      // if ((w != NULL) && (w->Typ() == INT_CMD))
1641      // {
1642      //   int flag = (int) (long) w->Data(); // TODO: this will indicate whether the
1643      //                                      // information provided are exact
1644      // }
1645    }
1646    gfan::ZCone* zc = PmCone2ZCone(&pc);
1647    res->rtyp = coneID;
1648    res->data = (char*) zc;
1649    return FALSE;
1650  }
1651  WerrorS("coneViaRays: unexpected parameters");
1652  return TRUE;
1653}
1654
1655
1656BOOLEAN PMpolytopeViaVertices(leftv res, leftv args)
1657{
1658  leftv u = args;
1659  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
1660  {
1661    polymake::perl::Object pp("Polytope<Rational>");
1662    intvec* points = (intvec*) u->Data(); // these will be vertices of or points in the polytope
1663    polymake::Matrix<polymake::Integer> pmpoints = Intvec2PmMatrixInteger(points);
1664
1665    leftv v = u->next;
1666    if ((v != NULL) && (v->Typ() == INT_CMD))
1667    {
1668      int flag = (int) (long) v->Data();
1669      switch(flag)
1670      {
1671        case 0:  pp.take("POINTS") << pmpoints;   // case means the matrix may contain points inside the polytope
1672        case 1:  pp.take("VERTICES") << pmpoints; // case means the matrix only contains vertices of the polytope
1673        default: WerrorS("polytopeViaVertices: invalid flag");
1674      }
1675    }
1676    else
1677      pp.take("POINTS") << pmpoints;              // by default, we assume that matrix may contain non-vertices
1678
1679    gfan::ZCone* zp = PmPolytope2ZPolytope(&pp);
1680    res->rtyp = polytopeID;
1681    res->data = (char*) zp;
1682    return FALSE;
1683  }
1684  WerrorS("polytopeViaVertices: unexpected parameters");
1685  return TRUE;
1686}
1687
1688
1689BOOLEAN PMvertexAdjacencyGraph(leftv res, leftv args)
1690{
1691  leftv u = args;
1692  if ((u != NULL) && (u->Typ() == polytopeID))
1693  {
1694    gfan::ZCone* zp = (gfan::ZCone*) u->Data();
1695    lists output=(lists)omAllocBin(slists_bin); output->Init(2);
1696    try
1697    {
1698      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1699      polymake::Matrix<polymake::Integer> vert0 = p->give("VERTICES");
1700      bigintmat* vert1 = PmMatrixInteger2Bigintmat(&vert0);
1701      output->m[0].rtyp = BIGINTMAT_CMD;
1702      output->m[0].data = (void*) vert1;
1703
1704      polymake::Graph<> gr=p->give("GRAPH.ADJACENCY");
1705      polymake::IncidenceMatrix<polymake::NonSymmetric> adj = adjacency_matrix(gr);
1706      lists listOfEdges = PmIncidenceMatrix2ListOfIntvecs(&adj);
1707      output->m[1].rtyp = LIST_CMD;
1708      output->m[1].data = (void*) listOfEdges;
1709      delete p;
1710    }
1711    catch (const std::exception& ex)
1712    {
1713      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1714      return TRUE;
1715    }
1716    res->rtyp = LIST_CMD;
1717    res->data = (void*) output;
1718    return FALSE;
1719  }
1720  WerrorS("vertexEdgeGraph: unexpected parameters");
1721  return TRUE;
1722}
1723
1724
1725BOOLEAN PMvertexEdgeGraph(leftv res, leftv args)
1726{
1727  leftv u = args;
1728  if ((u != NULL) && (u->Typ() == polytopeID))
1729  {
1730    gfan::ZCone* zp = (gfan::ZCone*) u->Data();
1731    lists output=(lists)omAllocBin(slists_bin); output->Init(2);
1732    try
1733    {
1734      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1735      polymake::Matrix<polymake::Integer> vert0 = p->give("VERTICES");
1736      bigintmat* vert1 = PmMatrixInteger2Bigintmat(&vert0);
1737      output->m[0].rtyp = BIGINTMAT_CMD;
1738      output->m[0].data = (void*) vert1;
1739
1740      polymake::Graph<> gr=p->give("GRAPH.ADJACENCY");
1741      polymake::IncidenceMatrix<polymake::NonSymmetric> adj = adjacency_matrix(gr);
1742      lists listOfEdges = PmAdjacencyMatrix2ListOfEdges(&adj);
1743      output->m[1].rtyp = LIST_CMD;
1744      output->m[1].data = (void*) listOfEdges;
1745      delete p;
1746    }
1747    catch (const std::exception& ex)
1748    {
1749      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1750      return TRUE;
1751    }
1752    res->rtyp = LIST_CMD;
1753    res->data = (void*) output;
1754    return FALSE;
1755  }
1756  WerrorS("vertexEdgeGraph: unexpected parameters");
1757  return TRUE;
1758}
1759
1760extern "C" int SI_MOD_INIT(polymake)(SModulFunctions* p)
1761{
1762  if (init_polymake==NULL)
1763    {init_polymake = new polymake::Main();}
1764  init_polymake->set_application("fan");
1765  // p->iiAddCproc("polymake.so","coneViaPoints",FALSE,PMconeViaRays);
1766  // p->iiAddCproc("polymake.so","polytopeViaPoints",FALSE,PMpolytopeViaVertices);
1767  p->iiAddCproc("polymake.so","isLatticePolytope",FALSE,PMisLatticePolytope);
1768  p->iiAddCproc("polymake.so","isBounded",FALSE,PMisBounded);
1769  p->iiAddCproc("polymake.so","isReflexive",FALSE,PMisReflexive);
1770  p->iiAddCproc("polymake.so","isGorenstein",FALSE,PMisGorenstein);
1771  p->iiAddCproc("polymake.so","gorensteinIndex",FALSE,PMgorensteinIndex);
1772  p->iiAddCproc("polymake.so","gorensteinVector",FALSE,PMgorensteinVector);
1773  p->iiAddCproc("polymake.so","isCanonical",FALSE,PMisCanonical);
1774  p->iiAddCproc("polymake.so","isTerminal",FALSE,PMisTerminal);
1775  p->iiAddCproc("polymake.so","isLatticeEmpty",FALSE,PMisLatticeEmpty);
1776  p->iiAddCproc("polymake.so","latticeVolume",FALSE,PMlatticeVolume);
1777  p->iiAddCproc("polymake.so","latticeDegree",FALSE,PMlatticeDegree);
1778  p->iiAddCproc("polymake.so","latticeCodegree",FALSE,PMlatticeCodegree);
1779  p->iiAddCproc("polymake.so","ehrhartPolynomialCoeff",FALSE,PMehrhartPolynomialCoeff);
1780  p->iiAddCproc("polymake.so","fVectorP",FALSE,PMfVector);
1781  p->iiAddCproc("polymake.so","hVector",FALSE,PMhVector);
1782  p->iiAddCproc("polymake.so","hStarVector",FALSE,PMhStarVector);
1783  p->iiAddCproc("polymake.so","isNormal",FALSE,PMisNormal);
1784  p->iiAddCproc("polymake.so","facetWidths",FALSE,PMfacetWidths);
1785  p->iiAddCproc("polymake.so","facetWidth",FALSE,PMfacetWidth);
1786  p->iiAddCproc("polymake.so","facetVertexLatticeDistances",FALSE,PMfacetVertexLatticeDistances);
1787  p->iiAddCproc("polymake.so","isCompressed",FALSE,PMisCompressed);
1788  p->iiAddCproc("polymake.so","isSmooth",FALSE,PMisSmooth);
1789  p->iiAddCproc("polymake.so","isVeryAmple",FALSE,PMisVeryAmple);
1790  p->iiAddCproc("polymake.so","latticePoints",FALSE,PMlatticePoints);
1791  p->iiAddCproc("polymake.so","nLatticePoints",FALSE,PMnLatticePoints);
1792  p->iiAddCproc("polymake.so","interiorLatticePoints",FALSE,PMinteriorLatticePoints);
1793  p->iiAddCproc("polymake.so","nInteriorLatticePoints",FALSE,PMnInteriorLatticePoints);
1794  p->iiAddCproc("polymake.so","boundaryLatticePoints",FALSE,PMboundaryLatticePoints);
1795  p->iiAddCproc("polymake.so","nBoundaryLatticePoints",FALSE,PMnBoundaryLatticePoints);
1796  p->iiAddCproc("polymake.so","hilbertBasis",FALSE,PMhilbertBasis);
1797  p->iiAddCproc("polymake.so","nHilbertBasis",FALSE,PMnHilbertBasis);
1798  p->iiAddCproc("polymake.so","minkowskiSum",FALSE,PMminkowskiSum);
1799  p->iiAddCproc("polymake.so","maximalFace",FALSE,PMmaximalFace);
1800  p->iiAddCproc("polymake.so","minimalFace",FALSE,PMminimalFace);
1801  p->iiAddCproc("polymake.so","maximalValue",FALSE,PMmaximalValue);
1802  p->iiAddCproc("polymake.so","minimalValue",FALSE,PMminimalValue);
1803  p->iiAddCproc("polymake.so","visual",FALSE,visual);
1804  p->iiAddCproc("polymake.so","normalFan",FALSE,normalFan);
1805  p->iiAddCproc("polymake.so","vertexAdjacencyGraph",FALSE,PMvertexAdjacencyGraph);
1806  p->iiAddCproc("polymake.so","vertexEdgeGraph",FALSE,PMvertexEdgeGraph);
1807
1808  blackbox* b=getBlackboxStuff(polytopeID);
1809  b->blackbox_Op2=bbpolytope_Op2;
1810
1811  init_polymake_help();
1812  return MAX_TOK;
1813}
1814
1815#endif /* HAVE_POLYMAKE */
Note: See TracBrowser for help on using the repository browser.