source: git/dyn_modules/callgfanlib/bbfan.cc @ 81384b

spielwiese
Last change on this file since 81384b was 81384b, checked in by Yue Ren <ren@…>, 11 years ago
new: newest version of callgfanlib package as master branch also includes minor changes in bigintmat to allow empty matrices and a new function which returns the regular print output as char* (necessary for the print routines of the convex objects)
  • Property mode set to 100644
File size: 25.7 KB
Line 
1#include <kernel/mod2.h>
2#ifdef HAVE_FANS
3
4#include <Singular/ipid.h>
5#include <Singular/ipshell.h>
6#include <Singular/blackbox.h>
7#include <libpolys/misc/intvec.h>
8#include <libpolys/coeffs/longrat.h>
9#include <libpolys/coeffs/bigintmat.h>
10
11#include <bbfan.h>
12#include <bbcone.h>
13#include <gfan.h>
14#include <sstream>
15
16// #include <kernel/bigintmat.h>
17// #include <omalloc/omalloc.h>
18// #include <kernel/febase.h>
19// #include <kernel/longrat.h>
20// #include <Singular/subexpr.h>
21// #include <Singular/lists.h>
22// #include <gfanlib/gfanlib.h>
23// #include <gfanlib/gfanlib_zfan.h>
24
25
26int fanID;
27
28void* bbfan_Init(blackbox *b)
29{
30  return (void*) new gfan::ZFan(0);
31}
32
33void bbfan_destroy(blackbox *b, void *d)
34{
35  if (d!=NULL)
36  {
37    gfan::ZFan* zf = (gfan::ZFan*) d;
38    delete zf;
39  }
40}
41
42char* bbfan_String(blackbox *b, void *d)
43{
44  if (d==NULL) return omStrDup("invalid object");
45  else
46  {
47    gfan::ZFan* zf = (gfan::ZFan*)d;
48    std::string s = zf->toString();
49    return omStrDup(s.c_str());
50  }
51}
52
53void* bbfan_Copy(blackbox*b, void *d)
54{
55  gfan::ZFan* zf = (gfan::ZFan*)d;
56  gfan::ZFan* newZf = new gfan::ZFan(*zf);
57  return newZf;
58}
59
60BOOLEAN bbfan_Assign(leftv l, leftv r)
61{
62  gfan::ZFan* newZf;
63  if (r==NULL)
64  {
65    if (l->Data()!=NULL)
66    {
67      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
68      delete zd;
69    }
70    newZf = new gfan::ZFan(0);
71  }
72  else if (r->Typ()==l->Typ())
73  {
74    if (l->Data()!=NULL)
75    {
76      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
77      delete zd;
78    }
79    gfan::ZFan* zf = (gfan::ZFan*) r->Data();
80    newZf = new gfan::ZFan(*zf);
81  }
82  else if (r->Typ()==INT_CMD)
83  {
84    int ambientDim = (int) (long) r->Data();
85    if (ambientDim < 0)
86    {
87      Werror("expected an int >= 0, but got %d", ambientDim);
88      return TRUE;
89    }
90    if (l->Data()!=NULL)
91    {
92      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
93      delete zd;
94    }
95    newZf = new gfan::ZFan(ambientDim);
96  }
97  else
98  {
99    Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
100    return TRUE;
101  }
102
103  if (l->rtyp==IDHDL)
104  {
105    IDDATA((idhdl)l->data) = (char*) newZf;
106  }
107  else
108  {
109    l->data = (void*) newZf;
110  }
111  return FALSE;
112}
113
114/* returns 1 iff all rows consist of entries 1..n,
115   where n is the number of columns of the provided
116   bigintmat; 0 otherwise */
117static gfan::IntMatrix permutationIntMatrix(const bigintmat* iv)
118{
119  int cc = iv->cols();
120  int rr = iv->rows();
121  bigintmat* ivCopy = new bigintmat(rr, cc, coeffs_BIGINT);
122  for (int r = 1; r <= rr; r++)
123    for (int c = 1; c <= cc; c++)
124    {
125      number temp1 = nlInit(1,NULL);
126      number temp2 = nlSub(IMATELEM(*iv, r, c),temp1,coeffs_BIGINT);
127      ivCopy->set(r,c,temp2);
128      nlDelete(&temp1,NULL);
129      nlDelete(&temp2,NULL);
130    }
131  gfan::ZMatrix* zm = bigintmatToZMatrix(ivCopy);
132  gfan::IntMatrix im = gfan::IntMatrix(gfan::ZToIntMatrix(*zm));
133  delete zm;
134  return im;
135}
136static BOOLEAN jjFANEMPTY_I(leftv res, leftv v)
137{
138  int ambientDim = (int)(long)v->Data();
139  if (ambientDim < 0)
140  {
141    Werror("expected non-negative ambient dim but got %d", ambientDim);
142    return TRUE;
143  }
144  res->rtyp = fanID;
145  res->data = (void*)(new gfan::ZFan(ambientDim));
146  return FALSE;
147}
148
149static BOOLEAN jjFANEMPTY_IM(leftv res, leftv v)
150{
151  bigintmat* permutations = (bigintmat*)v->Data();
152  int ambientDim = permutations->cols();
153  gfan::IntMatrix im = permutationIntMatrix(permutations);
154  if (!gfan::Permutation::arePermutations(im))
155  {
156    Werror("provided bigintmat contains invalid permutations of {1, ..., %d}", ambientDim);
157    return TRUE;
158  }
159  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
160  sg.computeClosure(im);
161  res->rtyp = fanID;
162  res->data = (void*)(new gfan::ZFan(sg));
163  return FALSE;
164}
165
166BOOLEAN emptyFan(leftv res, leftv args)
167{
168  leftv u = args;
169  if (u == NULL)
170  {
171    res->rtyp = fanID;
172    res->data = (void*) new gfan::ZFan(0);
173    return FALSE;
174  }
175  if ((u != NULL) && (u->Typ() == INT_CMD))
176  {
177    if (u->next == NULL) return jjFANEMPTY_I(res, u);
178  }
179  if ((u != NULL) && (u->Typ() == BIGINTMAT_CMD))
180  {
181    if (u->next == NULL) return jjFANEMPTY_IM(res, u);
182  }
183  WerrorS("emptyFan: unexpected parameters");
184  return TRUE;
185}
186
187static BOOLEAN jjFANFULL_I(leftv res, leftv v)
188{
189  int ambientDim = (int)(long)v->Data();
190  if (ambientDim < 0)
191  {
192    Werror("expected non-negative ambient dim but got %d", ambientDim);
193    return TRUE;
194  }
195  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(ambientDim));
196  res->rtyp = fanID;
197  res->data = (void*) zf;
198  return FALSE;
199}
200static BOOLEAN jjFANFULL_IM(leftv res, leftv v)
201{
202  bigintmat* permutations = (bigintmat*)v->Data();
203  int ambientDim = permutations->cols();
204  gfan::IntMatrix im = permutationIntMatrix(permutations);
205  if (!gfan::Permutation::arePermutations(im))
206  {
207    Werror("provided bigintmat contains invalid permutations of {1, ..., %d}", ambientDim);
208    return TRUE;
209  }
210  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
211  sg.computeClosure(im);
212  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(sg));
213  res->rtyp = fanID;
214  res->data = (void*) zf;
215  return FALSE;
216}
217
218BOOLEAN fullFan(leftv res, leftv args)
219{
220  leftv u = args;
221  if (u == NULL)
222  {
223    res->rtyp = fanID;
224    res->data = (void*) new gfan::ZFan(0);
225    return FALSE;
226  }
227  if ((u != NULL) && (u->Typ() == INT_CMD))
228    if (u->next == NULL) return jjFANFULL_I(res, u);
229  if ((u != NULL) && (u->Typ() == BIGINTMAT_CMD))
230    if (u->next == NULL) return jjFANFULL_IM(res, u);
231  WerrorS("fullFan: unexpected parameters");
232  return TRUE;
233}
234
235int getAmbientDimension(gfan::ZFan* zf)
236{
237  return zf->getAmbientDimension();
238}
239
240int getCodimension(gfan::ZFan* zf)
241{
242  return zf->getCodimension();
243}
244
245int getDimension(gfan::ZFan* zf)
246{
247  return zf->getDimension();
248}
249
250int getLinealityDimension(gfan::ZFan* zf)
251{
252  return zf->getLinealityDimension();
253}
254
255BOOLEAN numberOfConesOfDimension(leftv res, leftv args)
256{
257  leftv u=args;
258  if ((u != NULL) && (u->Typ() == fanID))
259  {
260    leftv v=u->next;
261    if ((v != NULL) && (v->Typ() == INT_CMD))
262    {
263      leftv w=v->next;
264      if ((w != NULL) && (w->Typ() == INT_CMD))
265      {
266        leftv x=w->next;
267        if ((x != NULL) && (x->Typ() == INT_CMD))
268        {
269          gfan::ZFan* zf = (gfan::ZFan*) u->Data();
270          int d = (int)(long)v->Data();
271          int o = (int)(long)w->Data();
272          int m = (int)(long)x->Data();
273          if ( (0<=d) && (d <= zf->getAmbientDimension())
274                      && ((o == 0) || (o == 1))
275                      && ((m == 0) || (m == 1)))
276          {
277            bool oo = (bool) o;
278            bool mm = (bool) m;
279            int ld = zf->getLinealityDimension();
280            if (d-ld>=0)
281            {
282              int n = zf->numberOfConesOfDimension(d,oo,mm);
283              res->rtyp = INT_CMD;
284              res->data = (void*) (long) n;
285              return FALSE;
286            }
287            else
288            {
289              res->rtyp = INT_CMD;
290              res->data = (void*) (long) 0;
291              return FALSE;
292            }
293          }
294        }
295      }
296    }
297  }
298  WerrorS("numberOfConesOfDimension: unexpected parameters");
299  return TRUE;
300}
301
302BOOLEAN ncones(leftv res, leftv args)
303{
304  leftv u=args;
305  if ((u != NULL) && (u->Typ() == fanID))
306    {
307      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
308      int d = zf->getAmbientDimension();
309      int n = 0;
310
311      for (int i=0; i<=d; i++)
312        { n = n + zf->numberOfConesOfDimension(i,0,0); }
313
314      res->rtyp = INT_CMD;
315      res->data = (void*) (long) n;
316      return FALSE;
317    }
318  else
319    {
320      WerrorS("check_compatibility: unexpected parameters");
321      return TRUE;
322    }
323}
324
325BOOLEAN nmaxcones(leftv res, leftv args)
326{
327  leftv u=args;
328  if ((u != NULL) && (u->Typ() == fanID))
329    {
330      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
331
332      int n = 0;
333      for (int d=0; d<=zf->getAmbientDimension(); d++)
334        { n = n + zf->numberOfConesOfDimension(d,0,1); }
335
336      res->rtyp = INT_CMD;
337      res->data = (void*) (long) n;
338      return FALSE;
339    }
340  else
341    {
342      WerrorS("nmaxcones: unexpected parameters");
343      return TRUE;
344    }
345}
346
347bool isCompatible(gfan::ZFan* zf, gfan::ZCone* zc)
348{
349  bool b = (zf->getAmbientDimension() == zc->ambientDimension());
350  if(b)
351  {
352    for (int d=0; d<=zf->getAmbientDimension(); d++)
353    {
354      for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
355      {
356        gfan::ZCone zd = zf->getCone(d,i,0,1);
357        gfan::ZCone zt = gfan::intersection(*zc,zd);
358        zt.canonicalize();
359        b = b && zd.hasFace(zt);
360      }
361    }
362  }
363  return b;
364}
365
366BOOLEAN isCompatible(leftv res, leftv args)
367{
368  leftv u=args;
369  if ((u != NULL) && (u->Typ() == fanID))
370  {
371    leftv v=u->next;
372    if ((v != NULL) && (v->Typ() == coneID))
373    {
374      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
375      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
376      int b = isCompatible(zf,zc);
377      res->rtyp = INT_CMD;
378      res->data = (void*) (long) b;
379      return FALSE;
380    }
381  }
382  WerrorS("isCompatible: unexpected parameters");
383  return TRUE;
384}
385
386BOOLEAN insertCone(leftv res, leftv args)
387{
388  leftv u=args;
389  if ((u != NULL) && (u->rtyp==IDHDL) && (u->e==NULL) && (u->Typ() == fanID))
390  {
391    leftv v=u->next;
392    if ((v != NULL) && (v->Typ() == coneID))
393    {
394      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
395      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
396      zc->canonicalize();
397
398      leftv w=v->next;
399      int n = 1;
400      if ((w != NULL) && (w->Typ() == INT_CMD))
401        int n = (int)(long) w;
402
403      if (n != 0)
404      {
405        if (!isCompatible(zf,zc))
406        {
407          WerrorS("insertCone: cone and fan not compatible");
408          return TRUE;
409        }
410      }
411
412      zf->insert(*zc);
413      res->rtyp = NONE;
414      res->data = NULL;
415      IDDATA((idhdl)u->data) = (char*) zf;
416      return FALSE;
417    }
418  }
419  else
420  {
421    WerrorS("insertCone: unexpected parameters");
422    return TRUE;
423  }
424}
425
426bool containsInCollection(gfan::ZFan* zf, gfan::ZCone* zc)
427{
428  gfan::ZVector zv=zc->getRelativeInteriorPoint();
429  for (int d=0; d<=zf->getAmbientDimension(); d++)
430  {
431    for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
432    {
433      gfan::ZCone zd = zf->getCone(d,i,0,1);
434      zd.canonicalize();
435      if (zd.containsRelatively(zv))
436      {
437        gfan::ZCone temp = *zc;
438        temp.canonicalize();
439        return (!(zd != temp));
440      }
441    }
442  }
443  return 0;
444}
445
446BOOLEAN containsInCollection(leftv res, leftv args)
447{
448  leftv u=args;
449  if ((u != NULL) && (u->Typ() == fanID))
450  {
451    leftv v=u->next;
452    if ((v != NULL) && (v->Typ() == coneID))
453    {
454      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
455      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
456      if((zf->getAmbientDimension() == zc->ambientDimension()))
457      {
458        res->rtyp = INT_CMD;
459        res->data = (void*) (long) (int) containsInCollection(zf,zc);
460        return FALSE;
461      }
462      WerrorS("containsInCollection: mismatching ambient dimensions");
463      return TRUE;
464    }
465  }
466  // if ((u != NULL) && (u->Typ() == coneID))
467  // {
468  //   leftv v=u->next;
469  //   if ((v != NULL) && (v->Typ() == coneID))
470  //   {
471  //     gfan::ZCone* zc = (gfan::ZCone*)u->Data();
472  //     gfan::ZCone* zd = (gfan::ZCone*)v->Data();
473  //     res->rtyp = INT_CMD;
474  //     res->data = (void*) (int) hasFace(zc,zd);
475  //     return FALSE;
476  //   }
477  // }
478  WerrorS("containsInCollection: unexpected parameters");
479  return TRUE;
480}
481
482// BOOLEAN coneContaining(leftv res, leftv args)
483// {
484//   leftv u=args;
485//   if ((u != NULL) && (u->Typ() == fanID))
486//   {
487//     if ((v != NULL) && (v->Typ() == BIGINTMAT_CMD))
488//     {
489//       gfan::ZFan* zf = (gfan::ZFan*)u->Data();
490//       bigintmat* vec = (bigintmat*)v->Data();
491//     }
492//   }
493//   WerrorS("coneContaining: unexpected parameters");
494//   return TRUE;
495// }
496
497BOOLEAN removeCone(leftv res, leftv args)
498{
499  leftv u=args;
500  if ((u != NULL) && (u->Typ() == fanID))
501  {
502    leftv v=u->next;
503    if ((v != NULL) && (v->Typ() == coneID))
504    {
505      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
506      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
507      zc->canonicalize();
508
509      leftv w=v->next; int n = 1;
510      if ((w != NULL) && (w->Typ() == INT_CMD))
511        int n = (int)(long) w;
512
513      if (n != 0)
514      {
515        if (!containsInCollection(zf,zc))
516        {
517          WerrorS("removeCone: cone not contained in fan");
518          return TRUE;
519        }
520      }
521
522      zf->remove(*zc);
523      res->rtyp = NONE;
524      res->data = NULL;
525      IDDATA((idhdl)u->data) = (char*) zf;
526      return FALSE;
527    }
528  }
529  WerrorS("removeCone: unexpected parameters");
530  return TRUE;
531}
532
533BOOLEAN getCone(leftv res, leftv args)
534{
535  leftv u=args;
536  if ((u != NULL) && (u->Typ() == fanID))
537  {
538    leftv v=u->next;
539    if ((v != NULL) && (v->Typ() == INT_CMD))
540    {
541      leftv w=v->next;
542      if ((w != NULL) && (w->Typ() == INT_CMD))
543      {
544        gfan::ZFan* zf = (gfan::ZFan*) u->Data();
545        int d = (int)(long)v->Data();
546        int i = (int)(long)w->Data();
547        int o = -1;
548        int m = -1;
549        leftv x=w->next;
550        if ((x != NULL) && (x->Typ() == INT_CMD))
551        {
552          o = (int)(long)x->Data();
553          leftv y=x->next;
554          if ((y != NULL) && (y->Typ() == INT_CMD))
555          {
556            m = (int)(long)y->Data();
557          }
558        }
559        if (o == -1) o = 0;
560        if (m == -1) m = 0;
561        if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
562        {
563          bool oo = (bool) o;
564          bool mm = (bool) m;
565          if (0<=d && d<=zf->getAmbientDimension())
566          {
567            if (0<i && i<=zf->numberOfConesOfDimension(d,oo,mm))
568            {
569              i=i-1;
570              int ld = zf->getLinealityDimension();
571              if (d-ld>=0)
572              {
573                gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
574                res->rtyp = coneID;
575                res->data = (void*)new gfan::ZCone(zc);
576                return FALSE;
577              }
578              else
579              {
580                WerrorS("getCone: invalid dimension; no cones in this dimension");
581                return TRUE;
582              }
583            }
584            else
585            {
586              WerrorS("getCone: invalid index");
587              return TRUE;
588            }
589          }
590          else
591          {
592            WerrorS("getCone: invalid dimension");
593            return TRUE;
594          }
595        }
596        else
597        {
598          WerrorS("getCone: invalid specifier for orbit or maximal");
599          return TRUE;
600        }
601      }
602    }
603  }
604  else
605  {
606    WerrorS("getCone: unexpected parameters");
607    return TRUE;
608  }
609}
610
611BOOLEAN getCones(leftv res, leftv args)
612{
613  leftv u=args;
614  if ((u != NULL) && (u->Typ() == fanID))
615  {
616    leftv v=u->next;
617    if ((v != NULL) && (v->Typ() == INT_CMD))
618    {
619      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
620      int d = (int)(long)v->Data();
621      int o = -1;
622      int m = -1;
623      leftv w=v->next;
624      if ((w != NULL) && (w->Typ() == INT_CMD))
625      {
626        o = (int)(long)w->Data();
627        leftv x=w->next;
628        if ((x != NULL) && (x->Typ() == INT_CMD))
629        {
630          m = (int)(long)x->Data();
631        }
632      }
633      if (o == -1) o = 0;
634      if (m == -1) m = 0;
635      if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
636      {
637        bool oo = (bool) o;
638        bool mm = (bool) m;
639        if (0<=d && d<=zf->getAmbientDimension())
640        {
641          int ld = zf->getLinealityDimension();
642          if (d-ld>=0)
643          {
644            lists L = (lists)omAllocBin(slists_bin);
645            int n = zf->numberOfConesOfDimension(d,oo,mm);
646            L->Init(n);
647            for (int i=0; i<n; i++)
648            {
649              gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
650              L->m[i].rtyp = coneID; L->m[i].data=(void*) new gfan::ZCone(zc);
651            }
652            res->rtyp = LIST_CMD;
653            res->data = (void*) L;
654            return FALSE;
655          }
656          else
657          {
658            WerrorS("getCones: invalid dimension; no cones in this dimension");
659            return TRUE;
660          }
661        }
662        else
663        {
664          WerrorS("getCones: invalid dimension");
665          return TRUE;
666        }
667      }
668      else
669      {
670        WerrorS("getCones: invalid specifier for orbit or maximal");
671        return TRUE;
672      }
673    }
674  }
675  else
676  {
677    WerrorS("getCones: unexpected parameters");
678    return TRUE;
679  }
680}
681
682int isSimplicial(gfan::ZFan* zf)
683{
684  int i = zf->isSimplicial() ? 1 : 0;
685  return i;
686}
687
688BOOLEAN isPure(leftv res, leftv args)
689{
690  leftv u=args;
691  if ((u != NULL) && (u->Typ() == fanID))
692  {
693    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
694    int b = zf->isPure();
695    res->rtyp = INT_CMD;
696    res->data = (void*) (long) b;
697    return FALSE;
698  }
699  WerrorS("isPure: unexpected parameters");
700  return TRUE;
701}
702
703BOOLEAN isComplete(leftv res, leftv args)
704{
705  leftv u=args;
706  if ((u != NULL) && (u->Typ() == fanID))
707  {
708    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
709    int b = zf->isComplete();
710    res->rtyp = INT_CMD;
711    res->data = (void*) (long) b;
712    return FALSE;
713  }
714  WerrorS("isComplete: unexpected parameters");
715  return TRUE;
716}
717
718BOOLEAN fVector(leftv res, leftv args)
719{
720  leftv u=args;
721  if ((u != NULL) && (u->Typ() == fanID))
722  {
723    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
724    gfan::ZVector zv=zf->getFVector();
725    res->rtyp = BIGINTMAT_CMD;
726    res->data = (void*) zVectorToBigintmat(zv);
727    return FALSE;
728  }
729  WerrorS("fVector: unexpected parameters");
730  return TRUE;
731}
732
733gfan::ZMatrix rays(gfan::ZFan* zf)
734{
735  int linDim = zf->getLinealityDimension();
736  gfan::ZMatrix rays(0,zf->getAmbientDimension());
737  for (int i=0; i<zf->numberOfConesOfDimension(linDim+1,0,0); i++)
738  {
739    gfan::ZCone zc = zf->getCone(linDim+1, i, 0, 0);
740    rays.append(zc.extremeRays());
741  }
742  return rays;
743}
744
745int numberOfConesWithVector(gfan::ZFan* zf, gfan::ZVector* v)
746{
747  int count = 0;
748  int ambientDim = zf->getAmbientDimension();
749  for (int i=0; i<zf->numberOfConesOfDimension(ambientDim, 0, 0); i++)
750  {
751    gfan::ZCone zc = zf->getCone(ambientDim, i, 0, 0);
752    if (zc.contains(*v))
753    {
754      count = count +1;
755      if (count > 1)
756        return count;
757    }
758  }
759  return count;
760}
761
762BOOLEAN numberOfConesWithVector(leftv res, leftv args)
763{
764  leftv u=args;
765  if ((u != NULL) && (u->Typ() == fanID))
766  {
767    leftv v=u->next;
768    if ((v != NULL) && (v->Typ() == BIGINTMAT_CMD))
769    {
770      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
771      bigintmat* v0 = (bigintmat*) v->Data();
772      int ambientDim = zf->getAmbientDimension();
773      if (ambientDim != v0->cols())
774      {
775        WerrorS("numberOfConesWithVector: mismatching dimensions");
776        return TRUE;
777      }
778      gfan::ZVector* v1 = bigintmatToZVector(*v0);
779      int count = numberOfConesWithVector(zf, v1);
780      delete v1;
781      res->rtyp = INT_CMD;
782      res->data = (void*) (long) count;
783      return FALSE;
784    }
785  }
786  WerrorS("numberOfConesWithVector: unexpected parameters");
787  return TRUE;
788}
789
790BOOLEAN fanFromString(leftv res, leftv args)
791{
792  leftv u=args;
793  if ((u != NULL) && (u->Typ() == STRING_CMD))
794    {
795      std::string fanInString = (char*) u->Data();
796      std::istringstream s(fanInString);
797      gfan::ZFan* zf = new gfan::ZFan(s);
798      res->rtyp = fanID;
799      res->data = (void*) zf;
800      return FALSE;
801    }
802  WerrorS("fanFromString: unexpected parameters");
803  return TRUE;
804}
805
806BOOLEAN fanViaCones(leftv res, leftv args)
807{
808  leftv u=args;
809  if ((u != NULL) && (u->Typ() == LIST_CMD))
810  {
811    lists L = (lists) u->Data();
812    if (lSize(L)>-1)
813    {
814      if (L->m[0].Typ() != coneID)
815      {
816        WerrorS("fanViaCones: list contains entries of wrong type");
817        return TRUE;
818      }
819      gfan::ZCone* zc = (gfan::ZCone*) L->m[0].Data();
820      gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
821      zf->insert(*zc);
822      for (int i=1; i<=lSize(L); i++)
823      {
824        if (L->m[i].Typ() != coneID)
825        {
826          WerrorS("fanViaCones: entries of wrong type in list");
827          return TRUE;
828        }
829        gfan::ZCone* zc = (gfan::ZCone*) L->m[i].Data();
830        if (zc->ambientDimension() != zf->getAmbientDimension())
831        {
832          WerrorS("fanViaCones: inconsistent ambient dimensions amongst cones in list");
833          return TRUE;
834        }
835        zf->insert(*zc);
836      }
837      res->rtyp = fanID;
838      res->data = (void*) zf;
839      return FALSE;
840    }
841    res->rtyp = fanID;
842    res->data = (void*) new gfan::ZFan(0);
843    return FALSE;
844  }
845  if ((u != NULL) && (u->Typ() == coneID))
846  {
847    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
848    gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
849    zf->insert(*zc);
850    while (u->next != NULL)
851    {
852      u = u->next;
853      if (u->Typ() != coneID)
854      {
855        WerrorS("fanViaCones: arguments of wrong type");
856        return TRUE;
857      }
858      gfan::ZCone* zc = (gfan::ZCone*) u->Data();
859      if (zc->ambientDimension() != zf->getAmbientDimension())
860      {
861        WerrorS("fanViaCones: inconsistent ambient dimensions amongst input cones");
862        return TRUE;
863      }
864      zf->insert(*zc);
865    }
866    res->rtyp = fanID;
867    res->data = (void*) zf;
868    return FALSE;
869  }
870  if (u == NULL)
871  {
872    res->rtyp = fanID;
873    res->data = (void*) new gfan::ZFan(0);
874    return FALSE;
875  }
876  WerrorS("fanViaCones: unexpected parameters");
877  return TRUE;
878}
879
880lists listOfFacets(const gfan::ZCone &zc)
881{
882  gfan::ZMatrix inequalities = zc.getFacets();
883  gfan::ZMatrix equations = zc.getImpliedEquations();
884  lists L = (lists)omAllocBin(slists_bin);
885  int r = inequalities.getHeight();
886  int c = inequalities.getWidth();
887  L->Init(r);
888
889  /* next we iterate over each of the r facets, build the respective cone and add it to the list */
890  /* this is the i=0 case */
891  gfan::ZMatrix newInequalities = inequalities.submatrix(1,0,r,c);
892  gfan::ZMatrix newEquations = equations;
893  newEquations.appendRow(inequalities[0]);
894  L->m[0].rtyp = coneID; L->m[0].data=(void*) new gfan::ZCone(newInequalities,newEquations);
895
896  /* these are the cases i=1,...,r-2 */
897  for (int i=1; i<r-1; i++)
898  {
899    newInequalities = inequalities.submatrix(0,0,i-1,c);
900    newInequalities.append(inequalities.submatrix(i+1,0,r,c));
901    newEquations = equations;
902    newEquations.appendRow(inequalities[i]);
903    L->m[i].rtyp = coneID; L->m[i].data=(void*) new gfan::ZCone(newInequalities,newEquations);
904  }
905
906  /* this is the i=r-1 case */
907  newInequalities = inequalities.submatrix(0,0,r-1,c);
908  newEquations = equations;
909  newEquations.appendRow(inequalities[r]);
910  L->m[r-1].rtyp = coneID; L->m[r-1].data=(void*) new gfan::ZCone(newInequalities,newEquations);
911
912  return L;
913}
914
915BOOLEAN listOfFacets(leftv res, leftv args)
916{
917  leftv u=args;
918  if ((u != NULL) && (u->Typ() == coneID))
919  {
920    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
921    lists L = listOfFacets(*zc);
922    res->rtyp = LIST_CMD;
923    res->data = (void*) L;
924    return FALSE;
925  }
926  WerrorS("listOfFacets: unexpected parameters");
927  return TRUE;
928}
929
930// BOOLEAN grFan(leftv res, leftv h)
931// {
932//   /*======== GFAN ==============*/
933//   /*
934//    WILL HAVE TO CHANGE RETURN TYPE TO LIST_CMD
935//   */
936//   /*
937//     heuristic:
938//     0 = keep all Groebner bases in memory
939//     1 = write all Groebner bases to disk and read whenever necessary
940//     2 = use a mixed heuristic, based on length of Groebner bases
941//   */
942//   if( h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
943//   {
944//     int heuristic;
945//     heuristic=(int)(long)h->next->Data();
946//     ideal I=((ideal)h->Data());
947//     #ifndef USE_ZFAN
948//         #define USE_ZFAN
949//     #endif
950//     #ifndef USE_ZFAN
951//     res->rtyp=LIST_CMD; //res->rtyp=coneID; res->data(void*)zcone;
952//     res->data=(lists) grfan(I,heuristic,FALSE);
953//     #else
954//     res->rtyp=fanID;
955//     res->data=(void*)(grfan(I,heuristic,FALSE));
956//     #endif
957//     return FALSE;
958//   }
959//   else
960//   {
961//     WerrorS("Usage: grfan(<ideal>,<int>)");
962//     return TRUE;
963//   }
964// }
965  //Possibility to have only one Groebner cone computed by specifying a weight vector FROM THE RELATIVE INTERIOR!
966  //Needs wp as ordering!
967//   if(strcmp(sys_cmd,"grcone")==0)
968//   {
969//     if(h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
970//     {
971//       ideal I=((ideal)h->Data());
972//       res->rtyp=LIST_CMD;
973//       res->data=(lists)grcone_by_intvec(I);
974//     }
975//   }
976
977
978void bbfan_setup()
979{
980  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
981  // all undefined entries will be set to default in setBlackboxStuff
982  // the default Print is quite usefule,
983  // all other are simply error messages
984  b->blackbox_destroy=bbfan_destroy;
985  b->blackbox_String=bbfan_String;
986  //b->blackbox_Print=blackbox_default_Print;
987  b->blackbox_Init=bbfan_Init;
988  b->blackbox_Copy=bbfan_Copy;
989  b->blackbox_Assign=bbfan_Assign;
990  iiAddCproc("","emptyFan",FALSE,emptyFan);
991  iiAddCproc("","fullFan",FALSE,fullFan);
992  /* the following functions are implemented in bbcone.cc */
993  // iiAddCproc("","containsInSupport",FALSE,containsInSupport);
994  // iiAddCproc("","getAmbientDimension",FALSE,getAmbientDimension);
995  // iiAddCproc("","getCodimension",FALSE,getDimension);
996  // iiAddCproc("","getDimension",FALSE,getDimension);
997  // iiAddCproc("","getLinealityDimension",FALSE,getLinealityDimension);
998  // iiAddCproc("","isSimplicial",FALSE,isSimplicial);
999  /********************************************************/
1000  iiAddCproc("","isCompatible",FALSE,isCompatible);
1001  iiAddCproc("","numberOfConesOfDimension",FALSE,numberOfConesOfDimension);
1002  iiAddCproc("","ncones",FALSE,ncones);
1003  iiAddCproc("","nmaxcones",FALSE,nmaxcones);
1004  iiAddCproc("","insertCone",FALSE,insertCone);
1005  iiAddCproc("","removeCone",FALSE,removeCone);
1006  iiAddCproc("","getCone",FALSE,getCone);
1007  iiAddCproc("","getCones",FALSE,getCones);
1008  iiAddCproc("","isPure",FALSE,isPure);
1009  iiAddCproc("","fanFromString",FALSE,fanFromString);
1010  iiAddCproc("","fanViaCones",FALSE,fanViaCones);
1011  iiAddCproc("","numberOfConesWithVector",FALSE,numberOfConesWithVector);
1012  // iiAddCproc("","isComplete",FALSE,isComplete);  not working as expected, should leave this to polymake
1013  iiAddCproc("","fVector",FALSE,fVector);
1014  iiAddCproc("","containsInCollection",FALSE,containsInCollection);
1015  // iiAddCproc("","grFan",FALSE,grFan);
1016  fanID=setBlackboxStuff(b,"fan");
1017  //Print("created type %d (fan)\n",fanID);
1018}
1019
1020#endif
1021/* HAVE_FANS */
Note: See TracBrowser for help on using the repository browser.