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

spielwiese
Last change on this file since ce2c61 was ce2c61, checked in by Hans Schoenemann <hannes@…>, 8 years ago
fix: support polymake 2.14r1
  • Property mode set to 100644
File size: 49.0 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          WerrorS("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          WerrorS("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      #if (POLYMAKEVERSION >=214)
975      polymake::Matrix<polymake::Integer> lp = p->CallPolymakeMethod("LATTICE_POINTS");
976      #elif (POLYMAKEVERSION >=212)
977      polymake::Matrix<polymake::Integer> lp = p->give("LATTICE_POINTS");
978      #else
979        #error polymake version too old
980      #endif
981      delete p;
982      iv = PmMatrixInteger2Intvec(&lp,ok);
983    }
984    catch (const std::exception& ex)
985    {
986      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
987      return TRUE;
988    }
989    if (!ok)
990    {
991      WerrorS("overflow while converting polymake::Integer to int");
992      return TRUE;
993    }
994    res->rtyp = INTMAT_CMD;
995    res->data = (char*) iv;
996    return FALSE;
997  }
998  WerrorS("LatticePoints: unexpected parameters");
999  return TRUE;
1000}
1001
1002
1003BOOLEAN PMnLatticePoints(leftv res, leftv args)
1004{
1005  leftv u = args;
1006  if ((u != NULL) && (u->Typ() == polytopeID))
1007  {
1008    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1009    int n;
1010    bool ok = true;
1011    try
1012    {
1013      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1014      polymake::Integer nlp = p->give("N_LATTICE_POINTS");
1015      delete p;
1016      n = PmInteger2Int(nlp,ok);
1017    }
1018    catch (const std::exception& ex)
1019    {
1020      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1021      return TRUE;
1022    }
1023    if (!ok)
1024    {
1025      WerrorS("overflow while converting polymake::Integer to int");
1026      return TRUE;
1027    }
1028    res->rtyp = INT_CMD;
1029    res->data = (char*) (long) n;
1030    return FALSE;
1031  }
1032  WerrorS("nLatticePoints: unexpected parameters");
1033  return TRUE;
1034}
1035
1036
1037BOOLEAN PMinteriorLatticePoints(leftv res, leftv args)
1038{
1039  leftv u = args;
1040  if ((u != NULL) && (u->Typ() == polytopeID))
1041  {
1042    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1043    intvec* iv;
1044    bool ok = true;
1045    try
1046    {
1047      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1048      polymake::Matrix<polymake::Integer> lp = p->give("INTERIOR_LATTICE_POINTS");
1049      delete p;
1050      iv = PmMatrixInteger2Intvec(&lp,ok);
1051    }
1052    catch (const std::exception& ex)
1053    {
1054      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1055      return TRUE;
1056    }
1057    if (!ok)
1058    {
1059      WerrorS("overflow while converting polymake::Integer to int");
1060      return TRUE;
1061    }
1062    res->rtyp = INTMAT_CMD;
1063    res->data = (char*) iv;
1064    return FALSE;
1065  }
1066  WerrorS("interiorLatticePoints: unexpected parameters");
1067  return TRUE;
1068}
1069
1070
1071BOOLEAN PMnInteriorLatticePoints(leftv res, leftv args)
1072{
1073  leftv u = args;
1074  if ((u != NULL) && (u->Typ() == polytopeID))
1075  {
1076    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1077    int n;
1078    bool ok = true;
1079    try
1080    {
1081      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1082      polymake::Integer nlp = p->give("N_INTERIOR_LATTICE_POINTS");
1083      delete p;
1084      n = PmInteger2Int(nlp,ok);
1085    }
1086    catch (const std::exception& ex)
1087    {
1088      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1089      return TRUE;
1090    }
1091    if (!ok)
1092    {
1093      WerrorS("overflow while converting polymake::Integer to int");
1094      return TRUE;
1095    }
1096    res->rtyp = INT_CMD;
1097    res->data = (char*) (long) n;
1098    return FALSE;
1099  }
1100  WerrorS("nInteriorLatticePoints: unexpected parameters");
1101  return TRUE;
1102}
1103
1104
1105BOOLEAN PMboundaryLatticePoints(leftv res, leftv args)
1106{
1107  leftv u = args;
1108  if ((u != NULL) && (u->Typ() == polytopeID))
1109  {
1110    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1111    intvec* iv;
1112    bool ok = true;
1113    try
1114    {
1115      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1116      polymake::Matrix<polymake::Integer> lp = p->give("BOUNDARY_LATTICE_POINTS");
1117      delete p;
1118      iv = PmMatrixInteger2Intvec(&lp,ok);
1119    }
1120    catch (const std::exception& ex)
1121    {
1122      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1123      return TRUE;
1124    }
1125    if (!ok)
1126    {
1127      WerrorS("overflow while converting polymake::Integer to int");
1128      return TRUE;
1129    }
1130    res->rtyp = INTMAT_CMD;
1131    res->data = (char*) iv;
1132    return FALSE;
1133  }
1134  WerrorS("boundaryLatticePoints: unexpected parameters");
1135  return TRUE;
1136}
1137
1138
1139BOOLEAN PMnBoundaryLatticePoints(leftv res, leftv args)
1140{
1141  leftv u = args;
1142  if ((u != NULL) && (u->Typ() == polytopeID))
1143  {
1144    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1145    int n;
1146    bool ok = true;
1147    try
1148    {
1149      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1150      polymake::Integer nlp = p->give("N_BOUNDARY_LATTICE_POINTS");
1151      delete p;
1152      n = PmInteger2Int(nlp,ok);
1153    }
1154    catch (const std::exception& ex)
1155    {
1156      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1157      return TRUE;
1158    }
1159    if (!ok)
1160    {
1161      WerrorS("overflow while converting polymake::Integer to int");
1162      return TRUE;
1163    }
1164    res->rtyp = INT_CMD;
1165    res->data = (char*) (long) n;
1166    return FALSE;
1167  }
1168  WerrorS("nBoundaryLatticePoints: unexpected parameters");
1169  return TRUE;
1170}
1171
1172
1173BOOLEAN PMhilbertBasis(leftv res, leftv args)
1174{
1175  leftv u = args;
1176  if ((u != NULL) && (u->Typ() == coneID))
1177  {
1178    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1179    intvec* iv;
1180    bool ok = true;
1181    try
1182    {
1183      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1184      #if (POLYMAKEVERSION >=214)
1185      polymake::Matrix<polymake::Integer> lp = p->CallPolymakeMethod("HILBERT_BASIS");
1186      #elif (POLYMAKEVERSION >=212)
1187      polymake::Matrix<polymake::Integer> lp = p->give("HILBERT_BASIS");
1188      #else
1189        #error polymake version too old
1190      #endif
1191      delete p;
1192      iv = PmMatrixInteger2Intvec(&lp,ok);
1193    }
1194    catch (const std::exception& ex)
1195    {
1196      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1197      return TRUE;
1198    }
1199    if (!ok)
1200    {
1201      WerrorS("overflow while converting polymake::Integer to int");
1202      return TRUE;
1203    }
1204    res->rtyp = INTMAT_CMD;
1205    res->data = (char*) iv;
1206    return FALSE;
1207  }
1208  WerrorS("hilbertBasis: unexpected parameters");
1209  return TRUE;
1210}
1211
1212
1213BOOLEAN PMnHilbertBasis(leftv res, leftv args)
1214{
1215  leftv u = args;
1216  if ((u != NULL) && (u->Typ() == coneID))
1217  {
1218    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1219    int n;
1220    bool ok = true;
1221    try
1222    {
1223      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1224      polymake::Integer nlp = p->give("N_HILBERT_BASIS");
1225      delete p;
1226      n = PmInteger2Int(nlp,ok);
1227    }
1228    catch (const std::exception& ex)
1229    {
1230      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1231      return TRUE;
1232    }
1233    if (!ok)
1234    {
1235      WerrorS("overflow while converting polymake::Integer to int");
1236      return TRUE;
1237    }
1238    res->rtyp = INT_CMD;
1239    res->data = (char*) (long) n;
1240    return FALSE;
1241  }
1242  WerrorS("nHilbertBasis: unexpected parameters");
1243  return TRUE;
1244}
1245
1246
1247BOOLEAN PMminkowskiSum(leftv res, leftv args)
1248{
1249  leftv u = args;
1250  if ((u != NULL) && (u->Typ() == polytopeID))
1251  {
1252    leftv v = u->next;
1253    if ((v != NULL) && (v->Typ() == polytopeID))
1254    {
1255      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1256      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1257      gfan::ZCone* ms;
1258      try
1259      {
1260        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1261        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1262        polymake::perl::Object pms;
1263        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1264        delete pp;
1265        delete pq;
1266        ms = PmPolytope2ZPolytope(&pms);
1267      }
1268      catch (const std::exception& ex)
1269      {
1270        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1271        return TRUE;
1272      }
1273      res->rtyp = polytopeID;
1274      res->data = (char*) ms;
1275      return FALSE;
1276    }
1277    if ((v != NULL) && (v->Typ() == coneID))
1278    {
1279      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1280      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
1281      gfan::ZCone* zq = new gfan::ZCone(liftUp(*zc));
1282      gfan::ZCone* ms;
1283      try
1284      {
1285        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1286        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1287        polymake::perl::Object pms;
1288        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1289        delete pp;
1290        delete pq;
1291        ms = PmPolytope2ZPolytope(&pms);
1292      }
1293      catch (const std::exception& ex)
1294      {
1295        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1296        delete zq;
1297        return TRUE;
1298      }
1299      res->rtyp = polytopeID;
1300      res->data = (char*) ms;
1301      delete zq;
1302      return FALSE;
1303    }
1304  }
1305  if ((u != NULL) && (u->Typ() == coneID))
1306  {
1307    leftv v = u->next;
1308    if ((v != NULL) && (v->Typ() == polytopeID))
1309    {
1310      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1311      gfan::ZCone* zp = new gfan::ZCone(liftUp(*zc));
1312      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1313      gfan::ZCone* ms;
1314      try
1315      {
1316        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1317        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1318        polymake::perl::Object pms;
1319        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1320        delete pp;
1321        delete pq;
1322        ms = PmPolytope2ZPolytope(&pms);
1323      }
1324      catch (const std::exception& ex)
1325      {
1326        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1327        delete zp;
1328        return TRUE;
1329      }
1330      res->rtyp = polytopeID;
1331      res->data = (char*) ms;
1332      delete zp;
1333      return FALSE;
1334    }
1335    if ((v != NULL) && (v->Typ() == coneID))
1336    {
1337      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1338      gfan::ZCone* zq = (gfan::ZCone*)v->Data();
1339      gfan::ZCone* ms;
1340      try
1341      {
1342        polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1343        polymake::perl::Object* pq = ZPolytope2PmPolytope(zq);
1344        polymake::perl::Object pms;
1345        CallPolymakeFunction("minkowski_sum", *pp, *pq) >> pms;
1346        delete pp;
1347        delete pq;
1348        ms = PmPolytope2ZPolytope(&pms);
1349      }
1350      catch (const std::exception& ex)
1351      {
1352        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1353        return TRUE;
1354      }
1355      res->rtyp = coneID;
1356      res->data = (char*) ms;
1357      return FALSE;
1358    }
1359  }
1360  WerrorS("minkowskiSum: unexpected parameters");
1361  return TRUE;
1362}
1363
1364
1365polymake::Matrix<polymake::Integer> verticesOf(const polymake::perl::Object* p,
1366                                               const polymake::Set<polymake::Integer>* s)
1367{
1368  polymake::Matrix<polymake::Integer> allrays = p->give("VERTICES");
1369  polymake::Matrix<polymake::Integer> wantedrays;
1370  bool ok = true;
1371  for(polymake::Entire<polymake::Set<polymake::Integer> >::const_iterator i=polymake::entire(*s); !i.at_end(); i++)
1372  {
1373    wantedrays = wantedrays / allrays.row(PmInteger2Int(*i,ok));
1374  }
1375  if (!ok)
1376  {
1377    WerrorS("overflow while converting polymake::Integer to int in raysOf");
1378  }
1379  return wantedrays;
1380}
1381
1382
1383BOOLEAN PMmaximalFace(leftv res, leftv args)
1384{
1385  leftv u = args;
1386  if ((u != NULL) && (u->Typ() == polytopeID))
1387  {
1388    leftv v = u->next;
1389    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1390    {
1391      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1392      intvec* iv = (intvec*) v->Data();
1393      intvec* maxface;
1394      bool ok = true;
1395      try
1396      {
1397        polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1398        polymake::perl::Object o("LinearProgram<Rational>");
1399        o.take("LINEAR_OBJECTIVE") << Intvec2PmVectorInteger(iv);
1400        p->take("LP") << o;
1401        polymake::Set<polymake::Integer> mf = p->give("LP.MAXIMAL_FACE");
1402        polymake::Matrix<polymake::Integer> vertices = verticesOf(p,&mf);
1403        delete p;
1404        maxface = new intvec(PmMatrixInteger2Intvec(&vertices,ok));
1405      }
1406      catch (const std::exception& ex)
1407      {
1408        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1409        return TRUE;
1410      }
1411      if (!ok)
1412      {
1413        WerrorS("overflow while converting polymake::Integer to int");
1414        return TRUE;
1415      }
1416      res->rtyp = INTVEC_CMD;
1417      res->data = (char*) maxface;
1418      return FALSE;
1419    }
1420  }
1421  WerrorS("maximalFace: unexpected parameters");
1422  return TRUE;
1423}
1424
1425
1426BOOLEAN PMminimalFace(leftv res, leftv args)
1427{
1428  leftv u = args;
1429  if ((u != NULL) && (u->Typ() == polytopeID))
1430  {
1431    leftv v = u->next;
1432    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1433    {
1434      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1435      intvec* iv = (intvec*) v->Data();
1436      intvec* minface;
1437      bool ok = true;
1438      try
1439      {
1440        polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1441        polymake::perl::Object o("LinearProgram<Rational>");
1442        o.take("LINEAR_OBJECTIVE") << Intvec2PmVectorInteger(iv);
1443        p->take("LP") << o;
1444        polymake::Set<polymake::Integer> mf = p->give("LP.MINIMAL_FACE");
1445        polymake::Matrix<polymake::Integer> vertices = verticesOf(p,&mf);
1446        delete p;
1447        minface = new intvec(PmMatrixInteger2Intvec(&vertices,ok));
1448      }
1449      catch (const std::exception& ex)
1450      {
1451        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1452        return TRUE;
1453      }
1454      if (!ok)
1455      {
1456        WerrorS("overflow while converting polymake::Integer to int");
1457        return TRUE;
1458      }
1459      res->rtyp = INTVEC_CMD;
1460      res->data = (char*) minface;
1461      return FALSE;
1462    }
1463  }
1464  WerrorS("minimalFace: unexpected parameters");
1465  return TRUE;
1466}
1467
1468
1469BOOLEAN PMmaximalValue(leftv res, leftv args)
1470{
1471  leftv u = args;
1472  if ((u != NULL) && (u->Typ() == polytopeID))
1473  {
1474    leftv v = u->next;
1475    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1476    {
1477      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1478      intvec* iv = (intvec*) v->Data();
1479      if (iv->rows()==zp->ambientDimension())
1480      {
1481        int m;
1482        bool ok = true;
1483        try
1484        {
1485          polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1486          polymake::Vector<polymake::Integer> lo = Intvec2PmVectorInteger(iv);
1487          polymake::perl::Object o("LinearProgram<Rational>");
1488          o.take("LINEAR_OBJECTIVE") << lo;
1489          p->take("LP") << o;
1490          polymake::Integer mv = p->give("LP.MAXIMAL_VALUE");
1491          delete p;
1492          m = PmInteger2Int(mv,ok);
1493        }
1494        catch (const std::exception& ex)
1495        {
1496          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1497          return TRUE;
1498        }
1499        if (!ok)
1500        {
1501          WerrorS("overflow while converting polymake::Integer to int");
1502          return TRUE;
1503        }
1504        res->rtyp = INT_CMD;
1505        res->data = (char*) (long) m;
1506        return FALSE;
1507      }
1508    }
1509    WerrorS("maximalValue: vector is of wrong size");
1510    return TRUE;
1511  }
1512  WerrorS("maximalValue: unexpected parameters");
1513  return TRUE;
1514}
1515
1516BOOLEAN PMminimalValue(leftv res, leftv args)
1517{
1518  leftv u = args;
1519  if ((u != NULL) && (u->Typ() == polytopeID))
1520  {
1521    leftv v = u->next;
1522    if ((v != NULL) && (v->Typ() == INTVEC_CMD))
1523    {
1524      gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1525      intvec* iv = (intvec*) v->Data();
1526      if (iv->rows()==zp->ambientDimension())
1527      {
1528        int m;
1529        bool ok = true;
1530        try
1531        {
1532          polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1533          polymake::Vector<polymake::Integer> lo = Intvec2PmVectorInteger(iv);
1534          polymake::perl::Object o("LinearProgram<Rational>");
1535          o.take("LINEAR_OBJECTIVE") << lo;
1536          p->take("LP") << o;
1537          polymake::Integer mv = p->give("LP.MINIMAL_VALUE");
1538          delete p;
1539          m = PmInteger2Int(mv,ok);
1540        }
1541        catch (const std::exception& ex)
1542        {
1543          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1544          return TRUE;
1545        }
1546        if (!ok)
1547        {
1548          WerrorS("overflow while converting polymake::Integer to int");
1549          return TRUE;
1550        }
1551        res->rtyp = INT_CMD;
1552        res->data = (char*) (long) m;
1553        return FALSE;
1554      }
1555    }
1556    WerrorS("minimalValue: vector is of wrong size");
1557    return TRUE;
1558  }
1559  WerrorS("minimalValue: unexpected parameters");
1560  return TRUE;
1561}
1562
1563
1564BOOLEAN visual(leftv res, leftv args)
1565{
1566  leftv u = args;
1567  if ((u != NULL) && (u->Typ() == polytopeID))
1568  {
1569    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1570    try
1571    {
1572      polymake::perl::Object* pp = ZPolytope2PmPolytope(zp);
1573      VoidCallPolymakeFunction("jreality",pp->CallPolymakeMethod("VISUAL"));
1574      delete pp;
1575    }
1576    catch (const std::exception& ex)
1577    {
1578      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1579      return TRUE;
1580    }
1581    res->rtyp = NONE;
1582    res->data = NULL;
1583    return FALSE;
1584  }
1585  if ((u != NULL) && (u->Typ() == fanID))
1586  {
1587    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
1588    try
1589    {
1590      polymake::perl::Object* pf=ZFan2PmFan(zf);
1591      VoidCallPolymakeFunction("jreality",pf->CallPolymakeMethod("VISUAL"));
1592    }
1593    catch (const std::exception& ex)
1594    {
1595      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1596      return TRUE;
1597    }
1598    res->rtyp = NONE;
1599    res->data = NULL;
1600    return FALSE;
1601  }
1602  WerrorS("visual: unexpected parameters");
1603  return TRUE;
1604}
1605
1606BOOLEAN normalFan(leftv res, leftv args)
1607{
1608  leftv u = args;
1609  if ((u != NULL) && (u->Typ() == polytopeID))
1610  {
1611    gfan::ZCone* zp = (gfan::ZCone*)u->Data();
1612    gfan::ZFan* zf = new gfan::ZFan(0);
1613    try
1614    {
1615      polymake::perl::Object* p=ZPolytope2PmPolytope(zp);
1616      polymake::perl::Object pf;
1617      CallPolymakeFunction("normal_fan", *p) >> pf;
1618      delete p;
1619      zf = PmFan2ZFan(&pf);
1620    }
1621    catch (const std::exception& ex)
1622    {
1623      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1624      return TRUE;
1625    }
1626    res->rtyp = fanID;
1627    res->data = (char*) zf;
1628    return FALSE;
1629  }
1630  WerrorS("normalFan: unexpected parameters");
1631  return TRUE;
1632}
1633
1634BOOLEAN PMconeViaRays(leftv res, leftv args)
1635{
1636  leftv u = args;
1637  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
1638  {
1639    polymake::perl::Object pc("Cone<Rational>");
1640    intvec* hlines = (intvec*) u->Data(); // these will are half lines in the cone
1641    polymake::Matrix<polymake::Integer> pmhlines = Intvec2PmMatrixInteger(hlines);
1642    pc.take("INPUT_RAYS") << pmhlines;
1643
1644    leftv v = u->next;
1645    if ((v != NULL) && (v->Typ() == INTMAT_CMD))
1646    {
1647      intvec* lines = (intvec*) v->Data(); // these will be lines in the cone
1648      polymake::Matrix<polymake::Integer> pmlines = Intvec2PmMatrixInteger(lines);
1649      pc.take("INPUT_LINEALITY") << pmlines;
1650
1651      // leftv w = v->next;
1652      // if ((w != NULL) && (w->Typ() == INT_CMD))
1653      // {
1654      //   int flag = (int) (long) w->Data(); // TODO: this will indicate whether the
1655      //                                      // information provided are exact
1656      // }
1657    }
1658    gfan::ZCone* zc = PmCone2ZCone(&pc);
1659    res->rtyp = coneID;
1660    res->data = (char*) zc;
1661    return FALSE;
1662  }
1663  WerrorS("coneViaRays: unexpected parameters");
1664  return TRUE;
1665}
1666
1667
1668BOOLEAN PMpolytopeViaVertices(leftv res, leftv args)
1669{
1670  leftv u = args;
1671  if ((u != NULL) && (u->Typ() == INTMAT_CMD))
1672  {
1673    polymake::perl::Object pp("Polytope<Rational>");
1674    intvec* points = (intvec*) u->Data(); // these will be vertices of or points in the polytope
1675    polymake::Matrix<polymake::Integer> pmpoints = Intvec2PmMatrixInteger(points);
1676
1677    leftv v = u->next;
1678    if ((v != NULL) && (v->Typ() == INT_CMD))
1679    {
1680      int flag = (int) (long) v->Data();
1681      switch(flag)
1682      {
1683        case 0:  pp.take("POINTS") << pmpoints;   // case means the matrix may contain points inside the polytope
1684        case 1:  pp.take("VERTICES") << pmpoints; // case means the matrix only contains vertices of the polytope
1685        default: WerrorS("polytopeViaVertices: invalid flag");
1686      }
1687    }
1688    else
1689      pp.take("POINTS") << pmpoints;              // by default, we assume that matrix may contain non-vertices
1690
1691    gfan::ZCone* zp = PmPolytope2ZPolytope(&pp);
1692    res->rtyp = polytopeID;
1693    res->data = (char*) zp;
1694    return FALSE;
1695  }
1696  WerrorS("polytopeViaVertices: unexpected parameters");
1697  return TRUE;
1698}
1699
1700
1701BOOLEAN PMvertexAdjacencyGraph(leftv res, leftv args)
1702{
1703  leftv u = args;
1704  if ((u != NULL) && (u->Typ() == polytopeID))
1705  {
1706    gfan::ZCone* zp = (gfan::ZCone*) u->Data();
1707    lists output=(lists)omAllocBin(slists_bin); output->Init(2);
1708    try
1709    {
1710      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1711      polymake::Matrix<polymake::Integer> vert0 = p->give("VERTICES");
1712      bigintmat* vert1 = PmMatrixInteger2Bigintmat(&vert0);
1713      output->m[0].rtyp = BIGINTMAT_CMD;
1714      output->m[0].data = (void*) vert1;
1715
1716      polymake::Graph<> gr=p->give("GRAPH.ADJACENCY");
1717      polymake::IncidenceMatrix<polymake::NonSymmetric> adj = adjacency_matrix(gr);
1718      lists listOfEdges = PmIncidenceMatrix2ListOfIntvecs(&adj);
1719      output->m[1].rtyp = LIST_CMD;
1720      output->m[1].data = (void*) listOfEdges;
1721      delete p;
1722    }
1723    catch (const std::exception& ex)
1724    {
1725      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1726      return TRUE;
1727    }
1728    res->rtyp = LIST_CMD;
1729    res->data = (void*) output;
1730    return FALSE;
1731  }
1732  WerrorS("vertexEdgeGraph: unexpected parameters");
1733  return TRUE;
1734}
1735
1736
1737BOOLEAN PMvertexEdgeGraph(leftv res, leftv args)
1738{
1739  leftv u = args;
1740  if ((u != NULL) && (u->Typ() == polytopeID))
1741  {
1742    gfan::ZCone* zp = (gfan::ZCone*) u->Data();
1743    lists output=(lists)omAllocBin(slists_bin); output->Init(2);
1744    try
1745    {
1746      polymake::perl::Object* p = ZPolytope2PmPolytope(zp);
1747      polymake::Matrix<polymake::Integer> vert0 = p->give("VERTICES");
1748      bigintmat* vert1 = PmMatrixInteger2Bigintmat(&vert0);
1749      output->m[0].rtyp = BIGINTMAT_CMD;
1750      output->m[0].data = (void*) vert1;
1751
1752      polymake::Graph<> gr=p->give("GRAPH.ADJACENCY");
1753      polymake::IncidenceMatrix<polymake::NonSymmetric> adj = adjacency_matrix(gr);
1754      lists listOfEdges = PmAdjacencyMatrix2ListOfEdges(&adj);
1755      output->m[1].rtyp = LIST_CMD;
1756      output->m[1].data = (void*) listOfEdges;
1757      delete p;
1758    }
1759    catch (const std::exception& ex)
1760    {
1761      WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
1762      return TRUE;
1763    }
1764    res->rtyp = LIST_CMD;
1765    res->data = (void*) output;
1766    return FALSE;
1767  }
1768  WerrorS("vertexEdgeGraph: unexpected parameters");
1769  return TRUE;
1770}
1771
1772#include <omp.h>
1773// extern "C" void omp_set_num_threads(int num_threads);
1774
1775extern "C" int SI_MOD_INIT(polymake)(SModulFunctions* p)
1776{
1777  omp_set_num_threads(1); // avoid multiple threads within polymake/libnormaliz
1778  if (init_polymake==NULL)
1779    {init_polymake = new polymake::Main();}
1780  init_polymake->set_application("fan");
1781  // p->iiAddCproc("polymake.so","coneViaPoints",FALSE,PMconeViaRays);
1782  // p->iiAddCproc("polymake.so","polytopeViaPoints",FALSE,PMpolytopeViaVertices);
1783  p->iiAddCproc("polymake.so","isLatticePolytope",FALSE,PMisLatticePolytope);
1784  p->iiAddCproc("polymake.so","isBounded",FALSE,PMisBounded);
1785  p->iiAddCproc("polymake.so","isReflexive",FALSE,PMisReflexive);
1786  p->iiAddCproc("polymake.so","isGorenstein",FALSE,PMisGorenstein);
1787  p->iiAddCproc("polymake.so","gorensteinIndex",FALSE,PMgorensteinIndex);
1788  p->iiAddCproc("polymake.so","gorensteinVector",FALSE,PMgorensteinVector);
1789  p->iiAddCproc("polymake.so","isCanonical",FALSE,PMisCanonical);
1790  p->iiAddCproc("polymake.so","isTerminal",FALSE,PMisTerminal);
1791  p->iiAddCproc("polymake.so","isLatticeEmpty",FALSE,PMisLatticeEmpty);
1792  p->iiAddCproc("polymake.so","latticeVolume",FALSE,PMlatticeVolume);
1793  p->iiAddCproc("polymake.so","latticeDegree",FALSE,PMlatticeDegree);
1794  p->iiAddCproc("polymake.so","latticeCodegree",FALSE,PMlatticeCodegree);
1795  p->iiAddCproc("polymake.so","ehrhartPolynomialCoeff",FALSE,PMehrhartPolynomialCoeff);
1796  p->iiAddCproc("polymake.so","fVectorP",FALSE,PMfVector);
1797  p->iiAddCproc("polymake.so","hVector",FALSE,PMhVector);
1798  p->iiAddCproc("polymake.so","hStarVector",FALSE,PMhStarVector);
1799  p->iiAddCproc("polymake.so","isNormal",FALSE,PMisNormal);
1800  p->iiAddCproc("polymake.so","facetWidths",FALSE,PMfacetWidths);
1801  p->iiAddCproc("polymake.so","facetWidth",FALSE,PMfacetWidth);
1802  p->iiAddCproc("polymake.so","facetVertexLatticeDistances",FALSE,PMfacetVertexLatticeDistances);
1803  p->iiAddCproc("polymake.so","isCompressed",FALSE,PMisCompressed);
1804  p->iiAddCproc("polymake.so","isSmooth",FALSE,PMisSmooth);
1805  p->iiAddCproc("polymake.so","isVeryAmple",FALSE,PMisVeryAmple);
1806  p->iiAddCproc("polymake.so","latticePoints",FALSE,PMlatticePoints);
1807  p->iiAddCproc("polymake.so","nLatticePoints",FALSE,PMnLatticePoints);
1808  p->iiAddCproc("polymake.so","interiorLatticePoints",FALSE,PMinteriorLatticePoints);
1809  p->iiAddCproc("polymake.so","nInteriorLatticePoints",FALSE,PMnInteriorLatticePoints);
1810  p->iiAddCproc("polymake.so","boundaryLatticePoints",FALSE,PMboundaryLatticePoints);
1811  p->iiAddCproc("polymake.so","nBoundaryLatticePoints",FALSE,PMnBoundaryLatticePoints);
1812  p->iiAddCproc("polymake.so","hilbertBasis",FALSE,PMhilbertBasis);
1813  p->iiAddCproc("polymake.so","nHilbertBasis",FALSE,PMnHilbertBasis);
1814  p->iiAddCproc("polymake.so","minkowskiSum",FALSE,PMminkowskiSum);
1815  p->iiAddCproc("polymake.so","maximalFace",FALSE,PMmaximalFace);
1816  p->iiAddCproc("polymake.so","minimalFace",FALSE,PMminimalFace);
1817  p->iiAddCproc("polymake.so","maximalValue",FALSE,PMmaximalValue);
1818  p->iiAddCproc("polymake.so","minimalValue",FALSE,PMminimalValue);
1819  p->iiAddCproc("polymake.so","visual",FALSE,visual);
1820  p->iiAddCproc("polymake.so","normalFan",FALSE,normalFan);
1821  p->iiAddCproc("polymake.so","vertexAdjacencyGraph",FALSE,PMvertexAdjacencyGraph);
1822  p->iiAddCproc("polymake.so","vertexEdgeGraph",FALSE,PMvertexEdgeGraph);
1823
1824  blackbox* b=getBlackboxStuff(polytopeID);
1825  b->blackbox_Op2=bbpolytope_Op2;
1826
1827  init_polymake_help();
1828  return MAX_TOK;
1829}
1830
1831#endif /* HAVE_POLYMAKE */
Note: See TracBrowser for help on using the repository browser.