1 // Univariate Polynomials over modular integers.
3 #ifndef _CL_UNIVPOLY_MODINT_H
4 #define _CL_UNIVPOLY_MODINT_H
7 #include "cl_univpoly.h"
8 #include "cl_modinteger.h"
9 #include "cl_integer_class.h"
11 // Normal univariate polynomials with stricter static typing:
12 // `cl_MI' instead of `cl_ring_element'.
14 class cl_heap_univpoly_modint_ring;
16 class cl_univpoly_modint_ring : public cl_univpoly_ring {
18 // Default constructor.
19 cl_univpoly_modint_ring () : cl_univpoly_ring () {}
21 cl_univpoly_modint_ring (const cl_univpoly_modint_ring&);
22 // Assignment operator.
23 cl_univpoly_modint_ring& operator= (const cl_univpoly_modint_ring&);
24 // Automatic dereferencing.
25 cl_heap_univpoly_modint_ring* operator-> () const
26 { return (cl_heap_univpoly_modint_ring*)heappointer; }
28 // Copy constructor and assignment operator.
29 CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_modint_ring,cl_univpoly_ring)
30 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_modint_ring,cl_univpoly_modint_ring)
32 class cl_UP_MI : public cl_UP {
34 const cl_univpoly_modint_ring& ring () const { return The(cl_univpoly_modint_ring)(_ring); }
36 CL_DEFINE_CONVERTER(cl_ring_element)
37 // Destructive modification.
38 void set_coeff (uintL index, const cl_MI& y);
41 const cl_MI operator() (const cl_MI& y) const;
42 public: // Ability to place an object at a given address.
43 void* operator new (size_t size) { return cl_malloc_hook(size); }
44 void* operator new (size_t size, cl_UP_MI* ptr) { (void)size; return ptr; }
45 void operator delete (void* ptr) { cl_free_hook(ptr); }
48 class cl_heap_univpoly_modint_ring : public cl_heap_univpoly_ring {
49 SUBCLASS_cl_heap_univpoly_ring()
50 const cl_modint_ring& basering () const { return The(cl_modint_ring)(_basering); }
51 // High-level operations.
52 void fprint (cl_ostream stream, const cl_UP_MI& x)
54 cl_heap_univpoly_ring::fprint(stream,x);
56 cl_boolean equal (const cl_UP_MI& x, const cl_UP_MI& y)
58 return cl_heap_univpoly_ring::equal(x,y);
60 const cl_UP_MI zero ()
62 return The2(cl_UP_MI)(cl_heap_univpoly_ring::zero());
64 cl_boolean zerop (const cl_UP_MI& x)
66 return cl_heap_univpoly_ring::zerop(x);
68 const cl_UP_MI plus (const cl_UP_MI& x, const cl_UP_MI& y)
70 return The2(cl_UP_MI)(cl_heap_univpoly_ring::plus(x,y));
72 const cl_UP_MI minus (const cl_UP_MI& x, const cl_UP_MI& y)
74 return The2(cl_UP_MI)(cl_heap_univpoly_ring::minus(x,y));
76 const cl_UP_MI uminus (const cl_UP_MI& x)
78 return The2(cl_UP_MI)(cl_heap_univpoly_ring::uminus(x));
82 return The2(cl_UP_MI)(cl_heap_univpoly_ring::one());
84 const cl_UP_MI canonhom (const cl_I& x)
86 return The2(cl_UP_MI)(cl_heap_univpoly_ring::canonhom(x));
88 const cl_UP_MI mul (const cl_UP_MI& x, const cl_UP_MI& y)
90 return The2(cl_UP_MI)(cl_heap_univpoly_ring::mul(x,y));
92 const cl_UP_MI square (const cl_UP_MI& x)
94 return The2(cl_UP_MI)(cl_heap_univpoly_ring::square(x));
96 const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y)
98 return The2(cl_UP_MI)(cl_heap_univpoly_ring::expt_pos(x,y));
100 const cl_UP_MI scalmul (const cl_MI& x, const cl_UP_MI& y)
102 return The2(cl_UP_MI)(cl_heap_univpoly_ring::scalmul(x,y));
104 sintL degree (const cl_UP_MI& x)
106 return cl_heap_univpoly_ring::degree(x);
108 const cl_UP_MI monomial (const cl_MI& x, uintL e)
110 return The2(cl_UP_MI)(cl_heap_univpoly_ring::monomial(x,e));
112 const cl_MI coeff (const cl_UP_MI& x, uintL index)
114 return The2(cl_MI)(cl_heap_univpoly_ring::coeff(x,index));
116 const cl_UP_MI create (sintL deg)
118 return The2(cl_UP_MI)(cl_heap_univpoly_ring::create(deg));
120 void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y)
122 cl_heap_univpoly_ring::set_coeff(x,index,y);
124 void finalize (cl_UP_MI& x)
126 cl_heap_univpoly_ring::finalize(x);
128 const cl_MI eval (const cl_UP_MI& x, const cl_MI& y)
130 return The2(cl_MI)(cl_heap_univpoly_ring::eval(x,y));
133 // No need for any constructors.
134 cl_heap_univpoly_modint_ring ();
137 // Lookup of polynomial rings.
138 inline const cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& r)
139 { return The(cl_univpoly_modint_ring) (cl_find_univpoly_ring((const cl_ring&)r)); }
140 inline const cl_univpoly_modint_ring cl_find_univpoly_ring (const cl_modint_ring& r, const cl_symbol& varname)
141 { return The(cl_univpoly_modint_ring) (cl_find_univpoly_ring((const cl_ring&)r,varname)); }
143 // Operations on polynomials.
146 inline const cl_UP_MI operator+ (const cl_UP_MI& x, const cl_UP_MI& y)
147 { return x.ring()->plus(x,y); }
150 inline const cl_UP_MI operator- (const cl_UP_MI& x)
151 { return x.ring()->uminus(x); }
154 inline const cl_UP_MI operator- (const cl_UP_MI& x, const cl_UP_MI& y)
155 { return x.ring()->minus(x,y); }
158 inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_UP_MI& y)
159 { return x.ring()->mul(x,y); }
162 inline const cl_UP_MI square (const cl_UP_MI& x)
163 { return x.ring()->square(x); }
165 // Exponentiation x^y, where y > 0.
166 inline const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y)
167 { return x.ring()->expt_pos(x,y); }
169 // Scalar multiplication.
170 #if 0 // less efficient
171 inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y)
172 { return y.ring()->mul(y.ring()->canonhom(x),y); }
173 inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y)
174 { return x.ring()->mul(x.ring()->canonhom(y),x); }
176 inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y)
177 { return y.ring()->scalmul(y.ring()->basering()->canonhom(x),y); }
178 inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y)
179 { return x.ring()->scalmul(x.ring()->basering()->canonhom(y),x); }
180 inline const cl_UP_MI operator* (const cl_MI& x, const cl_UP_MI& y)
181 { return y.ring()->scalmul(x,y); }
182 inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_MI& y)
183 { return x.ring()->scalmul(y,x); }
186 inline const cl_MI coeff (const cl_UP_MI& x, uintL index)
187 { return x.ring()->coeff(x,index); }
189 // Destructive modification.
190 inline void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y)
191 { x.ring()->set_coeff(x,index,y); }
192 inline void finalize (cl_UP_MI& x)
193 { x.ring()->finalize(x); }
194 inline void cl_UP_MI::set_coeff (uintL index, const cl_MI& y)
195 { ring()->set_coeff(*this,index,y); }
196 inline void cl_UP_MI::finalize ()
197 { ring()->finalize(*this); }
199 // Evaluation. (No extension of the base ring allowed here for now.)
200 inline const cl_MI cl_UP_MI::operator() (const cl_MI& y) const
202 return ring()->eval(*this,y);
206 inline const cl_UP_MI deriv (const cl_UP_MI& x)
207 { return The2(cl_UP_MI)(deriv((const cl_UP&)x)); }
209 #endif /* _CL_UNIVPOLY_MODINT_H */