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