You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.7 KiB

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