~ubuntu-branches/ubuntu/saucy/gfan/saucy-proposed

« back to all changes in this revision

Viewing changes to tropicalbasis.cpp

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2013-07-09 10:44:01 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130709104401-5q66ozz5j5af0dak
Tags: 0.5+dfsg-3
* Upload to unstable.
* modify remove_failing_tests_on_32bits.patch to replace command of
  0009RenderStairCase test with an empty one instead of deleting it.
* remove lintian override about spelling error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "tropicalbasis.h"
 
2
 
 
3
#include <iostream>
 
4
 
2
5
#include "buchberger.h"
3
 
 
 
6
#include "groebnerengine.h"
4
7
#include "tropical.h"
5
8
#include "tropical2.h"
6
9
#include "division.h"
7
10
#include "wallideal.h"
 
11
#include "halfopencone.h"
8
12
#include "log.h"
9
13
 
10
14
#include "timer.h"
13
17
 
14
18
typedef set<IntegerVector> IntegerVectorSet;
15
19
 
 
20
static Polynomial cleverSaturation(Polynomial const &p, IntegerVector const &w)
 
21
{
 
22
  PolynomialRing theRing=p.getRing();
 
23
  if(p.isZero())return p;
 
24
  if(w.size()==0)return p;
 
25
  Polynomial f=initialForm(p,w);
 
26
  debug<<"P:"<<p<<" w: "<<w<<" f: "<<f<<"\n";
 
27
  IntegerVector gcd=f.greatestCommonMonomialDivisor();
 
28
  if(!gcd.isZero())
 
29
    {
 
30
      debug<<"OLD"<<p<<"\n";
 
31
      debug<<"initialForm"<<initialForm(p,w)<<"\n";
 
32
      PolynomialSet reducer(p.getRing());
 
33
      reducer.push_back(theRing.monomialFromExponentVector(IntegerVector::allOnes(theRing.getNumberOfVariables()))-theRing.one());
 
34
      reducer.markAndScale(LexicographicTermOrder());
 
35
      debug<<gcd;
 
36
      int s=gcd.max();
 
37
      Polynomial all=theRing.monomialFromExponentVector(s*IntegerVector::allOnes(theRing.getNumberOfVariables())-gcd);
 
38
      Polynomial g= division(all*p,reducer,LexicographicTermOrder());
 
39
      g.saturate();
 
40
      debug<<"NEW"<<g<<"\n";
 
41
      return g;
 
42
    }
 
43
  return p;
 
44
}
 
45
 
 
46
static void initialSaturatingBuchberger(PolynomialSet *g, TermOrder const &termOrder_, IntegerVector const &w_)
 
47
{
 
48
  int n=w_.size();
 
49
  IntegerVectorList temp;
 
50
  temp.push_back(IntegerVector::standardVector(n+1,n));
 
51
  IntegerVector w=concatenation(w_,IntegerVector(1));w[w.size()-1]=-w_.sum();
 
52
  temp.push_back(w);
 
53
  MatrixTermOrder termOrder(temp);
 
54
  PolynomialRing theRing=g->getRing().withVariablesAppended("Z");
 
55
  PolynomialSet sPolynomials(theRing);
 
56
 
 
57
  for(PolynomialSet::const_iterator i=g->begin();i!=g->end();i++)
 
58
    if(!i->isZero())sPolynomials.push_back(cleverSaturation(i->embeddedInto(theRing),w)); // It is safe and useful to ignore the 0 polynomial
 
59
 
 
60
  sPolynomials.push_back(theRing.monomialFromExponentVector(IntegerVector::allOnes(theRing.getNumberOfVariables()))-theRing.one());
 
61
 
 
62
  sPolynomials.saturate();
 
63
  sPolynomials.markAndScale(termOrder);
 
64
 
 
65
  *g=PolynomialSet(theRing);
 
66
 
 
67
  while(!sPolynomials.empty())
 
68
    {
 
69
      Polynomial p=*sPolynomials.begin();
 
70
      sPolynomials.pop_front();
 
71
 
 
72
      p=division(p,*g,termOrder);
 
73
      if(!p.isZero())
 
74
        {
 
75
          p=cleverSaturation(p,w);
 
76
          p.mark(termOrder);
 
77
          p.scaleMarkedCoefficientToOne();
 
78
          bool isMonomial=p.isMonomial();
 
79
          for(PolynomialSet::const_iterator i=g->begin();i!=g->end();i++)
 
80
            if((!isMonomial) || (!i->isMonomial())) // 2 % speed up!
 
81
            {
 
82
              if(!relativelyPrime(i->getMarked().m.exponent,p.getMarked().m.exponent))
 
83
                {
 
84
                  Polynomial s=sPolynomial(*i,p);
 
85
                  s.mark(termOrder); // with respect to some termorder
 
86
                  s.scaleMarkedCoefficientToOne();
 
87
                  sPolynomials.push_back(s);
 
88
                }
 
89
            }
 
90
          g->push_back(p);
 
91
          {
 
92
            static int t;
 
93
            t++;
 
94
            //      if((t&31)==0)fprintf(Stderr," gsize %i  spolys:%i\n",g->size(),sPolynomials.size());
 
95
          }
 
96
        }
 
97
    }
 
98
  minimize(g);
 
99
  autoReduce(g,termOrder);
 
100
}
 
101
 
 
102
 
 
103
 
16
104
PolynomialSet tropicalBasisOfCurve(int n, PolynomialSet g, PolyhedralFan *intersectionFan, int linealitySpaceDimension) //Assuming g is homogeneous
17
105
{
18
 
  int homog=linealitySpaceDimension;
 
106
 
 
107
  /*
 
108
   * TODO: Introduce the notion of a tropical prebasis:
 
109
   *
 
110
   * Definition. A set of polynomials f_1,...,f_m is called a tropical prebasis for the ideal they
 
111
   * generate if for every w not in the tropical variety of that ideal there exists a monomial in
 
112
   * the ideal generated by the initial forms of f_1,...,f_m w.r.t. w.
 
113
   *
 
114
   * Computing a tropical prebasis could be faster than computing a tropical basis since fewer
 
115
   * groebner bases for the originial ideal might be needed. Still, however, it is relatively easy
 
116
   * to determine the tropical variety given a tropical prebasis.
 
117
   */
 
118
//  bool prebasis=true;
 
119
//  debug<<"PREBASIS="<<prebasis<<"\n";
 
120
  log2 debug<<"TropicalBasis begin\n";
 
121
        log2 debug<<g;
 
122
        int homog=linealitySpaceDimension;
19
123
  assert(homog>0 || n==0);
20
124
  TimerScope ts(&iterativeTropicalBasisTimer);
21
125
  PolyhedralFan f(n);
22
126
  if(!intersectionFan)intersectionFan=&f;
23
 
     
24
 
  *intersectionFan=tropicalPrincipalIntersection(n,g,linealitySpaceDimension);
 
127
 
 
128
  //  *intersectionFan=tropicalPrincipalIntersection(n,g,linealitySpaceDimension);
 
129
//      log1 fprintf(Stderr,"WARINING USING EXPERIMENTAL TROPICAL HYPERSURFACE INTERSECTION ROUTINE!!\n");
 
130
  *intersectionFan=tropicalHyperSurfaceIntersectionClosed(n, g);
25
131
 
26
132
  IntegerVectorSet containsNoMonomialCache;
27
133
 
28
134
  while(1)
29
135
    {
30
136
      PolyhedralFan::coneIterator i;
 
137
 
 
138
//      {AsciiPrinter P(Stderr);intersectionFan->printWithIndices(&P);}
 
139
restart:
 
140
//      {AsciiPrinter P(Stderr);intersectionFan->printWithIndices(&P);}
31
141
      for(i=intersectionFan->conesBegin();i!=intersectionFan->conesEnd();i++)
32
142
        {
 
143
//        log1 cerr<<"!@#$";
33
144
          IntegerVector relativeInteriorPoint=i->getRelativeInteriorPoint();
 
145
//        log1 cerr<<"1234/n";
 
146
 
34
147
          if(containsNoMonomialCache.count(relativeInteriorPoint)>0)
35
148
            {
36
149
              log2 fprintf(Stderr,"Weight vector found in cache.... contains no monomial.\n");
37
150
            }
38
151
          else
39
152
            {
 
153
/*            if(prebasis)
 
154
                {
 
155
                  if(containsMonomial(initialForms(g,relativeInteriorPoint)))
 
156
                    {
 
157
                      intersectionFan->insertFacetsOfCone(*i);
 
158
                      intersectionFan->remove(*i);
 
159
                      debug<<"LOWERING DIMENSION OF CONE\n";//TODO: checking cones in order of dimension could avoid this.
 
160
                      goto restart;
 
161
                    }
 
162
                }*/
40
163
              WeightReverseLexicographicTermOrder t(relativeInteriorPoint);
41
164
              log2 fprintf(Stderr,"Computing Gr\"obner basis with respect to:");
42
165
              log2 AsciiPrinter(Stderr).printVector(relativeInteriorPoint);
43
166
              log2 fprintf(Stderr,"\n");
44
167
              PolynomialSet h2=g;
45
 
              buchberger(&h2,t);
 
168
  //            debug<<"g"<<g;
 
169
 
 
170
/*            {Adjust these lines somehow to enable the saturating buchberger.
 
171
                debug<<h2;
 
172
                initialSaturatingBuchberger(&h2, t, relativeInteriorPoint);
 
173
                //buchberger(&h2,t,true);
 
174
                debug<<"SATURATING BUCHBERGER DONE\n";
 
175
              }*/
 
176
             // buchberger(&h2,t);
 
177
              h2=GE_groebnerBasis(h2,t,true);
 
178
        //      debug<<"h2"<<h2;
46
179
              log2 fprintf(Stderr,"Done computing Gr\"obner basis.\n");
47
 
              
48
 
              log3 AsciiPrinter(Stderr).printPolynomialSet(h2);
 
180
 
 
181
          //    debug<<h2;
 
182
//            log3 AsciiPrinter(Stderr).printPolynomialSet(h2);
49
183
 
50
184
              PolynomialSet wall=initialFormsAssumeMarked(h2,relativeInteriorPoint);
51
 
              
 
185
 
52
186
              if(containsMonomial(wall))
53
187
                {
54
188
                  log2 fprintf(Stderr,"Initial ideal contains a monomial.\n");
55
189
                  Polynomial m(computeTermInIdeal(wall));
56
190
                  log2 fprintf(Stderr,"Done computing term in ideal\n");
57
 
                  
 
191
 
58
192
                  Polynomial temp=m-division(m,h2,LexicographicTermOrder());
59
193
                  g.push_back(temp);
60
 
                  
 
194
 
61
195
                  log2 fprintf(Stderr,"Adding element to basis:\n");
62
196
                  log2 AsciiPrinter(Stderr).printPolynomial(temp);
63
197
                  log2 fprintf(Stderr,"\n");
64
 
                  
 
198
 
65
199
                  *intersectionFan=refinement(*intersectionFan,PolyhedralFan::bergmanOfPrincipalIdeal(temp),linealitySpaceDimension,true);
66
200
                  break;
67
201
                }
90
224
                      dual.canonicalize();
91
225
                      IntegerVectorList basis=dual.getEquations();
92
226
                      PolynomialSet witnessLiftBasis=h2;//basis with respect to relativeInteriorPoint
 
227
                      log2 debug<<"basis"<<basis<<"\n";
93
228
                      for(IntegerVectorList::const_iterator j=basis.begin();j!=basis.end();j++)
94
229
                        {
 
230
                          log2 debug<<"wall"<<wall<<"\n";
95
231
                          WeightReverseLexicographicTermOrder t(*j);
96
232
                          PolynomialSet h3=wall;
97
233
                          buchberger(&h3,t);
98
234
                          wall=initialFormsAssumeMarked(h3,*j);
99
235
                          witnessLiftBasis=liftBasis(h3,witnessLiftBasis);
100
236
                        }
 
237
                      log2 debug<<"wall"<<wall<<"\n";
101
238
                      if(containsMonomial(wall))
102
239
                        {
103
240
                          Polynomial m(computeTermInIdeal(wall));
116
253
        }
117
254
      if(i==intersectionFan->conesEnd())break;
118
255
    }
 
256
 
 
257
  log2 debug<<"TropicalBasis end\n";
 
258
  log2 cerr <<"RETURNING";
119
259
  return g;
120
260
}
121
261
 
125
265
  TimerScope ts(&iterativeTropicalBasisTimer);
126
266
  PolyhedralFan f(n);
127
267
  if(!intersectionFan)intersectionFan=&f;
128
 
     
 
268
 
129
269
  *intersectionFan=tropicalPrincipalIntersection(n,g,linealitySpaceDimension);
130
270
 
131
271
  IntegerVectorSet containsNoMonomialCache;
153
293
              PolynomialSet h2=g;
154
294
              buchberger(&h2,t);
155
295
              if(doPrint)fprintf(Stderr,"Done computing Gr\"obner basis.\n");
156
 
              
 
296
 
157
297
              //              AsciiPrinter(Stderr).printPolynomialSet(h2);
158
298
              PolynomialSet wall=initialFormsAssumeMarked(h2,*i);
159
299
              //fprintf(Stderr,"Wall ideal:\n");
160
300
              //AsciiPrinter(Stderr).printPolynomialSet(wall);
161
 
              
 
301
 
162
302
              if(containsMonomial(wall))
163
303
                {
164
304
                  if(doPrint)fprintf(Stderr,"Initial ideal contains a monomial.\n");
165
305
                  Polynomial m(computeTermInIdeal(wall));
166
306
                  if(doPrint)fprintf(Stderr,"Done computing term in ideal\n");
167
 
                  
 
307
 
168
308
                  Polynomial temp=m-division(m,h2,LexicographicTermOrder());
169
309
                  g.push_back(temp);
170
 
                  
 
310
 
171
311
                  if(doPrint)fprintf(Stderr,"Adding element to basis:\n");
172
312
                  if(doPrint)AsciiPrinter(Stderr).printPolynomial(temp);
173
313
                  if(doPrint)fprintf(Stderr,"\n");
174
 
                  
 
314
 
175
315
                  *intersectionFan=refinement(*intersectionFan,PolyhedralFan::bergmanOfPrincipalIdeal(temp),linealitySpaceDimension,true);
176
316
                  break;
177
317
                }