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

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