File: field_rationals.cpp

package info (click to toggle)
gfan 0.3dfsg-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,012 kB
  • ctags: 1,935
  • sloc: cpp: 17,728; makefile: 251
file content (254 lines) | stat: -rw-r--r-- 6,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "field_rationals.h"

#include <memory>
#include <assert.h>
#include <gmp.h>

#include "lp_cdd.h"
#include "printer.h"

#include "timer.h"
#include "log.h"

static Timer rationalTimer("Rational",1);


int FieldElementRationalsLiving;

class FieldElementRational : public FieldElementImplementation
{
  mpq_t value;
 public:
  FieldElementRational(FieldImplementation &a):FieldElementImplementation(a)
    {
      FieldElementRationalsLiving++;
      mpq_init(value);
    }
  FieldElementRational(FieldImplementation &a,int n_):FieldElementImplementation(a)
    {
      FieldElementRationalsLiving++;
      mpz_init_set_si(mpq_numref(value), n_);
      mpz_init_set_ui(mpq_denref(value), 1);
    }
  virtual ~FieldElementRational()
    {
      FieldElementRationalsLiving--;
      mpq_clear(value);
    }
  FieldElementRational& operator=(const FieldElementRational& a)
    {
      assert(0);
      const FieldElementRational *A=(const FieldElementRational*)&a;
      if (this != A) {
        mpq_clear(value);
        mpz_init_set(mpq_numref(value), mpq_numref(a.value));
        mpz_init_set(mpq_denref(value), mpq_denref(a.value));
      }
      return *this;
    }
  
  mpq_t const *getGmpRationalTemporaryPointer()const
    {
      return &value;
    }
  void operator*=(const FieldElementImplementation &a)
    {
      const FieldElementRational *A=(const FieldElementRational*)&a;
      assert(A);

      TimerScope ts(&rationalTimer);
      mpq_mul(value,value,A->value);
    }

  FieldElementRational *one() const;
  bool isZero() const
    {
      return mpq_sgn(value)==0;
    }

  FieldElementRational *sum(const FieldElementImplementation &b)const
    {
      TimerScope ts(&rationalTimer);
      const FieldElementRational *B=(const FieldElementRational*)&b;
      FieldElementRational *r= new FieldElementRational(*getField());
      // fprintf(Stderr,"NEW\n");
      mpq_add(r->value,value,B->value);
      return r;
    }
  FieldElementRational *difference(const FieldElementImplementation &b)const
    {
      TimerScope ts(&rationalTimer);
      const FieldElementRational *B=(const FieldElementRational*)&b;
      FieldElementRational *r= new FieldElementRational(*getField());
      // fprintf(Stderr,"NEW\n");
      mpq_sub(r->value,value,B->value);
      return r;
    }
  FieldElementRational *negation()const
    {
      FieldElementRational *r= new FieldElementRational(*getField());
      // fprintf(Stderr,"NEW\n");
      mpq_neg(r->value,value);

      return r;
    }
  FieldElementImplementation *inverse()const
  {
    FieldElementRational *r= new FieldElementRational(*getField());
    // fprintf(Stderr,"NEW\n");

    if(isZero())
      {
	AsciiPrinter P(Stderr);
	P.printString("Error inverting FieldElement: ");
	//	P.printFieldElement(*this);
	P.printString("\n");
	assert(0);
      }
    
    mpq_inv(r->value,value);
    return r;
  }

  static string LaTeXTranslator(const string &s)
  {
    int startIndex=0;
    string sign;
    if(s[0]=='-')
      {
	sign=string("-");
	startIndex=1;
      }
    int slashIndex=-1;
    for(int i=startIndex;i<s.length();i++)if(s[i]=='/')slashIndex=i;
    if(slashIndex==-1)
      return string(s);

    return sign+string("{").append(s,startIndex,slashIndex-startIndex)+string("\\over ").append(s,slashIndex+1,s.length()-slashIndex-1)+string("}");
  }

  std::string toString(bool writeIfOne=true, bool alwaysWriteSign=false, bool latexMode=false) const
  {
    FieldElementRational *tempOne=one();
    FieldElementRational *temp=difference(*tempOne);
    bool isOne=temp->isZero();
    //fprintf(Stderr,"DELETE\n");
    delete temp;
    temp=sum(*tempOne);
    bool isMinusOne=temp->isZero();
    //fprintf(Stderr,"DELETE\n");
    delete temp;
    //fprintf(Stderr,"DELETE\n");
    delete tempOne;

    if(!writeIfOne && isOne)
      {
	if(alwaysWriteSign)return std::string("+");
	return std::string("");
      }
    if(!writeIfOne && isMinusOne)
      return std::string("-");
    static char s[1290*1000];
    //    mpq_get_str(s,10,value); //// CHECK BUFFER SIZE!!!!
    // Changed to make code gmp 3.1.1 compatible
    mpz_get_str(s,10,mpq_numref(value)); //CHECK BUFFER SIZE!!!!
    string S(s);
    if(mpz_cmp_ui(mpq_denref(value),1)!=0)
      {
	mpz_get_str(s,10,mpq_denref(value)); //CHECK BUFFER SIZE!!!!
	S=S+string("/")+string(s);
      }

    if(latexMode)S=LaTeXTranslator(S);

    if(alwaysWriteSign && mpq_sgn(value)!=-1)
      return std::string("+")+S;
    return S;
  }
  
  FieldElementRational *copy()const
  {
    FieldElementRational *r= new FieldElementRational(*getField());
    //      fprintf(Stderr,"NEW\n");

    mpq_clear(r->value);
    mpz_init_set(mpq_numref(r->value), mpq_numref(value));
    mpz_init_set(mpq_denref(r->value), mpq_denref(value));

    return r;
  }
};


FieldRationalsImplementation::FieldRationalsImplementation()
{
}

std::string FieldRationalsImplementation::toString()const
{
  return std::string("Q");
}

FieldElementImplementation *FieldRationalsImplementation::zHomomorphismImplementation(int n)
{
  FieldElementImplementation *ret=new FieldElementRational(*this,n);
  //      fprintf(Stderr,"NEW\n");
  //  ret->refCount++;
  return ret;
}

FieldElement FieldRationalsImplementation::zHomomorphism(int n)
{
  //      fprintf(Stderr,"NEW\n");
  return FieldElement(new FieldElementRational(*this,n));
}

const char *FieldRationalsImplementation::name()
{
  return "GmpRationals";
}

/*FieldRationals::FieldRationals():
  Field(new FieldRationalsImplementation())
{
  /*  fprintf(Stderr,"Adding field rationals\n");
  next=list;
  list=this;
  */
/*
  log2 fprintf(Stderr,"Initializing field Rationals\n");
}
*/
				//FieldRationals Q;
Field Q(new FieldRationalsImplementation());

FieldElementRational *FieldElementRational::one() const
{
  //      fprintf(Stderr,"NEW\n");
  return new FieldElementRational(*getField(),1);
}


IntegerVector primitiveVector(vector<FieldElement> const &v)
{
  int n=v.size();
  
  mpq_t *point = new mpq_t [n];
  for(int i=0;i<n;i++)mpq_init(point[i]);

  for(int i=0;i<n;i++)
    {
      mpq_set(point[i],*(v[i].getGmpRationalTemporaryPointer()));
    }

  scaleToIntegerVector(point,n);

  IntegerVector ret=arrayToIntegerVector(point,n);

  for(int i=0;i<n;i++)mpq_clear(point[i]);
  
  delete [] point;
  
  return ret;
  //  return IntegerVector(n);
}