1 // Ring of complex numbers.
7 #include "cln/complex_ring.h"
12 #include "cln/complex.h"
13 #include "cln/complex_io.h"
18 static void N_fprint (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x)
21 fprint(stream,The(cl_N)(x));
24 static bool N_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
27 return equal(The(cl_N)(x),The(cl_N)(y));
30 static const _cl_ring_element N_zero (cl_heap_ring* R)
32 return _cl_ring_element(R, (cl_N)0);
35 static bool N_zerop (cl_heap_ring* R, const _cl_ring_element& x)
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));
45 static const _cl_ring_element N_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
47 return _cl_ring_element(R, The(cl_N)(x) + The(cl_N)(y));
50 static const _cl_ring_element N_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
52 return _cl_ring_element(R, The(cl_N)(x) - The(cl_N)(y));
55 static const _cl_ring_element N_uminus (cl_heap_ring* R, const _cl_ring_element& x)
57 return _cl_ring_element(R, - The(cl_N)(x));
60 static const _cl_ring_element N_one (cl_heap_ring* R)
62 return _cl_ring_element(R, (cl_N)1);
65 static const _cl_ring_element N_canonhom (cl_heap_ring* R, const cl_I& x)
67 return _cl_ring_element(R, (cl_N)x);
70 static const _cl_ring_element N_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
72 return _cl_ring_element(R, The(cl_N)(x) * The(cl_N)(y));
75 static const _cl_ring_element N_square (cl_heap_ring* R, const _cl_ring_element& x)
77 return _cl_ring_element(R, square(The(cl_N)(x)));
80 static const _cl_ring_element N_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
82 return _cl_ring_element(R, expt(The(cl_N)(x),y));
85 static bool cl_N_p (const cl_number& x)
87 return (!x.pointer_p()
88 || (x.pointer_type()->flags & cl_class_flags_subclass_complex) != 0);
91 static cl_ring_setops N_setops = {
95 static cl_ring_addops N_addops = {
102 static cl_ring_mulops N_mulops = {
110 static cl_number_ring_ops<cl_N> N_ops = {
122 class cl_heap_complex_ring : public cl_heap_number_ring {
123 SUBCLASS_cl_heap_ring()
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; }
131 ~cl_heap_complex_ring () {}
134 static void cl_complex_ring_destructor (cl_heap* pointer)
136 (*(cl_heap_complex_ring*)pointer).~cl_heap_complex_ring();
139 static void cl_complex_ring_dprint (cl_heap* pointer)
142 fprint(cl_debugout, "(cl_complex_ring) cl_C_ring");
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;
151 inline cl_complex_ring::cl_specialized_number_ring ()
152 : cl_number_ring(cl_heap_complex_ring_instance) { }
154 int cl_C_ring_init_helper::count = 0;
156 cl_C_ring_init_helper::cl_C_ring_init_helper()
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();
167 cl_C_ring_init_helper::~cl_C_ring_init_helper()
170 delete cl_heap_complex_ring_instance;