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.

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