Basic Polynomial Algebra Subprograms (BPAS)  v. 1.700
BPASRing.hpp
1 #ifndef _BPAS_RING_H_
2 #define _BPAS_RING_H_
3 
4 #include <gmpxx.h>
5 #include <vector>
6 #include "../ExpressionTree/ExpressionTree.hpp"
7 #include <sstream>
8 
9 /**
10  * An enumeration which describes the properties that a particular ring has.
11  * This operates as a bit-wise mask such that, given an unsigned int representing
12  * a particular ring's properties, one can do :
13  * if (ringProp & (PRIME_FIELD | FINITE_FILE))
14  * to test if a Ring is a (in this instance) a prime field or a finite field.
15  */
16 typedef enum RingProperty {
17  COMMUTATIVE_RING = 0x001,
18  INTEGRAL_DOMAIN = 0x003,
19  GCD_DOMAIN = 0x007,
20  UNIQUE_FACTORIZATION_DOMAIN = 0x00f,
21  PRINICPAL_IDEAL_DOMAIN = 0x01f,
22  EUCLIDEAN_DOMAIN = 0x03f,
23  FIELD = 0x07f,
24  PRIME_FIELD = 0x0ff,
25  FINITE_FIELD = 0x1ff,
26  SMALL_PRIME_FIELD = 0x3ff,
27  COMPLEX_FIELD = 0x47f
28 } RingProperty;
29 
30 /**
31  * Class enapsulates a set of RingProperty(s) that the Ring may have.
32  */
33 class RingProperties {
34 private:
35  unsigned int prop;
36  // int n;
37 
38 public:
39 
40  /**
41  * Default constructor which specifies no properties.
42  */
43  RingProperties();
44 
45  /**
46  * Construct a RingProperties from a RingProperty enum element.
47  */
48  RingProperties(RingProperty p);
49 
50  /**
51  * Construct a RingProperties from a collection of RingProperty elements.
52  */
53  RingProperties(std::vector<RingProperty> v);
54 
55  /**
56  * Determine if *this RingProperties has a RingProperty.
57  */
58  inline bool has(RingProperty p);
59 
60  /**
61  * Determine if *this RingProperties has all properties defined
62  * by the other RingProperties p.
63  */
64  inline bool has(const RingProperties& p);
65 };
66 
67 
68 
69 /**
70  * An abstract class defining the interface of a commutative ring.
71  *
72  * The template Derived is a concrete class derived from (implemeneting the
73  * interface of) BPASRing. This is the "curiously recurring template pattern" (CTRP).
74  * This pattern is used among all sub-classes of BPASRing.
75  */
76 template <class Derived>
77 class BPASRing : public virtual ExpressionTreeConvert {
78 public:
79 
80  /**
81  * Static element describing the properties of this ring class.
82  */
83  static RingProperties properties;
84 
85  /**
86  * The characteristic of this ring class.
87  */
88  virtual mpz_class characteristic() {
89  return 0;
90  }
91 
92  /**
93  * Determine if *this ring element is zero, that is the additive identity.
94  *
95  * returns true iff *this is zero.
96  */
97  virtual bool isZero() const = 0;
98 
99  /**
100  * Make *this ring element zero.
101  */
102  virtual void zero() = 0;
103 
104  /**
105  * Determine if *this ring element is one, that is the multiplication identity.
106  *
107  * returns true iff *this is one.
108  */
109  virtual bool isOne() const = 0;
110 
111  /**
112  * Make *this ring element one.
113  */
114  virtual void one() = 0;
115 
116  /**
117  * Obtain the unit normal (a.k.a canonical associate) of an element.
118  * If either parameters u, v, are non-NULL then the units are returned such that
119  * b = ua, v = u^-1. Where b is the unit normal of a, and is the returned value.
120  */
121  virtual Derived unitCanonical(Derived* u = NULL, Derived* v = NULL) const = 0;
122 
123  /**
124  * Copy assignment.
125  */
126  virtual Derived& operator= (const Derived&) = 0;
127 
128  /**
129  * Addition.
130  */
131  virtual Derived operator+ (const Derived&) const = 0;
132 
133  /**
134  * Addition assignment.
135  */
136  virtual Derived& operator+= (const Derived&) =0;
137 
138  /**
139  * Subtraction.
140  */
141  virtual Derived operator- (const Derived&) const = 0;
142 
143  /**
144  * Subtraction assignment.
145  */
146  virtual Derived& operator-= (const Derived&) = 0;
147 
148  /**
149  * Negation.
150  */
151  virtual Derived operator- () const = 0;
152 
153  /**
154  * Multiplication.
155  */
156  virtual Derived operator* (const Derived&) const = 0;
157 
158  /**
159  * Multiplication assignment.
160  */
161  virtual Derived& operator*= (const Derived&) = 0;
162 
163  /**
164  * Exponentiation.
165  */
166  virtual Derived operator^ (long long int e) const = 0;
167 
168  /**
169  * Exponentiation assignment.
170  */
171  virtual Derived& operator^= (long long int e) = 0;
172 
173  /**
174  * Equality test,
175  *
176  * returns true iff equal
177  */
178  virtual bool operator== (const Derived&) const = 0;
179 
180  /**
181  * Inequality test,
182  *
183  * returns true iff not equal.
184  */
185  virtual bool operator!= (const Derived&) const = 0;
186 
187  /**
188  * Print the Ring element.
189  *
190  * Derived classes may override this to get custom printing that may
191  * be more expressive (and prettier) than expression tree printing.
192  */
193  virtual void print(std::ostream& ostream) const {
194  ostream << convertToExpressionTree().toString();
195  }
196 
197  /**
198  * Convert the Ring element to a string.
199  *
200  * Simple delegation of printing to a stringstream to obtain a string.
201  * Overriding the print method is sufficient for sub-classes
202  * to make use of this method.
203  *
204  * returns the string representation of the Ring element.
205  */
206  virtual std::string toString() const {
207  std::stringstream ss;
208  print(ss);
209  return ss.str();
210  }
211 
212  /**
213  * Output operator.
214  *
215  * Defines a to string conversion.
216  */
217  friend std::ostream& operator<< (std::ostream& ostream, const Derived& d) {
218  d.print(ostream);
219  return ostream;
220  }
221 
222  friend std::ostream& operator<< (std::ostream& ostream, Derived&& d) {
223  d.print(ostream);
224  return ostream;
225  }
226 
227  // virtual bool isNegativeOne() = 0;
228  // virtual void negativeOne() = 0;
229  // virtual int isConstant() = 0;
230  // static bool isPrimeField;
231  // static bool isSmallPrimeField;
232  // static bool isComplexField;
233 };
234 
235 #endif
An abstract class defining the interface of a commutative ring.
Definition: BPASRing.hpp:77
virtual std::string toString() const
Convert the Ring element to a string.
Definition: BPASRing.hpp:206
static RingProperties properties
Static element describing the properties of this ring class.
Definition: BPASRing.hpp:83
virtual mpz_class characteristic()
The characteristic of this ring class.
Definition: BPASRing.hpp:88
An interface defining conversion of a class to an ExpressionTree.
Definition: ExpressionTree.hpp:195
virtual void print(std::ostream &ostream) const
Print the Ring element.
Definition: BPASRing.hpp:193