close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/monom.lisp@ 3326

Last change on this file since 3326 was 3326, checked in by Marek Rychlik, 10 years ago

* empty log message *

File size: 10.3 KB
Line 
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 "MONOM"
23 (:use :cl :ring)
24 (:export "MONOM"
25 "EXPONENT"
26 "MONOM-DIMENSION"
27 "MONOM-EXPONENTS"
28 "MAKE-MONOM-VARIABLE")
29 (:documentation
30 "This package implements basic operations on monomials.
31DATA STRUCTURES: Conceptually, monomials can be represented as lists:
32
33 monom: (n1 n2 ... nk) where ni are non-negative integers
34
35However, lists may be implemented as other sequence types, so the
36flexibility to change the representation should be maintained in the
37code to use general operations on sequences whenever possible. The
38optimization for the actual representation should be left to
39declarations and the compiler.
40
41EXAMPLES: Suppose that variables are x and y. Then
42
43 Monom x*y^2 ---> (1 2) "))
44
45(in-package :monom)
46
47(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
48
49(deftype exponent ()
50 "Type of exponent in a monomial."
51 'fixnum)
52
53(defclass monom ()
54 ((exponents :initarg :exponents :accessor monom-exponents
55 :documentation "The powers of the variables."))
56 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
57 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
58 (:documentation
59 "Implements a monomial, i.e. a product of powers
60of variables, like X*Y^2."))
61
62(defmethod print-object ((self monom) stream)
63 (print-unreadable-object (self stream :type t :identity t)
64 (with-accessors ((exponents monom-exponents))
65 self
66 (format stream "EXPONENTS=~A"
67 exponents))))
68
69;; The following INITIALIZE-INSTANCE method allows instance
70;; initialization in a style similar to MAKE-ARRAY, e.g.
71;;
72;; (MAKE-INSTANCE :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
73;; (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
74;; (MAKE-INSTANCE :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
75;;
76(defmethod initialize-instance :after ((self monom)
77 &key
78 (dimension 0 dimension-supplied-p)
79 (exponents nil exponents-supplied-p)
80 (exponent 0)
81 &allow-other-keys
82 )
83 (cond
84 (exponents-supplied-p
85 (when dimension-supplied-p
86 (cond
87 ((/= dimension (length exponents))
88 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
89 exponents dimension))))
90 (let ((dim (length exponents)))
91 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
92 (dimension-supplied-p
93 ;; when all exponents are to be identical
94 (setf (slot-value self 'exponents) (make-array (list dimension)
95 :initial-element exponent
96 :element-type 'exponent)))
97 (t
98 (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
99
100(defmethod monom-dimension ((m monom))
101 (length (monom-exponents m)))
102
103(defmethod r-equalp ((m1 monom) (m2 monom))
104 "Returns T iff monomials M1 and M2 have identical
105EXPONENTS."
106 (equalp (monom-exponents m1) (monom-exponents m2)))
107
108(defmethod r-coeff ((m monom))
109 "A MONOM can be treated as a special case of TERM,
110where the coefficient is 1."
111 1)
112
113(defmethod r-elt ((m monom) index)
114 "Return the power in the monomial M of variable number INDEX."
115 (with-slots (exponents)
116 m
117 (elt exponents index)))
118
119(defmethod (setf r-elt) (new-value (m monom) index)
120 "Return the power in the monomial M of variable number INDEX."
121 (with-slots (exponents)
122 m
123 (setf (elt exponents index) new-value)))
124
125(defmethod r-total-degree ((m monom) &optional (start 0) (end (monom-dimension m)))
126 "Return the todal degree of a monomoal M. Optinally, a range
127of variables may be specified with arguments START and END."
128 (declare (type fixnum start end))
129 (with-slots (exponents)
130 m
131 (reduce #'+ exponents :start start :end end)))
132
133
134(defmethod r-sugar ((m monom) &aux (start 0) (end (monom-dimension m)))
135 "Return the sugar of a monomial M. Optinally, a range
136of variables may be specified with arguments START and END."
137 (declare (type fixnum start end))
138 (r-total-degree m start end))
139
140(defmethod multiply-by ((self monom) (other monom))
141 (with-slots ((exponents1 exponents))
142 self
143 (with-slots ((exponents2 exponents))
144 other
145 (unless (= (length exponents1) (length exponents2))
146 (error "Incompatible dimensions"))
147 (map-into exponents1 #'+ exponents1 exponents2)))
148 self)
149
150(defmethod divide-by ((self monom) (other monom))
151 (with-slots ((exponents1 exponents))
152 self
153 (with-slots ((exponents2 exponents))
154 other
155 (unless (= (length exponents1) (length exponents2))
156 (error "Incompatible dimensions"))
157 (map-into exponents1 #'- exponents1 exponents2)))
158 self)
159
160(defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
161 "An :AROUNT method for COPY-INSTANCE. The primary method is a shallow copy,
162 while for monomials we typically need a fresh copy of the
163 exponents."
164 (declare (ignore object initargs))
165 (let ((copy (call-next-method)))
166 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
167 copy))
168
169(defmethod r* ((m1 monom) (m2 monom))
170 "Non-destructively multiply monomial M1 by M2."
171 (multiply-by (copy-instance m1) (copy-instance m2)))
172
173(defmethod r/ ((m1 monom) (m2 monom))
174 "Non-destructively divide monomial M1 by monomial M2."
175 (divide-by (copy-instance m1) (copy-instance m2)))
176
177(defmethod r-divides-p ((m1 monom) (m2 monom))
178 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
179 (with-slots ((exponents1 exponents))
180 m1
181 (with-slots ((exponents2 exponents))
182 m2
183 (every #'<= exponents1 exponents2))))
184
185
186(defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
187 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
188 (every #'(lambda (x y z) (<= x (max y z)))
189 m1 m2 m3))
190
191
192(defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
193 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
194 (declare (type monom m1 m2 m3 m4))
195 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
196 m1 m2 m3 m4))
197
198(defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
199 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
200 (with-slots ((exponents1 exponents))
201 m1
202 (with-slots ((exponents2 exponents))
203 m2
204 (with-slots ((exponents3 exponents))
205 m3
206 (with-slots ((exponents4 exponents))
207 m4
208 (every
209 #'(lambda (x y z w) (= (max x y) (max z w)))
210 exponents1 exponents2 exponents3 exponents4))))))
211
212(defmethod r-divisible-by-p ((m1 monom) (m2 monom))
213 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
214 (with-slots ((exponents1 exponents))
215 m1
216 (with-slots ((exponents2 exponents))
217 m2
218 (every #'>= exponents1 exponents2))))
219
220(defmethod r-rel-prime-p ((m1 monom) (m2 monom))
221 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
222 (with-slots ((exponents1 exponents))
223 m1
224 (with-slots ((exponents2 exponents))
225 m2
226 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
227
228
229(defmethod r-lcm ((m1 monom) (m2 monom))
230 "Returns least common multiple of monomials M1 and M2."
231 (with-slots ((exponents1 exponents))
232 m1
233 (with-slots ((exponents2 exponents))
234 m2
235 (let* ((exponents (copy-seq exponents1)))
236 (map-into exponents #'max exponents1 exponents2)
237 (make-instance 'monom :exponents exponents)))))
238
239
240(defmethod r-gcd ((m1 monom) (m2 monom))
241 "Returns greatest common divisor of monomials M1 and M2."
242 (with-slots ((exponents1 exponents))
243 m1
244 (with-slots ((exponents2 exponents))
245 m2
246 (let* ((exponents (copy-seq exponents1)))
247 (map-into exponents #'min exponents1 exponents2)
248 (make-instance 'monom :exponents exponents)))))
249
250(defmethod r-depends-p ((m monom) k)
251 "Return T if the monomial M depends on variable number K."
252 (declare (type fixnum k))
253 (with-slots (exponents)
254 m
255 (plusp (elt exponents k))))
256
257(defmethod left-tensor-product-by ((self monom) (other monom))
258 (with-slots ((exponents1 exponents))
259 self
260 (with-slots ((exponents2 exponents))
261 other
262 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
263 self)
264
265(defmethod right-tensor-product-by ((self monom) (other monom))
266 (with-slots ((exponents1 exponents))
267 self
268 (with-slots ((exponents2 exponents))
269 other
270 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
271 self)
272
273(defmethod left-contract ((self monom) k)
274 "Drop the first K variables in monomial M."
275 (declare (fixnum k))
276 (with-slots (exponents)
277 self
278 (setf exponents (subseq exponents k)))
279 self)
280
281(defun make-monom-variable (nvars pos &optional (power 1)
282 &aux (m (make-instance 'monom :dimension nvars)))
283 "Construct a monomial in the polynomial ring
284RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
285which represents a single variable. It assumes number of variables
286NVARS and the variable is at position POS. Optionally, the variable
287may appear raised to power POWER. "
288 (declare (type fixnum nvars pos power) (type monom m))
289 (with-slots (exponents)
290 m
291 (setf (elt exponents pos) power)
292 m))
293
294(defmethod r->list ((m monom))
295 "A human-readable representation of a monomial M as a list of exponents."
296 (coerce (monom-exponents m) 'list))
297
298(defmethod r-dimension ((self monom))
299 (monom-dimension self))
300
301(defmethod r-exponents ((self monom))
302 (monom-exponents self))
Note: See TracBrowser for help on using the repository browser.