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