[4224] | 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"
|
---|
[4233] | 30 | "UNARY-MINUS"
|
---|
[4247] | 31 | "UNIVERSAL-GCD"
|
---|
[4252] | 32 | "UNIVERSAL-EZGCD"
|
---|
[4224] | 33 | "UNIVERSAL-EQUALP"
|
---|
[4227] | 34 | "UNIVERSAL-ZEROP"
|
---|
[4238] | 35 | "->SEXP")
|
---|
[4227] | 36 | (:documentation "Wraps integers into an object."))
|
---|
[4224] | 37 |
|
---|
[4227] | 38 | (in-package "INTEGER-RING")
|
---|
[4224] | 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 |
|
---|
[4227] | 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 | (setf value (/ value other-value))))
|
---|
| 65 | self)
|
---|
| 66 |
|
---|
[4224] | 67 | (defmethod add-to ((self integer-ring) (other integer-ring))
|
---|
| 68 | (with-slots (value)
|
---|
| 69 | self
|
---|
| 70 | (with-slots ((other-value value))
|
---|
| 71 | other
|
---|
| 72 | (setf value (+ value other-value))))
|
---|
| 73 | self)
|
---|
| 74 |
|
---|
| 75 | (defmethod subtract-from ((self integer-ring) (other integer-ring))
|
---|
| 76 | (with-slots (value)
|
---|
| 77 | self
|
---|
| 78 | (with-slots ((other-value value))
|
---|
| 79 | other
|
---|
| 80 | (setf value (- value other-value))))
|
---|
| 81 | self)
|
---|
| 82 |
|
---|
[4232] | 83 | (defmethod unary-minus ((self integer-ring))
|
---|
| 84 | (with-slots (value)
|
---|
| 85 | self
|
---|
| 86 | (setf value (- value)))
|
---|
| 87 | self)
|
---|
| 88 |
|
---|
[4244] | 89 | (defmethod universal-zerop ((self integer-ring))
|
---|
| 90 | (with-slots (value)
|
---|
| 91 | self
|
---|
| 92 | (zerop value)))
|
---|
| 93 |
|
---|
[4247] | 94 | (defmethod universal-gcd ((self integer-ring) (other integer-ring))
|
---|
[4224] | 95 | (with-slots (value)
|
---|
| 96 | self
|
---|
| 97 | (with-slots ((other-value value))
|
---|
| 98 | other
|
---|
| 99 | (setf value (gcd value other-value))))
|
---|
| 100 | self)
|
---|
[4227] | 101 |
|
---|
[4250] | 102 | (defmethod universal-ezgcd ((object integer-ring) (other integer-ring))
|
---|
| 103 | (with-slots (value)
|
---|
| 104 | object
|
---|
[4255] | 105 | (with-slots ((other-value value))
|
---|
[4250] | 106 | other
|
---|
[4254] | 107 | (let ((c (gcd value other-value)))
|
---|
[4250] | 108 | (values (make-instance 'integer-ring :value c)
|
---|
| 109 | (make-instance 'integer-ring :value (/ value c))
|
---|
| 110 | (make-instance 'integer-ring :value (/ other-value c)))))))
|
---|
[4244] | 111 |
|
---|
[4227] | 112 | (defmethod universal-equalp ((self integer-ring) (other integer-ring))
|
---|
| 113 | (with-slots (value)
|
---|
| 114 | self
|
---|
| 115 | (with-slots ((other-value value))
|
---|
| 116 | other
|
---|
| 117 | (= value other-value))))
|
---|
| 118 |
|
---|
[4229] | 119 | (defmethod ->sexp ((self integer-ring) &optional vars)
|
---|
| 120 | (declare (ignore vars))
|
---|
[4227] | 121 | (integer-ring-value self))
|
---|