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.

226 lines
7.0 KiB

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