[4228] | 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 "RING"
|
---|
[4332] | 23 | (:use :cl :copy)
|
---|
[4228] | 24 | (:export "RING"
|
---|
[4342] | 25 | "FIELD"
|
---|
[4228] | 26 | "ADD-TO"
|
---|
| 27 | "SUBTRACT-FROM"
|
---|
| 28 | "MULTIPLY-BY"
|
---|
| 29 | "DIVIDE-BY"
|
---|
[4354] | 30 | "MULTIPLY"
|
---|
| 31 | "DIVIDE"
|
---|
[4231] | 32 | "UNARY-MINUS"
|
---|
[4246] | 33 | "UNARY-INVERSE"
|
---|
[4249] | 34 | "UNIVERSAL-GCD"
|
---|
[4228] | 35 | "UNIVERSAL-EZGCD"
|
---|
| 36 | "UNIVERSAL-EQUALP"
|
---|
| 37 | "UNIVERSAL-ZEROP"
|
---|
[4279] | 38 | "MAKE-ZERO-FOR"
|
---|
| 39 | "MAKE-UNIT-FOR"
|
---|
[4323] | 40 | "->SEXP"
|
---|
| 41 | "RATIONAL-FIELD"
|
---|
| 42 | "RATIONAL-FIELD-VALUE"
|
---|
| 43 | "MAKE-ZERO-FOR"
|
---|
| 44 | "MAKE-UNIT-FOR"
|
---|
| 45 | "INTEGER-RING"
|
---|
| 46 | "INTEGER-RING-VALUE"
|
---|
| 47 | )
|
---|
| 48 | (:documentation "Defines an abstract ring class and ring operations.")
|
---|
| 49 | )
|
---|
[4228] | 50 |
|
---|
| 51 | (in-package "RING")
|
---|
| 52 |
|
---|
| 53 | (defclass ring () () (:documentation "An abstract ring."))
|
---|
[4342] | 54 | (defclass field (ring) () (:documentation "An abstract ring."))
|
---|
[4228] | 55 |
|
---|
[4246] | 56 | (defgeneric multiply-by (self other)
|
---|
[4228] | 57 | (:documentation "Multiply SELF by OTHER."))
|
---|
| 58 |
|
---|
[4246] | 59 | (defgeneric divide-by (self other)
|
---|
| 60 | (:documentation "Divide SELF by OTHER."))
|
---|
| 61 |
|
---|
[4228] | 62 | (defgeneric add-to (self other)
|
---|
| 63 | (:documentation "Add OTHER to SELF."))
|
---|
| 64 |
|
---|
[4246] | 65 | (defgeneric subtract-from (self other)
|
---|
[4228] | 66 | (:documentation "Subtract OTHER from SELF."))
|
---|
| 67 |
|
---|
[4230] | 68 | (defgeneric unary-minus (self)
|
---|
[4234] | 69 | (:documentation "Changes SELF to its negative."))
|
---|
[4230] | 70 |
|
---|
[4246] | 71 | (defgeneric unary-inverse (self)
|
---|
[4273] | 72 | (:documentation "Changes SELF to the unary inverse of SELF."))
|
---|
[4246] | 73 |
|
---|
[4257] | 74 | (defgeneric universal-gcd (object other)
|
---|
[4274] | 75 | (:documentation "Returns GCD(OBJECT, OTHER)"))
|
---|
[4228] | 76 |
|
---|
[4248] | 77 | (defgeneric universal-ezgcd (x y)
|
---|
| 78 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
[4258] | 79 | C=GCD(X,Y). It returns three values: C, X1 and Y1. The result may be obtained by
|
---|
[4248] | 80 | the Euclidean algorithm."))
|
---|
| 81 |
|
---|
[4228] | 82 | (defgeneric universal-equalp (self other)
|
---|
| 83 | (:documentation "Return T if objects SELF and OTHER are equal, NIL otherwise.")
|
---|
[4311] | 84 | (:method ((object1 cons) (object2 cons)) (every #'universal-equalp object1 object2))
|
---|
| 85 | (:method ((self number) (other number)) (= self other)))
|
---|
[4228] | 86 |
|
---|
| 87 | (defgeneric universal-zerop (self)
|
---|
| 88 | (:documentation "Return T if SELF is a zero in the ring it belongs to."))
|
---|
| 89 |
|
---|
[4230] | 90 | (defgeneric ->sexp (self &optional vars)
|
---|
[4228] | 91 | (:documentation "Convert SELF to an S-expression."))
|
---|
[4248] | 92 |
|
---|
[4278] | 93 | (defgeneric make-zero-for (self)
|
---|
[4322] | 94 | (:documentation "Create a zero in the ring of SELF. A fresh instance
|
---|
| 95 | should be returned upon every call."))
|
---|
[4278] | 96 |
|
---|
| 97 | (defgeneric make-unit-for (self)
|
---|
[4322] | 98 | (:documentation "Create a unit in the ring of SELF. A fresh instance
|
---|
| 99 | should be returned upon every call."))
|
---|
[4349] | 100 |
|
---|
| 101 | (defgeneric multiply (factor &rest more-factors)
|
---|
| 102 | (:documentation "Successively multiplies factor FACTOR by the remaining arguments
|
---|
[4352] | 103 | MORE-FACTORS, using MULTIPLY-BY to multiply two factors. FACTOR must not be destructively modified.
|
---|
| 104 | Instead, a copy of factor should be made and returned as the value of this function.")
|
---|
[4351] | 105 | (:method ((factor t) &rest more-factors)
|
---|
[4349] | 106 | (reduce #'multiply-by more-factors :initial-value (copy-instance factor))))
|
---|
| 107 |
|
---|
[4354] | 108 |
|
---|
| 109 | (defgeneric divide (numerator &rest denominators)
|
---|
| 110 | (:documentation "Successively divides NUMERATOR by elements of DENOMINATORS. The operation
|
---|
| 111 | should not modify the NUMERATOR. Instead, a copy of factor should be made and returned as the value of this function.")
|
---|
| 112 | (:method ((numerator t) &rest denominators)
|
---|
| 113 | (cond ((endp denominators)
|
---|
| 114 | (unary-inverse (copy-instance numerator)))
|
---|
| 115 | (t (reduce #'divide-by denominators :initial-value (copy-instance numerator))))))
|
---|
| 116 |
|
---|