226 lines
6.7 KiB

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