1 | // Univariate Polynomials over modular integers. |
2 | |
3 | #ifndef _CL_UNIVPOLY_MODINT_H |
4 | #define _CL_UNIVPOLY_MODINT_H |
5 | |
6 | #include "cln/ring.h" |
7 | #include "cln/univpoly.h" |
8 | #include "cln/modinteger.h" |
9 | #include "cln/integer_class.h" |
10 | |
11 | namespace cln { |
12 | |
13 | // Normal univariate polynomials with stricter static typing: |
14 | // `cl_MI' instead of `cl_ring_element'. |
15 | |
16 | class cl_heap_univpoly_modint_ring; |
17 | |
18 | class cl_univpoly_modint_ring : public cl_univpoly_ring { |
19 | public: |
20 | // Default constructor. |
21 | cl_univpoly_modint_ring () : cl_univpoly_ring () {} |
22 | // Copy constructor. |
23 | cl_univpoly_modint_ring (const cl_univpoly_modint_ring&); |
24 | // Assignment operator. |
25 | cl_univpoly_modint_ring& operator= (const cl_univpoly_modint_ring&); |
26 | // Automatic dereferencing. |
27 | cl_heap_univpoly_modint_ring* operator-> () const |
28 | { return (cl_heap_univpoly_modint_ring*)heappointer; } |
29 | }; |
30 | // Copy constructor and assignment operator. |
31 | CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_modint_ring,cl_univpoly_ring) |
32 | CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_modint_ring,cl_univpoly_modint_ring) |
33 | |
34 | class cl_UP_MI : public cl_UP { |
35 | public: |
36 | const cl_univpoly_modint_ring& ring () const { return The(cl_univpoly_modint_ring)(_ring); } |
37 | // Conversion. |
38 | CL_DEFINE_CONVERTER(cl_ring_element) |
39 | // Destructive modification. |
40 | void set_coeff (uintL index, const cl_MI& y); |
41 | void finalize(); |
42 | // Evaluation. |
43 | const cl_MI operator() (const cl_MI& y) const; |
44 | public: // Ability to place an object at a given address. |
45 | void* operator new (size_t size) { return malloc_hook(size); } |
46 | void* operator new (size_t size, void* ptr) { (void)size; return ptr; } |
47 | void operator delete (void* ptr) { free_hook(ptr); } |
48 | }; |
49 | |
50 | class cl_heap_univpoly_modint_ring : public cl_heap_univpoly_ring { |
51 | SUBCLASS_cl_heap_univpoly_ring() |
52 | const cl_modint_ring& basering () const { return The(cl_modint_ring)(_basering); } |
53 | // High-level operations. |
54 | void fprint (std::ostream& stream, const cl_UP_MI& x) |
55 | { |
56 | cl_heap_univpoly_ring::fprint(stream,x); |
57 | } |
58 | bool equal (const cl_UP_MI& x, const cl_UP_MI& y) |
59 | { |
60 | return cl_heap_univpoly_ring::equal(x,y); |
61 | } |
62 | const cl_UP_MI zero () |
63 | { |
64 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::zero()); |
65 | } |
66 | bool zerop (const cl_UP_MI& x) |
67 | { |
68 | return cl_heap_univpoly_ring::zerop(x); |
69 | } |
70 | const cl_UP_MI plus (const cl_UP_MI& x, const cl_UP_MI& y) |
71 | { |
72 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::plus(x,y)); |
73 | } |
74 | const cl_UP_MI minus (const cl_UP_MI& x, const cl_UP_MI& y) |
75 | { |
76 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::minus(x,y)); |
77 | } |
78 | const cl_UP_MI uminus (const cl_UP_MI& x) |
79 | { |
80 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::uminus(x)); |
81 | } |
82 | const cl_UP_MI one () |
83 | { |
84 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::one()); |
85 | } |
86 | const cl_UP_MI canonhom (const cl_I& x) |
87 | { |
88 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::canonhom(x)); |
89 | } |
90 | const cl_UP_MI mul (const cl_UP_MI& x, const cl_UP_MI& y) |
91 | { |
92 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::mul(x,y)); |
93 | } |
94 | const cl_UP_MI square (const cl_UP_MI& x) |
95 | { |
96 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::square(x)); |
97 | } |
98 | const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y) |
99 | { |
100 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::expt_pos(x,y)); |
101 | } |
102 | const cl_UP_MI scalmul (const cl_MI& x, const cl_UP_MI& y) |
103 | { |
104 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::scalmul(x,y)); |
105 | } |
106 | sintL degree (const cl_UP_MI& x) |
107 | { |
108 | return cl_heap_univpoly_ring::degree(x); |
109 | } |
110 | sintL ldegree (const cl_UP_MI& x) |
111 | { |
112 | return cl_heap_univpoly_ring::ldegree(x); |
113 | } |
114 | const cl_UP_MI monomial (const cl_MI& x, uintL e) |
115 | { |
116 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::monomial(x,e)); |
117 | } |
118 | const cl_MI coeff (const cl_UP_MI& x, uintL index) |
119 | { |
120 | return The2(cl_MI)(cl_heap_univpoly_ring::coeff(x,index)); |
121 | } |
122 | const cl_UP_MI create (sintL deg) |
123 | { |
124 | return The2(cl_UP_MI)(cl_heap_univpoly_ring::create(deg)); |
125 | } |
126 | void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y) |
127 | { |
128 | cl_heap_univpoly_ring::set_coeff(x,index,y); |
129 | } |
130 | void finalize (cl_UP_MI& x) |
131 | { |
132 | cl_heap_univpoly_ring::finalize(x); |
133 | } |
134 | const cl_MI eval (const cl_UP_MI& x, const cl_MI& y) |
135 | { |
136 | return The2(cl_MI)(cl_heap_univpoly_ring::eval(x,y)); |
137 | } |
138 | private: |
139 | // No need for any constructors. |
140 | cl_heap_univpoly_modint_ring (); |
141 | }; |
142 | |
143 | // Lookup of polynomial rings. |
144 | inline const cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& r) |
145 | { return The(cl_univpoly_modint_ring) (find_univpoly_ring((const cl_ring&)r)); } |
146 | inline const cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& r, const cl_symbol& varname) |
147 | { return The(cl_univpoly_modint_ring) (find_univpoly_ring((const cl_ring&)r,varname)); } |
148 | |
149 | // Operations on polynomials. |
150 | |
151 | // Add. |
152 | inline const cl_UP_MI operator+ (const cl_UP_MI& x, const cl_UP_MI& y) |
153 | { return x.ring()->plus(x,y); } |
154 | |
155 | // Negate. |
156 | inline const cl_UP_MI operator- (const cl_UP_MI& x) |
157 | { return x.ring()->uminus(x); } |
158 | |
159 | // Subtract. |
160 | inline const cl_UP_MI operator- (const cl_UP_MI& x, const cl_UP_MI& y) |
161 | { return x.ring()->minus(x,y); } |
162 | |
163 | // Multiply. |
164 | inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_UP_MI& y) |
165 | { return x.ring()->mul(x,y); } |
166 | |
167 | // Squaring. |
168 | inline const cl_UP_MI square (const cl_UP_MI& x) |
169 | { return x.ring()->square(x); } |
170 | |
171 | // Exponentiation x^y, where y > 0. |
172 | inline const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y) |
173 | { return x.ring()->expt_pos(x,y); } |
174 | |
175 | // Scalar multiplication. |
176 | #if 0 // less efficient |
177 | inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y) |
178 | { return y.ring()->mul(y.ring()->canonhom(x),y); } |
179 | inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y) |
180 | { return x.ring()->mul(x.ring()->canonhom(y),x); } |
181 | #endif |
182 | inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y) |
183 | { return y.ring()->scalmul(y.ring()->basering()->canonhom(x),y); } |
184 | inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y) |
185 | { return x.ring()->scalmul(x.ring()->basering()->canonhom(y),x); } |
186 | inline const cl_UP_MI operator* (const cl_MI& x, const cl_UP_MI& y) |
187 | { return y.ring()->scalmul(x,y); } |
188 | inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_MI& y) |
189 | { return x.ring()->scalmul(y,x); } |
190 | |
191 | // Coefficient. |
192 | inline const cl_MI coeff (const cl_UP_MI& x, uintL index) |
193 | { return x.ring()->coeff(x,index); } |
194 | |
195 | // Destructive modification. |
196 | inline void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y) |
197 | { x.ring()->set_coeff(x,index,y); } |
198 | inline void finalize (cl_UP_MI& x) |
199 | { x.ring()->finalize(x); } |
200 | inline void cl_UP_MI::set_coeff (uintL index, const cl_MI& y) |
201 | { ring()->set_coeff(*this,index,y); } |
202 | inline void cl_UP_MI::finalize () |
203 | { ring()->finalize(*this); } |
204 | |
205 | // Evaluation. (No extension of the base ring allowed here for now.) |
206 | inline const cl_MI cl_UP_MI::operator() (const cl_MI& y) const |
207 | { |
208 | return ring()->eval(*this,y); |
209 | } |
210 | |
211 | // Derivative. |
212 | inline const cl_UP_MI deriv (const cl_UP_MI& x) |
213 | { return The2(cl_UP_MI)(deriv((const cl_UP&)x)); } |
214 | |
215 | } // namespace cln |
216 | |
217 | #endif /* _CL_UNIVPOLY_MODINT_H */ |
218 | |