1 // Ring of rational numbers.
9 #include "cln/rational_ring.h"
14 #include "cln/rational.h"
15 #include "cln/rational_io.h"
16 #define zerop zerop_inline
22 static void RA_fprint (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x)
25 fprint(stream,The(cl_RA)(x));
28 static bool RA_equal (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
31 return equal(The(cl_RA)(x),The(cl_RA)(y));
34 static const _cl_ring_element RA_zero (cl_heap_ring* R)
36 return _cl_ring_element(R, (cl_RA)0);
39 static bool CL_FLATTEN RA_zerop (cl_heap_ring* R, const _cl_ring_element& x)
42 return zerop_inline(The(cl_RA)(x));
45 static const _cl_ring_element RA_plus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
47 return _cl_ring_element(R, The(cl_RA)(x) + The(cl_RA)(y));
50 static const _cl_ring_element RA_minus (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
52 return _cl_ring_element(R, The(cl_RA)(x) - The(cl_RA)(y));
55 static const _cl_ring_element RA_uminus (cl_heap_ring* R, const _cl_ring_element& x)
57 return _cl_ring_element(R, - The(cl_RA)(x));
60 static const _cl_ring_element RA_one (cl_heap_ring* R)
62 return _cl_ring_element(R, (cl_RA)1);
65 static const _cl_ring_element RA_canonhom (cl_heap_ring* R, const cl_I& x)
67 return _cl_ring_element(R, (cl_RA)x);
70 static const _cl_ring_element RA_mul (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y)
72 return _cl_ring_element(R, The(cl_RA)(x) * The(cl_RA)(y));
75 static const _cl_ring_element RA_square (cl_heap_ring* R, const _cl_ring_element& x)
77 return _cl_ring_element(R, square(The(cl_RA)(x)));
80 static const _cl_ring_element RA_expt_pos (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y)
82 return _cl_ring_element(R, expt_pos(The(cl_RA)(x),y));
85 static bool cl_RA_p (const cl_number& x)
87 return (!x.pointer_p()
88 ? x.nonpointer_tag() == cl_FN_tag
89 : (x.pointer_type()->flags & cl_class_flags_subclass_rational) != 0);
92 static cl_ring_setops RA_setops = {
96 static cl_ring_addops RA_addops = {
103 static cl_ring_mulops RA_mulops = {
111 static cl_number_ring_ops<cl_RA> RA_ops = {
123 class cl_heap_rational_ring : public cl_heap_number_ring {
124 SUBCLASS_cl_heap_ring()
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; }
132 ~cl_heap_rational_ring () {}
135 static void cl_rational_ring_destructor (cl_heap* pointer)
137 (*(cl_heap_rational_ring*)pointer).~cl_heap_rational_ring();
140 static void cl_rational_ring_dprint (cl_heap* pointer)
143 fprint(cl_debugout, "(cl_rational_ring) cl_RA_ring");
146 cl_class cl_class_rational_ring = {
147 cl_rational_ring_destructor,
148 cl_class_flags_number_ring,
149 cl_rational_ring_dprint
154 inline cl_rational_ring::cl_specialized_number_ring ()
155 : cl_number_ring (new cl_heap_rational_ring()) {}
157 const cl_rational_ring cl_RA_ring;
161 CL_PROVIDE_END(cl_RA_ring)