PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/cln-1.3.2/src/modinteger/cl_MI_int.h

#
C Header | 152 lines | 124 code | 21 blank | 7 comment | 13 complexity | a856e0365f7d472ba5a6445413046631 MD5 | raw file
Possible License(s): GPL-2.0
  1. // m = 0 : Z/mZ \isomorph Z
  2. namespace cln {
  3. static void int_fprint (cl_heap_modint_ring* R, std::ostream& stream, const _cl_MI &x)
  4. {
  5. fprint(stream,R->_retract(x));
  6. }
  7. static const cl_I int_reduce_modulo (cl_heap_modint_ring* R, const cl_I& x)
  8. {
  9. unused R;
  10. return x; // reducing modulo 0 does nothing
  11. }
  12. // This is the only case where canonhom is injective.
  13. static const _cl_MI int_canonhom (cl_heap_modint_ring* R, const cl_I& x)
  14. {
  15. return _cl_MI(R, x);
  16. }
  17. // This is the only case where retract is surjective.
  18. static const cl_I int_retract (cl_heap_modint_ring* R, const _cl_MI& x)
  19. {
  20. unused R;
  21. return x.rep;
  22. }
  23. // This is the only case where random yields an error.
  24. static const _cl_MI int_random (cl_heap_modint_ring* R, random_state& randomstate)
  25. {
  26. unused R;
  27. unused randomstate;
  28. throw runtime_exception("Z / 0 Z not a finite set - no equidistributed random function.");
  29. #if ((defined(__sparc__) || defined(__sparc64__)) && !defined(__GNUC__)) // Sun CC wants a return value
  30. return _cl_MI(R, 0);
  31. #endif
  32. }
  33. static const _cl_MI int_zero (cl_heap_modint_ring* R)
  34. {
  35. return _cl_MI(R, 0);
  36. }
  37. static bool int_zerop (cl_heap_modint_ring* R, const _cl_MI& x)
  38. {
  39. unused R;
  40. return zerop(x.rep);
  41. }
  42. static const _cl_MI int_plus (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
  43. {
  44. return _cl_MI(R, x.rep + y.rep);
  45. }
  46. static const _cl_MI int_minus (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
  47. {
  48. return _cl_MI(R, x.rep - y.rep);
  49. }
  50. static const _cl_MI int_uminus (cl_heap_modint_ring* R, const _cl_MI& x)
  51. {
  52. return _cl_MI(R, - x.rep);
  53. }
  54. static const _cl_MI int_one (cl_heap_modint_ring* R)
  55. {
  56. return _cl_MI(R, 1);
  57. }
  58. static const _cl_MI int_mul (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
  59. {
  60. return _cl_MI(R, x.rep * y.rep);
  61. }
  62. static const _cl_MI int_square (cl_heap_modint_ring* R, const _cl_MI& x)
  63. {
  64. return _cl_MI(R, square(x.rep));
  65. }
  66. static const cl_MI_x int_recip (cl_heap_modint_ring* R, const _cl_MI& x)
  67. {
  68. var const cl_I& xr = x.rep;
  69. if (eq(xr,1) || eq(xr,-1)) { return cl_MI(R,x); }
  70. if (zerop(xr)) { throw division_by_0_exception(); }
  71. return cl_notify_composite(R,xr);
  72. }
  73. static const cl_MI_x int_div (cl_heap_modint_ring* R, const _cl_MI& x, const _cl_MI& y)
  74. {
  75. var const cl_I& yr = y.rep;
  76. if (eq(yr,1)) { return cl_MI(R,x.rep); }
  77. if (eq(yr,-1)) { return cl_MI(R,-x.rep); }
  78. if (zerop(yr)) { throw division_by_0_exception(); }
  79. return cl_notify_composite(R,yr);
  80. }
  81. static const _cl_MI int_expt_pos (cl_heap_modint_ring* R, const _cl_MI& x, const cl_I& y)
  82. {
  83. return _cl_MI(R, expt_pos(x.rep,y));
  84. }
  85. static const cl_MI_x int_expt (cl_heap_modint_ring* R, const _cl_MI& x, const cl_I& y)
  86. {
  87. if (eq(x.rep,1)) { return cl_MI(R,1); }
  88. if (eq(x.rep,-1)) { return cl_MI(R,evenp(y)?1:-1); }
  89. if (!minusp(y)) {
  90. if (zerop(y))
  91. return cl_MI(R,1);
  92. else
  93. return cl_MI(R,expt_pos(x.rep,y));
  94. }
  95. // y < 0, x nonunit.
  96. if (zerop(x.rep)) { throw division_by_0_exception(); }
  97. return cl_notify_composite(R,x.rep);
  98. }
  99. static cl_modint_setops int_setops = {
  100. int_fprint,
  101. modint_equal,
  102. int_random
  103. };
  104. static cl_modint_addops int_addops = {
  105. int_zero,
  106. int_zerop,
  107. int_plus,
  108. int_minus,
  109. int_uminus
  110. };
  111. static cl_modint_mulops int_mulops = {
  112. int_one,
  113. int_canonhom,
  114. int_mul,
  115. int_square,
  116. int_expt_pos,
  117. int_recip,
  118. int_div,
  119. int_expt,
  120. int_reduce_modulo,
  121. int_retract
  122. };
  123. class cl_heap_modint_ring_int : public cl_heap_modint_ring {
  124. SUBCLASS_cl_heap_modint_ring()
  125. public:
  126. // Constructor.
  127. cl_heap_modint_ring_int () : cl_heap_modint_ring (0, &int_setops, &int_addops, &int_mulops) {}
  128. // Virtual destructor.
  129. ~cl_heap_modint_ring_int () {}
  130. };
  131. } // namespace cln