1 // Univariate Polynomials over the real numbers.
3 #ifndef _CL_UNIVPOLY_REAL_H
4 #define _CL_UNIVPOLY_REAL_H
7 #include "cln/univpoly.h"
8 #include "cln/number.h"
9 #include "cln/real_class.h"
10 #include "cln/integer_class.h"
11 #include "cln/real_ring.h"
15 // Normal univariate polynomials with stricter static typing:
16 // `cl_R' instead of `cl_ring_element'.
20 typedef cl_UP_specialized<cl_R> cl_UP_R;
21 typedef cl_univpoly_specialized_ring<cl_R> cl_univpoly_real_ring;
22 //typedef cl_heap_univpoly_specialized_ring<cl_R> cl_heap_univpoly_real_ring;
26 class cl_heap_univpoly_real_ring;
28 class cl_univpoly_real_ring : public cl_univpoly_ring {
30 // Default constructor.
31 cl_univpoly_real_ring () : cl_univpoly_ring () {}
33 cl_univpoly_real_ring (const cl_univpoly_real_ring&);
34 // Assignment operator.
35 cl_univpoly_real_ring& operator= (const cl_univpoly_real_ring&);
36 // Automatic dereferencing.
37 cl_heap_univpoly_real_ring* operator-> () const
38 { return (cl_heap_univpoly_real_ring*)heappointer; }
40 // Copy constructor and assignment operator.
41 CL_DEFINE_COPY_CONSTRUCTOR2(cl_univpoly_real_ring,cl_univpoly_ring)
42 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_univpoly_real_ring,cl_univpoly_real_ring)
44 class cl_UP_R : public cl_UP {
46 const cl_univpoly_real_ring& ring () const { return The(cl_univpoly_real_ring)(_ring); }
48 CL_DEFINE_CONVERTER(cl_ring_element)
49 // Destructive modification.
50 void set_coeff (uintL index, const cl_R& y);
53 const cl_R operator() (const cl_R& y) const;
54 public: // Ability to place an object at a given address.
55 void* operator new (size_t size) { return malloc_hook(size); }
56 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
57 void operator delete (void* ptr) { free_hook(ptr); }
60 class cl_heap_univpoly_real_ring : public cl_heap_univpoly_ring {
61 SUBCLASS_cl_heap_univpoly_ring()
62 // High-level operations.
63 void fprint (std::ostream& stream, const cl_UP_R& x)
65 cl_heap_univpoly_ring::fprint(stream,x);
67 bool equal (const cl_UP_R& x, const cl_UP_R& y)
69 return cl_heap_univpoly_ring::equal(x,y);
73 return The2(cl_UP_R)(cl_heap_univpoly_ring::zero());
75 bool zerop (const cl_UP_R& x)
77 return cl_heap_univpoly_ring::zerop(x);
79 const cl_UP_R plus (const cl_UP_R& x, const cl_UP_R& y)
81 return The2(cl_UP_R)(cl_heap_univpoly_ring::plus(x,y));
83 const cl_UP_R minus (const cl_UP_R& x, const cl_UP_R& y)
85 return The2(cl_UP_R)(cl_heap_univpoly_ring::minus(x,y));
87 const cl_UP_R uminus (const cl_UP_R& x)
89 return The2(cl_UP_R)(cl_heap_univpoly_ring::uminus(x));
93 return The2(cl_UP_R)(cl_heap_univpoly_ring::one());
95 const cl_UP_R canonhom (const cl_I& x)
97 return The2(cl_UP_R)(cl_heap_univpoly_ring::canonhom(x));
99 const cl_UP_R mul (const cl_UP_R& x, const cl_UP_R& y)
101 return The2(cl_UP_R)(cl_heap_univpoly_ring::mul(x,y));
103 const cl_UP_R square (const cl_UP_R& x)
105 return The2(cl_UP_R)(cl_heap_univpoly_ring::square(x));
107 const cl_UP_R expt_pos (const cl_UP_R& x, const cl_I& y)
109 return The2(cl_UP_R)(cl_heap_univpoly_ring::expt_pos(x,y));
111 const cl_UP_R scalmul (const cl_R& x, const cl_UP_R& y)
113 return The2(cl_UP_R)(cl_heap_univpoly_ring::scalmul(cl_ring_element(cl_R_ring,x),y));
115 sintL degree (const cl_UP_R& x)
117 return cl_heap_univpoly_ring::degree(x);
119 sintL ldegree (const cl_UP_R& x)
121 return cl_heap_univpoly_ring::ldegree(x);
123 const cl_UP_R monomial (const cl_R& x, uintL e)
125 return The2(cl_UP_R)(cl_heap_univpoly_ring::monomial(cl_ring_element(cl_R_ring,x),e));
127 const cl_R coeff (const cl_UP_R& x, uintL index)
129 return The(cl_R)(cl_heap_univpoly_ring::coeff(x,index));
131 const cl_UP_R create (sintL deg)
133 return The2(cl_UP_R)(cl_heap_univpoly_ring::create(deg));
135 void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
137 cl_heap_univpoly_ring::set_coeff(x,index,cl_ring_element(cl_R_ring,y));
139 void finalize (cl_UP_R& x)
141 cl_heap_univpoly_ring::finalize(x);
143 const cl_R eval (const cl_UP_R& x, const cl_R& y)
145 return The(cl_R)(cl_heap_univpoly_ring::eval(x,cl_ring_element(cl_R_ring,y)));
148 // No need for any constructors.
149 cl_heap_univpoly_real_ring ();
152 // Lookup of polynomial rings.
153 inline const cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& r)
154 { return The(cl_univpoly_real_ring) (find_univpoly_ring((const cl_ring&)r)); }
155 inline const cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& r, const cl_symbol& varname)
156 { return The(cl_univpoly_real_ring) (find_univpoly_ring((const cl_ring&)r,varname)); }
158 // Operations on polynomials.
161 inline const cl_UP_R operator+ (const cl_UP_R& x, const cl_UP_R& y)
162 { return x.ring()->plus(x,y); }
165 inline const cl_UP_R operator- (const cl_UP_R& x)
166 { return x.ring()->uminus(x); }
169 inline const cl_UP_R operator- (const cl_UP_R& x, const cl_UP_R& y)
170 { return x.ring()->minus(x,y); }
173 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_UP_R& y)
174 { return x.ring()->mul(x,y); }
177 inline const cl_UP_R square (const cl_UP_R& x)
178 { return x.ring()->square(x); }
180 // Exponentiation x^y, where y > 0.
181 inline const cl_UP_R expt_pos (const cl_UP_R& x, const cl_I& y)
182 { return x.ring()->expt_pos(x,y); }
184 // Scalar multiplication.
185 #if 0 // less efficient
186 inline const cl_UP_R operator* (const cl_I& x, const cl_UP_R& y)
187 { return y.ring()->mul(y.ring()->canonhom(x),y); }
188 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_I& y)
189 { return x.ring()->mul(x.ring()->canonhom(y),x); }
191 inline const cl_UP_R operator* (const cl_I& x, const cl_UP_R& y)
192 { return y.ring()->scalmul(x,y); }
193 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_I& y)
194 { return x.ring()->scalmul(y,x); }
195 inline const cl_UP_R operator* (const cl_R& x, const cl_UP_R& y)
196 { return y.ring()->scalmul(x,y); }
197 inline const cl_UP_R operator* (const cl_UP_R& x, const cl_R& y)
198 { return x.ring()->scalmul(y,x); }
201 inline const cl_R coeff (const cl_UP_R& x, uintL index)
202 { return x.ring()->coeff(x,index); }
204 // Destructive modification.
205 inline void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
206 { x.ring()->set_coeff(x,index,y); }
207 inline void finalize (cl_UP_R& x)
208 { x.ring()->finalize(x); }
209 inline void cl_UP_R::set_coeff (uintL index, const cl_R& y)
210 { ring()->set_coeff(*this,index,y); }
211 inline void cl_UP_R::finalize ()
212 { ring()->finalize(*this); }
214 // Evaluation. (No extension of the base ring allowed here for now.)
215 inline const cl_R cl_UP_R::operator() (const cl_R& y) const
217 return ring()->eval(*this,y);
221 inline const cl_UP_R deriv (const cl_UP_R& x)
222 { return The2(cl_UP_R)(deriv((const cl_UP&)x)); }
228 #endif /* _CL_UNIVPOLY_REAL_H */