source: git/dyn_modules/callgfanlib/bbcone.cc @ 5417ff

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