[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 |
|
---|
[4325] | 22 | (in-package "RING")
|
---|
[4224] | 23 |
|
---|
[4300] | 24 | (defclass integer-ring (rational-field)
|
---|
[4267] | 25 | ((value :initarg :value :initform 0 :accessor integer-ring-value :type integer))
|
---|
[4224] | 26 | (:documentation "An object representing an integer.")
|
---|
| 27 | )
|
---|
| 28 |
|
---|
| 29 | (defmethod print-object ((self integer-ring) stream)
|
---|
| 30 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 31 | (with-accessors ((value integer-ring-value))
|
---|
| 32 | self
|
---|
| 33 | (format stream "VALUE=~A" value))))
|
---|
| 34 |
|
---|
[4426] | 35 | (defmethod shared-initialize :around ((self integer-ring) slot-names
|
---|
[4424] | 36 | &rest initargs
|
---|
[4426] | 37 | &key (value nil value-supplied-p))
|
---|
| 38 | "Checks the argument VALUE. Calls error if it is not an integer."
|
---|
| 39 | (declare (ignore initargs))
|
---|
| 40 | (when value-supplied-p
|
---|
| 41 | (assert (integerp value) nil "When initializing INTEGER-RING, argument VALUE must be an integer, got ~S." value))
|
---|
| 42 | (call-next-method))
|
---|
[4424] | 43 |
|
---|
| 44 |
|
---|
[4264] | 45 | (defmethod unary-inverse ((self integer-ring))
|
---|
[4326] | 46 | "Invert SELF, possibly changing class to RATIONAL-FIELD."
|
---|
[4264] | 47 | (with-slots (value)
|
---|
| 48 | self
|
---|
| 49 | (case value
|
---|
| 50 | (1 self)
|
---|
| 51 | (-1 self)
|
---|
[4312] | 52 | (t
|
---|
[4426] | 53 | (warn "In UNARY-INVERSE with argument ~S, conversion from INTEGER-RING to RATIONAL-FIELD." self)
|
---|
[4304] | 54 | (unary-inverse (change-class self 'rational-field))))))
|
---|
[4264] | 55 |
|
---|
[4247] | 56 | (defmethod universal-gcd ((self integer-ring) (other integer-ring))
|
---|
[4224] | 57 | (with-slots (value)
|
---|
| 58 | self
|
---|
| 59 | (with-slots ((other-value value))
|
---|
| 60 | other
|
---|
[4260] | 61 | (make-instance 'integer-ring :value (gcd value other-value)))))
|
---|
[4227] | 62 |
|
---|
[4250] | 63 | (defmethod universal-ezgcd ((object integer-ring) (other integer-ring))
|
---|
| 64 | (with-slots (value)
|
---|
| 65 | object
|
---|
[4255] | 66 | (with-slots ((other-value value))
|
---|
[4250] | 67 | other
|
---|
[4254] | 68 | (let ((c (gcd value other-value)))
|
---|
[4250] | 69 | (values (make-instance 'integer-ring :value c)
|
---|
| 70 | (make-instance 'integer-ring :value (/ value c))
|
---|
| 71 | (make-instance 'integer-ring :value (/ other-value c)))))))
|
---|