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