1 // Univariate Polynomials over the real numbers.
3 #ifndef _CL_UNIVPOLY_REAL_H
4 #define _CL_UNIVPOLY_REAL_H
7 #include "cl_univpoly.h"
9 #include "cl_real_class.h"
10 #include "cl_integer_class.h"
11 #include "cl_real_ring.h"
13 // Normal univariate polynomials with stricter static typing:
14 // `cl_R' instead of `cl_ring_element'.
18 typedef cl_UP_specialized<cl_R> cl_UP_R;
19 typedef cl_univpoly_specialized_ring<cl_R> cl_univpoly_real_ring;
20 //typedef cl_heap_univpoly_specialized_ring<cl_R> cl_heap_univpoly_real_ring;
24 class cl_heap_univpoly_real_ring;
26 class cl_univpoly_real_ring : public cl_univpoly_ring {
28 // Default constructor.
29 cl_univpoly_real_ring () : cl_univpoly_ring () {}
31 cl_univpoly_real_ring (const cl_univpoly_real_ring&);
32 // Assignment operator.
33 cl_univpoly_real_ring& operator= (const cl_univpoly_real_ring&);
34 // Automatic dereferencing.
35 cl_heap_univpoly_real_ring* operator-> () const
36 { return (cl_heap_univpoly_real_ring*)heappointer; }
38 // Copy constructor and assignment operator.
39 CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_real_ring,cl_univpoly_ring)
40 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_real_ring,cl_univpoly_real_ring)
42 class cl_UP_R : public cl_UP {
44 const cl_univpoly_real_ring& ring () const { return The(cl_univpoly_real_ring)(_ring); }
46 CL_DEFINE_CONVERTER(cl_ring_element)
47 // Destructive modification.
48 void set_coeff (uintL index, const cl_R& y);
51 const cl_R operator() (const cl_R& y) const;
52 public: // Ability to place an object at a given address.
53 void* operator new (size_t size) { return cl_malloc_hook(size); }
54 void* operator new (size_t size, cl_UP_R* ptr) { (void)size; return ptr; }
55 void operator delete (void* ptr) { cl_free_hook(ptr); }
58 class cl_heap_univpoly_real_ring : public cl_heap_univpoly_ring {
59 SUBCLASS_cl_heap_univpoly_ring()
60 // High-level operations.
61 void fprint (cl_ostream stream, const cl_UP_R& x)
63 cl_heap_univpoly_ring::fprint(stream,x);
65 cl_boolean equal (const cl_UP_R& x, const cl_UP_R& y)
67 return cl_heap_univpoly_ring::equal(x,y);
71 return The2(cl_UP_R)(cl_heap_univpoly_ring::zero());
73 cl_boolean zerop (const cl_UP_R& x)
75 return cl_heap_univpoly_ring::zerop(x);
77 const cl_UP_R plus (const cl_UP_R& x, const cl_UP_R& y)
79 return The2(cl_UP_R)(cl_heap_univpoly_ring::plus(x,y));
81 const cl_UP_R minus (const cl_UP_R& x, const cl_UP_R& y)
83 return The2(cl_UP_R)(cl_heap_univpoly_ring::minus(x,y));
85 const cl_UP_R uminus (const cl_UP_R& x)
87 return The2(cl_UP_R)(cl_heap_univpoly_ring::uminus(x));
91 return The2(cl_UP_R)(cl_heap_univpoly_ring::one());
93 const cl_UP_R canonhom (const cl_I& x)
95 return The2(cl_UP_R)(cl_heap_univpoly_ring::canonhom(x));
97 const cl_UP_R mul (const cl_UP_R& x, const cl_UP_R& y)
99 return The2(cl_UP_R)(cl_heap_univpoly_ring::mul(x,y));
101 const cl_UP_R square (const cl_UP_R& x)
103 return The2(cl_UP_R)(cl_heap_univpoly_ring::square(x));
105 const cl_UP_R expt_pos (const cl_UP_R& x, const cl_I& y)
107 return The2(cl_UP_R)(cl_heap_univpoly_ring::expt_pos(x,y));
109 const cl_UP_R scalmul (const cl_R& x, const cl_UP_R& y)
111 return The2(cl_UP_R)(cl_heap_univpoly_ring::scalmul(cl_ring_element(cl_R_ring,x),y));
113 sintL degree (const cl_UP_R& x)
115 return cl_heap_univpoly_ring::degree(x);
117 const cl_UP_R monomial (const cl_R& x, uintL e)
119 return The2(cl_UP_R)(cl_heap_univpoly_ring::monomial(cl_ring_element(cl_R_ring,x),e));
121 const cl_R coeff (const cl_UP_R& x, uintL index)
123 return The(cl_R)(cl_heap_univpoly_ring::coeff(x,index));
125 const cl_UP_R create (sintL deg)
127 return The2(cl_UP_R)(cl_heap_univpoly_ring::create(deg));
129 void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
131 cl_heap_univpoly_ring::set_coeff(x,index,cl_ring_element(cl_R_ring,y));
133 void finalize (cl_UP_R& x)
135 cl_heap_univpoly_ring::finalize(x);
137 const cl_R eval (const cl_UP_R& x, const cl_R& y)
139 return The(cl_R)(cl_heap_univpoly_ring::eval(x,cl_ring_element(cl_R_ring,y)));
142 // No need for any constructors.
143 cl_heap_univpoly_real_ring ();
146 // Lookup of polynomial rings.
147 inline const cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& r)
148 { return The(cl_univpoly_real_ring) (cl_find_univpoly_ring((const cl_ring&)r)); }
149 inline const cl_univpoly_real_ring cl_find_univpoly_ring (const cl_real_ring& r, const cl_symbol& varname)
150 { return The(cl_univpoly_real_ring) (cl_find_univpoly_ring((const cl_ring&)r,varname)); }
152 // Operations on polynomials.
155 inline const cl_UP_R operator+ (const cl_UP_R& x, const cl_UP_R& y)
156 { return x.ring()->plus(x,y); }
159 inline const cl_UP_R operator- (const cl_UP_R& x)
160 { return x.ring()->uminus(x); }
163 inline const cl_UP_R operator- (const cl_UP_R& x, const cl_UP_R& y)
164 { return x.ring()->minus(x,y); }
167 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_UP_R& y)
168 { return x.ring()->mul(x,y); }
171 inline const cl_UP_R square (const cl_UP_R& x)
172 { return x.ring()->square(x); }
174 // Exponentiation x^y, where y > 0.
175 inline const cl_UP_R expt_pos (const cl_UP_R& x, const cl_I& y)
176 { return x.ring()->expt_pos(x,y); }
178 // Scalar multiplication.
179 #if 0 // less efficient
180 inline const cl_UP_R operator* (const cl_I& x, const cl_UP_R& y)
181 { return y.ring()->mul(y.ring()->canonhom(x),y); }
182 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_I& y)
183 { return x.ring()->mul(x.ring()->canonhom(y),x); }
185 inline const cl_UP_R operator* (const cl_I& x, const cl_UP_R& y)
186 { return y.ring()->scalmul(x,y); }
187 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_I& y)
188 { return x.ring()->scalmul(y,x); }
189 inline const cl_UP_R operator* (const cl_R& x, const cl_UP_R& y)
190 { return y.ring()->scalmul(x,y); }
191 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_R& y)
192 { return x.ring()->scalmul(y,x); }
195 inline const cl_R coeff (const cl_UP_R& x, uintL index)
196 { return x.ring()->coeff(x,index); }
198 // Destructive modification.
199 inline void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
200 { x.ring()->set_coeff(x,index,y); }
201 inline void finalize (cl_UP_R& x)
202 { x.ring()->finalize(x); }
203 inline void cl_UP_R::set_coeff (uintL index, const cl_R& y)
204 { ring()->set_coeff(*this,index,y); }
205 inline void cl_UP_R::finalize ()
206 { ring()->finalize(*this); }
208 // Evaluation. (No extension of the base ring allowed here for now.)
209 inline const cl_R cl_UP_R::operator() (const cl_R& y) const
211 return ring()->eval(*this,y);
215 inline const cl_UP_R deriv (const cl_UP_R& x)
216 { return The2(cl_UP_R)(deriv((const cl_UP&)x)); }
220 CL_REQUIRE(cl_R_ring)
222 #endif /* _CL_UNIVPOLY_REAL_H */