]> www.ginac.de Git - cln.git/blob - src/real/ring/cl_R_ring.cc
Finalize CLN 1.3.7 release.
[cln.git] / src / real / ring / cl_R_ring.cc
1 // Ring of real numbers.
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "cln/real_ring.h"
8
9
10 // Implementation.
11
12 #include "cln/real.h"
13 #include "real/cl_R.h"
14 #include "cln/io.h"
15 #include "cln/real_io.h"
16
17 namespace cln {
18
19 static void R_fprint (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x)
20 {
21         cl_unused R;
22         fprint(stream,The(cl_R)(x));
23 }
24
25 static bool R_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
26 {
27         cl_unused R;
28         return equal(The(cl_R)(x),The(cl_R)(y));
29 }
30
31 static const _cl_ring_element R_zero (cl_heap_ring* R)
32 {
33         return _cl_ring_element(R, (cl_R)0);
34 }
35
36 static bool R_zerop (cl_heap_ring* R, const _cl_ring_element& x)
37 {
38         cl_unused R;
39         // Here we return true only if x is the *exact* zero. Because we
40         // don't want the degree of polynomials to depend on rounding errors.
41         // For all ring theoretic purposes, we treat 0.0 as if it were a
42         // zero divisor.
43         return exact_zerop(The(cl_R)(x));
44 }
45
46 static const _cl_ring_element R_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
47 {
48         return _cl_ring_element(R, The(cl_R)(x) + The(cl_R)(y));
49 }
50
51 static const _cl_ring_element R_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
52 {
53         return _cl_ring_element(R, The(cl_R)(x) - The(cl_R)(y));
54 }
55
56 static const _cl_ring_element R_uminus (cl_heap_ring* R, const _cl_ring_element& x)
57 {
58         return _cl_ring_element(R, - The(cl_R)(x));
59 }
60
61 static const _cl_ring_element R_one (cl_heap_ring* R)
62 {
63         return _cl_ring_element(R, (cl_R)1);
64 }
65
66 static const _cl_ring_element R_canonhom (cl_heap_ring* R, const cl_I& x)
67 {
68         return _cl_ring_element(R, (cl_R)x);
69 }
70
71 static const _cl_ring_element R_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
72 {
73         return _cl_ring_element(R, The(cl_R)(x) * The(cl_R)(y));
74 }
75
76 static const _cl_ring_element R_square (cl_heap_ring* R, const _cl_ring_element& x)
77 {
78         return _cl_ring_element(R, square(The(cl_R)(x)));
79 }
80
81 static const _cl_ring_element R_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
82 {
83         return _cl_ring_element(R, expt(The(cl_R)(x),y));
84 }
85
86 static bool cl_R_p (const cl_number& x)
87 {
88         return (!x.pointer_p()
89                 || (x.pointer_type()->flags & cl_class_flags_subclass_real) != 0);
90 }
91
92 static cl_ring_setops R_setops = {
93         R_fprint,
94         R_equal
95 };
96 static cl_ring_addops R_addops = {
97         R_zero,
98         R_zerop,
99         R_plus,
100         R_minus,
101         R_uminus
102 };
103 static cl_ring_mulops R_mulops = {
104         R_one,
105         R_canonhom,
106         R_mul,
107         R_square,
108         R_expt_pos
109 };
110
111 static cl_number_ring_ops<cl_R> R_ops = {
112         cl_R_p,
113         equal,
114         exact_zerop,
115         operator+,
116         operator-,
117         operator-,
118         operator*,
119         square,
120         expt
121 };
122
123 class cl_heap_real_ring : public cl_heap_number_ring {
124         SUBCLASS_cl_heap_ring()
125 public:
126         // Constructor.
127         cl_heap_real_ring ()
128                 : cl_heap_number_ring (&R_setops,&R_addops,&R_mulops,
129                                        (cl_number_ring_ops<cl_number>*) &R_ops)
130                 { type = &cl_class_real_ring; }
131         // Destructor.
132         ~cl_heap_real_ring () {}
133 };
134
135 static void cl_real_ring_destructor (cl_heap* pointer)
136 {
137         (*(cl_heap_real_ring*)pointer).~cl_heap_real_ring();
138 }
139
140 static void cl_real_ring_dprint (cl_heap* pointer)
141 {
142         cl_unused pointer;
143         fprint(cl_debugout, "(cl_real_ring) cl_R_ring");
144 }
145
146 static cl_heap_real_ring* cl_heap_real_ring_instance;
147 cl_class cl_class_real_ring;
148
149 // Constructor.
150 template <>
151 inline cl_real_ring::cl_specialized_number_ring ()
152         : cl_number_ring (cl_heap_real_ring_instance) {}
153
154 const cl_real_ring cl_R_ring = cl_R_ring;
155
156 int cl_R_ring_init_helper::count = 0;
157
158 cl_R_ring_init_helper::cl_R_ring_init_helper()
159 {
160         if (count++ == 0) {
161                 cl_class_real_ring.destruct = cl_real_ring_destructor;
162                 cl_class_real_ring.flags = cl_class_flags_number_ring;
163                 cl_class_real_ring.dprint = cl_real_ring_dprint;
164                 cl_heap_real_ring_instance = new cl_heap_real_ring();
165                 new((void *)&cl_R_ring) cl_real_ring();
166         }
167 }
168
169 cl_R_ring_init_helper::~cl_R_ring_init_helper()
170 {
171         if (--count == 0) {
172                 delete cl_heap_real_ring_instance;
173         }
174 }
175
176
177 }  // namespace cln
178