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 :rational-field)
|
---|
24 | (:shadowing-import-from "RATIONAL-FIELD" value)
|
---|
25 | (:export "INTEGER-RING"
|
---|
26 | "INTEGER-RING-VALUE"
|
---|
27 | "ADD-TO"
|
---|
28 | "SUBTRACT-FROM"
|
---|
29 | "MULTIPLY-BY"
|
---|
30 | "DIVIDE-BY"
|
---|
31 | "UNARY-MINUS"
|
---|
32 | "UNARY-INVERSE"
|
---|
33 | "UNIVERSAL-GCD"
|
---|
34 | "UNIVERSAL-EZGCD"
|
---|
35 | "UNIVERSAL-EQUALP"
|
---|
36 | "UNIVERSAL-ZEROP"
|
---|
37 | "->SEXP")
|
---|
38 | (:documentation "Wraps integers into an object."))
|
---|
39 |
|
---|
40 | (in-package "INTEGER-RING")
|
---|
41 |
|
---|
42 | (defclass integer-ring (rational-field)
|
---|
43 | ((value :initarg :value :initform 0 :accessor integer-ring-value :type integer))
|
---|
44 | (:documentation "An object representing an integer.")
|
---|
45 | )
|
---|
46 |
|
---|
47 | (defmethod print-object ((self integer-ring) stream)
|
---|
48 | (print-unreadable-object (self stream :type t :identity t)
|
---|
49 | (with-accessors ((value integer-ring-value))
|
---|
50 | self
|
---|
51 | (format stream "VALUE=~A" value))))
|
---|
52 |
|
---|
53 | (defmethod unary-inverse ((self integer-ring))
|
---|
54 | (with-slots (value)
|
---|
55 | self
|
---|
56 | (case value
|
---|
57 | (1 self)
|
---|
58 | (-1 self)
|
---|
59 | (t
|
---|
60 | (unary-inverse (change-class self 'rational-field))))))
|
---|
61 |
|
---|
62 | (defmethod universal-gcd ((self integer-ring) (other integer-ring))
|
---|
63 | (with-slots (value)
|
---|
64 | self
|
---|
65 | (with-slots ((other-value value))
|
---|
66 | other
|
---|
67 | (make-instance 'integer-ring :value (gcd value other-value)))))
|
---|
68 |
|
---|
69 | (defmethod universal-ezgcd ((object integer-ring) (other integer-ring))
|
---|
70 | (with-slots (value)
|
---|
71 | object
|
---|
72 | (with-slots ((other-value value))
|
---|
73 | other
|
---|
74 | (let ((c (gcd value other-value)))
|
---|
75 | (values (make-instance 'integer-ring :value c)
|
---|
76 | (make-instance 'integer-ring :value (/ value c))
|
---|
77 | (make-instance 'integer-ring :value (/ other-value c)))))))
|
---|