source: git/dyn_modules/callgfanlib/bbfan.cc @ f80a530

spielwiese
Last change on this file since f80a530 was f80a530, checked in by Yue Ren <ren@…>, 11 years ago
chg: print routines for polyhedral fans corresponds to 7add05a9a5fb7b1e61023a1b6873e366d004d453 in master
  • Property mode set to 100644
File size: 28.6 KB
RevLine 
[81384b]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;
[f80a530]47    std::string s = zf->toStringJustRaysAndMaximalCones();
[81384b]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            {
[e44b149]281              int n = zf->numberOfConesOfDimension(d-ld,oo,mm);
[81384b]282              res->rtyp = INT_CMD;
283              res->data = (void*) (long) n;
284              return FALSE;
285            }
[e44b149]286            res->rtyp = INT_CMD;
287            res->data = (void*) (long) 0;
288            return FALSE;
[81384b]289          }
290        }
291      }
292    }
293  }
294  WerrorS("numberOfConesOfDimension: unexpected parameters");
295  return TRUE;
296}
297
298BOOLEAN ncones(leftv res, leftv args)
299{
300  leftv u=args;
301  if ((u != NULL) && (u->Typ() == fanID))
302    {
303      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
304      int d = zf->getAmbientDimension();
305      int n = 0;
306
307      for (int i=0; i<=d; i++)
[e44b149]308        n = n + zf->numberOfConesOfDimension(i,0,0);
[81384b]309
310      res->rtyp = INT_CMD;
311      res->data = (void*) (long) n;
312      return FALSE;
313    }
314  else
315    {
316      WerrorS("check_compatibility: unexpected parameters");
317      return TRUE;
318    }
319}
320
321BOOLEAN nmaxcones(leftv res, leftv args)
322{
323  leftv u=args;
324  if ((u != NULL) && (u->Typ() == fanID))
325    {
326      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
327
328      int n = 0;
329      for (int d=0; d<=zf->getAmbientDimension(); d++)
330        { n = n + zf->numberOfConesOfDimension(d,0,1); }
331
332      res->rtyp = INT_CMD;
333      res->data = (void*) (long) n;
334      return FALSE;
335    }
336  else
337    {
338      WerrorS("nmaxcones: unexpected parameters");
339      return TRUE;
340    }
341}
342
343bool isCompatible(gfan::ZFan* zf, gfan::ZCone* zc)
344{
345  bool b = (zf->getAmbientDimension() == zc->ambientDimension());
346  if(b)
347  {
348    for (int d=0; d<=zf->getAmbientDimension(); d++)
349    {
350      for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
351      {
352        gfan::ZCone zd = zf->getCone(d,i,0,1);
353        gfan::ZCone zt = gfan::intersection(*zc,zd);
354        zt.canonicalize();
355        b = b && zd.hasFace(zt);
356      }
357    }
358  }
359  return b;
360}
361
362BOOLEAN isCompatible(leftv res, leftv args)
363{
364  leftv u=args;
365  if ((u != NULL) && (u->Typ() == fanID))
366  {
367    leftv v=u->next;
368    if ((v != NULL) && (v->Typ() == coneID))
369    {
370      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
371      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
372      int b = isCompatible(zf,zc);
373      res->rtyp = INT_CMD;
374      res->data = (void*) (long) b;
375      return FALSE;
376    }
377  }
378  WerrorS("isCompatible: unexpected parameters");
379  return TRUE;
380}
381
382BOOLEAN insertCone(leftv res, leftv args)
383{
384  leftv u=args;
385  if ((u != NULL) && (u->rtyp==IDHDL) && (u->e==NULL) && (u->Typ() == fanID))
386  {
387    leftv v=u->next;
388    if ((v != NULL) && (v->Typ() == coneID))
389    {
390      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
391      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
392      zc->canonicalize();
393
394      leftv w=v->next;
[e44b149]395      int n;
[81384b]396      if ((w != NULL) && (w->Typ() == INT_CMD))
[e44b149]397        n = (int)(long) w;
[81384b]398
399      if (n != 0)
400      {
401        if (!isCompatible(zf,zc))
402        {
403          WerrorS("insertCone: cone and fan not compatible");
404          return TRUE;
405        }
406      }
407
408      zf->insert(*zc);
409      res->rtyp = NONE;
410      res->data = NULL;
411      IDDATA((idhdl)u->data) = (char*) zf;
412      return FALSE;
413    }
414  }
[e44b149]415  WerrorS("insertCone: unexpected parameters");
416  return TRUE;
[81384b]417}
418
419bool containsInCollection(gfan::ZFan* zf, gfan::ZCone* zc)
420{
421  gfan::ZVector zv=zc->getRelativeInteriorPoint();
422  for (int d=0; d<=zf->getAmbientDimension(); d++)
423  {
424    for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
425    {
426      gfan::ZCone zd = zf->getCone(d,i,0,1);
427      zd.canonicalize();
428      if (zd.containsRelatively(zv))
429      {
430        gfan::ZCone temp = *zc;
431        temp.canonicalize();
432        return (!(zd != temp));
433      }
434    }
435  }
436  return 0;
437}
438
439BOOLEAN containsInCollection(leftv res, leftv args)
440{
441  leftv u=args;
442  if ((u != NULL) && (u->Typ() == fanID))
443  {
444    leftv v=u->next;
445    if ((v != NULL) && (v->Typ() == coneID))
446    {
447      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
448      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
449      if((zf->getAmbientDimension() == zc->ambientDimension()))
450      {
451        res->rtyp = INT_CMD;
452        res->data = (void*) (long) (int) containsInCollection(zf,zc);
453        return FALSE;
454      }
455      WerrorS("containsInCollection: mismatching ambient dimensions");
456      return TRUE;
457    }
458  }
459  // if ((u != NULL) && (u->Typ() == coneID))
460  // {
461  //   leftv v=u->next;
462  //   if ((v != NULL) && (v->Typ() == coneID))
463  //   {
464  //     gfan::ZCone* zc = (gfan::ZCone*)u->Data();
465  //     gfan::ZCone* zd = (gfan::ZCone*)v->Data();
466  //     res->rtyp = INT_CMD;
467  //     res->data = (void*) (int) hasFace(zc,zd);
468  //     return FALSE;
469  //   }
470  // }
471  WerrorS("containsInCollection: unexpected parameters");
472  return TRUE;
473}
474
475// BOOLEAN coneContaining(leftv res, leftv args)
476// {
477//   leftv u=args;
478//   if ((u != NULL) && (u->Typ() == fanID))
479//   {
480//     if ((v != NULL) && (v->Typ() == BIGINTMAT_CMD))
481//     {
482//       gfan::ZFan* zf = (gfan::ZFan*)u->Data();
483//       bigintmat* vec = (bigintmat*)v->Data();
484//     }
485//   }
486//   WerrorS("coneContaining: unexpected parameters");
487//   return TRUE;
488// }
489
490BOOLEAN removeCone(leftv res, leftv args)
491{
492  leftv u=args;
493  if ((u != NULL) && (u->Typ() == fanID))
494  {
495    leftv v=u->next;
496    if ((v != NULL) && (v->Typ() == coneID))
497    {
498      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
499      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
500      zc->canonicalize();
501
502      leftv w=v->next; int n = 1;
503      if ((w != NULL) && (w->Typ() == INT_CMD))
504        int n = (int)(long) w;
505
506      if (n != 0)
507      {
508        if (!containsInCollection(zf,zc))
509        {
510          WerrorS("removeCone: cone not contained in fan");
511          return TRUE;
512        }
513      }
514
515      zf->remove(*zc);
516      res->rtyp = NONE;
517      res->data = NULL;
518      IDDATA((idhdl)u->data) = (char*) zf;
519      return FALSE;
520    }
521  }
522  WerrorS("removeCone: unexpected parameters");
523  return TRUE;
524}
525
526BOOLEAN getCone(leftv res, leftv args)
527{
528  leftv u=args;
529  if ((u != NULL) && (u->Typ() == fanID))
530  {
531    leftv v=u->next;
532    if ((v != NULL) && (v->Typ() == INT_CMD))
533    {
534      leftv w=v->next;
535      if ((w != NULL) && (w->Typ() == INT_CMD))
536      {
537        gfan::ZFan* zf = (gfan::ZFan*) u->Data();
538        int d = (int)(long)v->Data();
539        int i = (int)(long)w->Data();
540        int o = -1;
541        int m = -1;
542        leftv x=w->next;
543        if ((x != NULL) && (x->Typ() == INT_CMD))
544        {
545          o = (int)(long)x->Data();
546          leftv y=x->next;
547          if ((y != NULL) && (y->Typ() == INT_CMD))
548          {
549            m = (int)(long)y->Data();
550          }
551        }
552        if (o == -1) o = 0;
553        if (m == -1) m = 0;
554        if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
555        {
556          bool oo = (bool) o;
557          bool mm = (bool) m;
558          if (0<=d && d<=zf->getAmbientDimension())
559          {
560            if (0<i && i<=zf->numberOfConesOfDimension(d,oo,mm))
561            {
562              i=i-1;
563              int ld = zf->getLinealityDimension();
564              if (d-ld>=0)
565              {
566                gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
567                res->rtyp = coneID;
568                res->data = (void*)new gfan::ZCone(zc);
569                return FALSE;
570              }
571              else
572              {
573                WerrorS("getCone: invalid dimension; no cones in this dimension");
574                return TRUE;
575              }
576            }
577            else
578            {
579              WerrorS("getCone: invalid index");
580              return TRUE;
581            }
582          }
583          else
584          {
585            WerrorS("getCone: invalid dimension");
586            return TRUE;
587          }
588        }
589        else
590        {
591          WerrorS("getCone: invalid specifier for orbit or maximal");
592          return TRUE;
593        }
594      }
595    }
596  }
[e44b149]597  WerrorS("getCone: unexpected parameters");
598  return TRUE;
[81384b]599}
600
601BOOLEAN getCones(leftv res, leftv args)
602{
603  leftv u=args;
604  if ((u != NULL) && (u->Typ() == fanID))
605  {
606    leftv v=u->next;
607    if ((v != NULL) && (v->Typ() == INT_CMD))
608    {
609      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
610      int d = (int)(long)v->Data();
611      int o = -1;
612      int m = -1;
613      leftv w=v->next;
614      if ((w != NULL) && (w->Typ() == INT_CMD))
615      {
616        o = (int)(long)w->Data();
617        leftv x=w->next;
618        if ((x != NULL) && (x->Typ() == INT_CMD))
619        {
620          m = (int)(long)x->Data();
621        }
622      }
623      if (o == -1) o = 0;
624      if (m == -1) m = 0;
625      if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
626      {
627        bool oo = (bool) o;
628        bool mm = (bool) m;
629        if (0<=d && d<=zf->getAmbientDimension())
630        {
631          int ld = zf->getLinealityDimension();
632          if (d-ld>=0)
633          {
634            lists L = (lists)omAllocBin(slists_bin);
[e44b149]635            int n = zf->numberOfConesOfDimension(d-ld,oo,mm);
[81384b]636            L->Init(n);
637            for (int i=0; i<n; i++)
638            {
639              gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
640              L->m[i].rtyp = coneID; L->m[i].data=(void*) new gfan::ZCone(zc);
641            }
642            res->rtyp = LIST_CMD;
643            res->data = (void*) L;
644            return FALSE;
645          }
646          else
647          {
648            WerrorS("getCones: invalid dimension; no cones in this dimension");
649            return TRUE;
650          }
651        }
652        else
653        {
654          WerrorS("getCones: invalid dimension");
655          return TRUE;
656        }
657      }
658      else
659      {
660        WerrorS("getCones: invalid specifier for orbit or maximal");
661        return TRUE;
662      }
663    }
664  }
[e44b149]665  WerrorS("getCones: unexpected parameters");
666  return TRUE;
[81384b]667}
668
669int isSimplicial(gfan::ZFan* zf)
670{
671  int i = zf->isSimplicial() ? 1 : 0;
672  return i;
673}
674
675BOOLEAN isPure(leftv res, leftv args)
676{
677  leftv u=args;
678  if ((u != NULL) && (u->Typ() == fanID))
679  {
680    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
681    int b = zf->isPure();
682    res->rtyp = INT_CMD;
683    res->data = (void*) (long) b;
684    return FALSE;
685  }
686  WerrorS("isPure: unexpected parameters");
687  return TRUE;
688}
689
690BOOLEAN isComplete(leftv res, leftv args)
691{
692  leftv u=args;
693  if ((u != NULL) && (u->Typ() == fanID))
694  {
695    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
696    int b = zf->isComplete();
697    res->rtyp = INT_CMD;
698    res->data = (void*) (long) b;
699    return FALSE;
700  }
701  WerrorS("isComplete: unexpected parameters");
702  return TRUE;
703}
704
705BOOLEAN fVector(leftv res, leftv args)
706{
707  leftv u=args;
708  if ((u != NULL) && (u->Typ() == fanID))
709  {
710    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
711    gfan::ZVector zv=zf->getFVector();
712    res->rtyp = BIGINTMAT_CMD;
713    res->data = (void*) zVectorToBigintmat(zv);
714    return FALSE;
715  }
716  WerrorS("fVector: unexpected parameters");
717  return TRUE;
718}
719
[f80a530]720gfan::ZMatrix rays(const gfan::ZFan* const zf)
[81384b]721{
722  gfan::ZMatrix rays(0,zf->getAmbientDimension());
[e44b149]723  for (int i=0; i<zf->numberOfConesOfDimension(1,0,0); i++)
[81384b]724  {
[e44b149]725    gfan::ZCone zc = zf->getCone(1, i, 0, 0);
[81384b]726    rays.append(zc.extremeRays());
727  }
728  return rays;
729}
730
731int numberOfConesWithVector(gfan::ZFan* zf, gfan::ZVector* v)
732{
733  int count = 0;
734  int ambientDim = zf->getAmbientDimension();
735  for (int i=0; i<zf->numberOfConesOfDimension(ambientDim, 0, 0); i++)
736  {
737    gfan::ZCone zc = zf->getCone(ambientDim, i, 0, 0);
738    if (zc.contains(*v))
739    {
740      count = count +1;
741      if (count > 1)
742        return count;
743    }
744  }
745  return count;
746}
747
748BOOLEAN numberOfConesWithVector(leftv res, leftv args)
749{
750  leftv u=args;
751  if ((u != NULL) && (u->Typ() == fanID))
752  {
753    leftv v=u->next;
754    if ((v != NULL) && (v->Typ() == BIGINTMAT_CMD))
755    {
756      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
757      bigintmat* v0 = (bigintmat*) v->Data();
758      int ambientDim = zf->getAmbientDimension();
759      if (ambientDim != v0->cols())
760      {
761        WerrorS("numberOfConesWithVector: mismatching dimensions");
762        return TRUE;
763      }
764      gfan::ZVector* v1 = bigintmatToZVector(*v0);
765      int count = numberOfConesWithVector(zf, v1);
766      delete v1;
767      res->rtyp = INT_CMD;
768      res->data = (void*) (long) count;
769      return FALSE;
770    }
771  }
772  WerrorS("numberOfConesWithVector: unexpected parameters");
773  return TRUE;
774}
775
776BOOLEAN fanFromString(leftv res, leftv args)
777{
778  leftv u=args;
779  if ((u != NULL) && (u->Typ() == STRING_CMD))
780    {
781      std::string fanInString = (char*) u->Data();
782      std::istringstream s(fanInString);
783      gfan::ZFan* zf = new gfan::ZFan(s);
784      res->rtyp = fanID;
785      res->data = (void*) zf;
786      return FALSE;
787    }
788  WerrorS("fanFromString: unexpected parameters");
789  return TRUE;
790}
791
792BOOLEAN fanViaCones(leftv res, leftv args)
793{
794  leftv u=args;
795  if ((u != NULL) && (u->Typ() == LIST_CMD))
796  {
797    lists L = (lists) u->Data();
798    if (lSize(L)>-1)
799    {
800      if (L->m[0].Typ() != coneID)
801      {
802        WerrorS("fanViaCones: list contains entries of wrong type");
803        return TRUE;
804      }
805      gfan::ZCone* zc = (gfan::ZCone*) L->m[0].Data();
806      gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
807      zf->insert(*zc);
808      for (int i=1; i<=lSize(L); i++)
809      {
810        if (L->m[i].Typ() != coneID)
811        {
812          WerrorS("fanViaCones: entries of wrong type in list");
813          return TRUE;
814        }
815        gfan::ZCone* zc = (gfan::ZCone*) L->m[i].Data();
816        if (zc->ambientDimension() != zf->getAmbientDimension())
817        {
818          WerrorS("fanViaCones: inconsistent ambient dimensions amongst cones in list");
819          return TRUE;
820        }
821        zf->insert(*zc);
822      }
823      res->rtyp = fanID;
824      res->data = (void*) zf;
825      return FALSE;
826    }
827    res->rtyp = fanID;
828    res->data = (void*) new gfan::ZFan(0);
829    return FALSE;
830  }
831  if ((u != NULL) && (u->Typ() == coneID))
832  {
833    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
834    gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
835    zf->insert(*zc);
836    while (u->next != NULL)
837    {
838      u = u->next;
839      if (u->Typ() != coneID)
840      {
841        WerrorS("fanViaCones: arguments of wrong type");
842        return TRUE;
843      }
844      gfan::ZCone* zc = (gfan::ZCone*) u->Data();
845      if (zc->ambientDimension() != zf->getAmbientDimension())
846      {
847        WerrorS("fanViaCones: inconsistent ambient dimensions amongst input cones");
848        return TRUE;
849      }
850      zf->insert(*zc);
851    }
852    res->rtyp = fanID;
853    res->data = (void*) zf;
854    return FALSE;
855  }
856  if (u == NULL)
857  {
858    res->rtyp = fanID;
859    res->data = (void*) new gfan::ZFan(0);
860    return FALSE;
861  }
862  WerrorS("fanViaCones: unexpected parameters");
863  return TRUE;
864}
865
866lists listOfFacets(const gfan::ZCone &zc)
867{
868  gfan::ZMatrix inequalities = zc.getFacets();
869  gfan::ZMatrix equations = zc.getImpliedEquations();
870  lists L = (lists)omAllocBin(slists_bin);
871  int r = inequalities.getHeight();
872  int c = inequalities.getWidth();
873  L->Init(r);
874
875  /* next we iterate over each of the r facets, build the respective cone and add it to the list */
876  /* this is the i=0 case */
877  gfan::ZMatrix newInequalities = inequalities.submatrix(1,0,r,c);
878  gfan::ZMatrix newEquations = equations;
879  newEquations.appendRow(inequalities[0]);
880  L->m[0].rtyp = coneID; L->m[0].data=(void*) new gfan::ZCone(newInequalities,newEquations);
881
882  /* these are the cases i=1,...,r-2 */
883  for (int i=1; i<r-1; i++)
884  {
885    newInequalities = inequalities.submatrix(0,0,i-1,c);
886    newInequalities.append(inequalities.submatrix(i+1,0,r,c));
887    newEquations = equations;
888    newEquations.appendRow(inequalities[i]);
889    L->m[i].rtyp = coneID; L->m[i].data=(void*) new gfan::ZCone(newInequalities,newEquations);
890  }
891
892  /* this is the i=r-1 case */
893  newInequalities = inequalities.submatrix(0,0,r-1,c);
894  newEquations = equations;
895  newEquations.appendRow(inequalities[r]);
896  L->m[r-1].rtyp = coneID; L->m[r-1].data=(void*) new gfan::ZCone(newInequalities,newEquations);
897
898  return L;
899}
900
901BOOLEAN listOfFacets(leftv res, leftv args)
902{
903  leftv u=args;
904  if ((u != NULL) && (u->Typ() == coneID))
905  {
906    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
907    lists L = listOfFacets(*zc);
908    res->rtyp = LIST_CMD;
909    res->data = (void*) L;
910    return FALSE;
911  }
912  WerrorS("listOfFacets: unexpected parameters");
913  return TRUE;
914}
915
[e44b149]916BOOLEAN tropicalVariety(leftv res, leftv args)
917{
918  leftv u=args;
919  if ((u != NULL) && (u->Typ() == POLY_CMD))
920  {
921    int n = rVar(currRing);
922    gfan::ZFan* zf = new gfan::ZFan(n);
923    int* expv1 = (int*)omAlloc((n+1)*sizeof(int));
924    int* expv2 = (int*)omAlloc((n+1)*sizeof(int));
925    int* expvr = (int*)omAlloc((n+1)*sizeof(int));
926    gfan::ZVector expw1 = gfan::ZVector(n);
927    gfan::ZVector expw2 = gfan::ZVector(n);
928    gfan::ZVector expwr = gfan::ZVector(n);
929    gfan::ZMatrix eq, ineq;
930    for (poly s1=(poly)u->Data(); s1!=NULL; pIter(s1))
931    {
932      pGetExpV(s1,expv1);
933      expw1 = intStar2ZVector(n,expv1);
934      for (poly s2=pNext(s1); s2!=NULL; pIter(s2))
935      {
936        pGetExpV(s2,expv2);
937        expw2 = intStar2ZVector(n,expv2);
938        eq = gfan::ZMatrix(0,n);
939        eq.appendRow(expw1-expw2);
940        ineq = gfan::ZMatrix(0,n);
941        for (poly r=(poly)u->Data(); r!=NULL; pIter(r))
942        {
943          pGetExpV(r,expvr);
944          expwr = intStar2ZVector(n,expvr);
945          if ((r!=s1) && (r!=s2))
946          {
947            ineq.appendRow(expw1-expwr);
948          }
949        }
950        gfan::ZCone zc = gfan::ZCone(ineq,eq);
951        zf->insert(zc);
952      }
953    }
954    omFreeSize(expv1,(n+1)*sizeof(int));
955    omFreeSize(expv2,(n+1)*sizeof(int));
956    omFreeSize(expvr,(n+1)*sizeof(int));
957    res->rtyp = fanID;
958    res->data = (void*) zf;
959    return FALSE;
960  }
961  WerrorS("tropicalVariety: unexpected parameters");
962  return TRUE;
963}
964
965gfan::ZFan* commonRefinement(gfan::ZFan* zf, gfan::ZFan* zg)
966{
967  assume(zf->getAmbientDimension() == zg->getAmbientDimension());
968
969  // gather all maximal cones of f and g
970  std::list<gfan::ZCone> maximalConesOfF;
971  for (int d=0; d<=zf->getAmbientDimension(); d++)
972  {
973    for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
974    {
975      maximalConesOfF.push_back(zf->getCone(d,i,0,1));
976    }
977  }
978
979  std::list<gfan::ZCone> maximalConesOfG;
980  for (int d=0; d<=zg->getAmbientDimension(); d++)
981  {
982    for (int i=0; i<zg->numberOfConesOfDimension(d,0,1); i++)
983    {
984      maximalConesOfG.push_back(zg->getCone(d,i,0,1));
985    }
986  }
987
988  // construct a new fan out of their intersections
989  gfan::ZFan* zr = new gfan::ZFan(zf->getAmbientDimension());
990  for (std::list<gfan::ZCone>::iterator itf=maximalConesOfF.begin();
991       itf != maximalConesOfF.end(); itf++)
992  {
993    for (std::list<gfan::ZCone>::iterator itg=maximalConesOfG.begin();
994         itg != maximalConesOfG.end(); itg++)
995    {
996      zr->insert(intersection(*itf,*itg));
997    }
998  }
999
1000  return zr;
1001}
1002
1003BOOLEAN commonRefinement(leftv res, leftv args)
1004{
1005  leftv u=args;
1006  if ((u != NULL) && (u->Typ() == fanID))
1007  {
1008    leftv v=u->next;
1009    if ((v != NULL) && (v->Typ() == fanID))
1010    {
1011      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
1012      gfan::ZFan* zg = (gfan::ZFan*) v->Data();
1013      gfan::ZFan* zr = commonRefinement(zf,zg);
1014      res->rtyp = fanID;
1015      res->data = (void*) zr;
1016      return FALSE;
1017    }
1018  }
1019  WerrorS("commonRefinement: unexpected parameters");
1020  return TRUE;
1021}
1022
[81384b]1023// BOOLEAN grFan(leftv res, leftv h)
1024// {
1025//   /*======== GFAN ==============*/
1026//   /*
1027//    WILL HAVE TO CHANGE RETURN TYPE TO LIST_CMD
1028//   */
1029//   /*
1030//     heuristic:
1031//     0 = keep all Groebner bases in memory
1032//     1 = write all Groebner bases to disk and read whenever necessary
1033//     2 = use a mixed heuristic, based on length of Groebner bases
1034//   */
1035//   if( h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
1036//   {
1037//     int heuristic;
1038//     heuristic=(int)(long)h->next->Data();
1039//     ideal I=((ideal)h->Data());
1040//     #ifndef USE_ZFAN
1041//         #define USE_ZFAN
1042//     #endif
1043//     #ifndef USE_ZFAN
1044//     res->rtyp=LIST_CMD; //res->rtyp=coneID; res->data(void*)zcone;
1045//     res->data=(lists) grfan(I,heuristic,FALSE);
1046//     #else
1047//     res->rtyp=fanID;
1048//     res->data=(void*)(grfan(I,heuristic,FALSE));
1049//     #endif
1050//     return FALSE;
1051//   }
1052//   else
1053//   {
1054//     WerrorS("Usage: grfan(<ideal>,<int>)");
1055//     return TRUE;
1056//   }
1057// }
1058  //Possibility to have only one Groebner cone computed by specifying a weight vector FROM THE RELATIVE INTERIOR!
1059  //Needs wp as ordering!
1060//   if(strcmp(sys_cmd,"grcone")==0)
1061//   {
1062//     if(h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
1063//     {
1064//       ideal I=((ideal)h->Data());
1065//       res->rtyp=LIST_CMD;
1066//       res->data=(lists)grcone_by_intvec(I);
1067//     }
1068//   }
1069
1070
[58b407]1071void bbfan_setup(SModulFunctions* p)
[81384b]1072{
1073  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
1074  // all undefined entries will be set to default in setBlackboxStuff
1075  // the default Print is quite usefule,
1076  // all other are simply error messages
1077  b->blackbox_destroy=bbfan_destroy;
1078  b->blackbox_String=bbfan_String;
1079  //b->blackbox_Print=blackbox_default_Print;
1080  b->blackbox_Init=bbfan_Init;
1081  b->blackbox_Copy=bbfan_Copy;
1082  b->blackbox_Assign=bbfan_Assign;
[0c2597]1083  p->iiAddCproc("","emptyFan",FALSE,emptyFan);
1084  p->iiAddCproc("","fullFan",FALSE,fullFan);
[81384b]1085  /* the following functions are implemented in bbcone.cc */
1086  // iiAddCproc("","containsInSupport",FALSE,containsInSupport);
1087  // iiAddCproc("","getAmbientDimension",FALSE,getAmbientDimension);
1088  // iiAddCproc("","getCodimension",FALSE,getDimension);
1089  // iiAddCproc("","getDimension",FALSE,getDimension);
1090  // iiAddCproc("","getLinealityDimension",FALSE,getLinealityDimension);
1091  // iiAddCproc("","isSimplicial",FALSE,isSimplicial);
1092  /********************************************************/
[58b407]1093  p->iiAddCproc("","isCompatible",FALSE,isCompatible);
1094  p->iiAddCproc("","numberOfConesOfDimension",FALSE,numberOfConesOfDimension);
1095  p->iiAddCproc("","ncones",FALSE,ncones);
1096  p->iiAddCproc("","nmaxcones",FALSE,nmaxcones);
1097  p->iiAddCproc("","insertCone",FALSE,insertCone);
1098  p->iiAddCproc("","removeCone",FALSE,removeCone);
1099  p->iiAddCproc("","getCone",FALSE,getCone);
1100  p->iiAddCproc("","getCones",FALSE,getCones);
1101  p->iiAddCproc("","isPure",FALSE,isPure);
1102  p->iiAddCproc("","fanFromString",FALSE,fanFromString);
1103  p->iiAddCproc("","fanViaCones",FALSE,fanViaCones);
1104  p->iiAddCproc("","numberOfConesWithVector",FALSE,numberOfConesWithVector);
[81384b]1105  // iiAddCproc("","isComplete",FALSE,isComplete);  not working as expected, should leave this to polymake
[58b407]1106  p->iiAddCproc("","fVector",FALSE,fVector);
1107  p->iiAddCproc("","containsInCollection",FALSE,containsInCollection);
[e44b149]1108  p->iiAddCproc("","tropicalVariety",FALSE,tropicalVariety);
1109  p->iiAddCproc("","commonRefinement",FALSE,commonRefinement);
[81384b]1110  // iiAddCproc("","grFan",FALSE,grFan);
1111  fanID=setBlackboxStuff(b,"fan");
1112  //Print("created type %d (fan)\n",fanID);
1113}
Note: See TracBrowser for help on using the repository browser.