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.

209 lines
6.6 KiB

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