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.

234 lines
7.1 KiB

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