source: git/Singular/dyn_modules/gfanlib/tropical.cc @ e5618c

spielwiese
Last change on this file since e5618c was e5618c, checked in by Yue Ren <ren@…>, 9 years ago
chg: final version for Singular release
  • Property mode set to 100644
File size: 8.8 KB
Line 
1#include <libpolys/polys/monomials/p_polys.h>
2#include <libpolys/coeffs/coeffs.h>
3
4#include <callgfanlib_conversion.h>
5#include <bbcone.h>
6#include <ppinitialReduction.h>
7#include <ttinitialReduction.h>
8#include <containsMonomial.h>
9#include <initial.h>
10#include <witness.h>
11#include <tropicalCurves.h>
12#include <neighbours.h>
13#include <tropicalStrategy.h>
14#include <startingCone.h>
15#include <tropicalVariety.h>
16
17
18gfan::ZCone homogeneitySpace(ideal I, ring r)
19{
20  int n = rVar(r);
21  poly g;
22  int* leadexpv = (int*) omAlloc((n+1)*sizeof(int));
23  int* tailexpv = (int*) omAlloc((n+1)*sizeof(int));
24  gfan::ZVector leadexpw = gfan::ZVector(n);
25  gfan::ZVector tailexpw = gfan::ZVector(n);
26  gfan::ZMatrix equations = gfan::ZMatrix(0,n);
27  for (int i=0; i<IDELEMS(I); i++)
28  {
29    g = (poly) I->m[i];
30    if (g)
31    {
32      p_GetExpV(g,leadexpv,r);
33      leadexpw = intStar2ZVector(n,leadexpv);
34      pIter(g);
35      while (g)
36      {
37        p_GetExpV(g,tailexpv,r);
38        tailexpw = intStar2ZVector(n,tailexpv);
39        equations.appendRow(leadexpw-tailexpw);
40        pIter(g);
41      }
42    }
43  }
44  omFreeSize(leadexpv,(n+1)*sizeof(int));
45  omFreeSize(tailexpv,(n+1)*sizeof(int));
46  return gfan::ZCone(gfan::ZMatrix(0, equations.getWidth()),equations);
47}
48
49
50BOOLEAN homogeneitySpace(leftv res, leftv args)
51{
52  leftv u = args;
53  if ((u != NULL) && (u->Typ() == IDEAL_CMD))
54  {
55    leftv v = u->next;
56    if (v == NULL)
57    {
58      ideal I = (ideal) u->Data();
59      res->rtyp = coneID;
60      res->data = (void*) new gfan::ZCone(homogeneitySpace(I,currRing));
61      return FALSE;
62    }
63  }
64  WerrorS("homogeneitySpace: unexpected parameters");
65  return TRUE;
66}
67
68
69BOOLEAN groebnerCone(leftv res, leftv args)
70{
71  leftv u = args;
72  if ((u != NULL) && (u->Typ() == IDEAL_CMD))
73  {
74    leftv v = u->next;
75    if (v == NULL)
76    {
77      int n = currRing->N;
78      ideal I = (ideal) u->Data();
79      poly g = NULL;
80      int* leadexpv = (int*) omAlloc((n+1)*sizeof(int));
81      int* tailexpv = (int*) omAlloc((n+1)*sizeof(int));
82      gfan::ZVector leadexpw = gfan::ZVector(n);
83      gfan::ZVector tailexpw = gfan::ZVector(n);
84      gfan::ZMatrix inequalities = gfan::ZMatrix(0,n);
85      gfan::ZMatrix equations = gfan::ZMatrix(0,n);
86      long d;
87      for (int i=0; i<IDELEMS(I); i++)
88      {
89        g = (poly) I->m[i]; pGetExpV(g,leadexpv);
90        leadexpw = intStar2ZVector(n, leadexpv);
91        pIter(g);
92        d = p_Deg(g,currRing);
93        while ((g != NULL) && (p_Deg(g,currRing) == d))
94        {
95          pGetExpV(g,tailexpv);
96          tailexpw = intStar2ZVector(n, tailexpv);
97          equations.appendRow(leadexpw-tailexpw);
98          pIter(g);
99        }
100
101        if (g != NULL)
102        {
103          while (g != NULL)
104          {
105            pGetExpV(g,tailexpv);
106            tailexpw = intStar2ZVector(n, tailexpv);
107            inequalities.appendRow(leadexpw-tailexpw);
108            pIter(g);
109          }
110        }
111      }
112      gfan::ZCone* gCone = new gfan::ZCone(inequalities,equations);
113      omFreeSize(leadexpv,(n+1)*sizeof(int));
114      omFreeSize(tailexpv,(n+1)*sizeof(int));
115
116      res->rtyp = coneID;
117      res->data = (void*) gCone;
118      return FALSE;
119    }
120  }
121  WerrorS("groebnerCone: unexpected parameters");
122  return TRUE;
123}
124
125
126gfan::ZCone* maximalGroebnerCone(const ring &r, const ideal &I)
127{
128  int n = rVar(r);
129  poly g = NULL;
130  int* leadexpv = (int*) omAlloc((n+1)*sizeof(int));
131  int* tailexpv = (int*) omAlloc((n+1)*sizeof(int));
132  gfan::ZVector leadexpw = gfan::ZVector(n);
133  gfan::ZVector tailexpw = gfan::ZVector(n);
134  gfan::ZMatrix inequalities = gfan::ZMatrix(0,n);
135  for (int i=0; i<IDELEMS(I); i++)
136  {
137    g = (poly) I->m[i]; pGetExpV(g,leadexpv);
138    leadexpw = intStar2ZVector(n, leadexpv);
139    pIter(g);
140    while (g != NULL)
141    {
142      pGetExpV(g,tailexpv);
143      tailexpw = intStar2ZVector(n, tailexpv);
144      inequalities.appendRow(leadexpw-tailexpw);
145      pIter(g);
146    }
147  }
148  omFreeSize(leadexpv,(n+1)*sizeof(int));
149  omFreeSize(tailexpv,(n+1)*sizeof(int));
150  return new gfan::ZCone(inequalities,gfan::ZMatrix(0, inequalities.getWidth()));
151}
152
153
154BOOLEAN maximalGroebnerCone(leftv res, leftv args)
155{
156  leftv u = args;
157  if ((u != NULL) && (u->Typ() == IDEAL_CMD))
158  {
159    leftv v = u->next;
160    if (v == NULL)
161    {
162      ideal I = (ideal) u->Data();
163      res->rtyp = coneID;
164      res->data = (void*) maximalGroebnerCone(currRing, I);
165      return FALSE;
166    }
167  }
168  WerrorS("maximalGroebnerCone: unexpected parameters");
169  return TRUE;
170}
171
172
173BOOLEAN initial(leftv res, leftv args)
174{
175  leftv u = args;
176  if ((u != NULL) && (u->Typ() == POLY_CMD))
177  {
178    leftv v = u->next;
179    if ((v !=NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
180    {
181      poly p = (poly) u->Data();
182      gfan::ZVector* weightVector;
183      if (v->Typ() == INTVEC_CMD)
184      {
185        intvec* w0 = (intvec*) v->Data();
186        bigintmat* w1 = iv2bim(w0,coeffs_BIGINT);
187        w1->inpTranspose();
188        weightVector = bigintmatToZVector(*w1);
189        delete w1;
190      }
191      else
192      {
193        bigintmat* w1 = (bigintmat*) v->Data();
194        weightVector = bigintmatToZVector(*w1);
195      }
196      res->rtyp = POLY_CMD;
197      res->data = (void*) initial(p, currRing, *weightVector);
198      delete weightVector;
199      return FALSE;
200    }
201  }
202  if ((u != NULL) && (u->Typ() == IDEAL_CMD))
203  {
204    leftv v = u->next;
205    if ((v !=NULL) && ((v->Typ() == BIGINTMAT_CMD) || (v->Typ() == INTVEC_CMD)))
206    {
207      ideal I = (ideal) u->Data();
208      gfan::ZVector* weightVector;
209      if (v->Typ() == INTVEC_CMD)
210      {
211        intvec* w0 = (intvec*) v->Data();
212        bigintmat* w1 = iv2bim(w0,coeffs_BIGINT);
213        w1->inpTranspose();
214        weightVector = bigintmatToZVector(*w1);
215        delete w1;
216      }
217      else
218      {
219        bigintmat* w1 = (bigintmat*) v->Data();
220        weightVector = bigintmatToZVector(*w1);
221      }
222      res->rtyp = IDEAL_CMD;
223      res->data = (void*) initial(I, currRing, *weightVector);
224      delete weightVector;
225      return FALSE;
226    }
227  }
228  WerrorS("initial: unexpected parameters");
229  return TRUE;
230}
231
232
233void tropical_setup(SModulFunctions* p)
234{
235  p->iiAddCproc("","groebnerCone",FALSE,groebnerCone);
236  p->iiAddCproc("","maximalGroebnerCone",FALSE,maximalGroebnerCone);
237  p->iiAddCproc("","initial",FALSE,initial);
238  // p->iiAddCproc("","tropicalNeighbours",FALSE,tropicalNeighbours);
239#ifndef NDEBUG
240  // p->iiAddCproc("","initial0",FALSE,initial0);
241  p->iiAddCproc("","pReduceDebug",FALSE,pReduceDebug);
242  // p->iiAddCproc("","ppreduceInitially0",FALSE,ppreduceInitially0);
243  // p->iiAddCproc("","ppreduceInitially1",FALSE,ppreduceInitially1);
244  // p->iiAddCproc("","ppreduceInitially2",FALSE,ppreduceInitially2);
245  p->iiAddCproc("","ptNormalize",FALSE,ptNormalize);
246  p->iiAddCproc("","ppreduceInitially3",FALSE,ppreduceInitially3);
247  // p->iiAddCproc("","ppreduceInitially4",FALSE,ppreduceInitially4);
248  // p->iiAddCproc("","ttpReduce",FALSE,ttpReduce);
249  // p->iiAddCproc("","ttreduceInitially0",FALSE,ttreduceInitially0);
250  // p->iiAddCproc("","ttreduceInitially1",FALSE,ttreduceInitially1);
251  // p->iiAddCproc("","ttreduceInitially2",FALSE,ttreduceInitially2);
252  // p->iiAddCproc("","ttreduceInitially3",FALSE,ttreduceInitially3);
253  // p->iiAddCproc("","ttreduceInitially4",FALSE,ttreduceInitially4);
254  // p->iiAddCproc("","checkForMonomial",FALSE,checkForMonomial);
255  // p->iiAddCproc("","dwr0",FALSE,dwr0);
256  // p->iiAddCproc("","witness0",FALSE,witness0);
257  // p->iiAddCproc("","tropicalVariety00",FALSE,tropicalVariety00);
258  // p->iiAddCproc("","tropicalVariety01",FALSE,tropicalVariety01);
259  // p->iiAddCproc("","tropicalCurve0",FALSE,tropicalCurve0);
260  // p->iiAddCproc("","tropicalCurve1",FALSE,tropicalCurve1);
261  p->iiAddCproc("","reduceInitiallyDebug",FALSE,reduceInitiallyDebug);
262  p->iiAddCproc("","computeWitnessDebug",FALSE,computeWitnessDebug);
263  p->iiAddCproc("","computeFlipDebug",FALSE,computeFlipDebug);
264  p->iiAddCproc("","flipConeDebug",FALSE,flipConeDebug);
265  p->iiAddCproc("","groebnerNeighboursDebug",FALSE,groebnerNeighboursDebug);
266  p->iiAddCproc("","tropicalNeighboursDebug",FALSE,tropicalNeighboursDebug);
267  p->iiAddCproc("","tropicalStarDebug",FALSE,tropicalStarDebug);
268  p->iiAddCproc("","tropicalStartingPoint",FALSE,tropicalStartingPoint);
269  p->iiAddCproc("","positiveTropicalStartingPoint",FALSE,positiveTropicalStartingPoint);
270  p->iiAddCproc("","nonNegativeTropicalStartingPoint",FALSE,nonNegativeTropicalStartingPoint);
271  p->iiAddCproc("","negativeTropicalStartingPoint",FALSE,negativeTropicalStartingPoint);
272  p->iiAddCproc("","nonPositiveTropicalStartingPoint",FALSE,nonPositiveTropicalStartingPoint);
273  p->iiAddCproc("","tropicalStartingCone",FALSE,tropicalStartingCone);
274  p->iiAddCproc("","tropicalVariety",FALSE,tropicalVariety);
275#endif //NDEBUG
276  // p->iiAddCproc("","ppreduceInitially",FALSE,ppreduceInitially);
277  // p->iiAddCproc("","ttreduceInitially",FALSE,ttreduceInitially);
278  // p->iiAddCproc("","homogeneitySpace",FALSE,homogeneitySpace);
279}
Note: See TracBrowser for help on using the repository browser.