source: git/Singular/dyn_modules/gfanlib/bbpolytope.cc @ c89014

spielwiese
Last change on this file since c89014 was c89014, checked in by Yue Ren <ren@…>, 10 years ago
status update 28.02.
  • Property mode set to 100644
File size: 12.7 KB
Line 
1#include <kernel/mod2.h>
2
3#if HAVE_GFANLIB
4
5#include <Singular/ipid.h>
6#include <Singular/ipshell.h>
7#include <Singular/blackbox.h>
8#include <misc/intvec.h>
9#include <coeffs/bigintmat.h>
10
11#include <callgfanlib_conversion.h>
12#include <sstream>
13
14#include <gfanlib/gfanlib.h>
15#include <gfanlib/gfanlib_q.h>
16
17int polytopeID;
18
19std::string bbpolytopeToString(gfan::ZCone const &c)
20{
21  std::stringstream s;
22  gfan::ZMatrix i=c.getInequalities();
23  gfan::ZMatrix e=c.getEquations();
24  s<<"AMBIENT_DIM"<<std::endl;
25  s<<c.ambientDimension()-1<<std::endl;
26  s<<"INEQUALITIES"<<std::endl;
27  s<<toString(i)<<std::endl;
28  s<<"EQUATIONS"<<std::endl;
29  s<<toString(e)<<std::endl;
30  return s.str();
31}
32
33void *bbpolytope_Init(blackbox* /*b*/)
34{
35  return (void*)(new gfan::ZCone());
36}
37
38BOOLEAN bbpolytope_Assign(leftv l, leftv r)
39{
40  gfan::ZCone* newZc;
41  if (r==NULL)
42  {
43    if (l->Data()!=NULL)
44    {
45      gfan::ZCone* zd = (gfan::ZCone*)l->Data();
46      delete zd;
47    }
48    newZc = new gfan::ZCone();
49  }
50  else if (r->Typ()==l->Typ())
51  {
52    if (l->Data()!=NULL)
53    {
54      gfan::ZCone* zd = (gfan::ZCone*)l->Data();
55      delete zd;
56    }
57    gfan::ZCone* zc = (gfan::ZCone*)r->Data();
58    newZc = new gfan::ZCone(*zc);
59  }
60  // else if (r->Typ()==INT_CMD)  TODO:r->Typ()==BIGINTMAT_CMD
61  // {
62  //   int ambientDim = (int)(long)r->Data();
63  //   if (ambientDim < 0)
64  //   {
65  //     Werror("expected an int >= 0, but got %d", ambientDim);
66  //     return TRUE;
67  //   }
68  //   if (l->Data()!=NULL)
69  //   {
70  //     gfan::ZCone* zd = (gfan::ZCone*)l->Data();
71  //     delete zd;
72  //   }
73  //   newZc = new gfan::ZCone(ambientDim);
74  // }
75  else
76  {
77    Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ());
78    return TRUE;
79  }
80
81  if (l->rtyp==IDHDL)
82  {
83    IDDATA((idhdl)l->data) = (char*) newZc;
84  }
85  else
86  {
87    l->data=(void *)newZc;
88  }
89  return FALSE;
90}
91
92char* bbpolytope_String(blackbox* /*b*/, void *d)
93{ if (d==NULL) return omStrDup("invalid object");
94   else
95   {
96     gfan::ZCone* zc = (gfan::ZCone*)d;
97     std::string s=bbpolytopeToString(*zc);
98     return omStrDup(s.c_str());
99   }
100}
101
102void bbpolytope_destroy(blackbox* /*b*/, void *d)
103{
104  if (d!=NULL)
105  {
106    gfan::ZCone* zc = (gfan::ZCone*) d;
107    delete zc;
108  }
109}
110
111void* bbpolytope_Copy(blackbox* /*b*/, void *d)
112{
113  gfan::ZCone* zc = (gfan::ZCone*)d;
114  gfan::ZCone* newZc = new gfan::ZCone(*zc);
115  return newZc;
116}
117
118static BOOLEAN ppCONERAYS1(leftv res, leftv v)
119{
120  /* method for generating a cone object from half-lines
121     (cone = convex hull of the half-lines; note: there may be
122     entire lines in the cone);
123     valid parametrizations: (bigintmat) */
124  bigintmat* rays = NULL;
125  if (v->Typ() == INTMAT_CMD)
126  {
127    intvec* rays0 = (intvec*) v->Data();
128    rays = iv2bim(rays0,coeffs_BIGINT);
129  }
130  else
131    rays = (bigintmat*) v->Data();
132
133  gfan::ZMatrix* zm = bigintmatToZMatrix(rays);
134  gfan::ZCone* zc = new gfan::ZCone();
135  *zc = gfan::ZCone::givenByRays(*zm, gfan::ZMatrix(0, zm->getWidth()));
136  res->rtyp = polytopeID;
137  res->data = (void*) zc;
138
139  delete zm;
140  if (v->Typ() == INTMAT_CMD)
141    delete rays;
142  return FALSE;
143}
144
145static BOOLEAN ppCONERAYS3(leftv res, leftv u, leftv v)
146{
147  /* method for generating a cone object from half-lines
148     (any point in the cone being the sum of a point
149     in the convex hull of the half-lines and a point in the span
150     of the lines), and an integer k;
151     valid parametrizations: (bigintmat, int);
152     Errors will be invoked in the following cases:
153     - k not 0 or 1;
154     if the k=1, then the extreme rays are known:
155     each half-line spans a (different) extreme ray */
156  bigintmat* rays = NULL;
157  if (u->Typ() == INTMAT_CMD)
158  {
159    intvec* rays0 = (intvec*) u->Data();
160    rays = iv2bim(rays0,coeffs_BIGINT);
161  }
162  else
163    rays = (bigintmat*) u->Data();
164  int k = (int)(long)v->Data();
165
166  if ((k < 0) || (k > 1))
167  {
168    WerrorS("expected int argument in [0..1]");
169    return TRUE;
170  }
171  k=k*2;
172  gfan::ZMatrix* zm = bigintmatToZMatrix(rays);
173  gfan::ZCone* zc = new gfan::ZCone();
174  *zc = gfan::ZCone::givenByRays(*zm,gfan::ZMatrix(0, zm->getWidth()));
175  //k should be passed on to zc; not available yet
176  res->rtyp = polytopeID;
177  res->data = (void*) zc;
178
179  delete zm;
180  if (v->Typ() == INTMAT_CMD)
181    delete rays;
182  return FALSE;
183}
184
185BOOLEAN polytopeViaVertices(leftv res, leftv args)
186{
187  leftv u = args;
188  if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
189  {
190    if (u->next == NULL) return ppCONERAYS1(res, u);
191    leftv v = u->next;
192    if ((v != NULL) && (v->Typ() == INT_CMD))
193    {
194      if (v->next == NULL) return ppCONERAYS3(res, u, v);
195    }
196  }
197  WerrorS("polytopeViaPoints: unexpected parameters");
198  return TRUE;
199}
200
201static BOOLEAN ppCONENORMALS1(leftv res, leftv v)
202{
203  /* method for generating a cone object from inequalities;
204     valid parametrizations: (bigintmat) */
205  bigintmat* ineq = NULL;
206  if (v->Typ() == INTMAT_CMD)
207  {
208    intvec* ineq0 = (intvec*) v->Data();
209    ineq = iv2bim(ineq0,coeffs_BIGINT);
210  }
211  else
212    ineq = (bigintmat*) v->Data();
213  gfan::ZMatrix* zm = bigintmatToZMatrix(ineq);
214  gfan::ZCone* zc = new gfan::ZCone(*zm, gfan::ZMatrix(0, zm->getWidth()));
215  delete zm;
216  if (v->Typ() == INTMAT_CMD)
217    delete ineq;
218  res->rtyp = polytopeID;
219  res->data = (void*) zc;
220  return FALSE;
221}
222
223static BOOLEAN ppCONENORMALS2(leftv res, leftv u, leftv v)
224{
225  /* method for generating a cone object from iequalities,
226     and equations (...)
227     valid parametrizations: (bigintmat, bigintmat)
228     Errors will be invoked in the following cases:
229     - u and v have different numbers of columns */
230  bigintmat* ineq = NULL; bigintmat* eq = NULL;
231  if (u->Typ() == INTMAT_CMD)
232  {
233    intvec* ineq0 = (intvec*) u->Data();
234    ineq = iv2bim(ineq0,coeffs_BIGINT);
235  }
236  else
237    ineq = (bigintmat*) u->Data();
238  if (v->Typ() == INTMAT_CMD)
239  {
240    intvec* eq0 = (intvec*) v->Data();
241    eq = iv2bim(eq0,coeffs_BIGINT);
242  }
243  else
244    eq = (bigintmat*) v->Data();
245
246  if (ineq->cols() != eq->cols())
247  {
248    Werror("expected same number of columns but got %d vs. %d",
249           ineq->cols(), eq->cols());
250    return TRUE;
251  }
252  gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
253  gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
254  gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2);
255  delete zm1;
256  delete zm2;
257  if (u->Typ() == INTMAT_CMD)
258    delete ineq;
259  if (v->Typ() == INTMAT_CMD)
260    delete eq;
261
262  res->rtyp = polytopeID;
263  res->data = (void*) zc;
264  return FALSE;
265}
266
267static BOOLEAN ppCONENORMALS3(leftv res, leftv u, leftv v, leftv w)
268{
269  /* method for generating a cone object from inequalities, equations,
270     and an integer k;
271     valid parametrizations: (bigintmat, bigintmat, int);
272     Errors will be invoked in the following cases:
273     - u and v have different numbers of columns,
274     - k not in [0..3];
275     if the 2^0-bit of k is set, then ... */
276  bigintmat* ineq = NULL; bigintmat* eq = NULL;
277  if (u->Typ() == INTMAT_CMD)
278  {
279    intvec* ineq0 = (intvec*) u->Data();
280    ineq = iv2bim(ineq0,coeffs_BIGINT);
281  }
282  else
283    ineq = (bigintmat*) u->Data();
284  if (v->Typ() == INTMAT_CMD)
285  {
286    intvec* eq0 = (intvec*) v->Data();
287    eq = iv2bim(eq0,coeffs_BIGINT);
288  }
289  else
290    eq = (bigintmat*) v->Data();
291
292  if (ineq->cols() != eq->cols())
293  {
294    Werror("expected same number of columns but got %d vs. %d",
295           ineq->cols(), eq->cols());
296    return TRUE;
297  }
298  int k = (int)(long)w->Data();
299  if ((k < 0) || (k > 3))
300  {
301    WerrorS("expected int argument in [0..3]");
302    return TRUE;
303  }
304  gfan::ZMatrix* zm1 = bigintmatToZMatrix(ineq);
305  gfan::ZMatrix* zm2 = bigintmatToZMatrix(eq);
306  gfan::ZCone* zc = new gfan::ZCone(*zm1, *zm2, k);
307  delete zm1;
308  delete zm2;
309  if (u->Typ() == INTMAT_CMD)
310    delete ineq;
311  if (v->Typ() == INTMAT_CMD)
312    delete eq;
313
314  res->rtyp = polytopeID;
315  res->data = (void*) zc;
316  return FALSE;
317}
318
319BOOLEAN polytopeViaNormals(leftv res, leftv args)
320{
321  leftv u = args;
322  if ((u != NULL) && ((u->Typ() == BIGINTMAT_CMD) || (u->Typ() == INTMAT_CMD)))
323  {
324    if (u->next == NULL) return ppCONENORMALS1(res, u);
325  }
326  leftv v = u->next;
327  if ((v != NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTMAT_CMD)))
328  {
329    if (v->next == NULL) return ppCONENORMALS2(res, u, v);
330  }
331  leftv w = v->next;
332  if ((w != NULL) && (w->Typ() == INT_CMD))
333  {
334    if (w->next == NULL) return ppCONENORMALS3(res, u, v, w);
335  }
336  WerrorS("polytopeViaInequalities: unexpected parameters");
337  return TRUE;
338}
339
340BOOLEAN vertices(leftv res, leftv args)
341{
342  leftv u = args;
343  if ((u != NULL) && (u->Typ() == polytopeID))
344    {
345      gfan::ZCone* zc = (gfan::ZCone*)u->Data();
346      gfan::ZMatrix zmat = zc->extremeRays();
347      res->rtyp = BIGINTMAT_CMD;
348      res->data = (void*)zMatrixToBigintmat(zmat);
349      return FALSE;
350    }
351  WerrorS("vertices: unexpected parameters");
352  return TRUE;
353}
354
355int getAmbientDimension(gfan::ZCone* zc) // zc is meant to represent a polytope here
356{                                        // hence ambientDimension-1
357  return zc->ambientDimension()-1;
358}
359
360int getCodimension(gfan::ZCone *zc)
361{
362  return zc->codimension();
363}
364
365int getDimension(gfan::ZCone* zc)
366{
367  return zc->dimension()-1;
368}
369
370gfan::ZVector intStar2ZVectorWithLeadingOne(const int d, const int* i)
371{
372  gfan::ZVector zv(d+1);
373  zv[0]=1;
374  for(int j=1; j<=d; j++)
375  {
376    zv[j]=i[j];
377  }
378  return zv;
379}
380
381BOOLEAN newtonPolytope(leftv res, leftv args)
382{
383  leftv u = args;
384  if ((u != NULL) && (u->Typ() == POLY_CMD))
385  {
386    poly p = (poly)u->Data();
387    int N = rVar(currRing);
388    gfan::ZMatrix zm(1,N+1);
389    int *leadexpv = (int*)omAlloc((N+1)*sizeof(int));
390    while (p!=NULL)
391    {
392      pGetExpV(p,leadexpv);
393      gfan::ZVector zv = intStar2ZVectorWithLeadingOne(N, leadexpv);
394      zm.appendRow(zv);
395      pIter(p);
396    }
397    omFreeSize(leadexpv,(N+1)*sizeof(int));
398    gfan::ZCone* zc = new gfan::ZCone();
399    *zc = gfan::ZCone::givenByRays(zm, gfan::ZMatrix(0, zm.getWidth()));
400    res->rtyp = polytopeID;
401    res->data = (void*) zc;
402    return FALSE;
403  }
404  WerrorS("newtonPolytope: unexpected parameters");
405  return TRUE;
406}
407
408BOOLEAN scalePolytope(leftv res, leftv args)
409{
410  leftv u = args;
411  if ((u != NULL) && (u->Typ() == INT_CMD))
412  {
413    leftv v = u->next;
414    if ((v != NULL) && (v->Typ() == polytopeID))
415    {
416      int s = (int)(long) u->Data();
417      gfan::ZCone* zp = (gfan::ZCone*) v->Data();
418      gfan::ZMatrix zm = zp->extremeRays();
419      for (int i=0; i<zm.getHeight(); i++)
420        for (int j=1; j<zm.getWidth(); j++)
421          zm[i][j]*=s;
422      gfan::ZCone* zq = new gfan::ZCone();
423      *zq = gfan::ZCone::givenByRays(zm,gfan::ZMatrix(0, zm.getWidth()));
424      res->rtyp = polytopeID;
425      res->data = (void*) zq;
426      return FALSE;
427    }
428  }
429  WerrorS("scalePolytope: unexpected parameters");
430  return TRUE;
431}
432
433BOOLEAN dualPolytope(leftv res, leftv args)
434{
435  leftv u = args;
436  if ((u != NULL) && (u->Typ() == polytopeID))
437  {
438    gfan::ZCone* zp = (gfan::ZCone*) u->Data();
439    gfan::ZCone* zq = new gfan::ZCone(zp->dualCone());
440    res->rtyp = polytopeID;
441    res->data = (void*) zq;
442    return FALSE;
443  }
444  WerrorS("dualPolytope: unexpected parameters");
445  return TRUE;
446}
447
448void bbpolytope_setup(SModulFunctions* p)
449{
450  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
451  // all undefined entries will be set to default in setBlackboxStuff
452  // the default Print is quite usefule,
453  // all other are simply error messages
454  b->blackbox_destroy=bbpolytope_destroy;
455  b->blackbox_String=bbpolytope_String;
456  //b->blackbox_Print=blackbox_default_Print;
457  b->blackbox_Init=bbpolytope_Init;
458  b->blackbox_Copy=bbpolytope_Copy;
459  b->blackbox_Assign=bbpolytope_Assign;
460  p->iiAddCproc("","polytopeViaPoints",FALSE,polytopeViaVertices);
461  p->iiAddCproc("","polytopeViaInequalities",FALSE,polytopeViaNormals);
462  p->iiAddCproc("","vertices",FALSE,vertices);
463  p->iiAddCproc("","newtonPolytope",FALSE,newtonPolytope);
464  p->iiAddCproc("","scalePolytope",FALSE,scalePolytope);
465  p->iiAddCproc("","dualPolytope",FALSE,dualPolytope);
466  /********************************************************/
467  /* the following functions are implemented in bbcone.cc */
468  // iiAddCproc("","getAmbientDimension",FALSE,getAmbientDimension);
469  // iiAddCproc("","getCodimension",FALSE,getAmbientDimension);
470  // iiAddCproc("","getDimension",FALSE,getDimension);
471  /********************************************************/
472  /* the following functions are identical to those in bbcone.cc */
473  // iiAddCproc("","facets",FALSE,facets);
474  // iiAddCproc("","setLinearForms",FALSE,setLinearForms);
475  // iiAddCproc("","getLinearForms",FALSE,getLinearForms);
476  // iiAddCproc("","setMultiplicity",FALSE,setMultiplicity);
477  // iiAddCproc("","getMultiplicity",FALSE,getMultiplicity);
478  // iiAddCproc("","hasFace",FALSE,hasFace);
479  /***************************************************************/
480  // iiAddCproc("","getEquations",FALSE,getEquations);
481  // iiAddCproc("","getInequalities",FALSE,getInequalities);
482  polytopeID=setBlackboxStuff(b,"polytope");
483  //Print("created type %d (polytope)\n",polytopeID);
484}
485
486#endif
Note: See TracBrowser for help on using the repository browser.