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