1 | ;;; -*- Mode: Lisp -*-
|
---|
2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
3 | ;;;
|
---|
4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
5 | ;;;
|
---|
6 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
7 | ;;; it under the terms of the GNU General Public License as published by
|
---|
8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
9 | ;;; (at your option) any later version.
|
---|
10 | ;;;
|
---|
11 | ;;; This program is distributed in the hope that it will be useful,
|
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | ;;; GNU General Public License for more details.
|
---|
15 | ;;;
|
---|
16 | ;;; You should have received a copy of the GNU General Public License
|
---|
17 | ;;; along with this program; if not, write to the Free Software
|
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
19 | ;;;
|
---|
20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
21 |
|
---|
22 | (defpackage "INTEGER-RING"
|
---|
23 | (:use :cl :copy :ring)
|
---|
24 | (:export "INTEGER-RING"
|
---|
25 | "INTEGER-RING-VALUE"
|
---|
26 | "ADD-TO"
|
---|
27 | "SUBTRACT-FROM"
|
---|
28 | "MULTIPLY-BY"
|
---|
29 | "DIVIDE-BY"
|
---|
30 | "UNARY-MINUS"
|
---|
31 | "UNIVERSAL-GCD"
|
---|
32 | "UNIVERSAL-EZGCD"
|
---|
33 | "UNIVERSAL-EQUALP"
|
---|
34 | "UNIVERSAL-ZEROP"
|
---|
35 | "->SEXP")
|
---|
36 | (:documentation "Wraps integers into an object."))
|
---|
37 |
|
---|
38 | (in-package "INTEGER-RING")
|
---|
39 |
|
---|
40 | (defclass integer-ring (ring)
|
---|
41 | ((value :initarg :value :initform 0 :accessor integer-ring-value))
|
---|
42 | (:documentation "An object representing an integer.")
|
---|
43 | )
|
---|
44 |
|
---|
45 | (defmethod print-object ((self integer-ring) stream)
|
---|
46 | (print-unreadable-object (self stream :type t :identity t)
|
---|
47 | (with-accessors ((value integer-ring-value))
|
---|
48 | self
|
---|
49 | (format stream "VALUE=~A" value))))
|
---|
50 |
|
---|
51 | (defmethod multiply-by ((self integer-ring) (other integer-ring))
|
---|
52 | (with-slots (value)
|
---|
53 | self
|
---|
54 | (with-slots ((other-value value))
|
---|
55 | other
|
---|
56 | (setf value (* value other-value))))
|
---|
57 | self)
|
---|
58 |
|
---|
59 | (defmethod divide-by ((self integer-ring) (other integer-ring))
|
---|
60 | (with-slots (value)
|
---|
61 | self
|
---|
62 | (with-slots ((other-value value))
|
---|
63 | other
|
---|
64 | (assert (zerop (mod value other-value)))
|
---|
65 | (setf value (/ value other-value))))
|
---|
66 | self)
|
---|
67 |
|
---|
68 | (defmethod add-to ((self integer-ring) (other integer-ring))
|
---|
69 | (with-slots (value)
|
---|
70 | self
|
---|
71 | (with-slots ((other-value value))
|
---|
72 | other
|
---|
73 | (setf value (+ value other-value))))
|
---|
74 | self)
|
---|
75 |
|
---|
76 | (defmethod subtract-from ((self integer-ring) (other integer-ring))
|
---|
77 | (with-slots (value)
|
---|
78 | self
|
---|
79 | (with-slots ((other-value value))
|
---|
80 | other
|
---|
81 | (setf value (- value other-value))))
|
---|
82 | self)
|
---|
83 |
|
---|
84 | (defmethod unary-minus ((self integer-ring))
|
---|
85 | (with-slots (value)
|
---|
86 | self
|
---|
87 | (setf value (- value)))
|
---|
88 | self)
|
---|
89 |
|
---|
90 | (defmethod universal-zerop ((self integer-ring))
|
---|
91 | (with-slots (value)
|
---|
92 | self
|
---|
93 | (zerop value)))
|
---|
94 |
|
---|
95 | (defmethod universal-gcd ((self integer-ring) (other integer-ring))
|
---|
96 | (with-slots (value)
|
---|
97 | self
|
---|
98 | (with-slots ((other-value value))
|
---|
99 | other
|
---|
100 | (make-instance 'integer-ring :value (gcd value other-value)))))
|
---|
101 |
|
---|
102 |
|
---|
103 | (defmethod universal-ezgcd ((object integer-ring) (other integer-ring))
|
---|
104 | (with-slots (value)
|
---|
105 | object
|
---|
106 | (with-slots ((other-value value))
|
---|
107 | other
|
---|
108 | (let ((c (gcd value other-value)))
|
---|
109 | (values (make-instance 'integer-ring :value c)
|
---|
110 | (make-instance 'integer-ring :value (/ value c))
|
---|
111 | (make-instance 'integer-ring :value (/ other-value c)))))))
|
---|
112 |
|
---|
113 | (defmethod universal-equalp ((self integer-ring) (other integer-ring))
|
---|
114 | (with-slots (value)
|
---|
115 | self
|
---|
116 | (with-slots ((other-value value))
|
---|
117 | other
|
---|
118 | (= value other-value))))
|
---|
119 |
|
---|
120 | (defmethod ->sexp ((self integer-ring) &optional vars)
|
---|
121 | (declare (ignore vars))
|
---|
122 | (integer-ring-value self))
|
---|