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