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