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

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