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