source: git/dyn_modules/callgfanlib/bbfan.cc @ 58b407

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