You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
16 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
  1. // Ring operations.
  2. #ifndef _CL_RING_H
  3. #define _CL_RING_H
  4. #include "cln/object.h"
  5. #include "cln/malloc.h"
  6. #include "cln/proplist.h"
  7. #include "cln/number.h"
  8. #include "cln/io.h"
  9. namespace cln {
  10. class cl_I;
  11. // This file defines the general layout of rings, ring elements, and
  12. // operations available on ring elements. Any subclass of `cl_ring'
  13. // must implement these operations, with the same memory layout.
  14. // (Because generic packages like the polynomial rings access the base
  15. // ring's operation vectors through inline functions defined in this file.)
  16. class cl_heap_ring;
  17. // Rings are reference counted, but not freed immediately when they aren't
  18. // used any more. Hence they inherit from `cl_rcpointer'.
  19. // Vectors of function pointers are more efficient than virtual member
  20. // functions. But it constrains us not to use multiple or virtual inheritance.
  21. //
  22. // Note! We are passing raw `cl_heap_ring*' pointers to the operations
  23. // for efficiency (compared to passing `const cl_ring&', we save a memory
  24. // access, and it is easier to cast to a `cl_heap_ring_specialized*').
  25. // These raw pointers are meant to be used downward (in the dynamic extent
  26. // of the call) only. If you need to save them in a data structure, cast
  27. // to `cl_ring'; this will correctly increment the reference count.
  28. // (This technique is safe because the inline wrapper functions make sure
  29. // that we have a `cl_ring' somewhere containing the pointer, so there
  30. // is no danger of dangling pointers.)
  31. //
  32. // Note! Because the `cl_heap_ring*' -> `cl_ring' conversion increments
  33. // the reference count, you have to use the `cl_private_thing' -> `cl_ring'
  34. // conversion if the reference count is already incremented.
  35. class cl_ring : public cl_rcpointer {
  36. public:
  37. // Constructor. Takes a cl_heap_ring*, increments its refcount.
  38. cl_ring (cl_heap_ring* r);
  39. // Private constructor. Doesn't increment the refcount.
  40. cl_ring (cl_private_thing);
  41. // Copy constructor.
  42. cl_ring (const cl_ring&);
  43. // Assignment operator.
  44. cl_ring& operator= (const cl_ring&);
  45. // Default constructor.
  46. cl_ring ();
  47. // Automatic dereferencing.
  48. cl_heap_ring* operator-> () const
  49. { return (cl_heap_ring*)heappointer; }
  50. };
  51. CL_DEFINE_COPY_CONSTRUCTOR2(cl_ring,cl_rcpointer)
  52. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_ring,cl_ring)
  53. // Normal constructor for `cl_ring'.
  54. inline cl_ring::cl_ring (cl_heap_ring* r)
  55. { cl_inc_pointer_refcount((cl_heap*)r); pointer = r; }
  56. // Private constructor for `cl_ring'.
  57. inline cl_ring::cl_ring (cl_private_thing p)
  58. { pointer = p; }
  59. inline bool operator== (const cl_ring& R1, const cl_ring& R2)
  60. { return (R1.pointer == R2.pointer); }
  61. inline bool operator!= (const cl_ring& R1, const cl_ring& R2)
  62. { return (R1.pointer != R2.pointer); }
  63. inline bool operator== (const cl_ring& R1, cl_heap_ring* R2)
  64. { return (R1.pointer == R2); }
  65. inline bool operator!= (const cl_ring& R1, cl_heap_ring* R2)
  66. { return (R1.pointer != R2); }
  67. // Representation of an element of a ring.
  68. //
  69. // In order to support true polymorphism (without C++ templates), all
  70. // ring elements share the same basic layout:
  71. // cl_ring ring; // the ring
  72. // cl_gcobject rep; // representation of the element
  73. // The representation of the element depends on the ring, of course,
  74. // but we constrain it to be a single pointer into the heap or an immediate
  75. // value.
  76. //
  77. // Any arithmetic operation on a ring R (like +, -, *) must return a value
  78. // with ring = R. This is
  79. // a. necessary if the computation is to proceed correctly (e.g. in cl_RA,
  80. // ((3/4)*4 mod 3) is 0, simplifying it to ((cl_I)4 mod (cl_I)3) = 1
  81. // wouldn't be correct),
  82. // b. possible even if R is an extension ring of some ring R1 (e.g. cl_N
  83. // being an extension ring of cl_R). Automatic retraction from R to R1
  84. // can be done through dynamic typing: An element of R which happens
  85. // to lie in R1 is stored using the internal representation of R1,
  86. // but with ring = R. Elements of R1 and R\R1 can be distinguished
  87. // through rep's type.
  88. // c. an advantage for the implementation of polynomials and other
  89. // entities which contain many elements of the same ring. They need
  90. // to store only the elements' representations, and a single pointer
  91. // to the ring.
  92. //
  93. // The ring operations exist in two versions:
  94. // - Low-level version, which only operates on the representation.
  95. // - High-level version, which operates on full cl_ring_elements.
  96. // We make this distinction for performance: Multiplication of polynomials
  97. // over Z/nZ, operating on the high-level operations, spends 40% of its
  98. // computing time with packing and unpacking of cl_ring_elements.
  99. // The low-level versions have an underscore prepended and are unsafe.
  100. class _cl_ring_element {
  101. public:
  102. cl_gcobject rep; // representation of the element
  103. // Default constructor.
  104. _cl_ring_element ();
  105. public: /* ugh */
  106. // Constructor.
  107. _cl_ring_element (const cl_heap_ring* R, const cl_gcobject& r) : rep (as_cl_private_thing(r)) { (void)R; }
  108. _cl_ring_element (const cl_ring& R, const cl_gcobject& r) : rep (as_cl_private_thing(r)) { (void)R; }
  109. public: // Ability to place an object at a given address.
  110. void* operator new (size_t size) { return malloc_hook(size); }
  111. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  112. void operator delete (void* ptr) { free_hook(ptr); }
  113. };
  114. class cl_ring_element : public _cl_ring_element {
  115. protected:
  116. cl_ring _ring; // ring
  117. public:
  118. const cl_ring& ring () const { return _ring; }
  119. // Default constructor.
  120. cl_ring_element ();
  121. public: /* ugh */
  122. // Constructor.
  123. cl_ring_element (const cl_ring& R, const cl_gcobject& r) : _cl_ring_element (R,r), _ring (R) {}
  124. cl_ring_element (const cl_ring& R, const _cl_ring_element& r) : _cl_ring_element (r), _ring (R) {}
  125. public: // Debugging output.
  126. void debug_print () const;
  127. // Ability to place an object at a given address.
  128. void* operator new (size_t size) { return malloc_hook(size); }
  129. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  130. void operator delete (void* ptr) { free_hook(ptr); }
  131. };
  132. // The ring operations are encoded as vectors of function pointers. You
  133. // can add more operations to the end of each vector or add new vectors,
  134. // but you must not reorder the operations nor reorder the vectors nor
  135. // change the functions' signatures incompatibly.
  136. // There should ideally be a template class for each vector, but unfortunately
  137. // you lose the ability to initialize the vector using "= { ... }" syntax
  138. // when you subclass it.
  139. struct _cl_ring_setops {
  140. // print
  141. void (* fprint) (cl_heap_ring* R, std::ostream& stream, const _cl_ring_element& x);
  142. // equality
  143. cl_boolean (* equal) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
  144. // ...
  145. };
  146. struct _cl_ring_addops {
  147. // 0
  148. const _cl_ring_element (* zero) (cl_heap_ring* R);
  149. cl_boolean (* zerop) (cl_heap_ring* R, const _cl_ring_element& x);
  150. // x+y
  151. const _cl_ring_element (* plus) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
  152. // x-y
  153. const _cl_ring_element (* minus) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
  154. // -x
  155. const _cl_ring_element (* uminus) (cl_heap_ring* R, const _cl_ring_element& x);
  156. // ...
  157. };
  158. struct _cl_ring_mulops {
  159. // 1
  160. const _cl_ring_element (* one) (cl_heap_ring* R);
  161. // canonical homomorphism
  162. const _cl_ring_element (* canonhom) (cl_heap_ring* R, const cl_I& x);
  163. // x*y
  164. const _cl_ring_element (* mul) (cl_heap_ring* R, const _cl_ring_element& x, const _cl_ring_element& y);
  165. // x^2
  166. const _cl_ring_element (* square) (cl_heap_ring* R, const _cl_ring_element& x);
  167. // x^y, y Integer >0
  168. const _cl_ring_element (* expt_pos) (cl_heap_ring* R, const _cl_ring_element& x, const cl_I& y);
  169. // ...
  170. };
  171. typedef const _cl_ring_setops cl_ring_setops;
  172. typedef const _cl_ring_addops cl_ring_addops;
  173. typedef const _cl_ring_mulops cl_ring_mulops;
  174. // Representation of a ring in memory.
  175. class cl_heap_ring : public cl_heap {
  176. public:
  177. // Allocation.
  178. void* operator new (size_t size) { return malloc_hook(size); }
  179. // Deallocation.
  180. void operator delete (void* ptr) { free_hook(ptr); }
  181. private:
  182. cl_property_list properties;
  183. protected:
  184. cl_ring_setops* setops;
  185. cl_ring_addops* addops;
  186. cl_ring_mulops* mulops;
  187. public:
  188. // More information comes here.
  189. // ...
  190. public:
  191. // Low-level operations.
  192. void _fprint (std::ostream& stream, const _cl_ring_element& x)
  193. { setops->fprint(this,stream,x); }
  194. cl_boolean _equal (const _cl_ring_element& x, const _cl_ring_element& y)
  195. { return setops->equal(this,x,y); }
  196. const _cl_ring_element _zero ()
  197. { return addops->zero(this); }
  198. cl_boolean _zerop (const _cl_ring_element& x)
  199. { return addops->zerop(this,x); }
  200. const _cl_ring_element _plus (const _cl_ring_element& x, const _cl_ring_element& y)
  201. { return addops->plus(this,x,y); }
  202. const _cl_ring_element _minus (const _cl_ring_element& x, const _cl_ring_element& y)
  203. { return addops->minus(this,x,y); }
  204. const _cl_ring_element _uminus (const _cl_ring_element& x)
  205. { return addops->uminus(this,x); }
  206. const _cl_ring_element _one ()
  207. { return mulops->one(this); }
  208. const _cl_ring_element _canonhom (const cl_I& x)
  209. { return mulops->canonhom(this,x); }
  210. const _cl_ring_element _mul (const _cl_ring_element& x, const _cl_ring_element& y)
  211. { return mulops->mul(this,x,y); }
  212. const _cl_ring_element _square (const _cl_ring_element& x)
  213. { return mulops->square(this,x); }
  214. const _cl_ring_element _expt_pos (const _cl_ring_element& x, const cl_I& y)
  215. { return mulops->expt_pos(this,x,y); }
  216. // High-level operations.
  217. void fprint (std::ostream& stream, const cl_ring_element& x)
  218. {
  219. if (!(x.ring() == this)) cl_abort();
  220. _fprint(stream,x);
  221. }
  222. cl_boolean equal (const cl_ring_element& x, const cl_ring_element& y)
  223. {
  224. if (!(x.ring() == this)) cl_abort();
  225. if (!(y.ring() == this)) cl_abort();
  226. return _equal(x,y);
  227. }
  228. const cl_ring_element zero ()
  229. {
  230. return cl_ring_element(this,_zero());
  231. }
  232. cl_boolean zerop (const cl_ring_element& x)
  233. {
  234. if (!(x.ring() == this)) cl_abort();
  235. return _zerop(x);
  236. }
  237. const cl_ring_element plus (const cl_ring_element& x, const cl_ring_element& y)
  238. {
  239. if (!(x.ring() == this)) cl_abort();
  240. if (!(y.ring() == this)) cl_abort();
  241. return cl_ring_element(this,_plus(x,y));
  242. }
  243. const cl_ring_element minus (const cl_ring_element& x, const cl_ring_element& y)
  244. {
  245. if (!(x.ring() == this)) cl_abort();
  246. if (!(y.ring() == this)) cl_abort();
  247. return cl_ring_element(this,_minus(x,y));
  248. }
  249. const cl_ring_element uminus (const cl_ring_element& x)
  250. {
  251. if (!(x.ring() == this)) cl_abort();
  252. return cl_ring_element(this,_uminus(x));
  253. }
  254. const cl_ring_element one ()
  255. {
  256. return cl_ring_element(this,_one());
  257. }
  258. const cl_ring_element canonhom (const cl_I& x)
  259. {
  260. return cl_ring_element(this,_canonhom(x));
  261. }
  262. const cl_ring_element mul (const cl_ring_element& x, const cl_ring_element& y)
  263. {
  264. if (!(x.ring() == this)) cl_abort();
  265. if (!(y.ring() == this)) cl_abort();
  266. return cl_ring_element(this,_mul(x,y));
  267. }
  268. const cl_ring_element square (const cl_ring_element& x)
  269. {
  270. if (!(x.ring() == this)) cl_abort();
  271. return cl_ring_element(this,_square(x));
  272. }
  273. const cl_ring_element expt_pos (const cl_ring_element& x, const cl_I& y)
  274. {
  275. if (!(x.ring() == this)) cl_abort();
  276. return cl_ring_element(this,_expt_pos(x,y));
  277. }
  278. // Property operations.
  279. cl_property* get_property (const cl_symbol& key)
  280. { return properties.get_property(key); }
  281. void add_property (cl_property* new_property)
  282. { properties.add_property(new_property); }
  283. // Constructor.
  284. cl_heap_ring (cl_ring_setops* setopv, cl_ring_addops* addopv, cl_ring_mulops* mulopv)
  285. : setops (setopv), addops (addopv), mulops (mulopv)
  286. { refcount = 0; } // will be incremented by the `cl_ring' constructor
  287. };
  288. #define SUBCLASS_cl_heap_ring() \
  289. public: \
  290. /* Allocation. */ \
  291. void* operator new (size_t size) { return malloc_hook(size); } \
  292. /* Deallocation. */ \
  293. void operator delete (void* ptr) { free_hook(ptr); }
  294. // Operations on ring elements.
  295. // Output.
  296. inline void fprint (std::ostream& stream, const cl_ring_element& x)
  297. { x.ring()->fprint(stream,x); }
  298. CL_DEFINE_PRINT_OPERATOR(cl_ring_element)
  299. // Add.
  300. inline const cl_ring_element operator+ (const cl_ring_element& x, const cl_ring_element& y)
  301. { return x.ring()->plus(x,y); }
  302. // Negate.
  303. inline const cl_ring_element operator- (const cl_ring_element& x)
  304. { return x.ring()->uminus(x); }
  305. // Subtract.
  306. inline const cl_ring_element operator- (const cl_ring_element& x, const cl_ring_element& y)
  307. { return x.ring()->minus(x,y); }
  308. // Equality.
  309. inline bool operator== (const cl_ring_element& x, const cl_ring_element& y)
  310. { return x.ring()->equal(x,y); }
  311. inline bool operator!= (const cl_ring_element& x, const cl_ring_element& y)
  312. { return !x.ring()->equal(x,y); }
  313. // Compare against 0.
  314. inline cl_boolean zerop (const cl_ring_element& x)
  315. { return x.ring()->zerop(x); }
  316. // Multiply.
  317. inline const cl_ring_element operator* (const cl_ring_element& x, const cl_ring_element& y)
  318. { return x.ring()->mul(x,y); }
  319. // Squaring.
  320. inline const cl_ring_element square (const cl_ring_element& x)
  321. { return x.ring()->square(x); }
  322. // Exponentiation x^y, where y > 0.
  323. inline const cl_ring_element expt_pos (const cl_ring_element& x, const cl_I& y)
  324. { return x.ring()->expt_pos(x,y); }
  325. // Scalar multiplication.
  326. // [Is this operation worth being specially optimized for the case of
  327. // polynomials?? Polynomials have a faster scalar multiplication.
  328. // We should use it.??]
  329. inline const cl_ring_element operator* (const cl_I& x, const cl_ring_element& y)
  330. { return y.ring()->mul(y.ring()->canonhom(x),y); }
  331. inline const cl_ring_element operator* (const cl_ring_element& x, const cl_I& y)
  332. { return x.ring()->mul(x.ring()->canonhom(y),x); }
  333. // Ring of uninitialized elements.
  334. // Any operation results in a run-time error.
  335. extern const cl_ring cl_no_ring;
  336. extern cl_class cl_class_no_ring;
  337. CL_REQUIRE(cl_no_ring)
  338. inline cl_ring::cl_ring ()
  339. : cl_rcpointer (as_cl_private_thing(cl_no_ring)) {}
  340. inline _cl_ring_element::_cl_ring_element ()
  341. : rep ((cl_private_thing) cl_combine(cl_FN_tag,0)) {}
  342. inline cl_ring_element::cl_ring_element ()
  343. : _cl_ring_element (), _ring () {}
  344. // Support for built-in number rings.
  345. // Beware, they are not optimally efficient.
  346. template <class T>
  347. struct cl_number_ring_ops {
  348. cl_boolean (* contains) (const cl_number&);
  349. cl_boolean (* equal) (const T&, const T&);
  350. cl_boolean (* zerop) (const T&);
  351. const T (* plus) (const T&, const T&);
  352. const T (* minus) (const T&, const T&);
  353. const T (* uminus) (const T&);
  354. const T (* mul) (const T&, const T&);
  355. const T (* square) (const T&);
  356. const T (* expt_pos) (const T&, const cl_I&);
  357. };
  358. class cl_heap_number_ring : public cl_heap_ring {
  359. public:
  360. cl_number_ring_ops<cl_number>* ops;
  361. // Constructor.
  362. cl_heap_number_ring (cl_ring_setops* setopv, cl_ring_addops* addopv, cl_ring_mulops* mulopv, cl_number_ring_ops<cl_number>* opv)
  363. : cl_heap_ring (setopv,addopv,mulopv), ops (opv) {}
  364. };
  365. class cl_number_ring : public cl_ring {
  366. public:
  367. cl_number_ring (cl_heap_number_ring* r)
  368. : cl_ring (r) {}
  369. };
  370. template <class T>
  371. class cl_specialized_number_ring : public cl_number_ring {
  372. public:
  373. cl_specialized_number_ring ();
  374. };
  375. // Type test.
  376. inline cl_boolean instanceof (const cl_number& x, const cl_number_ring& R)
  377. {
  378. return ((cl_heap_number_ring*) R.heappointer)->ops->contains(x);
  379. }
  380. // Hack section.
  381. // Conversions to subtypes without checking:
  382. // The2(cl_MI)(x) converts x to a cl_MI, without change of representation!
  383. #define The(type) *(const type *) & cl_identity
  384. #define The2(type) *(const type *) & cl_identity2
  385. // This inline function is for type checking purposes only.
  386. inline const cl_ring& cl_identity (const cl_ring& r) { return r; }
  387. inline const cl_ring_element& cl_identity2 (const cl_ring_element& x) { return x; }
  388. inline const cl_gcobject& cl_identity (const _cl_ring_element& x) { return x.rep; }
  389. // Debugging support.
  390. #ifdef CL_DEBUG
  391. extern int cl_ring_debug_module;
  392. CL_FORCE_LINK(cl_ring_debug_dummy, cl_ring_debug_module)
  393. #endif
  394. } // namespace cln
  395. #endif /* _CL_RING_H */