source: git/dyn_modules/callgfanlib/bbcone.cc @ e44b149

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