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