1 | ;;; -*- mode: lisp; package: maxima; syntax: common-lisp; base: 10 -*-
|
---|
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)
|
---|
24 | (:export "R-PARSE"
|
---|
25 | "UNIT-ELEMENT"
|
---|
26 | "R-ZEROP"
|
---|
27 | "R+"
|
---|
28 | "R-"
|
---|
29 | "R*"
|
---|
30 | "R+"
|
---|
31 | "R/"
|
---|
32 | "R-EXPT"
|
---|
33 | "R-LCM"
|
---|
34 | "R-EZGCD"
|
---|
35 | "R-GCD"
|
---|
36 | "R-TOTAL-DEGREE"
|
---|
37 | "R-DIMENSION"
|
---|
38 | "R-EXPONENTS"
|
---|
39 | "R-COEFF"
|
---|
40 | "R-SUGAR"
|
---|
41 | "R-DIVIDES-P"
|
---|
42 | "R-DIVIDES-LCM-P"
|
---|
43 | "R-LCM-DIVIDES-LCM-P"
|
---|
44 | "R-LCM-EQUAL-LCM-P"
|
---|
45 | "R-REL-PRIME-P"
|
---|
46 | "R-EQUALP"
|
---|
47 | "R-CLONE"
|
---|
48 | "R-ELT"
|
---|
49 | "R->LIST"
|
---|
50 | "R-DIVISIBLE-BY-P"
|
---|
51 | "R-REL-PRIME-P"
|
---|
52 | "R-DEPENDS-P"
|
---|
53 | "R-TENSOR-PRODUCT"
|
---|
54 | "R-CONTRACT"
|
---|
55 | "R-LENGTH"
|
---|
56 | "MULTIPLY-BY"
|
---|
57 | "DIVIDE-BY"
|
---|
58 | "ADD-TO"
|
---|
59 | "SUBTRACT-FROM"
|
---|
60 | "UNARY-MINUS"
|
---|
61 | "SCALAR"
|
---|
62 | "INSERT-ITEM"
|
---|
63 | "APPEND-ITEM"
|
---|
64 | "COPY-INSTANCE")
|
---|
65 | (:shadowing-import-from
|
---|
66 | #+openmcl-native-threads #:ccl
|
---|
67 | #+cmu #:pcl
|
---|
68 | #+sbcl #:sb-pcl
|
---|
69 | #+lispworks #:hcl
|
---|
70 | #+allegro #:mop
|
---|
71 | #+clisp #:clos
|
---|
72 | #:class-slots #:slot-definition-name)
|
---|
73 | (:documentation
|
---|
74 | "Implements ring operations. These are all operations that are
|
---|
75 | performed on the coefficients by the package, and thus the coefficient
|
---|
76 | ring can be changed by merely redefining these operations."))
|
---|
77 |
|
---|
78 | (in-package :ring)
|
---|
79 |
|
---|
80 | (defclass scalar ()
|
---|
81 | ((value :initarg :value :accessor value))
|
---|
82 | (:documentation "Wraps objects suitable as scalars/polynomial coefficients"))
|
---|
83 |
|
---|
84 | (defgeneric unit-element (class))
|
---|
85 |
|
---|
86 | (defgeneric r-zerop (object)
|
---|
87 | (:method ((self number)) (zerop self))
|
---|
88 | (:documentation "Tests whether a ring element is 0."))
|
---|
89 |
|
---|
90 | (defgeneric r+ (x y)
|
---|
91 | (:method ((x number) (y number)) (+ x y))
|
---|
92 | (:documentation "Adds ring elements."))
|
---|
93 |
|
---|
94 | (defgeneric r- (x y)
|
---|
95 | (:method ((x number) (y number)) (- x y))
|
---|
96 | (:documentation "Subtracts ring elements."))
|
---|
97 |
|
---|
98 | (defgeneric r* (x y)
|
---|
99 | (:method ((x number) (y number)) (* x y))
|
---|
100 | (:documentation "Multiplies ring elements."))
|
---|
101 |
|
---|
102 | (defgeneric lef-tensor-productby (self other)
|
---|
103 | (:documentation "Takes a tensor product of SELF with OTHER, where
|
---|
104 | OTHER is the left factor."))
|
---|
105 |
|
---|
106 | (defgeneric r/ (x y)
|
---|
107 | (:method ((x number) (y number)) (/ x y))
|
---|
108 | (:documentation "Divides ring elements."))
|
---|
109 |
|
---|
110 | (defgeneric r-lcm (x y)
|
---|
111 | (:method ((x integer) (y integer)) (lcm x y))
|
---|
112 | (:documentation "Returns the least common multiple of ring elements."))
|
---|
113 |
|
---|
114 | (defgeneric r-expt (x y)
|
---|
115 | (:method ((x integer) (y integer)) (expt x y))
|
---|
116 | (:documentation "Raises X to power Y."))
|
---|
117 |
|
---|
118 | (defgeneric r-ezgcd (x y)
|
---|
119 | (:method ((x integer) (y integer)
|
---|
120 | &aux (c (gcd x y)))
|
---|
121 | (values c (/ x c) (/ y c)))
|
---|
122 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
123 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
---|
124 | the Euclidean algorithm."))
|
---|
125 |
|
---|
126 | (defgeneric r-gcd (x y)
|
---|
127 | (:method ((x integer) (y integer))
|
---|
128 | (gcd x y))
|
---|
129 | (:documentation "Returns GCD(X,Y)."))
|
---|
130 |
|
---|
131 | (defgeneric r-dimension (object))
|
---|
132 | (defgeneric r-exponents (object))
|
---|
133 |
|
---|
134 | (defgeneric r-coeff (object))
|
---|
135 | (defgeneric (setf r-coeff) (new-value object))
|
---|
136 |
|
---|
137 | (defgeneric r-total-degree (object &optional start end))
|
---|
138 |
|
---|
139 | (defgeneric r-divides-p (object1 object2)
|
---|
140 | (:method ((object1 integer) (object2 integer))
|
---|
141 | (zerop (rem object2 object1)))
|
---|
142 | (:documentation "Returns T if OBJECT1 divides OBJECT2"))
|
---|
143 |
|
---|
144 | (defgeneric r-divides-lcm-p (object1 object2 object3)
|
---|
145 | (:documentation "Returns T if OBJECT divides LCM(OBJECT2, OBJECT3), NIL otherwise."))
|
---|
146 |
|
---|
147 | (defgeneric r-lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
148 | (:documentation "Returns T if LCM(OBJECT1,OBJECT2) divides LCM(OBJECT3,OBJECT4), NIL otherwise."))
|
---|
149 |
|
---|
150 | (defgeneric r-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
151 | (:documentation "Returns T if object LCM(OBJECT1,OBJECT2) equals LCM(OBJECT3,OBJECT4), NIL otherwise."))
|
---|
152 |
|
---|
153 | (defgeneric r-equalp (object1 object2)
|
---|
154 | (:method (object1 object2) (equalp object1 object2))
|
---|
155 | (:documentation "Equality using deep comparison of object slots."))
|
---|
156 |
|
---|
157 | (defgeneric r-elt (object index))
|
---|
158 |
|
---|
159 | (defgeneric (setf r-elt) (new-value object index))
|
---|
160 |
|
---|
161 | (defgeneric r-length (object))
|
---|
162 |
|
---|
163 | (defgeneric r->list (object))
|
---|
164 | (defgeneric r-sugar (object))
|
---|
165 | (defgeneric r-rel-prime-p (object1 object2))
|
---|
166 | (defgeneric left-contract (object k))
|
---|
167 | (defgeneric r-divisible-by-p (object1 object2))
|
---|
168 | (defgeneric r-depends-p (object k))
|
---|
169 |
|
---|
170 | (defgeneric multiply-by (self other)
|
---|
171 | (:method (self other) (r* self other))
|
---|
172 | (:documentation "Multiply object SELF and OTHER and store the result
|
---|
173 | into SELF. It returns SELF. For instances of a class, this operation
|
---|
174 | may be destructive."))
|
---|
175 |
|
---|
176 | (defgeneric divide-by (self other)
|
---|
177 | (:method (self other) (r/ self other))
|
---|
178 | (:documentation "Divided object SELF by OTHER and store the result
|
---|
179 | into SELF. It returns SELF. For instances of a class, this operation
|
---|
180 | may be destructive."))
|
---|
181 |
|
---|
182 | (defgeneric add-to (self other)
|
---|
183 | (:documentation "Add to object SELF another object OTHER. For
|
---|
184 | complex objects, it may destructively modify SELF and destructively
|
---|
185 | modify/invalidate object OTHER. For standard classes implementing this
|
---|
186 | method, the result should be an object which is EQ to SELF. For
|
---|
187 | built-in classes, such as NUMBER, the returned object may not be EQ to
|
---|
188 | the original, but it will be EQL to it.")
|
---|
189 | (:method (self other) (r+ self other)))
|
---|
190 |
|
---|
191 | (defgeneric subtract-from (self other)
|
---|
192 | (:documentation "Subtract from an object SELF another object OTHER.
|
---|
193 | For complex objects, it may destructively modify SELF and
|
---|
194 | destructively modify/invalidate object OTHER. For standard classes
|
---|
195 | implementing this method, the result should be an object which is EQ
|
---|
196 | to SELF. For built-in classes, such as NUMBER, the returned object may
|
---|
197 | not be EQ to the original.")
|
---|
198 | (:method (self other) (r- self other)))
|
---|
199 |
|
---|
200 | (defgeneric unary-minus (self)
|
---|
201 | (:method ((x number)) (- x)))
|
---|
202 |
|
---|
203 | (defgeneric insert-item (self item))
|
---|
204 | (defgeneric append-item (self item))
|
---|
205 |
|
---|
206 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
207 | ;; NOTE: This is a shallow copy. Add an around method for classes which need deep copy of the slots.
|
---|
208 | (defgeneric copy-instance (object &rest initargs &key &allow-other-keys)
|
---|
209 | (:documentation "Makes and returns a shallow copy of OBJECT.
|
---|
210 |
|
---|
211 | An uninitialized object of the same class as OBJECT is allocated by
|
---|
212 | calling ALLOCATE-INSTANCE. For all slots returned by
|
---|
213 | CLASS-SLOTS, the returned object has the
|
---|
214 | same slot values and slot-unbound status as OBJECT.
|
---|
215 |
|
---|
216 | REINITIALIZE-INSTANCE is called to update the copy with INITARGS.")
|
---|
217 | (:method ((object standard-object) &rest initargs &key &allow-other-keys)
|
---|
218 | (let* ((class (class-of object))
|
---|
219 | (copy (allocate-instance class)))
|
---|
220 | (dolist (slot-name (mapcar #'slot-definition-name (class-slots class)))
|
---|
221 | (when (slot-boundp object slot-name)
|
---|
222 | (setf (slot-value copy slot-name)
|
---|
223 | (slot-value object slot-name))))
|
---|
224 | (apply #'reinitialize-instance copy initargs))))
|
---|
225 |
|
---|
226 | #|
|
---|
227 | ;; A stripped-down version of shallow copy
|
---|
228 | ;; Source: http://stackoverflow.com/questions/11067899/is-there-a-generic-method-for-cloning-clos-objects
|
---|
229 | (defun shallow-copy-object (original)
|
---|
230 | (let* ((class (class-of original))
|
---|
231 | (copy (allocate-instance class)))
|
---|
232 | (dolist (slot (mapcar #'slot-definition-name (class-slots class)))
|
---|
233 | (when (slot-boundp original slot)
|
---|
234 | (setf (slot-value copy slot)
|
---|
235 | (slot-value original slot))))
|
---|
236 | copy))
|
---|
237 | |#
|
---|