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.

213 lines
6.6 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
  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, cl_UP_MI* 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 (cl_ostream stream, const cl_UP_MI& x)
  47. {
  48. cl_heap_univpoly_ring::fprint(stream,x);
  49. }
  50. cl_boolean 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. cl_boolean 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. const cl_UP_MI monomial (const cl_MI& x, uintL e)
  103. {
  104. return The2(cl_UP_MI)(cl_heap_univpoly_ring::monomial(x,e));
  105. }
  106. const cl_MI coeff (const cl_UP_MI& x, uintL index)
  107. {
  108. return The2(cl_MI)(cl_heap_univpoly_ring::coeff(x,index));
  109. }
  110. const cl_UP_MI create (sintL deg)
  111. {
  112. return The2(cl_UP_MI)(cl_heap_univpoly_ring::create(deg));
  113. }
  114. void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y)
  115. {
  116. cl_heap_univpoly_ring::set_coeff(x,index,y);
  117. }
  118. void finalize (cl_UP_MI& x)
  119. {
  120. cl_heap_univpoly_ring::finalize(x);
  121. }
  122. const cl_MI eval (const cl_UP_MI& x, const cl_MI& y)
  123. {
  124. return The2(cl_MI)(cl_heap_univpoly_ring::eval(x,y));
  125. }
  126. private:
  127. // No need for any constructors.
  128. cl_heap_univpoly_modint_ring ();
  129. };
  130. // Lookup of polynomial rings.
  131. inline const cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& r)
  132. { return The(cl_univpoly_modint_ring) (find_univpoly_ring((const cl_ring&)r)); }
  133. inline const cl_univpoly_modint_ring find_univpoly_ring (const cl_modint_ring& r, const cl_symbol& varname)
  134. { return The(cl_univpoly_modint_ring) (find_univpoly_ring((const cl_ring&)r,varname)); }
  135. // Operations on polynomials.
  136. // Add.
  137. inline const cl_UP_MI operator+ (const cl_UP_MI& x, const cl_UP_MI& y)
  138. { return x.ring()->plus(x,y); }
  139. // Negate.
  140. inline const cl_UP_MI operator- (const cl_UP_MI& x)
  141. { return x.ring()->uminus(x); }
  142. // Subtract.
  143. inline const cl_UP_MI operator- (const cl_UP_MI& x, const cl_UP_MI& y)
  144. { return x.ring()->minus(x,y); }
  145. // Multiply.
  146. inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_UP_MI& y)
  147. { return x.ring()->mul(x,y); }
  148. // Squaring.
  149. inline const cl_UP_MI square (const cl_UP_MI& x)
  150. { return x.ring()->square(x); }
  151. // Exponentiation x^y, where y > 0.
  152. inline const cl_UP_MI expt_pos (const cl_UP_MI& x, const cl_I& y)
  153. { return x.ring()->expt_pos(x,y); }
  154. // Scalar multiplication.
  155. #if 0 // less efficient
  156. inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y)
  157. { return y.ring()->mul(y.ring()->canonhom(x),y); }
  158. inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y)
  159. { return x.ring()->mul(x.ring()->canonhom(y),x); }
  160. #endif
  161. inline const cl_UP_MI operator* (const cl_I& x, const cl_UP_MI& y)
  162. { return y.ring()->scalmul(y.ring()->basering()->canonhom(x),y); }
  163. inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_I& y)
  164. { return x.ring()->scalmul(x.ring()->basering()->canonhom(y),x); }
  165. inline const cl_UP_MI operator* (const cl_MI& x, const cl_UP_MI& y)
  166. { return y.ring()->scalmul(x,y); }
  167. inline const cl_UP_MI operator* (const cl_UP_MI& x, const cl_MI& y)
  168. { return x.ring()->scalmul(y,x); }
  169. // Coefficient.
  170. inline const cl_MI coeff (const cl_UP_MI& x, uintL index)
  171. { return x.ring()->coeff(x,index); }
  172. // Destructive modification.
  173. inline void set_coeff (cl_UP_MI& x, uintL index, const cl_MI& y)
  174. { x.ring()->set_coeff(x,index,y); }
  175. inline void finalize (cl_UP_MI& x)
  176. { x.ring()->finalize(x); }
  177. inline void cl_UP_MI::set_coeff (uintL index, const cl_MI& y)
  178. { ring()->set_coeff(*this,index,y); }
  179. inline void cl_UP_MI::finalize ()
  180. { ring()->finalize(*this); }
  181. // Evaluation. (No extension of the base ring allowed here for now.)
  182. inline const cl_MI cl_UP_MI::operator() (const cl_MI& y) const
  183. {
  184. return ring()->eval(*this,y);
  185. }
  186. // Derivative.
  187. inline const cl_UP_MI deriv (const cl_UP_MI& x)
  188. { return The2(cl_UP_MI)(deriv((const cl_UP&)x)); }
  189. } // namespace cln
  190. #endif /* _CL_UNIVPOLY_MODINT_H */