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

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