]> www.ginac.de Git - cln.git/blob - src/complex/ring/cl_C_ring.cc
6e6cb7433c0dc09e71cf669d555157de96664065
[cln.git] / src / complex / ring / cl_C_ring.cc
1 // Ring of complex numbers.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_C_ring)
7
8 // Specification.
9 #include "cln/complex_ring.h"
10
11
12 // Implementation.
13
14 #include "cln/complex.h"
15 #include "cln/complex_io.h"
16 #include "cl_C.h"
17
18 namespace cln {
19
20 static void N_fprint (cl_heap_ring* R, cl_ostream stream, const _cl_ring_element& x)
21 {
22         unused R;
23         fprint(stream,The(cl_N)(x));
24 }
25
26 static cl_boolean N_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
27 {
28         unused R;
29         return equal(The(cl_N)(x),The(cl_N)(y));
30 }
31
32 static const _cl_ring_element N_zero (cl_heap_ring* R)
33 {
34         return _cl_ring_element(R, (cl_N)0);
35 }
36
37 static cl_boolean N_zerop (cl_heap_ring* R, const _cl_ring_element& x)
38 {
39         unused R;
40         // Here we return true only if x is the *exact* zero. Because we
41         // don't want the degree of polynomials to depend on rounding errors.
42         // For all ring theoretic purposes, we treat 0.0, 0+0.0i etc. as if
43         // they were zero divisors.
44         return exact_zerop(The(cl_N)(x));
45 }
46
47 static const _cl_ring_element N_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
48 {
49         return _cl_ring_element(R, The(cl_N)(x) + The(cl_N)(y));
50 }
51
52 static const _cl_ring_element N_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
53 {
54         return _cl_ring_element(R, The(cl_N)(x) - The(cl_N)(y));
55 }
56
57 static const _cl_ring_element N_uminus (cl_heap_ring* R, const _cl_ring_element& x)
58 {
59         return _cl_ring_element(R, - The(cl_N)(x));
60 }
61
62 static const _cl_ring_element N_one (cl_heap_ring* R)
63 {
64         return _cl_ring_element(R, (cl_N)1);
65 }
66
67 static const _cl_ring_element N_canonhom (cl_heap_ring* R, const cl_I& x)
68 {
69         return _cl_ring_element(R, (cl_N)x);
70 }
71
72 static const _cl_ring_element N_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
73 {
74         return _cl_ring_element(R, The(cl_N)(x) * The(cl_N)(y));
75 }
76
77 static const _cl_ring_element N_square (cl_heap_ring* R, const _cl_ring_element& x)
78 {
79         return _cl_ring_element(R, square(The(cl_N)(x)));
80 }
81
82 static const _cl_ring_element N_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
83 {
84         return _cl_ring_element(R, expt(The(cl_N)(x),y));
85 }
86
87 static cl_boolean cl_N_p (const cl_number& x)
88 {
89         return (cl_boolean)
90                (!x.pointer_p()
91                 || (x.pointer_type()->flags & cl_class_flags_subclass_complex) != 0
92                );
93 }
94
95 static cl_ring_setops N_setops = {
96         N_fprint,
97         N_equal
98 };
99 static cl_ring_addops N_addops = {
100         N_zero,
101         N_zerop,
102         N_plus,
103         N_minus,
104         N_uminus
105 };
106 static cl_ring_mulops N_mulops = {
107         N_one,
108         N_canonhom,
109         N_mul,
110         N_square,
111         N_expt_pos
112 };
113
114 static cl_number_ring_ops<cl_N> N_ops = {
115         cl_N_p,
116         equal,
117         exact_zerop,
118         operator+,
119         operator-,
120         operator-,
121         operator*,
122         square,
123         expt
124 };
125
126 class cl_heap_complex_ring : public cl_heap_number_ring {
127         SUBCLASS_cl_heap_ring()
128 public:
129         // Constructor.
130         cl_heap_complex_ring ()
131                 : cl_heap_number_ring (&N_setops,&N_addops,&N_mulops,
132                                        (cl_number_ring_ops<cl_number>*) &N_ops)
133                 { type = &cl_class_complex_ring; }
134         // Destructor.
135         ~cl_heap_complex_ring () {}
136 };
137
138 static void cl_complex_ring_destructor (cl_heap* pointer)
139 {
140         (*(cl_heap_complex_ring*)pointer).~cl_heap_complex_ring();
141 }
142
143 static void cl_complex_ring_dprint (cl_heap* pointer)
144 {
145         unused pointer;
146         fprint(cl_debugout, "(cl_complex_ring) cl_C_ring");
147 }
148
149 cl_class cl_class_complex_ring = {
150         cl_complex_ring_destructor,
151         cl_class_flags_number_ring,
152         cl_complex_ring_dprint
153 };
154
155 // Constructor.
156 inline cl_complex_ring::cl_specialized_number_ring ()
157         : cl_number_ring (new cl_heap_complex_ring()) {}
158
159 const cl_complex_ring cl_C_ring;
160
161 }  // namespace cln
162
163 CL_PROVIDE_END(cl_C_ring)