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.

230 lines
6.8 KiB

25 years ago
25 years ago
25 years ago
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, void* 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 (std::ostream& stream, const cl_UP_R& x)
  53. {
  54. cl_heap_univpoly_ring::fprint(stream,x);
  55. }
  56. bool 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. bool 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. sintL ldegree (const cl_UP_R& x)
  109. {
  110. return cl_heap_univpoly_ring::ldegree(x);
  111. }
  112. const cl_UP_R monomial (const cl_R& x, uintL e)
  113. {
  114. return The2(cl_UP_R)(cl_heap_univpoly_ring::monomial(cl_ring_element(cl_R_ring,x),e));
  115. }
  116. const cl_R coeff (const cl_UP_R& x, uintL index)
  117. {
  118. return The(cl_R)(cl_heap_univpoly_ring::coeff(x,index));
  119. }
  120. const cl_UP_R create (sintL deg)
  121. {
  122. return The2(cl_UP_R)(cl_heap_univpoly_ring::create(deg));
  123. }
  124. void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
  125. {
  126. cl_heap_univpoly_ring::set_coeff(x,index,cl_ring_element(cl_R_ring,y));
  127. }
  128. void finalize (cl_UP_R& x)
  129. {
  130. cl_heap_univpoly_ring::finalize(x);
  131. }
  132. const cl_R eval (const cl_UP_R& x, const cl_R& y)
  133. {
  134. return The(cl_R)(cl_heap_univpoly_ring::eval(x,cl_ring_element(cl_R_ring,y)));
  135. }
  136. private:
  137. // No need for any constructors.
  138. cl_heap_univpoly_real_ring ();
  139. };
  140. // Lookup of polynomial rings.
  141. inline const cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& r)
  142. { return The(cl_univpoly_real_ring) (find_univpoly_ring((const cl_ring&)r)); }
  143. inline const cl_univpoly_real_ring find_univpoly_ring (const cl_real_ring& r, const cl_symbol& varname)
  144. { return The(cl_univpoly_real_ring) (find_univpoly_ring((const cl_ring&)r,varname)); }
  145. // Operations on polynomials.
  146. // Add.
  147. inline const cl_UP_R operator+ (const cl_UP_R& x, const cl_UP_R& y)
  148. { return x.ring()->plus(x,y); }
  149. // Negate.
  150. inline const cl_UP_R operator- (const cl_UP_R& x)
  151. { return x.ring()->uminus(x); }
  152. // Subtract.
  153. inline const cl_UP_R operator- (const cl_UP_R& x, const cl_UP_R& y)
  154. { return x.ring()->minus(x,y); }
  155. // Multiply.
  156. inline const cl_UP_R operator* (const cl_UP_R& x, const cl_UP_R& y)
  157. { return x.ring()->mul(x,y); }
  158. // Squaring.
  159. inline const cl_UP_R square (const cl_UP_R& x)
  160. { return x.ring()->square(x); }
  161. // Exponentiation x^y, where y > 0.
  162. inline const cl_UP_R expt_pos (const cl_UP_R& x, const cl_I& y)
  163. { return x.ring()->expt_pos(x,y); }
  164. // Scalar multiplication.
  165. #if 0 // less efficient
  166. inline const cl_UP_R operator* (const cl_I& x, const cl_UP_R& y)
  167. { return y.ring()->mul(y.ring()->canonhom(x),y); }
  168. inline const cl_UP_R operator* (const cl_UP_R& x, const cl_I& y)
  169. { return x.ring()->mul(x.ring()->canonhom(y),x); }
  170. #endif
  171. inline const cl_UP_R operator* (const cl_I& 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_I& y)
  174. { return x.ring()->scalmul(y,x); }
  175. inline const cl_UP_R operator* (const cl_R& x, const cl_UP_R& y)
  176. { return y.ring()->scalmul(x,y); }
  177. inline const cl_UP_R operator* (const cl_UP_R& x, const cl_R& y)
  178. { return x.ring()->scalmul(y,x); }
  179. // Coefficient.
  180. inline const cl_R coeff (const cl_UP_R& x, uintL index)
  181. { return x.ring()->coeff(x,index); }
  182. // Destructive modification.
  183. inline void set_coeff (cl_UP_R& x, uintL index, const cl_R& y)
  184. { x.ring()->set_coeff(x,index,y); }
  185. inline void finalize (cl_UP_R& x)
  186. { x.ring()->finalize(x); }
  187. inline void cl_UP_R::set_coeff (uintL index, const cl_R& y)
  188. { ring()->set_coeff(*this,index,y); }
  189. inline void cl_UP_R::finalize ()
  190. { ring()->finalize(*this); }
  191. // Evaluation. (No extension of the base ring allowed here for now.)
  192. inline const cl_R cl_UP_R::operator() (const cl_R& y) const
  193. {
  194. return ring()->eval(*this,y);
  195. }
  196. // Derivative.
  197. inline const cl_UP_R deriv (const cl_UP_R& x)
  198. { return The2(cl_UP_R)(deriv((const cl_UP&)x)); }
  199. #endif
  200. CL_REQUIRE(cl_R_ring)
  201. } // namespace cln
  202. #endif /* _CL_UNIVPOLY_REAL_H */