source: git/dyn_modules/callgfanlib/bbpolytope.cc @ 58b407

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