]> www.ginac.de Git - cln.git/blob - src/rational/ring/cl_RA_ring.cc
* Add support for OpenBSD.
[cln.git] / src / rational / ring / cl_RA_ring.cc
1 // Ring of rational numbers.
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_RA_ring)
7
8 // Specification.
9 #include "cln/rational_ring.h"
10
11
12 // Implementation.
13
14 #include "cln/rational.h"
15 #include "cln/rational_io.h"
16 #include "cl_RA.h"
17
18 namespace cln {
19
20 static void RA_fprint (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x)
21 {
22         unused R;
23         fprint(stream,The(cl_RA)(x));
24 }
25
26 static cl_boolean RA_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
27 {
28         unused R;
29         return equal(The(cl_RA)(x),The(cl_RA)(y));
30 }
31
32 static const _cl_ring_element RA_zero (cl_heap_ring* R)
33 {
34         return _cl_ring_element(R, (cl_RA)0);
35 }
36
37 static cl_boolean RA_zerop (cl_heap_ring* R, const _cl_ring_element& x)
38 {
39         unused R;
40         return zerop(The(cl_RA)(x));
41 }
42
43 static const _cl_ring_element RA_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
44 {
45         return _cl_ring_element(R, The(cl_RA)(x) + The(cl_RA)(y));
46 }
47
48 static const _cl_ring_element RA_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
49 {
50         return _cl_ring_element(R, The(cl_RA)(x) - The(cl_RA)(y));
51 }
52
53 static const _cl_ring_element RA_uminus (cl_heap_ring* R, const _cl_ring_element& x)
54 {
55         return _cl_ring_element(R, - The(cl_RA)(x));
56 }
57
58 static const _cl_ring_element RA_one (cl_heap_ring* R)
59 {
60         return _cl_ring_element(R, (cl_RA)1);
61 }
62
63 static const _cl_ring_element RA_canonhom (cl_heap_ring* R, const cl_I& x)
64 {
65         return _cl_ring_element(R, (cl_RA)x);
66 }
67
68 static const _cl_ring_element RA_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
69 {
70         return _cl_ring_element(R, The(cl_RA)(x) * The(cl_RA)(y));
71 }
72
73 static const _cl_ring_element RA_square (cl_heap_ring* R, const _cl_ring_element& x)
74 {
75         return _cl_ring_element(R, square(The(cl_RA)(x)));
76 }
77
78 static const _cl_ring_element RA_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
79 {
80         return _cl_ring_element(R, expt_pos(The(cl_RA)(x),y));
81 }
82
83 static cl_boolean cl_RA_p (const cl_number& x)
84 {
85         return (cl_boolean)
86                (!x.pointer_p()
87                 ? x.nonpointer_tag() == cl_FN_tag
88                 : (x.pointer_type()->flags & cl_class_flags_subclass_rational) != 0
89                );
90 }
91
92 static cl_ring_setops RA_setops = {
93         RA_fprint,
94         RA_equal
95 };
96 static cl_ring_addops RA_addops = {
97         RA_zero,
98         RA_zerop,
99         RA_plus,
100         RA_minus,
101         RA_uminus
102 };
103 static cl_ring_mulops RA_mulops = {
104         RA_one,
105         RA_canonhom,
106         RA_mul,
107         RA_square,
108         RA_expt_pos
109 };
110
111 static cl_number_ring_ops<cl_RA> RA_ops = {
112         cl_RA_p,
113         equal,
114         zerop,
115         operator+,
116         operator-,
117         operator-,
118         operator*,
119         square,
120         expt_pos
121 };
122
123 class cl_heap_rational_ring : public cl_heap_number_ring {
124         SUBCLASS_cl_heap_ring()
125 public:
126         // Constructor.
127         cl_heap_rational_ring ()
128                 : cl_heap_number_ring (&RA_setops,&RA_addops,&RA_mulops,
129                                        (cl_number_ring_ops<cl_number>*) &RA_ops)
130                 { type = &cl_class_rational_ring; }
131         // Destructor.
132         ~cl_heap_rational_ring () {}
133 };
134
135 static void cl_rational_ring_destructor (cl_heap* pointer)
136 {
137         (*(cl_heap_rational_ring*)pointer).~cl_heap_rational_ring();
138 }
139
140 static void cl_rational_ring_dprint (cl_heap* pointer)
141 {
142         unused pointer;
143         fprint(cl_debugout, "(cl_rational_ring) cl_RA_ring");
144 }
145
146 cl_class cl_class_rational_ring = {
147         cl_rational_ring_destructor,
148         cl_class_flags_number_ring,
149         cl_rational_ring_dprint
150 };
151
152 // Constructor.
153 template <>
154 inline cl_rational_ring::cl_specialized_number_ring ()
155         : cl_number_ring (new cl_heap_rational_ring()) {}
156
157 const cl_rational_ring cl_RA_ring;
158
159 }  // namespace cln
160
161 CL_PROVIDE_END(cl_RA_ring)