source: git/Singular/dyn_modules/gfanlib/bbcone.cc @ 4bf76b5

spielwiese
Last change on this file since 4bf76b5 was 4bf76b5, checked in by Hans Schoenemann <hannes@…>, 8 years ago
fix: add missing listContainsCone to bbcone.cc
  • Property mode set to 100644
File size: 50.3 KB
Line 
1#include <kernel/mod2.h>
2
3#if HAVE_GFANLIB
4
5#include <misc/intvec.h>
6#include <misc/sirandom.h>
7
8#include <coeffs/bigintmat.h>
9#include <coeffs/longrat.h>
10
11#include <Singular/ipid.h>
12#include <Singular/ipshell.h>
13#include <Singular/blackbox.h>
14
15#include <Singular/links/ssiLink.h>
16
17#include <callgfanlib_conversion.h>
18#include <sstream>
19
20#include <gfanlib/gfanlib.h>
21#include <gfanlib/gfanlib_q.h>
22
23#include <bbfan.h>
24#include <bbpolytope.h>
25
26int coneID;
27
28std::string toString(const gfan::ZCone* const c)
29{
30  std::stringstream s;
31  s<<"AMBIENT_DIM"<<std::endl;
32  s<<c->ambientDimension()<<std::endl;
33
34  gfan::ZMatrix i=c->getInequalities();
35  char* ineqs = toString(i);
36  if (c->areFacetsKnown())
37    s<<"FACETS"<<std::endl;
38  else
39    s<<"INEQUALITIES"<<std::endl;
40  if (ineqs!=NULL)
41  {
42    s<<ineqs<<std::endl;
43    omFree(ineqs);
44  }
45
46  gfan::ZMatrix e=c->getEquations();
47  char* eqs = toString(e);
48  if (c->areImpliedEquationsKnown())
49    s<<"LINEAR_SPAN"<<std::endl;
50  else
51    s<<"EQUATIONS"<<std::endl;
52  if (eqs!=NULL)
53  {
54    s<<eqs<<std::endl;
55    omFree(eqs);
56  }
57
58  if (c->areExtremeRaysKnown())
59  {
60    gfan::ZMatrix r=c->extremeRays();
61    char* rs = toString(r);
62    s<<"RAYS"<<std::endl;
63    if (rs!=NULL)
64    {
65      s<<rs<<std::endl;
66      omFree(rs);
67    }
68    gfan::ZMatrix l=c->generatorsOfLinealitySpace();
69    char* ls = toString(l);
70    s<<"LINEALITY_SPACE"<<std::endl;
71    if (ls!=NULL)
72    {
73      s<<ls<<std::endl;
74      omFree(ls);
75    }
76  }
77
78  return s.str();
79}
80
81void* bbcone_Init(blackbox* /*b*/)
82{
83  return (void*)(new gfan::ZCone());
84}
85
86BOOLEAN bbcone_Assign(leftv l, leftv r)
87{
88  gfan::ZCone* newZc;
89  if (r==NULL)
90  {
91    if (l->Data()!=NULL)
92    {
93      gfan::ZCone* zd = (gfan::ZCone*)l->Data();
94      delete zd;
95    }
96    newZc = new gfan::ZCone();
97  }
98  else if (r->Typ()==l->Typ())
99  {
100    if (l->Data()!=NULL)
101    {
102      gfan::ZCone* zd = (gfan::ZCone*)l->Data();
103      delete zd;
104    }
105    newZc = (gfan::ZCone*)r->CopyD();
106  }
107  else if (r->Typ()==INT_CMD)
108  {
109    int ambientDim = (int)(long)r->Data();
110    if (ambientDim < 0)
111    {
112      Werror("expected an int >= 0, but got %d", ambientDim);
113      return TRUE;
114    }
115    if (l->Data()!=NULL)
116    {
117      gfan::ZCone* zd = (gfan::ZCone*)l->Data();
118      delete zd;
119    }
120    newZc = new gfan::ZCone(ambientDim);
121  }
122  else
123  {
124    Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
125    return TRUE;
126  }
127
128  if (l->rtyp==IDHDL)
129  {
130    IDDATA((idhdl)l->data)=(char*) newZc;
131  }
132  else
133  {
134    l->data=(void *)newZc;
135  }
136  return FALSE;
137}
138
139char* bbcone_String(blackbox* /*b*/, void *d)
140{
141  if (d==NULL) return omStrDup("invalid object");
142  else
143  {
144    std::string s=toString((gfan::ZCone*) d);
145    return omStrDup(s.c_str());
146  }
147}
148
149void bbcone_destroy(blackbox* /*b*/, void *d)
150{
151  if (d!=NULL)
152  {
153    gfan::ZCone* zc = (gfan::ZCone*) d;
154    delete zc;
155  }
156}
157
158void* bbcone_Copy(blackbox* /*b*/, void *d)
159{
160  gfan::ZCone* zc = (gfan::ZCone*)d;
161  gfan::ZCone* newZc = new gfan::ZCone(*zc);
162  return newZc;
163}
164
165static BOOLEAN bbcone_Op2(int op, leftv res, leftv i1, leftv i2)
166{
167  gfan::ZCone* zp = (gfan::ZCone*) i1->Data();
168  switch(op)
169  {
170    case '&':
171    {
172      if (i2->Typ()==coneID)
173      {
174        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
175        int d1 = zp->ambientDimension();
176        int d2 = zq->ambientDimension();
177        if (d1 != d2)
178        {
179          WerrorS("mismatching ambient dimensions");
180          return TRUE;
181        }
182        gfan::ZCone* zs = new gfan::ZCone();
183        *zs = gfan::intersection(*zp, *zq);
184        zs->canonicalize();
185        res->rtyp = coneID;
186        res->data = (void*) zs;
187        return FALSE;
188      }
189      return blackboxDefaultOp2(op,res,i1,i2);
190    }
191    case '|':
192    {
193      if(i2->Typ()==coneID)
194      {
195        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
196        int d1 = zp->ambientDimension();
197        int d2 = zq->ambientDimension();
198        if (d1 != d2)
199        {
200          WerrorS("mismatching ambient dimensions");
201          return TRUE;
202        }
203        gfan::ZMatrix rays = zp->extremeRays();
204        rays.append(zq->extremeRays());
205        gfan::ZMatrix lineality = zp->generatorsOfLinealitySpace();
206        lineality.append(zq->generatorsOfLinealitySpace());
207        gfan::ZCone* zs = new gfan::ZCone();
208        *zs = gfan::ZCone::givenByRays(rays,lineality);
209        zs->canonicalize();
210        res->rtyp = coneID;
211        res->data = (void*) zs;
212        return FALSE;
213      }
214    return blackboxDefaultOp2(op,res,i1,i2);
215    }
216    case EQUAL_EQUAL:
217    {
218      if(i2->Typ()==coneID)
219      {
220        gfan::ZCone* zq = (gfan::ZCone*) i2->Data();
221        zp->canonicalize();
222        zq->canonicalize();
223        bool b = !((*zp)!=(*zq));
224        res->rtyp = INT_CMD;
225        res->data = (void*) b;
226        return FALSE;
227      }
228      return blackboxDefaultOp2(op,res,i1,i2);
229    }
230    default:
231      return blackboxDefaultOp2(op,res,i1,i2);
232  }
233  return blackboxDefaultOp2(op,res,i1,i2);
234}
235
236
237/* singular wrapper for gfanlib functions */
238static BOOLEAN jjCONENORMALS1(leftv res, leftv v)
239{
240  /* method for generating a cone object from inequalities;
241     valid parametrizations: (intmat) */
242  bigintmat* ineq = NULL;
243  if (v->Typ() == INTMAT_CMD)
244  {
245    intvec* ineq0 = (intvec*) v->Data();
246    ineq = iv2bim(ineq0,coeffs_BIGINT);
247  }
248  else
249    ineq = (bigintmat*) v->Data();
250  gfan::ZMatrix* zm = bigintmatToZMatrix(ineq);
251  gfan::ZCone* zc = new gfan::ZCone(*zm, gfan::ZMatrix(0, zm->getWidth()));
252  delete zm;
253  if (v->Typ() == INTMAT_CMD)
254    delete ineq;
255  res->rtyp = coneID;
256  res->data = (void*) zc;
257  return FALSE;
258}
259
260static BOOLEAN jjCONENORMALS2(leftv res, leftv u, leftv v)
261{
262  /* method for generating a cone object from iequalities,
263     and equations (...)
264     valid parametrizations: (intmat, intmat)
265     Errors will be invoked in the following cases:
266     - u and v have different numbers of columns */
267  bigintmat* ineq = NULL; bigintmat* eq = NULL;
268  if (u->Typ() == INTMAT_CMD)
269  {
270    intvec* ineq0 = (intvec*) u->Data();
271    ineq = iv2bim(ineq0,coeffs_BIGINT);
272  }
273  else
274    ineq = (bigintmat*) u->Data();
275  if (v->Typ() == INTMAT_CMD)
276  {
277    intvec* eq0 = (intvec*) v->Data();
278    eq = iv2bim(eq0,coeffs_BIGINT);
279  }
280  else
281    eq = (bigintmat*) v->Data();
282
283  if (ineq->cols() != eq->cols())
284  {
285    Werror("expected same number of columns but got %d vs. %d",
286           ineq->cols(), eq->cols());
287    return TRUE;
288  }
289  gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
290  gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
291  gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2);
292  delete zm1;
293  delete zm2;
294  if (u->Typ() == INTMAT_CMD)
295    delete ineq;
296  if (v->Typ() == INTMAT_CMD)
297    delete eq;
298  res->rtyp = coneID;
299  res->data = (void*) zc;
300  return FALSE;
301}
302
303static BOOLEAN jjCONENORMALS3(leftv res, leftv u, leftv v, leftv w)
304{
305  /* method for generating a cone object from inequalities, equations,
306     and an integer k;
307     valid parametrizations: (intmat, intmat, int);
308     Errors will be invoked in the following cases:
309     - u and v have different numbers of columns,
310     - k not in [0..3];
311     if the 2^0-bit of k is set, then ... */
312  bigintmat* ineq = NULL; bigintmat* eq = NULL;
313  if (u->Typ() == INTMAT_CMD)
314  {
315    intvec* ineq0 = (intvec*) u->Data();
316    ineq = iv2bim(ineq0,coeffs_BIGINT);
317  }
318  else
319    ineq = (bigintmat*) u->Data();
320  if (v->Typ() == INTMAT_CMD)
321  {
322    intvec* eq0 = (intvec*) v->Data();
323    eq = iv2bim(eq0,coeffs_BIGINT);
324  }
325  else
326    eq = (bigintmat*) v->Data();
327  if (ineq->cols() != eq->cols())
328  {
329    Werror("expected same number of columns but got %d vs. %d",
330            ineq->cols(), eq->cols());
331    return TRUE;
332  }
333  int k = (int)(long)w->Data();
334  if ((k < 0) || (k > 3))
335  {
336    WerrorS("expected int argument in [0..3]");
337    return TRUE;
338  }
339  gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
340  gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
341  gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2, k);
342  delete zm1;
343  delete zm2;
344  if (u->Typ() == INTMAT_CMD)
345    delete ineq;
346  if (v->Typ() == INTMAT_CMD)
347    delete eq;
348  res->rtyp = coneID;
349  res->data = (void*) zc;
350  return FALSE;
351}
352
353BOOLEAN coneViaNormals(leftv res, leftv args)
354{
355  gfan::initializeCddlibIfRequired();
356  leftv u = args;
357  if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
358  {
359    if (u->next == NULL) return jjCONENORMALS1(res, u);
360  }
361  leftv v = u->next;
362  if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTMAT_CMD)))
363  {
364    if (v->next == NULL) return jjCONENORMALS2(res, u, v);
365  }
366  leftv w = v->next;
367  if ((w != NULL) && (w->Typ() == INT_CMD))
368  {
369    if (w->next == NULL) return jjCONENORMALS3(res, u, v, w);
370  }
371  WerrorS("coneViaInequalities: unexpected parameters");
372  return TRUE;
373}
374
375static BOOLEAN jjCONERAYS1(leftv res, leftv v)
376{
377  /* method for generating a cone object from half-lines
378     (cone = convex hull of the half-lines; note: there may be
379     entire lines in the cone);
380     valid parametrizations: (intmat) */
381  bigintmat* rays = NULL;
382  if (v->Typ() == INTMAT_CMD)
383  {
384    intvec* rays0 = (intvec*) v->Data();
385    rays = iv2bim(rays0,coeffs_BIGINT);
386  }
387  else
388    rays = (bigintmat*) v->Data();
389
390  gfan::ZMatrix* zm = bigintmatToZMatrix(rays);
391  gfan::ZCone* zc = new gfan::ZCone();
392  *zc = gfan::ZCone::givenByRays(*zm, gfan::ZMatrix(0, zm->getWidth()));
393  res->rtyp = coneID;
394  res->data = (void*) zc;
395
396  delete zm;
397  if (v->Typ() == INTMAT_CMD)
398    delete rays;
399  return FALSE;
400}
401
402static BOOLEAN jjCONERAYS2(leftv res, leftv u, leftv v)
403{
404  /* method for generating a cone object from half-lines,
405     and lines (any point in the cone being the sum of a point
406     in the convex hull of the half-lines and a point in the span
407     of the lines; the second argument may contain or entirely consist
408     of zero rows);
409     valid parametrizations: (intmat, intmat)
410     Errors will be invoked in the following cases:
411     - u and v have different numbers of columns */
412  bigintmat* rays = NULL; bigintmat* linSpace = NULL;
413  if (u->Typ() == INTMAT_CMD)
414  {
415    intvec* rays0 = (intvec*) u->Data();
416    rays = iv2bim(rays0,coeffs_BIGINT);
417  }
418  else
419    rays = (bigintmat*) u->Data();
420  if (v->Typ() == INTMAT_CMD)
421  {
422    intvec* linSpace0 = (intvec*) v->Data();
423    linSpace = iv2bim(linSpace0,coeffs_BIGINT);
424  }
425  else
426    linSpace = (bigintmat*) v->Data();
427
428  if (rays->cols() != linSpace->cols())
429  {
430    Werror("expected same number of columns but got %d vs. %d",
431           rays->cols(), linSpace->cols());
432    return TRUE;
433  }
434  gfan::ZMatrix* zm1 = bigintmatToZMatrix(rays);
435  gfan::ZMatrix* zm2 = bigintmatToZMatrix(linSpace);
436  gfan::ZCone* zc = new gfan::ZCone();
437  *zc = gfan::ZCone::givenByRays(*zm1, *zm2);
438  res->rtyp = coneID;
439  res->data = (void*) zc;
440
441  delete zm1;
442  delete zm2;
443  if (u->Typ() == INTMAT_CMD)
444    delete rays;
445  if (v->Typ() == INTMAT_CMD)
446    delete linSpace;
447  return FALSE;
448}
449
450static BOOLEAN jjCONERAYS3(leftv res, leftv u, leftv v, leftv w)
451{
452  /* method for generating a cone object from half-lines,
453     and lines (any point in the cone being the sum of a point
454     in the convex hull of the half-lines and a point in the span
455     of the lines), and an integer k;
456     valid parametrizations: (intmat, intmat, int);
457     Errors will be invoked in the following cases:
458     - u and v have different numbers of columns,
459     - k not in [0..3];
460     if the 2^0-bit of k is set, then the lineality space is known
461     to be the span of the provided lines;
462     if the 2^1-bit of k is set, then the extreme rays are known:
463     each half-line spans a (different) extreme ray */
464  bigintmat* rays = NULL; bigintmat* linSpace = NULL;
465  if (u->Typ() == INTMAT_CMD)
466  {
467    intvec* rays0 = (intvec*) u->Data();
468    rays = iv2bim(rays0,coeffs_BIGINT);
469  }
470  else
471    rays = (bigintmat*) u->Data();
472  if (v->Typ() == INTMAT_CMD)
473  {
474    intvec* linSpace0 = (intvec*) v->Data();
475    linSpace = iv2bim(linSpace0,coeffs_BIGINT);
476  }
477  else
478    linSpace = (bigintmat*) v->Data();
479
480  if (rays->cols() != linSpace->cols())
481  {
482    Werror("expected same number of columns but got %d vs. %d",
483           rays->cols(), linSpace->cols());
484    return TRUE;
485  }
486  int k = (int)(long)w->Data();
487  if ((k < 0) || (k > 3))
488  {
489    WerrorS("expected int argument in [0..3]");
490    return TRUE;
491  }
492  gfan::ZMatrix* zm1 = bigintmatToZMatrix(rays);
493  gfan::ZMatrix* zm2 = bigintmatToZMatrix(linSpace);
494  gfan::ZCone* zc = new gfan::ZCone();
495  *zc = gfan::ZCone::givenByRays(*zm1, *zm2);
496  //k should be passed on to zc; not available yet
497  res->rtyp = coneID;
498  res->data = (void*) zc;
499
500  delete zm1;
501  delete zm2;
502  if (u->Typ() == INTMAT_CMD)
503    delete rays;
504  if (v->Typ() == INTMAT_CMD)
505    delete linSpace;
506  return FALSE;
507}
508
509BOOLEAN coneViaRays(leftv res, leftv args)
510{
511  gfan::initializeCddlibIfRequired();
512  leftv u = args;
513  if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
514  {
515    if (u->next == NULL) return jjCONERAYS1(res, u);
516    leftv v = u->next;
517    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTMAT_CMD)))
518    {
519      if (v->next == NULL) return jjCONERAYS2(res, u, v);
520      leftv w = v->next;
521      if ((w != NULL) && (w->Typ() == INT_CMD))
522      {
523        if (w->next == NULL) return jjCONERAYS3(res, u, v, w);
524      }
525    }
526  }
527  WerrorS("coneViaPoints: unexpected parameters");
528  return TRUE;
529}
530
531BOOLEAN inequalities(leftv res, leftv args)
532{
533  gfan::initializeCddlibIfRequired();
534  leftv u = args;
535  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
536  {
537    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
538
539    gfan::ZMatrix zmat = zc->getInequalities();
540    res->rtyp = BIGINTMAT_CMD;
541    res->data = (void*) zMatrixToBigintmat(zmat);
542    return FALSE;
543  }
544  WerrorS("inequalities: unexpected parameters");
545  return TRUE;
546}
547
548BOOLEAN equations(leftv res, leftv args)
549{
550  gfan::initializeCddlibIfRequired();
551  leftv u = args;
552  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
553  {
554    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
555    gfan::ZMatrix zmat = zc->getEquations();
556    res->rtyp = BIGINTMAT_CMD;
557    res->data = (void*) zMatrixToBigintmat(zmat);
558    return FALSE;
559  }
560  WerrorS("equations: unexpected parameters");
561  return TRUE;
562}
563
564BOOLEAN facets(leftv res, leftv args)
565{
566  gfan::initializeCddlibIfRequired();
567  leftv u = args;
568  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
569  {
570    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
571    gfan::ZMatrix zm = zc->getFacets();
572    res->rtyp = BIGINTMAT_CMD;
573    res->data = (void*) zMatrixToBigintmat(zm);
574    return FALSE;
575  }
576  WerrorS("facets: unexpected parameters");
577  return TRUE;
578}
579
580BOOLEAN impliedEquations(leftv res, leftv args)
581{
582  gfan::initializeCddlibIfRequired();
583  leftv u = args;
584  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
585  {
586    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
587    gfan::ZMatrix zmat = zc->getImpliedEquations();
588    res->rtyp = BIGINTMAT_CMD;
589    res->data = (void*) zMatrixToBigintmat(zmat);
590    return FALSE;
591  }
592  WerrorS("span: unexpected parameters");
593  return TRUE;
594}
595
596BOOLEAN generatorsOfSpan(leftv res, leftv args)
597{
598  gfan::initializeCddlibIfRequired();
599  leftv u = args;
600  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
601  {
602    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
603    gfan::ZMatrix zmat = zc->generatorsOfSpan();
604    res->rtyp = BIGINTMAT_CMD;
605    res->data = (void*) zMatrixToBigintmat(zmat);
606    return FALSE;
607  }
608  WerrorS("generatorsOfSpan: unexpected parameters");
609  return TRUE;
610}
611
612BOOLEAN generatorsOfLinealitySpace(leftv res, leftv args)
613{
614  gfan::initializeCddlibIfRequired();
615  leftv u = args;
616  if ((u != NULL) && (u->Typ() == coneID || u->Typ() == polytopeID))
617  {
618    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
619    gfan::ZMatrix zmat = zc->generatorsOfLinealitySpace();
620    res->rtyp = BIGINTMAT_CMD;
621    res->data = (void*) zMatrixToBigintmat(zmat);
622    return FALSE;
623  }
624  WerrorS("generatorsOfLinealitySpace: unexpected parameters");
625  return TRUE;
626}
627
628BOOLEAN rays(leftv res, leftv args)
629{
630  gfan::initializeCddlibIfRequired();
631  leftv u = args;
632  if ((u != NULL) && (u->Typ() == coneID))
633  {
634    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
635    gfan::ZMatrix zm = zc->extremeRays();
636    res->rtyp = BIGINTMAT_CMD;
637    res->data = (void*)zMatrixToBigintmat(zm);
638    return FALSE;
639  }
640  if ((u != NULL) && (u->Typ() == fanID))
641  {
642    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
643    gfan::ZMatrix zmat = rays(zf);
644    res->rtyp = BIGINTMAT_CMD;
645    res->data = (void*) zMatrixToBigintmat(zmat);
646    return FALSE;
647  }
648  WerrorS("rays: unexpected parameters");
649  return TRUE;
650}
651
652BOOLEAN quotientLatticeBasis(leftv res, leftv args)
653{
654  gfan::initializeCddlibIfRequired();
655  leftv u = args;
656  if ((u != NULL) && (u->Typ() == coneID))
657  {
658    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
659    gfan::ZMatrix zmat = zc->quotientLatticeBasis();
660    res->rtyp = BIGINTMAT_CMD;
661    res->data = (void*) zMatrixToBigintmat(zmat);
662    return FALSE;
663  }
664  WerrorS("quotientLatticeBasis: unexpected parameters");
665  return TRUE;
666}
667
668BOOLEAN getLinearForms(leftv res, leftv args)
669{
670  gfan::initializeCddlibIfRequired();
671  leftv u = args;
672  if ((u != NULL) && (u->Typ() == coneID))
673  {
674    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
675    gfan::ZMatrix zmat = zc->getLinearForms();
676    res->rtyp = BIGINTMAT_CMD;
677    res->data = (void*) zMatrixToBigintmat(zmat);
678    return FALSE;
679  }
680  WerrorS("getLinearForms: unexpected parameters");
681  return TRUE;
682}
683
684BOOLEAN ambientDimension(leftv res, leftv args)
685{
686  gfan::initializeCddlibIfRequired();
687  leftv u=args;
688  if ((u != NULL) && (u->Typ() == coneID))
689  {
690    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
691    res->rtyp = INT_CMD;
692    res->data = (void*) (long) zc->ambientDimension();
693    return FALSE;
694  }
695  if ((u != NULL) && (u->Typ() == fanID))
696  {
697    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
698    res->rtyp = INT_CMD;
699    res->data = (void*) (long) getAmbientDimension(zf);
700    return FALSE;
701  }
702  if ((u != NULL) && (u->Typ() == polytopeID))
703  {
704    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
705    res->rtyp = INT_CMD;
706    res->data = (void*) (long) getAmbientDimension(zc);
707    return FALSE;
708  }
709  WerrorS("ambientDimension: unexpected parameters");
710  return TRUE;
711}
712
713BOOLEAN dimension(leftv res, leftv args)
714{
715  gfan::initializeCddlibIfRequired();
716  leftv u=args;
717  if ((u != NULL) && (u->Typ() == coneID))
718  {
719    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
720    res->rtyp = INT_CMD;
721    res->data = (void*) (long) zc->dimension();
722    return FALSE;
723  }
724  if ((u != NULL) && (u->Typ() == fanID))
725  {
726    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
727    res->rtyp = INT_CMD;
728    res->data = (void*) (long) getDimension(zf);
729    return FALSE;
730  }
731  if ((u != NULL) && (u->Typ() == polytopeID))
732  {
733    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
734    res->rtyp = INT_CMD;
735    res->data = (void*) (long) getDimension(zc);
736    return FALSE;
737  }
738  WerrorS("dimension: unexpected parameters");
739  return TRUE;
740}
741
742BOOLEAN codimension(leftv res, leftv args)
743{
744  gfan::initializeCddlibIfRequired();
745  leftv u=args;
746  if ((u != NULL) && (u->Typ() == coneID))
747    {
748      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
749      res->rtyp = INT_CMD;
750      res->data = (void*) (long) zc->codimension();
751      return FALSE;
752    }
753  if ((u != NULL) && (u->Typ() == fanID))
754    {
755      gfan::ZFan* zf = (gfan::ZFan*)u->Data();
756      res->rtyp = INT_CMD;
757      res->data = (void*) (long) getCodimension(zf);
758      return FALSE;
759    }
760  if ((u != NULL) && (u->Typ() == polytopeID))
761    {
762      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
763      res->rtyp = INT_CMD;
764      res->data = (void*) (long) getCodimension(zc);
765      return FALSE;
766    }
767  WerrorS("getCodimension: unexpected parameters");
768  return TRUE;
769}
770
771BOOLEAN linealityDimension(leftv res, leftv args)
772{
773  gfan::initializeCddlibIfRequired();
774  leftv u=args;
775  if ((u != NULL) && (u->Typ() == coneID))
776  {
777    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
778    res->rtyp = INT_CMD;
779    res->data = (void*) (long) zc->dimensionOfLinealitySpace();
780    return FALSE;
781  }
782  if ((u != NULL) && (u->Typ() == fanID))
783  {
784    gfan::ZFan* zf = (gfan::ZFan*)u->Data();
785    res->rtyp = INT_CMD;
786    res->data = (void*) (long) getLinealityDimension(zf);
787    return FALSE;
788  }
789  WerrorS("linealityDimension: unexpected parameters");
790  return TRUE;
791}
792
793BOOLEAN getMultiplicity(leftv res, leftv args)
794{
795  gfan::initializeCddlibIfRequired();
796  leftv u = args;
797  if ((u != NULL) && (u->Typ() == coneID))
798  {
799    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
800    number i = integerToNumber(zc->getMultiplicity());
801    res->rtyp = BIGINT_CMD;
802    res->data = (void*) i;
803    return FALSE;
804  }
805  WerrorS("getMultiplicity: unexpected parameters");
806  return TRUE;
807}
808
809BOOLEAN isOrigin(leftv res, leftv args)
810{
811  gfan::initializeCddlibIfRequired();
812  leftv u = args;
813  if ((u != NULL) && (u->Typ() == coneID))
814  {
815    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
816    int i = zc->isOrigin();
817    res->rtyp = INT_CMD;
818    res->data = (void*) (long) i;
819    return FALSE;
820  }
821  WerrorS("isOrigin: unexpected parameters");
822  return TRUE;
823}
824
825BOOLEAN isFullSpace(leftv res, leftv args)
826{
827  gfan::initializeCddlibIfRequired();
828  leftv u = args;
829  if ((u != NULL) && (u->Typ() == coneID))
830  {
831    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
832    int i = zc->isFullSpace();
833    res->rtyp = INT_CMD;
834    res->data = (void*) (long) i;
835    return FALSE;
836  }
837  WerrorS("isFullSpace: unexpected parameters");
838  return TRUE;
839}
840
841BOOLEAN isSimplicial(leftv res, leftv args)
842{
843  gfan::initializeCddlibIfRequired();
844  leftv u=args;
845  if ((u != NULL) && (u->Typ() == coneID))
846  {
847    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
848    int b = zc->isSimplicial();
849    res->rtyp = INT_CMD;
850    res->data = (void*) (long) b;
851    return FALSE;
852  }
853  if ((u != NULL) && (u->Typ() == fanID))
854  {
855    gfan::ZFan* zf = (gfan::ZFan*) u->Data();
856    bool b = isSimplicial(zf);
857    res->rtyp = INT_CMD;
858    res->data = (void*) (long) b;
859    return FALSE;
860  }
861  WerrorS("isSimplicial: unexpected parameters");
862  return TRUE;
863}
864
865BOOLEAN containsPositiveVector(leftv res, leftv args)
866{
867  gfan::initializeCddlibIfRequired();
868  leftv u = args;
869  if ((u != NULL) && (u->Typ() == coneID))
870  {
871    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
872    int i = zc->containsPositiveVector();
873    res->rtyp = INT_CMD;
874    res->data = (void*) (long) i;
875    return FALSE;
876  }
877  WerrorS("containsPositiveVector: unexpected parameters");
878  return TRUE;
879}
880
881BOOLEAN linealitySpace(leftv res, leftv args)
882{
883  gfan::initializeCddlibIfRequired();
884  leftv u = args;
885  if ((u != NULL) && (u->Typ() == coneID))
886  {
887    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
888    gfan::ZCone* zd = new gfan::ZCone(zc->linealitySpace());
889    res->rtyp = coneID;
890    res->data = (void*) zd;
891    return FALSE;
892  }
893  WerrorS("linealitySpace: unexpected parameters");
894  return TRUE;
895}
896
897BOOLEAN dualCone(leftv res, leftv args)
898{
899  gfan::initializeCddlibIfRequired();
900  leftv u = args;
901  if ((u != NULL) && (u->Typ() == coneID))
902  {
903    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
904    gfan::ZCone* zd = new gfan::ZCone(zc->dualCone());
905    res->rtyp = coneID;
906    res->data = (void*) zd;
907    return FALSE;
908  }
909  WerrorS("dual: unexpected parameters");
910  return TRUE;
911}
912
913BOOLEAN negatedCone(leftv res, leftv args)
914{
915  gfan::initializeCddlibIfRequired();
916  leftv u = args;
917  if ((u != NULL) && (u->Typ() == coneID))
918  {
919    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
920    gfan::ZCone* zd = new gfan::ZCone(zc->negated());
921    res->rtyp = coneID;
922    res->data = (void*) zd;
923    return FALSE;
924  }
925  WerrorS("negatedCone: unexpected parameters");
926  return TRUE;
927}
928
929BOOLEAN semigroupGenerator(leftv res, leftv args)
930{
931  gfan::initializeCddlibIfRequired();
932  leftv u = args;
933  if ((u != NULL) && (u->Typ() == coneID))
934  {
935    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
936    int d = zc->dimension();
937    int dLS = zc->dimensionOfLinealitySpace();
938    if (d == dLS + 1)
939    {
940      gfan::ZVector zv = zc->semiGroupGeneratorOfRay();
941      res->rtyp = BIGINTMAT_CMD;
942      res->data = (void*) zVectorToBigintmat(zv);
943      return FALSE;
944    }
945    Werror("expected dim of cone one larger than dim of lin space\n"
946            "but got dimensions %d and %d", d, dLS);
947  }
948  WerrorS("semigroupGenerator: unexpected parameters");
949  return TRUE;
950}
951
952BOOLEAN relativeInteriorPoint(leftv res, leftv args)
953{
954  gfan::initializeCddlibIfRequired();
955  leftv u = args;
956  if ((u != NULL) && (u->Typ() == coneID))
957  {
958    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
959    gfan::ZVector zv = zc->getRelativeInteriorPoint();
960    res->rtyp = BIGINTMAT_CMD;
961    res->data = (void*) zVectorToBigintmat(zv);
962    return FALSE;
963  }
964  WerrorS("relativeInteriorPoint: unexpected parameters");
965  return TRUE;
966}
967
968BOOLEAN uniquePoint(leftv res, leftv args)
969{
970  gfan::initializeCddlibIfRequired();
971  leftv u = args;
972  if ((u != NULL) && (u->Typ() == coneID))
973  {
974    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
975    gfan::ZVector zv = zc->getUniquePoint();
976    res->rtyp = BIGINTMAT_CMD;
977    res->data = (void*) zVectorToBigintmat(zv);
978    return FALSE;
979  }
980  WerrorS("uniquePoint: unexpected parameters");
981  return TRUE;
982}
983
984gfan::ZVector randomPoint(const gfan::ZCone* zc)
985{
986  gfan::ZVector rp = gfan::ZVector(zc->ambientDimension());
987
988  gfan::ZMatrix rays = zc->extremeRays();
989  for (int i=0; i<rays.getHeight(); i++)
990  {
991    int n = siRand();
992    rp = rp + n * rays[i].toVector();
993  }
994
995  gfan::ZMatrix lins = zc->generatorsOfLinealitySpace();
996  for (int i=0; i<lins.getHeight(); i++)
997  {
998    int n = siRand();
999    rp = rp + n * lins[i].toVector();
1000  }
1001
1002  return rp;
1003}
1004
1005BOOLEAN randomPoint(leftv res, leftv args)
1006{
1007  gfan::initializeCddlibIfRequired();
1008  leftv u = args;
1009  if ((u != NULL) && (u->Typ() == coneID))
1010  {
1011    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1012    gfan::ZVector zv = randomPoint(zc);
1013    res->rtyp = BIGINTMAT_CMD;
1014    res->data = (void*) zVectorToBigintmat(zv);
1015    return FALSE;
1016  }
1017  WerrorS("randomPoint: unexpected parameters");
1018  return TRUE;
1019}
1020
1021BOOLEAN setMultiplicity(leftv res, leftv args)
1022{
1023  gfan::initializeCddlibIfRequired();
1024  leftv u = args;
1025  if ((u != NULL) && (u->Typ() == coneID))
1026  {
1027    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1028    leftv v = u->next;
1029    if ((v != NULL) && (v->Typ() == INT_CMD))
1030    {
1031      int val = (int)(long)v->Data();
1032      zc->setMultiplicity(gfan::Integer(val));
1033      res->rtyp = NONE;
1034      res->data = NULL;
1035      return FALSE;
1036    }
1037  }
1038  WerrorS("setMultiplicity: unexpected parameters");
1039  return TRUE;
1040}
1041
1042BOOLEAN setLinearForms(leftv res, leftv args)
1043{
1044  gfan::initializeCddlibIfRequired();
1045  leftv u = args;
1046  if ((u != NULL) && (u->Typ() == coneID))
1047  {
1048    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1049    leftv v = u->next;
1050    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
1051    {
1052      bigintmat* mat=NULL;
1053      if (v->Typ() == INTVEC_CMD)
1054      {
1055        intvec* mat0 = (intvec*) v->Data();
1056        mat = iv2bim(mat0,coeffs_BIGINT)->transpose();
1057      }
1058      else
1059        mat = (bigintmat*) v->Data();
1060      gfan::ZMatrix* zm = bigintmatToZMatrix(mat);
1061      zc->setLinearForms(*zm);
1062      res->rtyp = NONE;
1063      res->data = NULL;
1064
1065      delete zm;
1066      if (v->Typ() == INTVEC_CMD)
1067        delete mat;
1068     return FALSE;
1069    }
1070  }
1071  WerrorS("setLinearForms: unexpected parameters");
1072  return TRUE;
1073}
1074
1075gfan::ZMatrix liftUp(const gfan::ZMatrix &zm)
1076{
1077  int r=zm.getHeight();
1078  int c=zm.getWidth();
1079  gfan::ZMatrix zn(r+1,c+1);
1080  zn[1][1]=1;
1081  for (int i=0; i<r; i++)
1082    for (int j=0; j<c; j++)
1083      zn[i+1][j+1]=zm[i][j];
1084  return zn;
1085}
1086
1087gfan::ZCone liftUp(const gfan::ZCone &zc)
1088{
1089  gfan::ZMatrix ineq=zc.getInequalities();
1090  gfan::ZMatrix eq=zc.getEquations();
1091  gfan::ZCone zd(liftUp(ineq),liftUp(eq));
1092  return zd;
1093}
1094
1095BOOLEAN coneToPolytope(leftv res, leftv args)
1096{
1097  gfan::initializeCddlibIfRequired();
1098  leftv u = args;
1099  if ((u != NULL) && (u->Typ() == coneID))
1100  {
1101    gfan::ZCone* zc = (gfan::ZCone*) u->Data();
1102    gfan::ZMatrix ineq=zc->getInequalities();
1103    gfan::ZMatrix eq=zc->getEquations();
1104    gfan::ZCone* zd = new gfan::ZCone(liftUp(ineq),liftUp(eq));
1105    res->rtyp = polytopeID;
1106    res->data = (void*) zd;
1107    return FALSE;
1108  }
1109  WerrorS("makePolytope: unexpected parameters");
1110  return TRUE;
1111}
1112
1113BOOLEAN intersectCones(leftv res, leftv args)
1114{
1115  gfan::initializeCddlibIfRequired();
1116  leftv u = args;
1117  if ((u != NULL) && (u->Typ() == coneID))
1118  {
1119    leftv v = u->next;
1120    if ((v != NULL) && (v->Typ() == coneID))
1121    {
1122      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1123      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1124      int d1 = zc1->ambientDimension();
1125      int d2 = zc2->ambientDimension();
1126      if (d1 != d2)
1127      {
1128        Werror("expected ambient dims of both cones to coincide\n"
1129                "but got %d and %d", d1, d2);
1130        return TRUE;
1131      }
1132      gfan::ZCone zc3 = gfan::intersection(*zc1, *zc2);
1133      zc3.canonicalize();
1134      res->rtyp = coneID;
1135      res->data = (void *)new gfan::ZCone(zc3);
1136      return FALSE;
1137    }
1138    if ((v != NULL) && (v->Typ() == polytopeID))
1139    {
1140      gfan::ZCone* zc11 = (gfan::ZCone*)u->Data();
1141      gfan::ZCone zc1 = liftUp(*zc11);
1142      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1143      int d1 = zc1.ambientDimension();
1144      int d2 = zc2->ambientDimension();
1145      if (d1 != d2)
1146      {
1147        Werror("expected ambient dims of both cones to coincide\n"
1148                "but got %d and %d", d1, d2);
1149        return TRUE;
1150      }
1151      gfan::ZCone zc3 = gfan::intersection(zc1, *zc2);
1152      zc3.canonicalize();
1153      res->rtyp = polytopeID;
1154      res->data = (void *)new gfan::ZCone(zc3);
1155      return FALSE;
1156    }
1157  }
1158  if ((u != NULL) && (u->Typ() == polytopeID))
1159  {
1160    leftv v = u->next;
1161    if ((v != NULL) && (v->Typ() == coneID))
1162    {
1163      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1164      gfan::ZCone* zc22 = (gfan::ZCone*)v->Data();
1165      gfan::ZCone zc2 = liftUp(*zc22);
1166      int d1 = zc1->ambientDimension();
1167      int d2 = zc2.ambientDimension();
1168      if (d1 != d2)
1169      {
1170        Werror("expected ambient dims of both cones to coincide\n"
1171                "but got %d and %d", d1, d2);
1172        return TRUE;
1173      }
1174      gfan::ZCone zc3 = gfan::intersection(*zc1, zc2);
1175      zc3.canonicalize();
1176      res->rtyp = polytopeID;
1177      res->data = (void *)new gfan::ZCone(zc3);
1178      return FALSE;
1179    }
1180    if ((v != NULL) && (v->Typ() == polytopeID))
1181    {
1182      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1183      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1184      int d1 = zc1->ambientDimension();
1185      int d2 = zc2->ambientDimension();
1186      if (d1 != d2)
1187      {
1188        Werror("expected ambient dims of both cones to coincide\n"
1189                "but got %d and %d", d1, d2);
1190        return TRUE;
1191      }
1192      gfan::ZCone zc3 = gfan::intersection(*zc1, *zc2);
1193      zc3.canonicalize();
1194      res->rtyp = polytopeID;
1195      res->data = (void *)new gfan::ZCone(zc3);
1196      return FALSE;
1197    }
1198  }
1199  WerrorS("convexIntersection: unexpected parameters");
1200  return TRUE;
1201}
1202
1203BOOLEAN convexHull(leftv res, leftv args)
1204{
1205  gfan::initializeCddlibIfRequired();
1206  leftv u = args;
1207  if ((u != NULL) && (u->Typ() == coneID))
1208  {
1209    leftv v = u->next;
1210    if ((v != NULL) && (v->Typ() == coneID))
1211    {
1212      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1213      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1214      int d1 = zc1->ambientDimension();
1215      int d2 = zc2->ambientDimension();
1216      if (d1 != d2)
1217      {
1218        Werror("expected ambient dims of both cones to coincide\n"
1219                "but got %d and %d", d1, d2);
1220        return TRUE;
1221      }
1222      gfan::ZMatrix zm1 = zc1->extremeRays();
1223      gfan::ZMatrix zm2 = zc2->extremeRays();
1224      gfan::ZMatrix zn1 = zc1->generatorsOfLinealitySpace();
1225      gfan::ZMatrix zn2 = zc2->generatorsOfLinealitySpace();
1226      gfan::ZMatrix zm = combineOnTop(zm1,zm2);
1227      gfan::ZMatrix zn = combineOnTop(zn1,zn2);
1228      gfan::ZCone* zc = new gfan::ZCone();
1229      *zc = gfan::ZCone::givenByRays(zm, zn);
1230      res->rtyp = coneID;
1231      res->data = (void*) zc;
1232      return FALSE;
1233    }
1234    if ((v != NULL) && (v->Typ() == polytopeID))
1235    {
1236      gfan::ZCone* zc11 = (gfan::ZCone*)u->Data();
1237      gfan::ZCone zc1 = liftUp(*zc11);
1238      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1239      int d1 = zc1.ambientDimension()-1;
1240      int d2 = zc2->ambientDimension()-1;
1241      if (d1 != d2)
1242      {
1243        Werror("expected ambient dims of both cones to coincide\n"
1244                "but got %d and %d", d1, d2);
1245        return TRUE;
1246      }
1247      gfan::ZMatrix zm1 = zc1.extremeRays();
1248      gfan::ZMatrix zm2 = zc2->extremeRays();
1249      gfan::ZMatrix zn = zc1.generatorsOfLinealitySpace();
1250      gfan::ZMatrix zm = combineOnTop(zm1,zm2);
1251      gfan::ZCone* zc = new gfan::ZCone();
1252      *zc = gfan::ZCone::givenByRays(zm, zn);
1253      res->rtyp = polytopeID;
1254      res->data = (void*) zc;
1255      return FALSE;
1256    }
1257  }
1258  if ((u != NULL) && (u->Typ() == polytopeID))
1259  {
1260    leftv v = u->next;
1261    if ((v != NULL) && (v->Typ() == coneID))
1262    {
1263      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1264      gfan::ZCone* zc22 = (gfan::ZCone*)v->Data();
1265      gfan::ZCone zc2 = liftUp(*zc22);
1266      int d1 = zc1->ambientDimension()-1;
1267      int d2 = zc2.ambientDimension()-1;
1268      if (d1 != d2)
1269      {
1270        Werror("expected ambient dims of both cones to coincide\n"
1271                "but got %d and %d", d1, d2);
1272        return TRUE;
1273      }
1274      gfan::ZMatrix zm1 = zc1->extremeRays();
1275      gfan::ZMatrix zm2 = zc2.extremeRays();
1276      gfan::ZMatrix zn = zc2.generatorsOfLinealitySpace();
1277      gfan::ZMatrix zm = combineOnTop(zm1,zm2);
1278      gfan::ZCone* zc = new gfan::ZCone();
1279      *zc = gfan::ZCone::givenByRays(zm,gfan::ZMatrix(0, zm.getWidth()));
1280      res->rtyp = polytopeID;
1281      res->data = (void*) zc;
1282      return FALSE;
1283    }
1284    if ((v != NULL) && (v->Typ() == polytopeID))
1285    {
1286      gfan::ZCone* zc1 = (gfan::ZCone*)u->Data();
1287      gfan::ZCone* zc2 = (gfan::ZCone*)v->Data();
1288      int d1 = zc1->ambientDimension()-1;
1289      int d2 = zc2->ambientDimension()-1;
1290      if (d1 != d2)
1291      {
1292        Werror("expected ambient dims of both cones to coincide\n"
1293                "but got %d and %d", d1, d2);
1294        return TRUE;
1295      }
1296      gfan::ZMatrix zm1 = zc1->extremeRays();
1297      gfan::ZMatrix zm2 = zc2->extremeRays();
1298      gfan::ZMatrix zm = combineOnTop(zm1,zm2);
1299      gfan::ZCone* zc = new gfan::ZCone();
1300      *zc = gfan::ZCone::givenByRays(zm,gfan::ZMatrix(0, zm.getWidth()));
1301      res->rtyp = polytopeID;
1302      res->data = (void*) zc;
1303      return FALSE;
1304    }
1305  }
1306  WerrorS("convexHull: unexpected parameters");
1307  return TRUE;
1308}
1309
1310BOOLEAN coneLink(leftv res, leftv args)
1311{
1312  gfan::initializeCddlibIfRequired();
1313  leftv u = args;
1314  if ((u != NULL) && (u->Typ() == coneID))
1315  {
1316    leftv v = u->next;
1317    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
1318    {
1319      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1320      bigintmat* iv = NULL;
1321      if (v->Typ() == INTVEC_CMD)
1322      {
1323        intvec* iv0 = (intvec*) v->Data();
1324        iv = iv2bim(iv0,coeffs_BIGINT)->transpose();
1325      }
1326      else
1327        iv = (bigintmat*)v->Data();
1328      gfan::ZVector* zv = bigintmatToZVector(iv);
1329      int d1 = zc->ambientDimension();
1330      int d2 = zv->size();
1331      if (d1 != d2)
1332      {
1333        Werror("expected ambient dim of cone and size of vector\n"
1334               " to be equal but got %d and %d", d1, d2);
1335        return TRUE;
1336      }
1337      if(!zc->contains(*zv))
1338      {
1339        WerrorS("the provided intvec does not lie in the cone");
1340        return TRUE;
1341      }
1342      gfan::ZCone* zd = new gfan::ZCone(zc->link(*zv));
1343      res->rtyp = coneID;
1344      res->data = (void *) zd;
1345
1346      delete zv;
1347      if (v->Typ() == INTMAT_CMD)
1348        delete iv;
1349      return FALSE;
1350    }
1351  }
1352  WerrorS("coneLink: unexpected parameters");
1353  return TRUE;
1354}
1355
1356BOOLEAN containsInSupport(leftv res, leftv args)
1357{
1358  gfan::initializeCddlibIfRequired();
1359  leftv u=args;
1360  if ((u != NULL) && (u->Typ() == coneID))
1361  {
1362    leftv v=u->next;
1363    if ((v != NULL) && (v->Typ() == coneID))
1364    {
1365      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1366      gfan::ZCone* zd = (gfan::ZCone*)v->Data();
1367      int d1 = zc->ambientDimension();
1368      int d2 = zd->ambientDimension();
1369      if (d1 != d2)
1370      {
1371        Werror("expected cones with same ambient dimensions\n but got"
1372               " dimensions %d and %d", d1, d2);
1373        return TRUE;
1374      }
1375      bool b = (zc->contains(*zd) ? 1 : 0);
1376      res->rtyp = INT_CMD;
1377      res->data = (void*) (long) b;
1378      return FALSE;
1379    }
1380    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
1381    {
1382      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1383      bigintmat* iv = NULL;
1384      if (v->Typ() == INTVEC_CMD)
1385      {
1386        intvec* iv0 = (intvec*) v->Data();
1387        iv = iv2bim(iv0,coeffs_BIGINT)->transpose();
1388      }
1389      else
1390        iv = (bigintmat*)v->Data();
1391
1392      gfan::ZVector* zv = bigintmatToZVector(iv);
1393      int d1 = zc->ambientDimension();
1394      int d2 = zv->size();
1395      if (d1 != d2)
1396      {
1397        Werror("expected cones with same ambient dimensions\n but got"
1398               " dimensions %d and %d", d1, d2);
1399        return TRUE;
1400      }
1401      int b = zc->contains(*zv);
1402      res->rtyp = INT_CMD;
1403      res->data = (void*) (long) b;
1404
1405      delete zv;
1406      if (v->Typ() == INTMAT_CMD)
1407        delete iv;
1408      return FALSE;
1409    }
1410  }
1411  WerrorS("containsInSupport: unexpected parameters");
1412  return TRUE;
1413}
1414
1415BOOLEAN containsRelatively(leftv res, leftv args)
1416{
1417  gfan::initializeCddlibIfRequired();
1418  leftv u = args;
1419  if ((u != NULL) && (u->Typ() == coneID))
1420  {
1421    leftv v = u->next;
1422    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
1423    {
1424      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1425      bigintmat* iv = NULL;
1426      if (v->Typ() == INTVEC_CMD)
1427      {
1428        intvec* iv0 = (intvec*) v->Data();
1429        iv = iv2bim(iv0,coeffs_BIGINT)->transpose();
1430      }
1431      else
1432        iv = (bigintmat*)v->Data();
1433      gfan::ZVector* zv = bigintmatToZVector(iv);
1434      int d1 = zc->ambientDimension();
1435      int d2 = zv->size();
1436      if (d1 == d2)
1437      {
1438        bool b = (zc->containsRelatively(*zv) ? 1 : 0);
1439        res->rtyp = INT_CMD;
1440        res->data = (void *) b;
1441        delete zv;
1442        if (v->Typ() == INTMAT_CMD)
1443          delete iv;
1444        return FALSE;
1445      }
1446      delete zv;
1447      if (v->Typ() == INTMAT_CMD)
1448        delete iv;
1449      Werror("expected ambient dim of cone and size of vector\n"
1450             "to be equal but got %d and %d", d1, d2);
1451    }
1452  }
1453  WerrorS("containsRelatively: unexpected parameters");
1454  return TRUE;
1455}
1456
1457BOOLEAN hasFace(leftv res, leftv args)
1458{
1459  gfan::initializeCddlibIfRequired();
1460  leftv u=args;
1461  if ((u != NULL) && (u->Typ() == coneID))
1462  {
1463    leftv v=u->next;
1464    if ((v != NULL) && (v->Typ() == coneID))
1465    {
1466      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1467      gfan::ZCone* zd = (gfan::ZCone*)v->Data();
1468      bool b = zc->hasFace(*zd);
1469      res->rtyp = INT_CMD;
1470      res->data = (void*) (long) b;
1471      return FALSE;
1472    }
1473  }
1474  if ((u != NULL) && (u->Typ() == polytopeID))
1475  {
1476    leftv v=u->next;
1477    if ((v != NULL) && (v->Typ() == polytopeID))
1478    {
1479      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1480      gfan::ZCone* zd = (gfan::ZCone*)v->Data();
1481      bool b = zc->hasFace(*zd);
1482      res->rtyp = INT_CMD;
1483      res->data = (void*) (long) b;
1484      return FALSE;
1485    }
1486  }
1487  WerrorS("containsAsFace: unexpected parameters");
1488  return TRUE;
1489}
1490
1491BOOLEAN canonicalizeCone(leftv res, leftv args)
1492{
1493  gfan::initializeCddlibIfRequired();
1494  leftv u=args;
1495  if ((u != NULL) && (u->Typ() == coneID))
1496  {
1497    gfan::ZCone* zc = (gfan::ZCone*)u->Data();
1498    gfan::ZCone* zd = new gfan::ZCone(*zc);
1499    zd->canonicalize();
1500    res->rtyp = coneID;
1501    res->data = (void*) zd;
1502    return FALSE;
1503  }
1504  WerrorS("canonicalizeCone: unexpected parameters");
1505  return TRUE;
1506}
1507
1508BOOLEAN containsCone(leftv res, leftv args)
1509{
1510  gfan::initializeCddlibIfRequired();
1511  leftv u=args;
1512  if ((u != NULL) && (u->Typ() == LIST_CMD))
1513  {
1514    leftv v=u->next;
1515    if ((v != NULL) && (v->Typ() == coneID))
1516    {
1517      lists l = (lists) u->Data();
1518      gfan::ZCone* zc = (gfan::ZCone*) v->Data();
1519      zc->canonicalize();
1520      int b = 0;
1521      for (int i=0; i<=lSize(l); i++)
1522      {
1523        if (l->m[i].Typ() != coneID)
1524        {
1525          WerrorS("containsCone: entries of wrong type in list");
1526          return TRUE;
1527        }
1528        gfan::ZCone* ll = (gfan::ZCone*) l->m[i].Data();
1529        ll->canonicalize();
1530        if (!((*ll) != (*zc)))
1531        {
1532          b = 1;
1533          break;
1534        }
1535      }
1536      res->rtyp = INT_CMD;
1537      res->data = (char*) (long) b;
1538      return FALSE;
1539    }
1540  }
1541  WerrorS("containsCone: unexpected parameters");
1542  return TRUE;
1543}
1544
1545BOOLEAN faceContaining(leftv res, leftv args)
1546{
1547  gfan::initializeCddlibIfRequired();
1548  leftv u = args;
1549  if ((u != NULL) && (u->Typ() == coneID))
1550  {
1551    leftv v = u->next;
1552    if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
1553    {
1554      gfan::ZCone* zc = (gfan::ZCone*) u->Data();
1555
1556      bigintmat* point1;
1557      if (v->Typ() == INTVEC_CMD)
1558      {
1559        intvec* point0 = (intvec*) v->Data();
1560        point1 = iv2bim(point0,coeffs_BIGINT)->transpose();
1561      }
1562      else
1563        point1 = (bigintmat*) v->Data();
1564      gfan::ZVector* point = bigintmatToZVector(*point1);
1565
1566      if (!zc->contains(*point))
1567      {
1568        WerrorS("faceContaining: point not in cone");
1569        return TRUE;
1570      }
1571      res->rtyp = coneID;
1572      res->data = (void*) new gfan::ZCone(zc->faceContaining(*point));
1573
1574      delete point;
1575      if (v->Typ() == INTVEC_CMD)
1576        delete point1;
1577      return FALSE;
1578    }
1579  }
1580  WerrorS("faceContaining: unexpected parameters");
1581  return TRUE;
1582}
1583
1584
1585/***
1586 * Computes a relative interior point for each facet of zc
1587 **/
1588gfan::ZMatrix interiorPointsOfFacets(const gfan::ZCone &zc, const std::set<gfan::ZVector> &exceptThese)
1589{
1590  gfan::ZMatrix inequalities = zc.getFacets();
1591  gfan::ZMatrix equations = zc.getImpliedEquations();
1592  int r = inequalities.getHeight();
1593  int c = inequalities.getWidth();
1594
1595  /* our cone has r facets, if r==0 return empty matrices */
1596  gfan::ZMatrix relativeInteriorPoints = gfan::ZMatrix(0,c);
1597  if (r==0) return relativeInteriorPoints;
1598
1599  /* next we iterate over each of the r facets,
1600   * build the respective cone and add it to the list
1601   * this is the i=0 case */
1602  gfan::ZMatrix newInequalities = inequalities.submatrix(1,0,r,c);
1603  gfan::ZMatrix newEquations = equations;
1604  newEquations.appendRow(inequalities[0]);
1605  gfan::ZCone facet = gfan::ZCone(newInequalities,newEquations);
1606  facet.canonicalize();
1607  gfan::ZVector interiorPoint = facet.getRelativeInteriorPoint();
1608  if (exceptThese.count(interiorPoint)==0)
1609    relativeInteriorPoints.appendRow(interiorPoint);
1610
1611  /* these are the cases i=1,...,r-2 */
1612  for (int i=1; i<r-1; i++)
1613  {
1614    newInequalities = inequalities.submatrix(0,0,i,c);
1615    newInequalities.append(inequalities.submatrix(i+1,0,r,c));
1616    newEquations = equations;
1617    newEquations.appendRow(inequalities[i]);
1618    facet = gfan::ZCone(newInequalities,newEquations);
1619    facet.canonicalize();
1620    interiorPoint = facet.getRelativeInteriorPoint();
1621    if (exceptThese.count(interiorPoint)==0)
1622      relativeInteriorPoints.appendRow(interiorPoint);
1623  }
1624
1625  /* this is the i=r-1 case */
1626  newInequalities = inequalities.submatrix(0,0,r-1,c);
1627  newEquations = equations;
1628  newEquations.appendRow(inequalities[r-1]);
1629  facet = gfan::ZCone(newInequalities,newEquations);
1630  facet.canonicalize();
1631  interiorPoint = facet.getRelativeInteriorPoint();
1632  if (exceptThese.count(interiorPoint)==0)
1633    relativeInteriorPoints.appendRow(interiorPoint);
1634
1635  return relativeInteriorPoints;
1636}
1637
1638
1639/***
1640 * Computes a relative interior point and an outer normal vector for each facet of zc
1641 **/
1642std::pair<gfan::ZMatrix,gfan::ZMatrix> interiorPointsAndNormalsOfFacets(const gfan::ZCone zc, const std::set<gfan::ZVector> &exceptThesePoints, const bool onlyLowerHalfSpace)
1643{
1644  gfan::ZMatrix inequalities = zc.getFacets();
1645  gfan::ZMatrix equations = zc.getImpliedEquations();
1646  int r = inequalities.getHeight();
1647  int c = inequalities.getWidth();
1648
1649  /* our cone has r facets, if r==0 return empty matrices */
1650  gfan::ZMatrix relativeInteriorPoints = gfan::ZMatrix(0,c);
1651  gfan::ZMatrix outerFacetNormals = gfan::ZMatrix(0,c);
1652  if (r==0)
1653    return std::make_pair(relativeInteriorPoints,outerFacetNormals);
1654
1655  /* next we iterate over each of the r facets,
1656   * build the respective cone and add it to the list
1657   * this is the i=0 case */
1658  gfan::ZMatrix newInequalities = inequalities.submatrix(1,0,r,c);
1659  gfan::ZMatrix newEquations = equations;
1660  newEquations.appendRow(inequalities[0]);
1661  gfan::ZCone facet = gfan::ZCone(newInequalities,newEquations);
1662  gfan::ZVector interiorPoint = facet.getRelativeInteriorPoint();
1663  if (onlyLowerHalfSpace==false || interiorPoint[0].sign()<0)
1664  {
1665    if (exceptThesePoints.count(interiorPoint)==0)
1666    {
1667      relativeInteriorPoints.appendRow(interiorPoint);
1668      outerFacetNormals.appendRow(-inequalities[0].toVector());
1669    }
1670  }
1671
1672  /* these are the cases i=1,...,r-2 */
1673  for (int i=1; i<r-1; i++)
1674  {
1675    newInequalities = inequalities.submatrix(0,0,i,c);
1676    newInequalities.append(inequalities.submatrix(i+1,0,r,c));
1677    newEquations = equations;
1678    newEquations.appendRow(inequalities[i]);
1679    facet = gfan::ZCone(newInequalities,newEquations);
1680    interiorPoint = facet.getRelativeInteriorPoint();
1681    if (onlyLowerHalfSpace==false || interiorPoint[0].sign()<0)
1682    {
1683      if (exceptThesePoints.count(interiorPoint)==0)
1684      {
1685        relativeInteriorPoints.appendRow(interiorPoint);
1686        outerFacetNormals.appendRow(-inequalities[i].toVector());
1687      }
1688    }
1689  }
1690
1691  /* this is the i=r-1 case */
1692  newInequalities = inequalities.submatrix(0,0,r-1,c);
1693  newEquations = equations;
1694  newEquations.appendRow(inequalities[r-1]);
1695  facet = gfan::ZCone(newInequalities,newEquations);
1696  interiorPoint = facet.getRelativeInteriorPoint();
1697  if (onlyLowerHalfSpace==false || interiorPoint[0].sign()<0)
1698  {
1699    if (exceptThesePoints.count(interiorPoint)==0)
1700    {
1701      relativeInteriorPoints.appendRow(interiorPoint);
1702      outerFacetNormals.appendRow(-inequalities[r-1].toVector());
1703    }
1704  }
1705
1706  return std::make_pair(relativeInteriorPoints,outerFacetNormals);
1707}
1708
1709
1710static void gfanIntegerWriteFd(gfan::Integer n, ssiInfo* dd)
1711{
1712  mpz_t tmp;
1713  mpz_init(tmp);
1714  n.setGmp(tmp);
1715  mpz_out_str (dd->f_write,SSI_BASE, tmp);
1716  mpz_clear(tmp);
1717  fputc(' ',dd->f_write);
1718}
1719
1720static void gfanZMatrixWriteFd(gfan::ZMatrix M, ssiInfo* dd)
1721{
1722  fprintf(dd->f_write,"%d %d ",M.getHeight(),M.getWidth());
1723
1724  for (int i=0; i<M.getHeight(); i++)
1725  {
1726    for (int j=0; j<M.getWidth(); j++)
1727    {
1728      gfanIntegerWriteFd(M[i][j],dd);
1729    }
1730  }
1731}
1732
1733BOOLEAN bbcone_serialize(blackbox *b, void *d, si_link f)
1734{
1735  ssiInfo *dd = (ssiInfo *)f->data;
1736
1737  sleftv l;
1738  memset(&l,0,sizeof(l));
1739  l.rtyp=STRING_CMD;
1740  l.data=(void*)"cone";
1741  f->m->Write(f, &l);
1742
1743  gfan::ZCone *Z = (gfan::ZCone*) d;
1744  fprintf(dd->f_write,"%d ",Z->areImpliedEquationsKnown()+Z->areFacetsKnown()*2);
1745
1746  gfan::ZMatrix i=Z->getInequalities();
1747  gfanZMatrixWriteFd(i,dd);
1748
1749  gfan::ZMatrix e=Z->getEquations();
1750  gfanZMatrixWriteFd(e,dd);
1751
1752  // assert(i.getWidth() == e.getWidth());
1753  return FALSE;
1754}
1755
1756static gfan::Integer gfanIntegerReadFd(ssiInfo* dd)
1757{
1758  mpz_t tmp;
1759  mpz_init(tmp);
1760  s_readmpz_base(dd->f_read,tmp,SSI_BASE);
1761  gfan::Integer n(tmp);
1762  mpz_clear(tmp);
1763  return n;
1764}
1765
1766static gfan::ZMatrix gfanZMatrixReadFd(ssiInfo* dd)
1767{
1768  int r=s_readint(dd->f_read);
1769  int c=s_readint(dd->f_read);
1770
1771  gfan::ZMatrix M(r,c);
1772  for (int i=0; i<r; i++)
1773  {
1774    for (int j=0; j<c; j++)
1775    {
1776      M[i][j] = gfanIntegerReadFd(dd);
1777    }
1778  }
1779  return M;
1780}
1781
1782BOOLEAN bbcone_deserialize(blackbox **b, void **d, si_link f)
1783{
1784  ssiInfo *dd = (ssiInfo *)f->data;
1785  int preassumptions = s_readint(dd->f_read);
1786
1787  gfan::ZMatrix i = gfanZMatrixReadFd(dd);
1788  gfan::ZMatrix e = gfanZMatrixReadFd(dd);
1789
1790  // if (e.getHeight()==0) // why is e sometimes 0x0 and sometimex 0xn???
1791  //   e = gfan::ZMatrix(0,i.getWidth());
1792
1793  gfan::ZCone* Z = new gfan::ZCone(i,e,preassumptions);
1794
1795  *d=Z;
1796  return FALSE;
1797}
1798
1799void bbcone_setup(SModulFunctions* p)
1800{
1801  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
1802  // all undefined entries will be set to default in setBlackboxStuff
1803  // the default Print is quite usefull,
1804  // all other are simply error messages
1805  b->blackbox_destroy=bbcone_destroy;
1806  b->blackbox_String=bbcone_String;
1807  // b->blackbox_Print=blackbox_default_Print;
1808  b->blackbox_Init=bbcone_Init;
1809  b->blackbox_Copy=bbcone_Copy;
1810  b->blackbox_Assign=bbcone_Assign;
1811  b->blackbox_Op2=bbcone_Op2;
1812  b->blackbox_serialize=bbcone_serialize;
1813  b->blackbox_deserialize=bbcone_deserialize;
1814  p->iiAddCproc("gfan.lib","coneViaInequalities",FALSE,coneViaNormals);
1815  p->iiAddCproc("gfan.lib","coneViaPoints",FALSE,coneViaRays);
1816  p->iiAddCproc("","listContainsCone",FALSE,containsCone);
1817  // iiAddCproc("gfan.lib","makePolytope",FALSE,coneToPolytope);
1818  p->iiAddCproc("gfan.lib","ambientDimension",FALSE,ambientDimension);
1819  p->iiAddCproc("gfan.lib","canonicalizeCone",FALSE,canonicalizeCone);
1820  p->iiAddCproc("gfan.lib","codimension",FALSE,codimension);
1821  p->iiAddCproc("gfan.lib","coneLink",FALSE,coneLink);
1822  p->iiAddCproc("gfan.lib","containsAsFace",FALSE,hasFace);
1823  p->iiAddCproc("gfan.lib","containsInSupport",FALSE,containsInSupport);
1824  p->iiAddCproc("gfan.lib","containsPositiveVector",FALSE,containsPositiveVector);
1825  p->iiAddCproc("gfan.lib","containsRelatively",FALSE,containsRelatively);
1826  p->iiAddCproc("gfan.lib","convexHull",FALSE,convexHull);
1827  p->iiAddCproc("gfan.lib","convexIntersection",FALSE,intersectCones);
1828  p->iiAddCproc("gfan.lib","dimension",FALSE,dimension);
1829  p->iiAddCproc("gfan.lib","dualCone",FALSE,dualCone);
1830  p->iiAddCproc("gfan.lib","equations",FALSE,equations);
1831  p->iiAddCproc("gfan.lib","facets",FALSE,facets);
1832  p->iiAddCproc("gfan.lib","generatorsOfLinealitySpace",FALSE,generatorsOfLinealitySpace);
1833  p->iiAddCproc("gfan.lib","generatorsOfSpan",FALSE,generatorsOfSpan);
1834  p->iiAddCproc("gfan.lib","getLinearForms",FALSE,getLinearForms);
1835  p->iiAddCproc("gfan.lib","getMultiplicity",FALSE,getMultiplicity);
1836  p->iiAddCproc("gfan.lib","inequalities",FALSE,inequalities);
1837  p->iiAddCproc("gfan.lib","isFullSpace",FALSE,isFullSpace);
1838  p->iiAddCproc("gfan.lib","isOrigin",FALSE,isOrigin);
1839  p->iiAddCproc("gfan.lib","isSimplicial",FALSE,isSimplicial);
1840  p->iiAddCproc("gfan.lib","linealityDimension",FALSE,linealityDimension);
1841  p->iiAddCproc("gfan.lib","linealitySpace",FALSE,linealitySpace);
1842  p->iiAddCproc("gfan.lib","negatedCone",FALSE,negatedCone);
1843  p->iiAddCproc("gfan.lib","quotientLatticeBasis",FALSE,quotientLatticeBasis);
1844  p->iiAddCproc("gfan.lib","randomPoint",FALSE,randomPoint);
1845  p->iiAddCproc("gfan.lib","rays",FALSE,rays);
1846  p->iiAddCproc("gfan.lib","relativeInteriorPoint",FALSE,relativeInteriorPoint);
1847  p->iiAddCproc("gfan.lib","semigroupGenerator",FALSE,semigroupGenerator);
1848  p->iiAddCproc("gfan.lib","setLinearForms",FALSE,setLinearForms);
1849  p->iiAddCproc("gfan.lib","setMultiplicity",FALSE,setMultiplicity);
1850  p->iiAddCproc("gfan.lib","span",FALSE,impliedEquations);
1851  p->iiAddCproc("gfan.lib","uniquePoint",FALSE,uniquePoint);
1852  p->iiAddCproc("gfan.lib","faceContaining",FALSE,faceContaining);
1853  coneID=setBlackboxStuff(b,"cone");
1854}
1855
1856#endif
Note: See TracBrowser for help on using the repository browser.