10
#include <cstdio> /* Always include cstdio before gmp.h.*/
10
11
#include <gmp.h> //SHOULD BE REMOVED
13
/** A FieldElement is an element of a Field in the algebraic sense. A
14
FieldElement always knows its associated Field to which it
15
belongs. We may perform arithmetic operations on a FieldElement
16
and a FieldElement is passed around by value. Thus in C++ it makes
17
no sense to derive classes from the FieldElement class. The actual
18
data is contained in an object of the class
19
FieldElementImplementation and the FieldElement serves as a smart
20
pointer with reference counting to this implementation.
14
25
class FieldElementImplementation *implementingObject;
16
26
FieldElement(class FieldElementImplementation *implementingObject_);
18
28
class FieldImplementation *getField()const;
23
33
Would it also work for this (a+a).getField(); or could the ref count get 0 before it is increased?
37
* The following two are only supported for rationals.
26
39
mpq_t const *getGmpRationalTemporaryPointer()const;
40
double floatingPointApproximation()const;
41
bool isInteger()const;
28
43
void setImplementingObject(FieldElementImplementation *implementingObject_){implementingObject=implementingObject_;assert(0);}
30
45
FieldElement one() const;
32
47
virtual bool isOne()const;
33
48
friend FieldElement operator+(const FieldElement &a,const FieldElement &b);
34
49
friend FieldElement operator-(const FieldElement &a,const FieldElement &b);
35
50
friend FieldElement operator-(const FieldElement &b);
36
51
FieldElement inverse()const;
52
int sign()const; // Only allowed for ordered field. Asserts otherwise.
53
int pAdicValuation(int p)const; // Only allowed for the field Q.
54
FieldElement pAdicRemainder(class Field const &ZModPZ)const; // Only allowed for the field Q.
55
int integerRepresentative()const; // Only allowed for Z/pZ
37
56
std::string toString(bool writeIfOne=true, bool alwaysWriteSign=false ,bool latexMode=false) const;
38
57
void operator*=(const FieldElement &a);
58
void operator+=(const FieldElement &a);
59
/** Adds a*b to the value of the object. */
60
void madd(const FieldElement &a, const FieldElement &b);
39
61
friend FieldElement operator*(const FieldElement &a,const FieldElement &b);
42
FieldElement(class Field const &f);
64
FieldElement(class Field const &f);//Initializes to zero
44
66
FieldElement() // This constructor causes a lot of trouble
45
67
{ // Having a legal FieldElement without an implementing object
46
68
implementingObject=0; // we must check for each assignment (copy constructor/assignment operator)
47
69
} // if the pointer is zero. - And some operation will be illegal on
48
70
// on this element ( one(),..... )
50
72
FieldElement(const FieldElement &a);
51
73
FieldElement& operator=(const FieldElement& a);
52
74
virtual ~FieldElement();
77
/** The Field class describes an object which has as its value a field
78
(in the algebraic sense). The value/object can be copied around as
79
a value. The Field object serves as a smart pointer with reference
80
counting to a FieldImplementation which is hidden for the user of
81
the Field class. In C++ it makes no sense to derive classes from
82
this class because the object must be passable by value. */
57
86
friend class FieldElement;
59
89
class FieldImplementation *implementingObject;
61
91
FieldElementImplementation *zHomomorphismImplementation(int n)const;
93
@return The image of the integer n under the unique ring homomorphism from the integers Z to the Field taking 1 to the multiplicative neutral element.
62
95
FieldElement zHomomorphism(int n)const;
96
bool isRationals()const;
63
98
const char *name();
64
99
std::string toString()const;
94
129
virtual FieldElementImplementation *inverse()const=0;
95
130
virtual std::string toString(bool writeIfOne=true, bool alwaysWriteSign=false, bool latexMode=false) const=0;
96
131
virtual void operator*=(const FieldElementImplementation &a)=0;
132
virtual void operator+=(const FieldElementImplementation &a)=0;
133
virtual void madd(const FieldElementImplementation &a,const FieldElementImplementation &b)=0;
134
virtual int sign()const
139
virtual int pAdicValuation(int p)const
144
virtual FieldElement pAdicRemainder(Field const &ZModPZ)const
147
return pAdicRemainder(ZModPZ);
149
virtual int integerRepresentative()const
97
154
virtual mpq_t const *getGmpRationalTemporaryPointer()const
99
156
fprintf(stderr,"*this object is not implemented using GMP\n");
161
virtual bool isInteger()const
105
167
Field& operator=(const Field& a)