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