]> www.ginac.de Git - cln.git/blob - include/cln/univpoly.h
23bd0336c3e48bda57302ca2c517ba3f0f3eb5a7
[cln.git] / include / cln / univpoly.h
1 // Univariate Polynomials.
2
3 #ifndef _CL_UNIVPOLY_H
4 #define _CL_UNIVPOLY_H
5
6 #include "cln/object.h"
7 #include "cln/ring.h"
8 #include "cln/malloc.h"
9 #include "cln/proplist.h"
10 #include "cln/symbol.h"
11 #include "cln/V.h"
12 #include "cln/io.h"
13
14 namespace cln {
15
16 // To protect against mixing elements of different polynomial rings, every
17 // polynomial carries its ring in itself.
18
19 class cl_heap_univpoly_ring;
20
21 class cl_univpoly_ring : public cl_ring {
22 public:
23         // Default constructor.
24         cl_univpoly_ring ();
25         // Constructor. Takes a cl_heap_univpoly_ring*, increments its refcount.
26         cl_univpoly_ring (cl_heap_univpoly_ring* r);
27         // Private constructor. Doesn't increment the refcount.
28         cl_univpoly_ring (cl_private_thing);
29         // Copy constructor.
30         cl_univpoly_ring (const cl_univpoly_ring&);
31         // Assignment operator.
32         cl_univpoly_ring& operator= (const cl_univpoly_ring&);
33         // Automatic dereferencing.
34         cl_heap_univpoly_ring* operator-> () const
35         { return (cl_heap_univpoly_ring*)heappointer; }
36 };
37 // Copy constructor and assignment operator.
38 CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_ring,cl_ring)
39 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_ring,cl_univpoly_ring)
40
41 // Normal constructor for `cl_univpoly_ring'.
42 inline cl_univpoly_ring::cl_univpoly_ring (cl_heap_univpoly_ring* r)
43         : cl_ring ((cl_private_thing) (cl_inc_pointer_refcount((cl_heap*)r), r)) {}
44 // Private constructor for `cl_univpoly_ring'.
45 inline cl_univpoly_ring::cl_univpoly_ring (cl_private_thing p)
46         : cl_ring (p) {}
47
48 // Operations on univariate polynomial rings.
49
50 inline bool operator== (const cl_univpoly_ring& R1, const cl_univpoly_ring& R2)
51 { return (R1.pointer == R2.pointer); }
52 inline bool operator!= (const cl_univpoly_ring& R1, const cl_univpoly_ring& R2)
53 { return (R1.pointer != R2.pointer); }
54 inline bool operator== (const cl_univpoly_ring& R1, cl_heap_univpoly_ring* R2)
55 { return (R1.pointer == R2); }
56 inline bool operator!= (const cl_univpoly_ring& R1, cl_heap_univpoly_ring* R2)
57 { return (R1.pointer != R2); }
58
59 // Representation of a univariate polynomial.
60
61 class _cl_UP /* cf. _cl_ring_element */ {
62 public:
63         cl_gcpointer rep; // vector of coefficients, a cl_V_any
64         // Default constructor.
65         _cl_UP ();
66 public: /* ugh */
67         // Constructor.
68         _cl_UP (const cl_heap_univpoly_ring* R, const cl_V_any& r) : rep (as_cl_private_thing(r)) { (void)R; }
69         _cl_UP (const cl_univpoly_ring& R, const cl_V_any& r) : rep (as_cl_private_thing(r)) { (void)R; }
70 public:
71         // Conversion.
72         CL_DEFINE_CONVERTER(_cl_ring_element)
73 public: // Ability to place an object at a given address.
74         void* operator new (size_t size) { return malloc_hook(size); }
75         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
76         void operator delete (void* ptr) { free_hook(ptr); }
77 };
78
79 class cl_UP /* cf. cl_ring_element */ : public _cl_UP {
80 protected:
81         cl_univpoly_ring _ring; // polynomial ring (references the base ring)
82 public:
83         const cl_univpoly_ring& ring () const { return _ring; }
84 private:
85         // Default constructor.
86         cl_UP ();
87 public: /* ugh */
88         // Constructor.
89         cl_UP (const cl_univpoly_ring& R, const cl_V_any& r)
90                 : _cl_UP (R,r), _ring (R) {}
91         cl_UP (const cl_univpoly_ring& R, const _cl_UP& r)
92                 : _cl_UP (r), _ring (R) {}
93 public:
94         // Conversion.
95         CL_DEFINE_CONVERTER(cl_ring_element)
96         // Destructive modification.
97         void set_coeff (uintL index, const cl_ring_element& y);
98         void finalize();
99         // Evaluation.
100         const cl_ring_element operator() (const cl_ring_element& y) const;
101         // Debugging output.
102         void debug_print () const;
103 public: // Ability to place an object at a given address.
104         void* operator new (size_t size) { return malloc_hook(size); }
105         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
106         void operator delete (void* ptr) { free_hook(ptr); }
107 };
108
109
110 // Ring operations.
111
112 struct _cl_univpoly_setops /* cf. _cl_ring_setops */ {
113         // print
114         void (* fprint) (cl_heap_univpoly_ring* R, std::ostream& stream, const _cl_UP& x);
115         // equality
116         // (Be careful: This is not well-defined for polynomials with
117         // floating-point coefficients.)
118         cl_boolean (* equal) (cl_heap_univpoly_ring* R, const _cl_UP& x, const _cl_UP& y);
119 };
120 struct _cl_univpoly_addops /* cf. _cl_ring_addops */ {
121         // 0
122         const _cl_UP (* zero) (cl_heap_univpoly_ring* R);
123         cl_boolean (* zerop) (cl_heap_univpoly_ring* R, const _cl_UP& x);
124         // x+y
125         const _cl_UP (* plus) (cl_heap_univpoly_ring* R, const _cl_UP& x, const _cl_UP& y);
126         // x-y
127         const _cl_UP (* minus) (cl_heap_univpoly_ring* R, const _cl_UP& x, const _cl_UP& y);
128         // -x
129         const _cl_UP (* uminus) (cl_heap_univpoly_ring* R, const _cl_UP& x);
130 };
131 struct _cl_univpoly_mulops /* cf. _cl_ring_mulops */ {
132         // 1
133         const _cl_UP (* one) (cl_heap_univpoly_ring* R);
134         // canonical homomorphism
135         const _cl_UP (* canonhom) (cl_heap_univpoly_ring* R, const cl_I& x);
136         // x*y
137         const _cl_UP (* mul) (cl_heap_univpoly_ring* R, const _cl_UP& x, const _cl_UP& y);
138         // x^2
139         const _cl_UP (* square) (cl_heap_univpoly_ring* R, const _cl_UP& x);
140         // x^y, y Integer >0
141         const _cl_UP (* expt_pos) (cl_heap_univpoly_ring* R, const _cl_UP& x, const cl_I& y);
142 };
143 struct _cl_univpoly_modulops {
144         // scalar multiplication x*y
145         const _cl_UP (* scalmul) (cl_heap_univpoly_ring* R, const cl_ring_element& x, const _cl_UP& y);
146 };
147 struct _cl_univpoly_polyops {
148         // degree
149         sintL (* degree) (cl_heap_univpoly_ring* R, const _cl_UP& x);
150         // monomial
151         const _cl_UP (* monomial) (cl_heap_univpoly_ring* R, const cl_ring_element& x, uintL e);
152         // coefficient (0 if index>degree)
153         const cl_ring_element (* coeff) (cl_heap_univpoly_ring* R, const _cl_UP& x, uintL index);
154         // create new polynomial, bounded degree
155         const _cl_UP (* create) (cl_heap_univpoly_ring* R, sintL deg);
156         // set coefficient in new polynomial
157         void (* set_coeff) (cl_heap_univpoly_ring* R, _cl_UP& x, uintL index, const cl_ring_element& y);
158         // finalize polynomial
159         void (* finalize) (cl_heap_univpoly_ring* R, _cl_UP& x);
160         // evaluate, substitute an element of R
161         const cl_ring_element (* eval) (cl_heap_univpoly_ring* R, const _cl_UP& x, const cl_ring_element& y);
162 };
163   typedef const _cl_univpoly_setops  cl_univpoly_setops;
164   typedef const _cl_univpoly_addops  cl_univpoly_addops;
165   typedef const _cl_univpoly_mulops  cl_univpoly_mulops;
166   typedef const _cl_univpoly_modulops  cl_univpoly_modulops;
167   typedef const _cl_univpoly_polyops  cl_univpoly_polyops;
168
169 // Representation of a univariate polynomial ring.
170
171 class cl_heap_univpoly_ring /* cf. cl_heap_ring */ : public cl_heap {
172         SUBCLASS_cl_heap_ring()
173 private:
174         cl_property_list properties;
175 protected:
176         cl_univpoly_setops* setops;
177         cl_univpoly_addops* addops;
178         cl_univpoly_mulops* mulops;
179         cl_univpoly_modulops* modulops;
180         cl_univpoly_polyops* polyops;
181 protected:
182         cl_ring _basering;      // the coefficients are elements of this ring
183 public:
184         const cl_ring& basering () const { return _basering; }
185 public:
186         // Low-level operations.
187         void _fprint (std::ostream& stream, const _cl_UP& x)
188                 { setops->fprint(this,stream,x); }
189         cl_boolean _equal (const _cl_UP& x, const _cl_UP& y)
190                 { return setops->equal(this,x,y); }
191         const _cl_UP _zero ()
192                 { return addops->zero(this); }
193         cl_boolean _zerop (const _cl_UP& x)
194                 { return addops->zerop(this,x); }
195         const _cl_UP _plus (const _cl_UP& x, const _cl_UP& y)
196                 { return addops->plus(this,x,y); }
197         const _cl_UP _minus (const _cl_UP& x, const _cl_UP& y)
198                 { return addops->minus(this,x,y); }
199         const _cl_UP _uminus (const _cl_UP& x)
200                 { return addops->uminus(this,x); }
201         const _cl_UP _one ()
202                 { return mulops->one(this); }
203         const _cl_UP _canonhom (const cl_I& x)
204                 { return mulops->canonhom(this,x); }
205         const _cl_UP _mul (const _cl_UP& x, const _cl_UP& y)
206                 { return mulops->mul(this,x,y); }
207         const _cl_UP _square (const _cl_UP& x)
208                 { return mulops->square(this,x); }
209         const _cl_UP _expt_pos (const _cl_UP& x, const cl_I& y)
210                 { return mulops->expt_pos(this,x,y); }
211         const _cl_UP _scalmul (const cl_ring_element& x, const _cl_UP& y)
212                 { return modulops->scalmul(this,x,y); }
213         sintL _degree (const _cl_UP& x)
214                 { return polyops->degree(this,x); }
215         const _cl_UP _monomial (const cl_ring_element& x, uintL e)
216                 { return polyops->monomial(this,x,e); }
217         const cl_ring_element _coeff (const _cl_UP& x, uintL index)
218                 { return polyops->coeff(this,x,index); }
219         const _cl_UP _create (sintL deg)
220                 { return polyops->create(this,deg); }
221         void _set_coeff (_cl_UP& x, uintL index, const cl_ring_element& y)
222                 { polyops->set_coeff(this,x,index,y); }
223         void _finalize (_cl_UP& x)
224                 { polyops->finalize(this,x); }
225         const cl_ring_element _eval (const _cl_UP& x, const cl_ring_element& y)
226                 { return polyops->eval(this,x,y); }
227         // High-level operations.
228         void fprint (std::ostream& stream, const cl_UP& x)
229         {
230                 if (!(x.ring() == this)) cl_abort();
231                 _fprint(stream,x);
232         }
233         cl_boolean equal (const cl_UP& x, const cl_UP& y)
234         {
235                 if (!(x.ring() == this)) cl_abort();
236                 if (!(y.ring() == this)) cl_abort();
237                 return _equal(x,y);
238         }
239         const cl_UP zero ()
240         {
241                 return cl_UP(this,_zero());
242         }
243         cl_boolean zerop (const cl_UP& x)
244         {
245                 if (!(x.ring() == this)) cl_abort();
246                 return _zerop(x);
247         }
248         const cl_UP plus (const cl_UP& x, const cl_UP& y)
249         {
250                 if (!(x.ring() == this)) cl_abort();
251                 if (!(y.ring() == this)) cl_abort();
252                 return cl_UP(this,_plus(x,y));
253         }
254         const cl_UP minus (const cl_UP& x, const cl_UP& y)
255         {
256                 if (!(x.ring() == this)) cl_abort();
257                 if (!(y.ring() == this)) cl_abort();
258                 return cl_UP(this,_minus(x,y));
259         }
260         const cl_UP uminus (const cl_UP& x)
261         {
262                 if (!(x.ring() == this)) cl_abort();
263                 return cl_UP(this,_uminus(x));
264         }
265         const cl_UP one ()
266         {
267                 return cl_UP(this,_one());
268         }
269         const cl_UP canonhom (const cl_I& x)
270         {
271                 return cl_UP(this,_canonhom(x));
272         }
273         const cl_UP mul (const cl_UP& x, const cl_UP& y)
274         {
275                 if (!(x.ring() == this)) cl_abort();
276                 if (!(y.ring() == this)) cl_abort();
277                 return cl_UP(this,_mul(x,y));
278         }
279         const cl_UP square (const cl_UP& x)
280         {
281                 if (!(x.ring() == this)) cl_abort();
282                 return cl_UP(this,_square(x));
283         }
284         const cl_UP expt_pos (const cl_UP& x, const cl_I& y)
285         {
286                 if (!(x.ring() == this)) cl_abort();
287                 return cl_UP(this,_expt_pos(x,y));
288         }
289         const cl_UP scalmul (const cl_ring_element& x, const cl_UP& y)
290         {
291                 if (!(y.ring() == this)) cl_abort();
292                 return cl_UP(this,_scalmul(x,y));
293         }
294         sintL degree (const cl_UP& x)
295         {
296                 if (!(x.ring() == this)) cl_abort();
297                 return _degree(x);
298         }
299         const cl_UP monomial (const cl_ring_element& x, uintL e)
300         {
301                 return cl_UP(this,_monomial(x,e));
302         }
303         const cl_ring_element coeff (const cl_UP& x, uintL index)
304         {
305                 if (!(x.ring() == this)) cl_abort();
306                 return _coeff(x,index);
307         }
308         const cl_UP create (sintL deg)
309         {
310                 return cl_UP(this,_create(deg));
311         }
312         void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)
313         {
314                 if (!(x.ring() == this)) cl_abort();
315                 _set_coeff(x,index,y);
316         }
317         void finalize (cl_UP& x)
318         {
319                 if (!(x.ring() == this)) cl_abort();
320                 _finalize(x);
321         }
322         const cl_ring_element eval (const cl_UP& x, const cl_ring_element& y)
323         {
324                 if (!(x.ring() == this)) cl_abort();
325                 return _eval(x,y);
326         }
327         // Property operations.
328         cl_property* get_property (const cl_symbol& key)
329                 { return properties.get_property(key); }
330         void add_property (cl_property* new_property)
331                 { properties.add_property(new_property); }
332 // Constructor.
333         cl_heap_univpoly_ring (const cl_ring& r, cl_univpoly_setops*, cl_univpoly_addops*, cl_univpoly_mulops*, cl_univpoly_modulops*, cl_univpoly_polyops*);
334 // This class is intented to be subclassable, hence needs a virtual destructor.
335         virtual ~cl_heap_univpoly_ring () {}
336 private:
337         virtual void dummy ();
338 };
339 #define SUBCLASS_cl_heap_univpoly_ring() \
340   SUBCLASS_cl_heap_ring()
341
342
343 // Lookup or create the "standard" univariate polynomial ring over a ring r.
344 extern const cl_univpoly_ring find_univpoly_ring (const cl_ring& r);
345 //CL_REQUIRE(cl_UP_unnamed)
346
347 // Lookup or create a univariate polynomial ring with a named variable over r.
348 extern const cl_univpoly_ring find_univpoly_ring (const cl_ring& r, const cl_symbol& varname);
349 //CL_REQUIRE(cl_UP_named)
350
351 CL_REQUIRE(cl_UP)
352
353 // Runtime typing support.
354 extern cl_class cl_class_univpoly_ring;
355
356
357 // Operations on polynomials.
358
359 // Output.
360 inline void fprint (std::ostream& stream, const cl_UP& x)
361         { x.ring()->fprint(stream,x); }
362 CL_DEFINE_PRINT_OPERATOR(cl_UP)
363
364 // Add.
365 inline const cl_UP operator+ (const cl_UP& x, const cl_UP& y)
366         { return x.ring()->plus(x,y); }
367
368 // Negate.
369 inline const cl_UP operator- (const cl_UP& x)
370         { return x.ring()->uminus(x); }
371
372 // Subtract.
373 inline const cl_UP operator- (const cl_UP& x, const cl_UP& y)
374         { return x.ring()->minus(x,y); }
375
376 // Equality.
377 inline bool operator== (const cl_UP& x, const cl_UP& y)
378         { return x.ring()->equal(x,y); }
379 inline bool operator!= (const cl_UP& x, const cl_UP& y)
380         { return !x.ring()->equal(x,y); }
381
382 // Compare against 0.
383 inline cl_boolean zerop (const cl_UP& x)
384         { return x.ring()->zerop(x); }
385
386 // Multiply.
387 inline const cl_UP operator* (const cl_UP& x, const cl_UP& y)
388         { return x.ring()->mul(x,y); }
389
390 // Squaring.
391 inline const cl_UP square (const cl_UP& x)
392         { return x.ring()->square(x); }
393
394 // Exponentiation x^y, where y > 0.
395 inline const cl_UP expt_pos (const cl_UP& x, const cl_I& y)
396         { return x.ring()->expt_pos(x,y); }
397
398 // Scalar multiplication.
399 #if 0 // less efficient
400 inline const cl_UP operator* (const cl_I& x, const cl_UP& y)
401         { return y.ring()->mul(y.ring()->canonhom(x),y); }
402 inline const cl_UP operator* (const cl_UP& x, const cl_I& y)
403         { return x.ring()->mul(x.ring()->canonhom(y),x); }
404 #endif
405 inline const cl_UP operator* (const cl_I& x, const cl_UP& y)
406         { return y.ring()->scalmul(y.ring()->basering()->canonhom(x),y); }
407 inline const cl_UP operator* (const cl_UP& x, const cl_I& y)
408         { return x.ring()->scalmul(x.ring()->basering()->canonhom(y),x); }
409 inline const cl_UP operator* (const cl_ring_element& x, const cl_UP& y)
410         { return y.ring()->scalmul(x,y); }
411 inline const cl_UP operator* (const cl_UP& x, const cl_ring_element& y)
412         { return x.ring()->scalmul(y,x); }
413
414 // Degree.
415 inline sintL degree (const cl_UP& x)
416         { return x.ring()->degree(x); }
417
418 // Coefficient.
419 inline const cl_ring_element coeff (const cl_UP& x, uintL index)
420         { return x.ring()->coeff(x,index); }
421
422 // Destructive modification.
423 inline void set_coeff (cl_UP& x, uintL index, const cl_ring_element& y)
424         { x.ring()->set_coeff(x,index,y); }
425 inline void finalize (cl_UP& x)
426         { x.ring()->finalize(x); }
427 inline void cl_UP::set_coeff (uintL index, const cl_ring_element& y)
428         { ring()->set_coeff(*this,index,y); }
429 inline void cl_UP::finalize ()
430         { ring()->finalize(*this); }
431
432 // Evaluation. (No extension of the base ring allowed here for now.)
433 inline const cl_ring_element cl_UP::operator() (const cl_ring_element& y) const
434 {
435         return ring()->eval(*this,y);
436 }
437
438 // Derivative.
439 extern const cl_UP deriv (const cl_UP& x);
440
441
442 // Ring of uninitialized elements.
443 // Any operation results in a run-time error.
444
445 extern const cl_univpoly_ring cl_no_univpoly_ring;
446 extern cl_class cl_class_no_univpoly_ring;
447 CL_REQUIRE(cl_UP_no_ring)
448
449 inline cl_univpoly_ring::cl_univpoly_ring ()
450         : cl_ring (as_cl_private_thing(cl_no_univpoly_ring)) {}
451 inline _cl_UP::_cl_UP ()
452         : rep ((cl_private_thing) cl_combine(cl_FN_tag,0)) {}
453 inline cl_UP::cl_UP ()
454         : _cl_UP (), _ring () {}
455
456
457 // Debugging support.
458 #ifdef CL_DEBUG
459 extern int cl_UP_debug_module;
460 CL_FORCE_LINK(cl_UP_debug_dummy, cl_UP_debug_module)
461 #endif
462
463 }  // namespace cln
464
465 #endif /* _CL_UNIVPOLY_H */
466
467 namespace cln {
468
469 // Templates for univariate polynomials of complex/real/rational/integers.
470
471 #ifdef notyet
472 // Unfortunately, this is not usable now, because of gcc-2.7 bugs:
473 // - A template inline function is not inline in the first function that
474 //   uses it.
475 // - Argument matching bug: User-defined conversions are not tried (or
476 //   tried with too low priority) for template functions w.r.t. normal
477 //   functions. For example, a call expt_pos(cl_UP_specialized<cl_N>,int)
478 //   is compiled as expt_pos(const cl_UP&, const cl_I&) instead of
479 //   expt_pos(const cl_UP_specialized<cl_N>&, const cl_I&).
480 // It will, however, be usable when gcc-2.8 is released.
481
482 #if defined(_CL_UNIVPOLY_COMPLEX_H) || defined(_CL_UNIVPOLY_REAL_H) || defined(_CL_UNIVPOLY_RATIONAL_H) || defined(_CL_UNIVPOLY_INTEGER_H)
483 #ifndef _CL_UNIVPOLY_AUX_H
484
485 // Normal univariate polynomials with stricter static typing:
486 // `class T' instead of `cl_ring_element'.
487
488 template <class T> class cl_univpoly_specialized_ring;
489 template <class T> class cl_UP_specialized;
490 template <class T> class cl_heap_univpoly_specialized_ring;
491
492 template <class T>
493 class cl_univpoly_specialized_ring : public cl_univpoly_ring {
494 public:
495         // Default constructor.
496         cl_univpoly_specialized_ring () : cl_univpoly_ring () {}
497         // Copy constructor.
498         cl_univpoly_specialized_ring (const cl_univpoly_specialized_ring&);
499         // Assignment operator.
500         cl_univpoly_specialized_ring& operator= (const cl_univpoly_specialized_ring&);
501         // Automatic dereferencing.
502         cl_heap_univpoly_specialized_ring<T>* operator-> () const
503         { return (cl_heap_univpoly_specialized_ring<T>*)heappointer; }
504 };
505 // Copy constructor and assignment operator.
506 template <class T>
507 _CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_specialized_ring<T>,cl_univpoly_specialized_ring,cl_univpoly_ring)
508 template <class T>
509 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_specialized_ring<T>,cl_univpoly_specialized_ring<T>)
510
511 template <class T>
512 class cl_UP_specialized : public cl_UP {
513 public:
514         const cl_univpoly_specialized_ring<T>& ring () const { return The(cl_univpoly_specialized_ring<T>)(_ring); }
515         // Conversion.
516         CL_DEFINE_CONVERTER(cl_ring_element)
517         // Destructive modification.
518         void set_coeff (uintL index, const T& y);
519         void finalize();
520         // Evaluation.
521         const T operator() (const T& y) const;
522 public: // Ability to place an object at a given address.
523         void* operator new (size_t size) { return malloc_hook(size); }
524         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
525         void operator delete (void* ptr) { free_hook(ptr); }
526 };
527
528 template <class T>
529 class cl_heap_univpoly_specialized_ring : public cl_heap_univpoly_ring {
530         SUBCLASS_cl_heap_univpoly_ring()
531         // High-level operations.
532         void fprint (std::ostream& stream, const cl_UP_specialized<T>& x)
533         {
534                 cl_heap_univpoly_ring::fprint(stream,x);
535         }
536         cl_boolean equal (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
537         {
538                 return cl_heap_univpoly_ring::equal(x,y);
539         }
540         const cl_UP_specialized<T> zero ()
541         {
542                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::zero());
543         }
544         cl_boolean zerop (const cl_UP_specialized<T>& x)
545         {
546                 return cl_heap_univpoly_ring::zerop(x);
547         }
548         const cl_UP_specialized<T> plus (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
549         {
550                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::plus(x,y));
551         }
552         const cl_UP_specialized<T> minus (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
553         {
554                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::minus(x,y));
555         }
556         const cl_UP_specialized<T> uminus (const cl_UP_specialized<T>& x)
557         {
558                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::uminus(x));
559         }
560         const cl_UP_specialized<T> one ()
561         {
562                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::one());
563         }
564         const cl_UP_specialized<T> canonhom (const cl_I& x)
565         {
566                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::canonhom(x));
567         }
568         const cl_UP_specialized<T> mul (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
569         {
570                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::mul(x,y));
571         }
572         const cl_UP_specialized<T> square (const cl_UP_specialized<T>& x)
573         {
574                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::square(x));
575         }
576         const cl_UP_specialized<T> expt_pos (const cl_UP_specialized<T>& x, const cl_I& y)
577         {
578                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::expt_pos(x,y));
579         }
580         const cl_UP_specialized<T> scalmul (const T& x, const cl_UP_specialized<T>& y)
581         {
582                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::scalmul(x,y));
583         }
584         sintL degree (const cl_UP_specialized<T>& x)
585         {
586                 return cl_heap_univpoly_ring::degree(x);
587         }
588         const cl_UP_specialized<T> monomial (const T& x, uintL e)
589         {
590                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::monomial(cl_ring_element(cl_C_ring??,x),e));
591         }
592         const T coeff (const cl_UP_specialized<T>& x, uintL index)
593         {
594                 return The(T)(cl_heap_univpoly_ring::coeff(x,index));
595         }
596         const cl_UP_specialized<T> create (sintL deg)
597         {
598                 return The2(cl_UP_specialized<T>)(cl_heap_univpoly_ring::create(deg));
599         }
600         void set_coeff (cl_UP_specialized<T>& x, uintL index, const T& y)
601         {
602                 cl_heap_univpoly_ring::set_coeff(x,index,cl_ring_element(cl_C_ring??,y));
603         }
604         void finalize (cl_UP_specialized<T>& x)
605         {
606                 cl_heap_univpoly_ring::finalize(x);
607         }
608         const T eval (const cl_UP_specialized<T>& x, const T& y)
609         {
610                 return The(T)(cl_heap_univpoly_ring::eval(x,cl_ring_element(cl_C_ring??,y)));
611         }
612 private:
613         // No need for any constructors.
614         cl_heap_univpoly_specialized_ring ();
615 };
616
617 // Lookup of polynomial rings.
618 template <class T>
619 inline const cl_univpoly_specialized_ring<T> find_univpoly_ring (const cl_specialized_number_ring<T>& r)
620 { return The(cl_univpoly_specialized_ring<T>) (find_univpoly_ring((const cl_ring&)r)); }
621 template <class T>
622 inline const cl_univpoly_specialized_ring<T> find_univpoly_ring (const cl_specialized_number_ring<T>& r, const cl_symbol& varname)
623 { return The(cl_univpoly_specialized_ring<T>) (find_univpoly_ring((const cl_ring&)r,varname)); }
624
625 // Operations on polynomials.
626
627 // Add.
628 template <class T>
629 inline const cl_UP_specialized<T> operator+ (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
630         { return x.ring()->plus(x,y); }
631
632 // Negate.
633 template <class T>
634 inline const cl_UP_specialized<T> operator- (const cl_UP_specialized<T>& x)
635         { return x.ring()->uminus(x); }
636
637 // Subtract.
638 template <class T>
639 inline const cl_UP_specialized<T> operator- (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
640         { return x.ring()->minus(x,y); }
641
642 // Multiply.
643 template <class T>
644 inline const cl_UP_specialized<T> operator* (const cl_UP_specialized<T>& x, const cl_UP_specialized<T>& y)
645         { return x.ring()->mul(x,y); }
646
647 // Squaring.
648 template <class T>
649 inline const cl_UP_specialized<T> square (const cl_UP_specialized<T>& x)
650         { return x.ring()->square(x); }
651
652 // Exponentiation x^y, where y > 0.
653 template <class T>
654 inline const cl_UP_specialized<T> expt_pos (const cl_UP_specialized<T>& x, const cl_I& y)
655         { return x.ring()->expt_pos(x,y); }
656
657 // Scalar multiplication.
658 // Need more discrimination on T ??
659 template <class T>
660 inline const cl_UP_specialized<T> operator* (const cl_I& x, const cl_UP_specialized<T>& y)
661         { return y.ring()->mul(y.ring()->canonhom(x),y); }
662 template <class T>
663 inline const cl_UP_specialized<T> operator* (const cl_UP_specialized<T>& x, const cl_I& y)
664         { return x.ring()->mul(x.ring()->canonhom(y),x); }
665 template <class T>
666 inline const cl_UP_specialized<T> operator* (const T& x, const cl_UP_specialized<T>& y)
667         { return y.ring()->scalmul(x,y); }
668 template <class T>
669 inline const cl_UP_specialized<T> operator* (const cl_UP_specialized<T>& x, const T& y)
670         { return x.ring()->scalmul(y,x); }
671
672 // Coefficient.
673 template <class T>
674 inline const T coeff (const cl_UP_specialized<T>& x, uintL index)
675         { return x.ring()->coeff(x,index); }
676
677 // Destructive modification.
678 template <class T>
679 inline void set_coeff (cl_UP_specialized<T>& x, uintL index, const T& y)
680         { x.ring()->set_coeff(x,index,y); }
681 template <class T>
682 inline void finalize (cl_UP_specialized<T>& x)
683         { x.ring()->finalize(x); }
684 template <class T>
685 inline void cl_UP_specialized<T>::set_coeff (uintL index, const T& y)
686         { ring()->set_coeff(*this,index,y); }
687 template <class T>
688 inline void cl_UP_specialized<T>::finalize ()
689         { ring()->finalize(*this); }
690
691 // Evaluation. (No extension of the base ring allowed here for now.)
692 template <class T>
693 inline const T cl_UP_specialized<T>::operator() (const T& y) const
694 {
695         return ring()->eval(*this,y);
696 }
697
698 // Derivative.
699 template <class T>
700 inline const cl_UP_specialized<T> deriv (const cl_UP_specialized<T>& x)
701         { return The(cl_UP_specialized<T>)(deriv((const cl_UP&)x)); }
702
703
704 #endif /* _CL_UNIVPOLY_AUX_H */
705 #endif
706
707 #endif /* notyet */
708
709 }  // namespace cln