]> www.ginac.de Git - cln.git/blob - include/cl_univpoly_modint.h
83d6aa9f257eb1daa8297072a8ead1eda168e04f
[cln.git] / include / cl_univpoly_modint.h
1 // Univariate Polynomials over modular integers.
2
3 #ifndef _CL_UNIVPOLY_MODINT_H
4 #define _CL_UNIVPOLY_MODINT_H
5
6 #include "cl_ring.h"
7 #include "cl_univpoly.h"
8 #include "cl_modinteger.h"
9 #include "cl_integer_class.h"
10
11 // Normal univariate polynomials with stricter static typing:
12 // `cl_MI' instead of `cl_ring_element'.
13
14 class cl_heap_univpoly_modint_ring;
15
16 class cl_univpoly_modint_ring : public cl_univpoly_ring {
17 public:
18         // Default constructor.
19         cl_univpoly_modint_ring () : cl_univpoly_ring () {}
20         // Copy constructor.
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; }
27 };
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)
31
32 class cl_UP_MI : public cl_UP {
33 public:
34         const cl_univpoly_modint_ring& ring () const { return The(cl_univpoly_modint_ring)(_ring); }
35         // Conversion.
36         CL_DEFINE_CONVERTER(cl_ring_element)
37         // Destructive modification.
38         void set_coeff (uintL index, const cl_MI& y);
39         void finalize();
40         // Evaluation.
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); }
46 };
47
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)
53         {
54                 cl_heap_univpoly_ring::fprint(stream,x);
55         }
56         cl_boolean equal (const cl_UP_MI& x, const cl_UP_MI& y)
57         {
58                 return cl_heap_univpoly_ring::equal(x,y);
59         }
60         const cl_UP_MI zero ()
61         {
62                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::zero());
63         }
64         cl_boolean zerop (const cl_UP_MI& x)
65         {
66                 return cl_heap_univpoly_ring::zerop(x);
67         }
68         const cl_UP_MI plus (const cl_UP_MI& x, const cl_UP_MI& y)
69         {
70                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::plus(x,y));
71         }
72         const cl_UP_MI minus (const cl_UP_MI& x, const cl_UP_MI& y)
73         {
74                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::minus(x,y));
75         }
76         const cl_UP_MI uminus (const cl_UP_MI& x)
77         {
78                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::uminus(x));
79         }
80         const cl_UP_MI one ()
81         {
82                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::one());
83         }
84         const cl_UP_MI canonhom (const cl_I& x)
85         {
86                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::canonhom(x));
87         }
88         const cl_UP_MI mul (const cl_UP_MI& x, const cl_UP_MI& y)
89         {
90                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::mul(x,y));
91         }
92         const cl_UP_MI square (const cl_UP_MI& x)
93         {
94                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::square(x));
95         }
96         const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y)
97         {
98                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::expt_pos(x,y));
99         }
100         const cl_UP_MI scalmul (const cl_MI& x, const cl_UP_MI& y)
101         {
102                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::scalmul(x,y));
103         }
104         sintL degree (const cl_UP_MI& x)
105         {
106                 return cl_heap_univpoly_ring::degree(x);
107         }
108         const cl_UP_MI monomial (const cl_MI& x, uintL e)
109         {
110                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::monomial(x,e));
111         }
112         const cl_MI coeff (const cl_UP_MI& x, uintL index)
113         {
114                 return The2(cl_MI)(cl_heap_univpoly_ring::coeff(x,index));
115         }
116         const cl_UP_MI create (sintL deg)
117         {
118                 return The2(cl_UP_MI)(cl_heap_univpoly_ring::create(deg));
119         }
120         void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y)
121         {
122                 cl_heap_univpoly_ring::set_coeff(x,index,y);
123         }
124         void finalize (cl_UP_MI& x)
125         {
126                 cl_heap_univpoly_ring::finalize(x);
127         }
128         const cl_MI eval (const cl_UP_MI& x, const cl_MI& y)
129         {
130                 return The2(cl_MI)(cl_heap_univpoly_ring::eval(x,y));
131         }
132 private:
133         // No need for any constructors.
134         cl_heap_univpoly_modint_ring ();
135 };
136
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)); }
142
143 // Operations on polynomials.
144
145 // Add.
146 inline const cl_UP_MI operator+ (const cl_UP_MI& x, const cl_UP_MI& y)
147         { return x.ring()->plus(x,y); }
148
149 // Negate.
150 inline const cl_UP_MI operator- (const cl_UP_MI& x)
151         { return x.ring()->uminus(x); }
152
153 // Subtract.
154 inline const cl_UP_MI operator- (const cl_UP_MI& x, const cl_UP_MI& y)
155         { return x.ring()->minus(x,y); }
156
157 // Multiply.
158 inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_UP_MI& y)
159         { return x.ring()->mul(x,y); }
160
161 // Squaring.
162 inline const cl_UP_MI square (const cl_UP_MI& x)
163         { return x.ring()->square(x); }
164
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); }
168
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); }
175 #endif
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); }
184
185 // Coefficient.
186 inline const cl_MI coeff (const cl_UP_MI& x, uintL index)
187         { return x.ring()->coeff(x,index); }
188
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); }
198
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
201 {
202         return ring()->eval(*this,y);
203 }
204
205 // Derivative.
206 inline const cl_UP_MI deriv (const cl_UP_MI& x)
207         { return The2(cl_UP_MI)(deriv((const cl_UP&)x)); }
208
209 #endif /* _CL_UNIVPOLY_MODINT_H */