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

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