source: git/Singular/dyn_modules/gfanlib/bbfan.cc @ a6574c3

spielwiese
Last change on this file since a6574c3 was a6574c3, checked in by Yue <ren@…>, 7 years ago
fix: memoryleak due to missing deallocation of cddlib global variables
  • Property mode set to 100644
File size: 30.9 KB
Line 
1#include <kernel/mod2.h>
2
3#if HAVE_GFANLIB
4
5#include <misc/intvec.h>
6#include <coeffs/coeffs.h>
7#include <coeffs/bigintmat.h>
8
9#include <Singular/ipid.h>
10#include <Singular/ipshell.h>
11#include <Singular/blackbox.h>
12
13#include <Singular/links/ssiLink.h>
14
15#include <callgfanlib_conversion.h>
16#include <bbfan.h>
17#include <gfan.h>
18#include <sstream>
19
20int fanID;
21
22void* bbfan_Init(blackbox* /*b*/)
23{
24  return (void*) new gfan::ZFan(0);
25}
26
27void bbfan_destroy(blackbox* /*b*/, void *d)
28{
29  if (d!=NULL)
30  {
31    gfan::ZFan* zf = (gfan::ZFan*) d;
32    delete zf;
33  }
34}
35
36char* bbfan_String(blackbox* /*b*/, void *d)
37{
38  if (d==NULL) return omStrDup("invalid object");
39  else
40  {
41    gfan::deinitializeCddlibIfRequired();
42    gfan::initializeCddlibIfRequired();
43    gfan::ZFan* zf = (gfan::ZFan*)d;
44    std::string s = zf->toString(2+4+8+128);
45    return omStrDup(s.c_str());
46// =======
47//     std::stringstream s;
48//     std::string raysAndCones = zf->toStringJustRaysAndMaximalCones();
49//     s << raysAndCones;
50//     if (zf->getDimension() >= 0) // <=> zf is not empty
51//     {
52//       assert(zf->numberOfConesOfDimension(zf->getDimension()-zf->getLinealityDimension(),0,0));
53//       gfan::ZCone zc = zf->getCone(zf->getDimension()-zf->getLinealityDimension(),0,0,0);
54//       gfan::ZMatrix genLinSpace = zc.generatorsOfLinealitySpace();
55//       char* gens = toString(genLinSpace);
56//       s << std::endl << "GENERATORS_LINEALITY_SPACE:" << std::endl;
57//       s << gens;
58//     }
59//     std::string sstring = s.str();
60//     return omStrDup(sstring.c_str());
61// >>>>>>> status updated 11.03.
62  }
63}
64
65void* bbfan_Copy(blackbox* /*b*/, void *d)
66{
67  gfan::ZFan* zf = (gfan::ZFan*)d;
68  gfan::ZFan* newZf = new gfan::ZFan(*zf);
69  return newZf;
70}
71
72BOOLEAN bbfan_Assign(leftv l, leftv r)
73{
74  gfan::ZFan* newZf;
75  if (r==NULL)
76  {
77    if (l->Data()!=NULL)
78    {
79      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
80      delete zd;
81    }
82    newZf = new gfan::ZFan(0);
83  }
84  else if (r->Typ()==l->Typ())
85  {
86    if (l->Data()!=NULL)
87    {
88      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
89      delete zd;
90    }
91    newZf = (gfan::ZFan*) r->CopyD();
92  }
93  else if (r->Typ()==INT_CMD)
94  {
95    int ambientDim = (int) (long) r->Data();
96    if (ambientDim < 0)
97    {
98      Werror("expected an int >= 0, but got %d", ambientDim);
99      return TRUE;
100    }
101    if (l->Data()!=NULL)
102    {
103      gfan::ZFan* zd = (gfan::ZFan*) l->Data();
104      delete zd;
105    }
106    newZf = new gfan::ZFan(ambientDim);
107  }
108  else
109  {
110    Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
111    return TRUE;
112  }
113
114  if (l->rtyp==IDHDL)
115  {
116    IDDATA((idhdl)l->data) = (char*) newZf;
117  }
118  else
119  {
120    l->data = (void*) newZf;
121  }
122  return FALSE;
123}
124
125/* returns 1 iff all rows consist of entries 1..n,
126   where n is the number of columns of the provided
127   bigintmat; 0 otherwise */
128static gfan::IntMatrix permutationIntMatrix(const bigintmat* iv)
129{
130  int cc = iv->cols();
131  int rr = iv->rows();
132  bigintmat* ivCopy = new bigintmat(rr, cc, coeffs_BIGINT);
133  number temp1 = n_Init(1,coeffs_BIGINT);
134  for (int r = 1; r <= rr; r++)
135    for (int c = 1; c <= cc; c++)
136    {
137      number temp2 = n_Sub(IMATELEM(*iv, r, c),temp1,coeffs_BIGINT);
138      ivCopy->set(r,c,temp2);
139      n_Delete(&temp2,coeffs_BIGINT);
140    }
141  n_Delete(&temp1,coeffs_BIGINT);
142  gfan::ZMatrix* zm = bigintmatToZMatrix(ivCopy);
143  gfan::IntMatrix im = gfan::IntMatrix(gfan::ZToIntMatrix(*zm));
144  delete zm;
145  return im;
146}
147static BOOLEAN jjFANEMPTY_I(leftv res, leftv v)
148{
149  int ambientDim = (int)(long)v->Data();
150  if (ambientDim < 0)
151  {
152    Werror("expected non-negative ambient dim but got %d", ambientDim);
153    return TRUE;
154  }
155  res->rtyp = fanID;
156  res->data = (void*)(new gfan::ZFan(ambientDim));
157  return FALSE;
158}
159
160static BOOLEAN jjFANEMPTY_IM(leftv res, leftv v)
161{
162  bigintmat* permutations = (bigintmat*)v->Data();
163  int ambientDim = permutations->cols();
164  gfan::IntMatrix im = permutationIntMatrix(permutations);
165  if (!gfan::Permutation::arePermutations(im))
166  {
167    Werror("provided bigintmat contains invalid permutations of {1, ..., %d}", ambientDim);
168    return TRUE;
169  }
170  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
171  sg.computeClosure(im);
172  res->rtyp = fanID;
173  res->data = (void*)(new gfan::ZFan(sg));
174  return FALSE;
175}
176
177BOOLEAN emptyFan(leftv res, leftv args)
178{
179  gfan::deinitializeCddlibIfRequired();
180  gfan::initializeCddlibIfRequired();
181  leftv u = args;
182  if (u == NULL)
183  {
184    res->rtyp = fanID;
185    res->data = (void*) new gfan::ZFan(0);
186    return FALSE;
187  }
188  if ((u != NULL) && (u->Typ() == INT_CMD))
189  {
190    if (u->next == NULL) return jjFANEMPTY_I(res, u);
191  }
192  if ((u != NULL) && (u->Typ() == BIGINTMAT_CMD))
193  {
194    if (u->next == NULL) return jjFANEMPTY_IM(res, u);
195  }
196  WerrorS("emptyFan: unexpected parameters");
197  return TRUE;
198}
199
200static BOOLEAN jjFANFULL_I(leftv res, leftv v)
201{
202  int ambientDim = (int)(long)v->Data();
203  if (ambientDim < 0)
204  {
205    Werror("expected non-negative ambient dim but got %d", ambientDim);
206    return TRUE;
207  }
208  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(ambientDim));
209  res->rtyp = fanID;
210  res->data = (void*) zf;
211  return FALSE;
212}
213static BOOLEAN jjFANFULL_IM(leftv res, leftv v)
214{
215  bigintmat* permutations = (bigintmat*)v->Data();
216  int ambientDim = permutations->cols();
217  gfan::IntMatrix im = permutationIntMatrix(permutations);
218  if (!gfan::Permutation::arePermutations(im))
219  {
220    Werror("provided bigintmat contains invalid permutations of {1, ..., %d}", ambientDim);
221    return TRUE;
222  }
223  gfan::SymmetryGroup sg = gfan::SymmetryGroup(ambientDim);
224  sg.computeClosure(im);
225  gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(sg));
226  res->rtyp = fanID;
227  res->data = (void*) zf;
228  return FALSE;
229}
230
231BOOLEAN fullFan(leftv res, leftv args)
232{
233  gfan::deinitializeCddlibIfRequired();
234  gfan::initializeCddlibIfRequired();
235  leftv u = args;
236  if (u == NULL)
237  {
238    res->rtyp = fanID;
239    res->data = (void*) new gfan::ZFan(0);
240    return FALSE;
241  }
242  if ((u != NULL) && (u->Typ() == INT_CMD))
243    if (u->next == NULL) return jjFANFULL_I(res, u);
244  if ((u != NULL) && (u->Typ() == BIGINTMAT_CMD))
245    if (u->next == NULL) return jjFANFULL_IM(res, u);
246  WerrorS("fullFan: unexpected parameters");
247  return TRUE;
248}
249
250int getAmbientDimension(gfan::ZFan* zf)
251{
252  return zf->getAmbientDimension();
253}
254
255int getCodimension(gfan::ZFan* zf)
256{
257  return zf->getCodimension();
258}
259
260int getDimension(gfan::ZFan* zf)
261{
262  return zf->getDimension();
263}
264
265int getLinealityDimension(gfan::ZFan* zf)
266{
267  return zf->getLinealityDimension();
268}
269
270BOOLEAN numberOfConesOfDimension(leftv res, leftv args)
271{
272  gfan::deinitializeCddlibIfRequired();
273  gfan::initializeCddlibIfRequired();
274  leftv u=args;
275  if ((u != NULL) && (u->Typ() == fanID))
276  {
277    leftv v=u->next;
278    if ((v != NULL) && (v->Typ() == INT_CMD))
279    {
280      leftv w=v->next;
281      if ((w != NULL) && (w->Typ() == INT_CMD))
282      {
283        leftv x=w->next;
284        if ((x != NULL) && (x->Typ() == INT_CMD))
285        {
286          gfan::ZFan* zf = (gfan::ZFan*) u->Data();
287          int d = (int)(long)v->Data();
288          int o = (int)(long)w->Data();
289          int m = (int)(long)x->Data();
290          if ( (0<=d) && (d <= zf->getAmbientDimension())
291                      && ((o == 0) || (o == 1))
292                      && ((m == 0) || (m == 1)))
293          {
294            bool oo = (bool) o;
295            bool mm = (bool) m;
296            int ld = zf->getLinealityDimension();
297            if (d-ld>=0)
298            {
299              int n = zf->numberOfConesOfDimension(d-ld,oo,mm);
300              res->rtyp = INT_CMD;
301              res->data = (void*) (long) n;
302              return FALSE;
303            }
304            res->rtyp = INT_CMD;
305            res->data = (void*) (long) 0;
306            return FALSE;
307          }
308        }
309      }
310    }
311  }
312  WerrorS("numberOfConesOfDimension: unexpected parameters");
313  return TRUE;
314}
315
316BOOLEAN ncones(leftv res, leftv args)
317{
318  gfan::deinitializeCddlibIfRequired();
319  gfan::initializeCddlibIfRequired();
320  leftv u=args;
321  if ((u != NULL) && (u->Typ() == fanID))
322  {
323    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
324    int d = zf->getAmbientDimension();
325    int n = 0;
326
327    for (int i=0; i<=d; i++)
328      n = n + zf->numberOfConesOfDimension(i,0,0);
329
330    res->rtyp = INT_CMD;
331    res->data = (void*) (long) n;
332    return FALSE;
333  }
334  WerrorS("ncones: unexpected parameters");
335  return TRUE;
336}
337
338BOOLEAN nmaxcones(leftv res, leftv args)
339{
340  gfan::deinitializeCddlibIfRequired();
341  gfan::initializeCddlibIfRequired();
342  leftv u=args;
343  if ((u != NULL) && (u->Typ() == fanID))
344  {
345    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
346
347    int n = 0;
348    for (int d=0; d<=zf->getAmbientDimension(); d++)
349      n = n + zf->numberOfConesOfDimension(d,0,1);
350
351    res->rtyp = INT_CMD;
352    res->data = (void*) (long) n;
353    return FALSE;
354  }
355  WerrorS("nmaxcones: unexpected parameters");
356  return TRUE;
357}
358
359bool isCompatible(const gfan::ZFan* zf, const gfan::ZCone* zc)
360{
361  bool b = (zf->getAmbientDimension() == zc->ambientDimension());
362  if(b)
363  {
364    for (int d=0; d<=zf->getAmbientDimension(); d++)
365    {
366      for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
367      {
368        gfan::ZCone zd = zf->getCone(d,i,0,1);
369        gfan::ZCone zt = gfan::intersection(*zc,zd);
370        zt.canonicalize();
371        b = b && zd.hasFace(zt);
372      }
373    }
374  }
375  return b;
376}
377
378BOOLEAN isCompatible(leftv res, leftv args)
379{
380  gfan::deinitializeCddlibIfRequired();
381  gfan::initializeCddlibIfRequired();
382  leftv u=args;
383  if ((u != NULL) && (u->Typ() == fanID))
384  {
385    leftv v=u->next;
386    if ((v != NULL) && (v->Typ() == coneID))
387    {
388      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
389      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
390      int b = isCompatible(zf,zc);
391      res->rtyp = INT_CMD;
392      res->data = (void*) (long) b;
393      return FALSE;
394    }
395  }
396  WerrorS("isCompatible: unexpected parameters");
397  return TRUE;
398}
399
400BOOLEAN insertCone(leftv res, leftv args)
401{
402  gfan::deinitializeCddlibIfRequired();
403  gfan::initializeCddlibIfRequired();
404  leftv u=args;
405  if ((u != NULL) && (u->rtyp==IDHDL) && (u->e==NULL) && (u->Typ() == fanID))
406  {
407    leftv v=u->next;
408    if ((v != NULL) && (v->Typ() == coneID))
409    {
410      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
411      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
412      zc->canonicalize();
413
414      leftv w=v->next;
415      int n;
416      if ((w != NULL) && (w->Typ() == INT_CMD))
417        n = (int)(long) w;
418
419      if (n != 0)
420      {
421        if (!isCompatible(zf,zc))
422        {
423          WerrorS("insertCone: cone and fan not compatible");
424          return TRUE;
425        }
426      }
427
428      zf->insert(*zc);
429      res->rtyp = NONE;
430      res->data = NULL;
431      IDDATA((idhdl)u->data) = (char*) zf;
432      return FALSE;
433    }
434  }
435  WerrorS("insertCone: unexpected parameters");
436  return TRUE;
437}
438
439bool containsInCollection(gfan::ZFan* zf, gfan::ZCone* zc)
440{
441  gfan::ZVector zv=zc->getRelativeInteriorPoint();
442  for (int d=0; d<=zf->getAmbientDimension(); d++)
443  {
444    for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
445    {
446      gfan::ZCone zd = zf->getCone(d,i,0,1);
447      zd.canonicalize();
448      if (zd.containsRelatively(zv))
449      {
450        gfan::ZCone temp = *zc;
451        temp.canonicalize();
452        return (!(zd != temp));
453      }
454    }
455  }
456  return 0;
457}
458
459BOOLEAN containsInCollection(leftv res, leftv args)
460{
461  gfan::deinitializeCddlibIfRequired();
462  gfan::initializeCddlibIfRequired();
463  leftv u=args;
464  if ((u != NULL) && (u->Typ() == fanID))
465  {
466    leftv v=u->next;
467    if ((v != NULL) && (v->Typ() == coneID))
468    {
469      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
470      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
471      if((zf->getAmbientDimension() == zc->ambientDimension()))
472      {
473        res->rtyp = INT_CMD;
474        res->data = (void*) (long) (int) containsInCollection(zf,zc);
475        return FALSE;
476      }
477      WerrorS("containsInCollection: mismatching ambient dimensions");
478      return TRUE;
479    }
480  }
481  WerrorS("containsInCollection: unexpected parameters");
482  return TRUE;
483}
484
485// BOOLEAN coneContaining(leftv res, leftv args)
486// {
487//   leftv u=args;
488//   if ((u != NULL) && (u->Typ() == fanID))
489//   {
490//     leftv v=u->next;
491//     if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
492//     {
493//       gfan::ZFan* zf = (gfan::ZFan*)u->Data();
494//       gfan::ZVector* point;
495//       if (v->Typ() == INTVEC_CMD)
496//       {
497//         intvec* w0 = (intvec*) v->Data();
498//         bigintmat* w1 = iv2bim(w0,coeffs_BIGINT);
499//         w1->inpTranspose();
500//         point = bigintmatToZVector(*w1);
501//         delete w1;
502//       }
503//       else
504//       {
505//         bigintmat* w1 = (bigintmat*) v->Data();
506//         point = bigintmatToZVector(*w1);
507//       }
508//       lists L = (lists)omAllocBin(slists_bin);
509//       res->rtyp = LIST_CMD;
510//       res->data = (void*) L;
511//       delete point;
512//       return FALSE;
513//     }
514//   }
515//   WerrorS("coneContaining: unexpected parameters");
516//   return TRUE;
517// }
518
519BOOLEAN removeCone(leftv res, leftv args)
520{
521  gfan::deinitializeCddlibIfRequired();
522  gfan::initializeCddlibIfRequired();
523  leftv u=args;
524  if ((u != NULL) && (u->Typ() == fanID))
525  {
526    leftv v=u->next;
527    if ((v != NULL) && (v->Typ() == coneID))
528    {
529      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
530      gfan::ZCone* zc = (gfan::ZCone*)v->Data();
531      zc->canonicalize();
532
533      leftv w=v->next; int n = 1;
534      if ((w != NULL) && (w->Typ() == INT_CMD))
535        n = (int)(long) w;
536
537      if (n != 0)
538      {
539        if (!containsInCollection(zf,zc))
540        {
541          WerrorS("removeCone: cone not contained in fan");
542          return TRUE;
543        }
544      }
545
546      zf->remove(*zc);
547      res->rtyp = NONE;
548      res->data = NULL;
549      IDDATA((idhdl)u->data) = (char*) zf;
550      return FALSE;
551    }
552  }
553  WerrorS("removeCone: unexpected parameters");
554  return TRUE;
555}
556
557BOOLEAN getCone(leftv res, leftv args)
558{
559  gfan::deinitializeCddlibIfRequired();
560  gfan::initializeCddlibIfRequired();
561  leftv u=args;
562  if ((u != NULL) && (u->Typ() == fanID))
563  {
564    leftv v=u->next;
565    if ((v != NULL) && (v->Typ() == INT_CMD))
566    {
567      leftv w=v->next;
568      if ((w != NULL) && (w->Typ() == INT_CMD))
569      {
570        gfan::ZFan* zf = (gfan::ZFan*) u->Data();
571        int d = (int)(long)v->Data();
572        int i = (int)(long)w->Data();
573        int o = -1;
574        int m = -1;
575        leftv x=w->next;
576        if ((x != NULL) && (x->Typ() == INT_CMD))
577        {
578          o = (int)(long)x->Data();
579          leftv y=x->next;
580          if ((y != NULL) && (y->Typ() == INT_CMD))
581          {
582            m = (int)(long)y->Data();
583          }
584        }
585        if (o == -1) o = 0;
586        if (m == -1) m = 0;
587        if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
588        {
589          bool oo = (bool) o;
590          bool mm = (bool) m;
591          if (0<=d && d<=zf->getAmbientDimension())
592          {
593            int ld = zf->getLinealityDimension();
594            if (0<i && i<=zf->numberOfConesOfDimension(d-ld,oo,mm))
595            {
596              i=i-1;
597              if (d-ld>=0)
598              {
599                gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
600                res->rtyp = coneID;
601                res->data = (void*)new gfan::ZCone(zc);
602                return FALSE;
603              }
604              else
605              {
606                WerrorS("getCone: invalid dimension; no cones in this dimension");
607                return TRUE;
608              }
609            }
610            else
611            {
612              WerrorS("getCone: invalid index");
613              return TRUE;
614            }
615          }
616          else
617          {
618            WerrorS("getCone: invalid dimension");
619            return TRUE;
620          }
621        }
622        else
623        {
624          WerrorS("getCone: invalid specifier for orbit or maximal");
625          return TRUE;
626        }
627      }
628    }
629  }
630  WerrorS("getCone: unexpected parameters");
631  return TRUE;
632}
633
634BOOLEAN getCones(leftv res, leftv args)
635{
636  gfan::deinitializeCddlibIfRequired();
637  gfan::initializeCddlibIfRequired();
638  leftv u=args;
639  if ((u != NULL) && (u->Typ() == fanID))
640  {
641    leftv v=u->next;
642    if ((v != NULL) && (v->Typ() == INT_CMD))
643    {
644      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
645      int d = (int)(long)v->Data();
646      int o = -1;
647      int m = -1;
648      leftv w=v->next;
649      if ((w != NULL) && (w->Typ() == INT_CMD))
650      {
651        o = (int)(long)w->Data();
652        leftv x=w->next;
653        if ((x != NULL) && (x->Typ() == INT_CMD))
654        {
655          m = (int)(long)x->Data();
656        }
657      }
658      if (o == -1) o = 0;
659      if (m == -1) m = 0;
660      if (((o == 0) || (o == 1)) && ((m == 0) || (m == 1)))
661      {
662        bool oo = (bool) o;
663        bool mm = (bool) m;
664        if (0<=d && d<=zf->getAmbientDimension())
665        {
666          int ld = zf->getLinealityDimension();
667          if (d-ld>=0)
668          {
669            lists L = (lists)omAllocBin(slists_bin);
670            int n = zf->numberOfConesOfDimension(d-ld,oo,mm);
671            L->Init(n);
672            for (int i=0; i<n; i++)
673            {
674              gfan::ZCone zc = zf->getCone(d-ld,i,oo,mm);
675              L->m[i].rtyp = coneID; L->m[i].data=(void*) new gfan::ZCone(zc);
676            }
677            res->rtyp = LIST_CMD;
678            res->data = (void*) L;
679            return FALSE;
680          }
681          else
682          {
683            WerrorS("getCones: invalid dimension; no cones in this dimension");
684            return TRUE;
685          }
686        }
687        else
688        {
689          WerrorS("getCones: invalid dimension");
690          return TRUE;
691        }
692      }
693      else
694      {
695        WerrorS("getCones: invalid specifier for orbit or maximal");
696        return TRUE;
697      }
698    }
699  }
700  WerrorS("getCones: unexpected parameters");
701  return TRUE;
702}
703
704int isSimplicial(gfan::ZFan* zf)
705{
706  int i = zf->isSimplicial() ? 1 : 0;
707  return i;
708}
709
710BOOLEAN isPure(leftv res, leftv args)
711{
712  gfan::deinitializeCddlibIfRequired();
713  gfan::initializeCddlibIfRequired();
714  leftv u=args;
715  if ((u != NULL) && (u->Typ() == fanID))
716  {
717    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
718    int b = zf->isPure();
719    res->rtyp = INT_CMD;
720    res->data = (void*) (long) b;
721    return FALSE;
722  }
723  WerrorS("isPure: unexpected parameters");
724  return TRUE;
725}
726
727// BOOLEAN isComplete(leftv res, leftv args)
728// {
729//   leftv u=args;
730//   if ((u != NULL) && (u->Typ() == fanID))
731//   {
732//     gfan::ZFan* zf = (gfan::ZFan*) u->Data();
733//     int b = zf->isComplete();
734//     res->rtyp = INT_CMD;
735//     res->data = (void*) (long) b;
736//     return FALSE;
737//   }
738//   WerrorS("isComplete: unexpected parameters");
739//   return TRUE;
740// }
741
742BOOLEAN fVector(leftv res, leftv args)
743{
744  gfan::deinitializeCddlibIfRequired();
745  gfan::initializeCddlibIfRequired();
746  leftv u=args;
747  if ((u != NULL) && (u->Typ() == fanID))
748  {
749    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
750    gfan::ZVector zv=zf->getFVector();
751    res->rtyp = BIGINTMAT_CMD;
752    res->data = (void*) zVectorToBigintmat(zv);
753    return FALSE;
754  }
755  WerrorS("fVector: unexpected parameters");
756  return TRUE;
757}
758
759gfan::ZMatrix rays(const gfan::ZFan* const zf)
760{
761  gfan::deinitializeCddlibIfRequired();
762  gfan::initializeCddlibIfRequired();
763  gfan::ZMatrix rays(0,zf->getAmbientDimension());
764  for (int i=0; i<zf->numberOfConesOfDimension(1,0,0); i++)
765  {
766    gfan::ZCone zc = zf->getCone(1, i, 0, 0);
767    rays.append(zc.extremeRays());
768  }
769  return rays;
770}
771
772int numberOfConesWithVector(gfan::ZFan* zf, gfan::ZVector* v)
773{
774  gfan::deinitializeCddlibIfRequired();
775  gfan::initializeCddlibIfRequired();
776  int count = 0;
777  int ambientDim = zf->getAmbientDimension();
778  for (int i=0; i<zf->numberOfConesOfDimension(ambientDim, 0, 0); i++)
779  {
780    gfan::ZCone zc = zf->getCone(ambientDim, i, 0, 0);
781    if (zc.contains(*v))
782    {
783      count = count +1;
784      if (count > 1)
785        return count;
786    }
787  }
788  return count;
789}
790
791BOOLEAN numberOfConesWithVector(leftv res, leftv args)
792{
793  gfan::deinitializeCddlibIfRequired();
794  gfan::initializeCddlibIfRequired();
795  leftv u=args;
796  if ((u != NULL) && (u->Typ() == fanID))
797  {
798    leftv v=u->next;
799    if ((v != NULL) && (v->Typ() == BIGINTMAT_CMD))
800    {
801      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
802      bigintmat* v0 = (bigintmat*) v->Data();
803      int ambientDim = zf->getAmbientDimension();
804      if (ambientDim != v0->cols())
805      {
806        WerrorS("numberOfConesWithVector: mismatching dimensions");
807        return TRUE;
808      }
809      gfan::ZVector* v1 = bigintmatToZVector(*v0);
810      int count = numberOfConesWithVector(zf, v1);
811      delete v1;
812      res->rtyp = INT_CMD;
813      res->data = (void*) (long) count;
814      return FALSE;
815    }
816  }
817  WerrorS("numberOfConesWithVector: unexpected parameters");
818  return TRUE;
819}
820
821BOOLEAN fanFromString(leftv res, leftv args)
822{
823  gfan::deinitializeCddlibIfRequired();
824  gfan::initializeCddlibIfRequired();
825  leftv u=args;
826  if ((u != NULL) && (u->Typ() == STRING_CMD))
827    {
828      std::string fanInString = (char*) u->Data();
829      std::istringstream s(fanInString);
830      gfan::ZFan* zf = new gfan::ZFan(s);
831      res->rtyp = fanID;
832      res->data = (void*) zf;
833      return FALSE;
834    }
835  WerrorS("fanFromString: unexpected parameters");
836  return TRUE;
837}
838
839BOOLEAN fanViaCones(leftv res, leftv args)
840{
841  gfan::deinitializeCddlibIfRequired();
842  gfan::initializeCddlibIfRequired();
843  leftv u=args;
844  if ((u != NULL) && (u->Typ() == LIST_CMD))
845  {
846    lists L = (lists) u->Data();
847    if (lSize(L)>-1)
848    {
849      if (L->m[0].Typ() != coneID)
850      {
851        WerrorS("fanViaCones: list contains entries of wrong type");
852        return TRUE;
853      }
854      gfan::ZCone* zc = (gfan::ZCone*) L->m[0].Data();
855      gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
856      zf->insert(*zc);
857      for (int i=1; i<=lSize(L); i++)
858      {
859        if (L->m[i].Typ() != coneID)
860        {
861          WerrorS("fanViaCones: entries of wrong type in list");
862          return TRUE;
863        }
864        gfan::ZCone* zc = (gfan::ZCone*) L->m[i].Data();
865        if (zc->ambientDimension() != zf->getAmbientDimension())
866        {
867          WerrorS("fanViaCones: inconsistent ambient dimensions amongst cones in list");
868          return TRUE;
869        }
870        zf->insert(*zc);
871      }
872      res->rtyp = fanID;
873      res->data = (void*) zf;
874      return FALSE;
875    }
876    res->rtyp = fanID;
877    res->data = (void*) new gfan::ZFan(0);
878    return FALSE;
879  }
880  if ((u != NULL) && (u->Typ() == coneID))
881  {
882    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
883    gfan::ZFan* zf = new gfan::ZFan(zc->ambientDimension());
884    zf->insert(*zc);
885    while (u->next != NULL)
886    {
887      u = u->next;
888      if (u->Typ() != coneID)
889      {
890        WerrorS("fanViaCones: arguments of wrong type");
891        return TRUE;
892      }
893      gfan::ZCone* zc = (gfan::ZCone*) u->Data();
894      if (zc->ambientDimension() != zf->getAmbientDimension())
895      {
896        WerrorS("fanViaCones: inconsistent ambient dimensions amongst input cones");
897        return TRUE;
898      }
899      zf->insert(*zc);
900    }
901    res->rtyp = fanID;
902    res->data = (void*) zf;
903    return FALSE;
904  }
905  if (u == NULL)
906  {
907    res->rtyp = fanID;
908    res->data = (void*) new gfan::ZFan(0);
909    return FALSE;
910  }
911  WerrorS("fanViaCones: unexpected parameters");
912  return TRUE;
913}
914
915
916// BOOLEAN 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    for (int i=0; i<zf.numberOfConesOfDimension(d,0,1); i++)
973      maximalConesOfF.push_back(zf.getCone(d,i,0,1));
974
975  std::list<gfan::ZCone> maximalConesOfG;
976  for (int d=0; d<=zg.getAmbientDimension(); d++)
977    for (int i=0; i<zg.numberOfConesOfDimension(d,0,1); i++)
978      maximalConesOfG.push_back(zg.getCone(d,i,0,1));
979
980  // construct a new fan out of their intersections
981  gfan::ZFan zr = gfan::ZFan(zf.getAmbientDimension());
982  for (std::list<gfan::ZCone>::iterator itf=maximalConesOfF.begin();
983       itf != maximalConesOfF.end(); itf++)
984    for (std::list<gfan::ZCone>::iterator itg=maximalConesOfG.begin();
985         itg != maximalConesOfG.end(); itg++)
986      zr.insert(intersection(*itf,*itg));
987
988  return zr;
989}
990
991BOOLEAN commonRefinement(leftv res, leftv args)
992{
993  gfan::deinitializeCddlibIfRequired();
994  gfan::initializeCddlibIfRequired();
995  leftv u=args;
996  if ((u != NULL) && (u->Typ() == fanID))
997  {
998    leftv v=u->next;
999    if ((v != NULL) && (v->Typ() == fanID))
1000    {
1001      gfan::ZFan* zf = (gfan::ZFan*) u->Data();
1002      gfan::ZFan* zg = (gfan::ZFan*) v->Data();
1003      gfan::ZFan* zr = new gfan::ZFan(commonRefinement(*zf,*zg));
1004      res->rtyp = fanID;
1005      res->data = (void*) zr;
1006      return FALSE;
1007    }
1008  }
1009  WerrorS("commonRefinement: unexpected parameters");
1010  return TRUE;
1011}
1012
1013// BOOLEAN grFan(leftv res, leftv h)
1014// {
1015//   /*======== GFAN ==============*/
1016//   /*
1017//    WILL HAVE TO CHANGE RETURN TYPE TO LIST_CMD
1018//   */
1019//   /*
1020//     heuristic:
1021//     0 = keep all Groebner bases in memory
1022//     1 = write all Groebner bases to disk and read whenever necessary
1023//     2 = use a mixed heuristic, based on length of Groebner bases
1024//   */
1025//   if( h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
1026//   {
1027//     int heuristic;
1028//     heuristic=(int)(long)h->next->Data();
1029//     ideal I=((ideal)h->Data());
1030//     #ifndef USE_ZFAN
1031//         #define USE_ZFAN
1032//     #endif
1033//     #ifndef USE_ZFAN
1034//     res->rtyp=LIST_CMD; //res->rtyp=coneID; res->data(void*)zcone;
1035//     res->data=(lists) grfan(I,heuristic,FALSE);
1036//     #else
1037//     res->rtyp=fanID;
1038//     res->data=(void*)(grfan(I,heuristic,FALSE));
1039//     #endif
1040//     return FALSE;
1041//   }
1042//   else
1043//   {
1044//     WerrorS("Usage: grfan(<ideal>,<int>)");
1045//     return TRUE;
1046//   }
1047// }
1048  //Possibility to have only one Groebner cone computed by specifying a weight vector FROM THE RELATIVE INTERIOR!
1049  //Needs wp as ordering!
1050//   if(strcmp(sys_cmd,"grcone")==0)
1051//   {
1052//     if(h!=NULL && h->Typ()==IDEAL_CMD && h->next!=NULL && h->next->Typ()==INT_CMD)
1053//     {
1054//       ideal I=((ideal)h->Data());
1055//       res->rtyp=LIST_CMD;
1056//       res->data=(lists)grcone_by_intvec(I);
1057//     }
1058//   }
1059
1060
1061BOOLEAN bbfan_serialize(blackbox *b, void *d, si_link f)
1062{
1063  ssiInfo *dd = (ssiInfo *)f->data;
1064
1065  sleftv l;
1066  memset(&l,0,sizeof(l));
1067  l.rtyp=STRING_CMD;
1068  l.data=(void*)"fan";
1069  f->m->Write(f, &l);
1070
1071  gfan::ZFan* zf = (gfan::ZFan*) d;
1072  std::string s = zf->toString(2+4+8+128);
1073
1074  fprintf(dd->f_write,"%d %s ",(int)s.size(),s.c_str());
1075
1076  return FALSE;
1077}
1078
1079
1080BOOLEAN bbfan_deserialize(blackbox **b, void **d, si_link f)
1081{
1082  ssiInfo *dd = (ssiInfo *)f->data;
1083
1084  int l = s_readint(dd->f_read);
1085  char* buf = (char*) omAlloc0(l+1);
1086  (void) s_getc(dd->f_read); // skip whitespace
1087  (void) s_readbytes(buf,l,dd->f_read);
1088  buf[l]='\0';
1089
1090  std::istringstream fanInString(std::string(buf,l));
1091  gfan::ZFan* zf = new gfan::ZFan(fanInString);
1092  *d=zf;
1093
1094  omFree(buf);
1095  return FALSE;
1096}
1097
1098
1099void bbfan_setup(SModulFunctions* p)
1100{
1101  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
1102  // all undefined entries will be set to default in setBlackboxStuff
1103  // the default Print is quite usefule,
1104  // all other are simply error messages
1105  b->blackbox_destroy=bbfan_destroy;
1106  b->blackbox_String=bbfan_String;
1107  //b->blackbox_Print=blackbox_default_Print;
1108  b->blackbox_Init=bbfan_Init;
1109  b->blackbox_Copy=bbfan_Copy;
1110  b->blackbox_Assign=bbfan_Assign;
1111  b->blackbox_serialize=bbfan_serialize;
1112  b->blackbox_deserialize=bbfan_deserialize;
1113  p->iiAddCproc("gfan.lib","emptyFan",FALSE,emptyFan);
1114  p->iiAddCproc("gfan.lib","fullFan",FALSE,fullFan);
1115  /* the following functions are implemented in bbcone.cc */
1116  // iiAddCproc("gfan.lib","containsInSupport",FALSE,containsInSupport);
1117  // iiAddCproc("gfan.lib","getAmbientDimension",FALSE,getAmbientDimension);
1118  // iiAddCproc("gfan.lib","getCodimension",FALSE,getDimension);
1119  // iiAddCproc("gfan.lib","getDimension",FALSE,getDimension);
1120  // iiAddCproc("gfan.lib","getLinealityDimension",FALSE,getLinealityDimension);
1121  // iiAddCproc("gfan.lib","isSimplicial",FALSE,isSimplicial);
1122  /********************************************************/
1123  p->iiAddCproc("gfan.lib","isCompatible",FALSE,isCompatible);
1124  p->iiAddCproc("gfan.lib","numberOfConesOfDimension",FALSE,numberOfConesOfDimension);
1125  p->iiAddCproc("gfan.lib","ncones",FALSE,ncones);
1126  p->iiAddCproc("gfan.lib","nmaxcones",FALSE,nmaxcones);
1127  p->iiAddCproc("gfan.lib","insertCone",FALSE,insertCone);
1128  p->iiAddCproc("gfan.lib","removeCone",FALSE,removeCone);
1129  p->iiAddCproc("gfan.lib","getCone",FALSE,getCone);
1130  p->iiAddCproc("gfan.lib","getCones",FALSE,getCones);
1131  p->iiAddCproc("gfan.lib","isPure",FALSE,isPure);
1132  p->iiAddCproc("gfan.lib","fanFromString",FALSE,fanFromString);
1133  p->iiAddCproc("gfan.lib","fanViaCones",FALSE,fanViaCones);
1134  p->iiAddCproc("gfan.lib","numberOfConesWithVector",FALSE,numberOfConesWithVector);
1135  // iiAddCproc("gfan.lib","isComplete",FALSE,isComplete);  not working as expected, should leave this to polymake
1136  p->iiAddCproc("gfan.lib","fVector",FALSE,fVector);
1137  p->iiAddCproc("gfan.lib","containsInCollection",FALSE,containsInCollection);
1138  // p->iiAddCproc("gfan.lib","tropicalVariety",FALSE,tropicalVariety);
1139  p->iiAddCproc("gfan.lib","commonRefinement",FALSE,commonRefinement);
1140  // iiAddCproc("gfan.lib","grFan",FALSE,grFan);
1141  fanID=setBlackboxStuff(b,"fan");
1142  //Print("created type %d (fan)\n",fanID);
1143}
1144
1145#endif
1146// gfan::ZFan commonRefinementCompleteFans(const gfan::ZFan &zf, const gfan::ZFan &zg)
1147// {
1148//   assume(zf->getAmbientDimension() == zg->getAmbientDimension());
1149
1150//   gfan::ZFan zfg = gfan::ZFan(zf->getAmbientDimension());
1151//   int d = zf->getAmbientDimension();
1152//   for (int i=0; i<zf->numberOfConesOfDimension(d,0,1); i++)
1153//     for (int j=0; j<zg->numberOfConesOfDimension(d,0,1); j++)
1154//     {
1155//       gfan::ZCone zc = intersection(zf->getCone(d,i,0,1),zg->getCone(d,j,0,1));
1156//       if (zc.dimension()==d) zgf.insert(zc);
1157//     }
1158
1159//   return zfg;
1160// }
Note: See TracBrowser for help on using the repository browser.