source: git/Singular/dyn_modules/gfanlib/groebnerFan.cc @ a3f0fea

spielwiese
Last change on this file since a3f0fea was a3f0fea, checked in by Reimer Behrends <behrends@…>, 5 years ago
Modify variable declarions for pSingular.
  • Property mode set to 100644
File size: 3.7 KB
Line 
1#include "misc/options.h"
2#include "bbfan.h"
3
4#include "groebnerCone.h"
5#include "startingCone.h"
6#include "tropicalTraversal.h"
7
8
9VAR BITSET groebnerBitsetSave1, groebnerBitsetSave2;
10
11/***
12 * sets option(redSB)
13 **/
14static void setOptionRedSB()
15{
16  SI_SAVE_OPT(groebnerBitsetSave1,groebnerBitsetSave2);
17  si_opt_1|=Sy_bit(OPT_REDSB);
18}
19
20/***
21 * sets option(noredSB);
22 **/
23static void undoSetOptionRedSB()
24{
25  SI_RESTORE_OPT(groebnerBitsetSave1,groebnerBitsetSave2);
26}
27
28gfan::ZFan* groebnerFan(const tropicalStrategy currentStrategy)
29{
30  groebnerCone startingCone = groebnerStartingCone(currentStrategy);
31  groebnerCones groebnerFan = groebnerTraversal(startingCone);
32  return toFanStar(groebnerFan);
33}
34
35
36gfan::ZFan* groebnerFanOfPolynomial(poly g, ring r, bool onlyLowerHalfSpace=false)
37{
38  int n = rVar(r);
39
40  if (g==NULL || g->next==NULL)
41  {
42    // if g is a term or zero, return the fan consisting of the whole weight space
43    gfan::ZFan* zf = new gfan::ZFan(gfan::ZFan::fullFan(n));
44    return zf;
45  }
46  else
47  {
48    gfan::ZVector lowerHalfSpaceCondition = gfan::ZVector(n);
49    lowerHalfSpaceCondition[0] = -1;
50
51    // otherwise, obtain a list of all the exponent vectors
52    int* expv = (int*) omAlloc((n+1)*sizeof(int));
53    gfan::ZMatrix exponents = gfan::ZMatrix(0,n);
54    for (poly s=g; s; pIter(s))
55    {
56      p_GetExpV(s,expv,r);
57      gfan::ZVector zv = intStar2ZVector(n,expv);
58      exponents.appendRow(intStar2ZVector(n,expv));
59    }
60    omFreeSize(expv,(n+1)*sizeof(int));
61
62    // and construct the Groebner fan
63    gfan::ZFan* zf = new gfan::ZFan(n);
64    int l = exponents.getHeight();
65    for (int i=0; i<l; i++)
66    {
67      // constructs the Groebner cone such that initial form is i-th term
68      gfan::ZMatrix inequalities = gfan::ZMatrix(0,n);
69      if (onlyLowerHalfSpace)
70        inequalities.appendRow(lowerHalfSpaceCondition);
71      for (int j=0; j<l; j++)
72      {
73        if (i!=j)
74          inequalities.appendRow(exponents[i].toVector()-exponents[j].toVector());
75      }
76      gfan::ZCone zc = gfan::ZCone(inequalities,gfan::ZMatrix(0,inequalities.getWidth()));
77      zc.canonicalize();
78      zf->insert(zc);
79    }
80    return zf;
81  }
82}
83
84
85BOOLEAN groebnerFan(leftv res, leftv args)
86{
87  leftv u = args;
88  if ((u!=NULL) && (u->Typ()==IDEAL_CMD))
89  {
90    ideal I = (ideal) u->CopyD();
91    leftv v = u->next;
92
93    if (v==NULL)
94    {
95      if ((I->m[0]!=NULL) && (idElem(I)==1))
96      {
97        try
98        {
99          poly g = I->m[0];
100          gfan::ZFan* zf = groebnerFanOfPolynomial(g,currRing);
101          res->rtyp = fanID;
102          res->data = (char*) zf;
103          return FALSE;
104        }
105        catch (const std::exception& ex)
106        {
107          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
108          return TRUE;
109        }
110      }
111      else
112      {
113        try
114        {
115          tropicalStrategy currentStrategy(I,currRing);
116          setOptionRedSB();
117          gfan::ZFan* zf = groebnerFan(currentStrategy);
118          undoSetOptionRedSB();
119          res->rtyp = fanID;
120          res->data = (char*) zf;
121          return FALSE;
122        }
123        catch (const std::exception& ex)
124        {
125          WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
126          return TRUE;
127        }
128      }
129    }
130  }
131  if ((u!=NULL) && (u->Typ()==POLY_CMD))
132  {
133    poly g = (poly) u->Data();
134    leftv v = u->next;
135    if (v==NULL)
136    {
137      try
138      {
139        gfan::ZFan* zf = groebnerFanOfPolynomial(g,currRing);
140        res->rtyp = fanID;
141        res->data = (char*) zf;
142        return FALSE;
143      }
144      catch (const std::exception& ex)
145      {
146        WerrorS("ERROR: "); WerrorS(ex.what()); WerrorS("\n");
147        return TRUE;
148      }
149    }
150  }
151  WerrorS("groebnerFan: unexpected parameters");
152  return TRUE;
153}
Note: See TracBrowser for help on using the repository browser.