source: git/Singular/dyn_modules/gfanlib/bbcone.cc @ 5d0cbd

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