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 :copy)
|
---|
24 | (:export "MONOM"
|
---|
25 | "TERM"
|
---|
26 | "EXPONENT"
|
---|
27 | "MONOM-DIMENSION"
|
---|
28 | "MONOM-EXPONENTS"
|
---|
29 | "UNIVERSAL-EQUALP"
|
---|
30 | "MONOM-ELT"
|
---|
31 | "TOTAL-DEGREE"
|
---|
32 | "SUGAR"
|
---|
33 | "MULTIPLY-BY"
|
---|
34 | "DIVIDE-BY"
|
---|
35 | "DIVIDE"
|
---|
36 | "MULTIPLY-2"
|
---|
37 | "MULTIPLY"
|
---|
38 | "DIVIDES-P"
|
---|
39 | "DIVIDES-LCM-P"
|
---|
40 | "LCM-DIVIDES-LCM-P"
|
---|
41 | "LCM-EQUAL-LCM-P"
|
---|
42 | "DIVISIBLE-BY-P"
|
---|
43 | "REL-PRIME-P"
|
---|
44 | "UNIVERSAL-LCM"
|
---|
45 | "UNIVERSAL-GCD"
|
---|
46 | "DEPENDS-P"
|
---|
47 | "LEFT-TENSOR-PRODUCT-BY"
|
---|
48 | "RIGHT-TENSOR-PRODUCT-BY"
|
---|
49 | "LEFT-CONTRACT"
|
---|
50 | "MAKE-MONOM-VARIABLE"
|
---|
51 | "->LIST"
|
---|
52 | "LEX>"
|
---|
53 | "GRLEX>"
|
---|
54 | "REVLEX>"
|
---|
55 | "GREVLEX>"
|
---|
56 | "INVLEX>"
|
---|
57 | "REVERSE-MONOMIAL-ORDER"
|
---|
58 | "MAKE-ELIMINATION-ORDER-FACTORY"
|
---|
59 | "TERM-COEFF"
|
---|
60 | "UNARY-MINUS"
|
---|
61 | "UNIVERSAL-ZEROP")
|
---|
62 | (:documentation
|
---|
63 | "This package implements basic operations on monomials, including
|
---|
64 | various monomial orders.
|
---|
65 |
|
---|
66 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
67 |
|
---|
68 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
69 |
|
---|
70 | However, lists may be implemented as other sequence types, so the
|
---|
71 | flexibility to change the representation should be maintained in the
|
---|
72 | code to use general operations on sequences whenever possible. The
|
---|
73 | optimization for the actual representation should be left to
|
---|
74 | declarations and the compiler.
|
---|
75 |
|
---|
76 | EXAMPLES: Suppose that variables are x and y. Then
|
---|
77 |
|
---|
78 | Monom x*y^2 ---> (1 2) "))
|
---|
79 |
|
---|
80 | (in-package :monom)
|
---|
81 |
|
---|
82 | (proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 0)))
|
---|
83 |
|
---|
84 | (deftype exponent ()
|
---|
85 | "Type of exponent in a monomial."
|
---|
86 | 'fixnum)
|
---|
87 |
|
---|
88 | (defclass monom ()
|
---|
89 | ((exponents :initarg :exponents :accessor monom-exponents
|
---|
90 | :documentation "The powers of the variables."))
|
---|
91 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
92 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
93 | (:documentation
|
---|
94 | "Implements a monomial, i.e. a product of powers
|
---|
95 | of variables, like X*Y^2."))
|
---|
96 |
|
---|
97 | (defmethod print-object ((self monom) stream)
|
---|
98 | (print-unreadable-object (self stream :type t :identity t)
|
---|
99 | (with-accessors ((exponents monom-exponents))
|
---|
100 | self
|
---|
101 | (format stream "EXPONENTS=~A"
|
---|
102 | exponents))))
|
---|
103 |
|
---|
104 | (defmethod initialize-instance :after ((self monom)
|
---|
105 | &key
|
---|
106 | (dimension 0 dimension-supplied-p)
|
---|
107 | (exponents nil exponents-supplied-p)
|
---|
108 | (exponent 0)
|
---|
109 | &allow-other-keys
|
---|
110 | )
|
---|
111 | "The following INITIALIZE-INSTANCE method allows instance initialization
|
---|
112 | of a MONOM in a style similar to MAKE-ARRAY, e.g.:
|
---|
113 |
|
---|
114 | (MAKE-INSTANCE 'MONOM :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
115 | (MAKE-INSTANCE 'MONOM :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
116 | (MAKE-INSTANCE 'MONOM :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
|
---|
117 |
|
---|
118 | If both DIMENSION and EXPONENTS are supplied, they must be compatible,
|
---|
119 | i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
|
---|
120 | is not supplied, a monom with repeated value EXPONENT is created.
|
---|
121 | By default EXPONENT is 0, which results in a constant monomial.
|
---|
122 | "
|
---|
123 | (cond
|
---|
124 | (exponents-supplied-p
|
---|
125 | (when (and dimension-supplied-p
|
---|
126 | (/= dimension (length exponents)))
|
---|
127 | (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
|
---|
128 | exponents dimension))
|
---|
129 | (let ((dim (length exponents)))
|
---|
130 | (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
131 | (dimension-supplied-p
|
---|
132 | ;; when all exponents are to be identical
|
---|
133 | (setf (slot-value self 'exponents) (make-array (list dimension)
|
---|
134 | :initial-element exponent
|
---|
135 | :element-type 'exponent)))
|
---|
136 | (t
|
---|
137 | (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
|
---|
138 |
|
---|
139 | (defgeneric monom-dimension (self)
|
---|
140 | (:method ((self monom))
|
---|
141 | (length (monom-exponents self))))
|
---|
142 |
|
---|
143 | (defgeneric universal-equalp (object1 object2)
|
---|
144 | (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
|
---|
145 | (:method ((object1 cons) (object2 cons)) (every #'universal-equalp object1 object2))
|
---|
146 | (:method ((object1 number) (object2 number)) (= object1 object2))
|
---|
147 | (:method ((m1 monom) (m2 monom))
|
---|
148 | "Returns T iff monomials M1 and M2 have identical EXPONENTS."
|
---|
149 | (equalp (monom-exponents m1) (monom-exponents m2))))
|
---|
150 |
|
---|
151 | (defgeneric monom-elt (m index)
|
---|
152 | (:documentation "Return the power in the monomial M of variable number INDEX.")
|
---|
153 | (:method ((m monom) index)
|
---|
154 | "Return the power in the monomial M of variable number INDEX."
|
---|
155 | (with-slots (exponents)
|
---|
156 | m
|
---|
157 | (elt exponents index))))
|
---|
158 |
|
---|
159 | (defgeneric (setf monom-elt) (new-value m index)
|
---|
160 | (:documentation "Set the power in the monomial M of variable number INDEX.")
|
---|
161 | (:method (new-value (m monom) index)
|
---|
162 | (with-slots (exponents)
|
---|
163 | m
|
---|
164 | (setf (elt exponents index) new-value))))
|
---|
165 |
|
---|
166 | (defgeneric total-degree (m &optional start end)
|
---|
167 | (:documentation "Return the total degree of a monomoal M. Optinally, a range
|
---|
168 | of variables may be specified with arguments START and END.")
|
---|
169 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
170 | (declare (type fixnum start end))
|
---|
171 | (with-slots (exponents)
|
---|
172 | m
|
---|
173 | (reduce #'+ exponents :start start :end end))))
|
---|
174 |
|
---|
175 | (defgeneric sugar (m &optional start end)
|
---|
176 | (:documentation "Return the sugar of a monomial M. Optinally, a range
|
---|
177 | of variables may be specified with arguments START and END.")
|
---|
178 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
179 | (declare (type fixnum start end))
|
---|
180 | (total-degree m start end)))
|
---|
181 |
|
---|
182 | (defgeneric multiply-by (self other)
|
---|
183 | (:documentation "Multiply SELF by OTHER, return SELF.")
|
---|
184 | (:method ((self number) (other number)) (* self other))
|
---|
185 | (:method ((self monom) (other monom))
|
---|
186 | (with-slots ((exponents1 exponents))
|
---|
187 | self
|
---|
188 | (with-slots ((exponents2 exponents))
|
---|
189 | other
|
---|
190 | (unless (= (length exponents1) (length exponents2))
|
---|
191 | (error "Incompatible dimensions"))
|
---|
192 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
193 | self)
|
---|
194 | (:method ((self number) (other term))
|
---|
195 | (reinitialize-instance other :coeff (multiply self (term-coeff other)))))
|
---|
196 |
|
---|
197 | (defgeneric divide-by (self other)
|
---|
198 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
199 | (:method ((self number) (other number)) (/ self other))
|
---|
200 | (:method ((self monom) (other monom))
|
---|
201 | (with-slots ((exponents1 exponents))
|
---|
202 | self
|
---|
203 | (with-slots ((exponents2 exponents))
|
---|
204 | other
|
---|
205 | (unless (= (length exponents1) (length exponents2))
|
---|
206 | (error "divide-by: Incompatible dimensions."))
|
---|
207 | (unless (every #'>= exponents1 exponents2)
|
---|
208 | (error "divide-by: Negative power would result."))
|
---|
209 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
210 | self)
|
---|
211 | (:method ((self term) (other number))
|
---|
212 | (reinitialize-instance self :coeff (divide (term-coeff self) other))))
|
---|
213 |
|
---|
214 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
215 | "An :AROUND method of COPY-INSTANCE. It replaces
|
---|
216 | exponents with a fresh copy of the sequence."
|
---|
217 | (declare (ignore object initargs))
|
---|
218 | (let ((copy (call-next-method)))
|
---|
219 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
220 | copy))
|
---|
221 |
|
---|
222 | (defun multiply-2 (object1 object2)
|
---|
223 | "Multiply OBJECT1 by OBJECT2"
|
---|
224 | (multiply-by (copy-instance object1) (copy-instance object2)))
|
---|
225 |
|
---|
226 | (defun multiply (&rest factors)
|
---|
227 | "Non-destructively multiply list FACTORS."
|
---|
228 | (cond ((endp factors) 1)
|
---|
229 | ((endp (rest factors)) (first factors))
|
---|
230 | (t (reduce #'multiply-2 factors :initial-value 1))))
|
---|
231 |
|
---|
232 | (defun divide (numerator &rest denominators)
|
---|
233 | "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
|
---|
234 | (divide-by (copy-instance numerator) (apply #'multiply denominators)))
|
---|
235 |
|
---|
236 | (defgeneric divides-p (object1 object2)
|
---|
237 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
238 | (:method ((m1 monom) (m2 monom))
|
---|
239 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
240 | (with-slots ((exponents1 exponents))
|
---|
241 | m1
|
---|
242 | (with-slots ((exponents2 exponents))
|
---|
243 | m2
|
---|
244 | (every #'<= exponents1 exponents2)))))
|
---|
245 |
|
---|
246 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
247 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
248 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
249 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
250 | (with-slots ((exponents1 exponents))
|
---|
251 | m1
|
---|
252 | (with-slots ((exponents2 exponents))
|
---|
253 | m2
|
---|
254 | (with-slots ((exponents3 exponents))
|
---|
255 | m3
|
---|
256 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
257 | exponents1 exponents2 exponents3))))))
|
---|
258 |
|
---|
259 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
260 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
261 | "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
|
---|
262 | (with-slots ((exponents1 exponents))
|
---|
263 | m1
|
---|
264 | (with-slots ((exponents2 exponents))
|
---|
265 | m2
|
---|
266 | (with-slots ((exponents3 exponents))
|
---|
267 | m3
|
---|
268 | (with-slots ((exponents4 exponents))
|
---|
269 | m4
|
---|
270 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
271 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
272 |
|
---|
273 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
274 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
275 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
276 | (with-slots ((exponents1 exponents))
|
---|
277 | m1
|
---|
278 | (with-slots ((exponents2 exponents))
|
---|
279 | m2
|
---|
280 | (with-slots ((exponents3 exponents))
|
---|
281 | m3
|
---|
282 | (with-slots ((exponents4 exponents))
|
---|
283 | m4
|
---|
284 | (every
|
---|
285 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
286 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
287 |
|
---|
288 | (defgeneric divisible-by-p (object1 object2)
|
---|
289 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
290 | (:method ((m1 monom) (m2 monom))
|
---|
291 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
292 | (with-slots ((exponents1 exponents))
|
---|
293 | m1
|
---|
294 | (with-slots ((exponents2 exponents))
|
---|
295 | m2
|
---|
296 | (every #'>= exponents1 exponents2)))))
|
---|
297 |
|
---|
298 | (defgeneric rel-prime-p (object1 object2)
|
---|
299 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
300 | (:method ((m1 monom) (m2 monom))
|
---|
301 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
302 | (with-slots ((exponents1 exponents))
|
---|
303 | m1
|
---|
304 | (with-slots ((exponents2 exponents))
|
---|
305 | m2
|
---|
306 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
307 |
|
---|
308 | (defgeneric universal-lcm (object1 object2)
|
---|
309 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
310 | (:method ((m1 monom) (m2 monom))
|
---|
311 | "Returns least common multiple of monomials M1 and M2."
|
---|
312 | (with-slots ((exponents1 exponents))
|
---|
313 | m1
|
---|
314 | (with-slots ((exponents2 exponents))
|
---|
315 | m2
|
---|
316 | (let* ((exponents (copy-seq exponents1)))
|
---|
317 | (map-into exponents #'max exponents1 exponents2)
|
---|
318 | (make-instance 'monom :exponents exponents))))))
|
---|
319 |
|
---|
320 |
|
---|
321 | (defgeneric universal-gcd (object1 object2)
|
---|
322 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
323 | (:method ((object1 number) (object2 number)) (gcd object1 object2))
|
---|
324 | (:method ((m1 monom) (m2 monom))
|
---|
325 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
326 | (with-slots ((exponents1 exponents))
|
---|
327 | m1
|
---|
328 | (with-slots ((exponents2 exponents))
|
---|
329 | m2
|
---|
330 | (let* ((exponents (copy-seq exponents1)))
|
---|
331 | (map-into exponents #'min exponents1 exponents2)
|
---|
332 | (make-instance 'monom :exponents exponents))))))
|
---|
333 |
|
---|
334 | (defgeneric depends-p (object k)
|
---|
335 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
336 | (:method ((m monom) k)
|
---|
337 | "Return T if the monomial M depends on variable number K."
|
---|
338 | (declare (type fixnum k))
|
---|
339 | (with-slots (exponents)
|
---|
340 | m
|
---|
341 | (plusp (elt exponents k)))))
|
---|
342 |
|
---|
343 | (defgeneric left-tensor-product-by (self other)
|
---|
344 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
345 | SELF. Return SELF.")
|
---|
346 | (:method ((self monom) (other monom))
|
---|
347 | (with-slots ((exponents1 exponents))
|
---|
348 | self
|
---|
349 | (with-slots ((exponents2 exponents))
|
---|
350 | other
|
---|
351 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
352 | self))
|
---|
353 |
|
---|
354 | (defgeneric right-tensor-product-by (self other)
|
---|
355 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
356 | into SELF. Returns SELF.")
|
---|
357 | (:method ((self monom) (other monom))
|
---|
358 | (with-slots ((exponents1 exponents))
|
---|
359 | self
|
---|
360 | (with-slots ((exponents2 exponents))
|
---|
361 | other
|
---|
362 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
363 | self))
|
---|
364 |
|
---|
365 | (defgeneric left-contract (self k)
|
---|
366 | (:documentation "Drop the first K variables in object SELF.")
|
---|
367 | (:method ((self monom) k)
|
---|
368 | "Drop the first K variables in monomial M."
|
---|
369 | (declare (fixnum k))
|
---|
370 | (with-slots (exponents)
|
---|
371 | self
|
---|
372 | (setf exponents (subseq exponents k)))
|
---|
373 | self))
|
---|
374 |
|
---|
375 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
376 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
377 | "Construct a monomial in the polynomial ring
|
---|
378 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
379 | which represents a single variable. It assumes number of variables
|
---|
380 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
381 | may appear raised to power POWER. "
|
---|
382 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
383 | (with-slots (exponents)
|
---|
384 | m
|
---|
385 | (setf (elt exponents pos) power)
|
---|
386 | m))
|
---|
387 |
|
---|
388 | (defgeneric ->list (object)
|
---|
389 | (:method ((m monom))
|
---|
390 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
391 | (coerce (monom-exponents m) 'list)))
|
---|
392 |
|
---|
393 | ;; pure lexicographic
|
---|
394 | (defgeneric lex> (p q &optional start end)
|
---|
395 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
396 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
397 | otherwise it is NIL.")
|
---|
398 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
399 | (declare (type fixnum start end))
|
---|
400 | (do ((i start (1+ i)))
|
---|
401 | ((>= i end) (values nil t))
|
---|
402 | (cond
|
---|
403 | ((> (monom-elt p i) (monom-elt q i))
|
---|
404 | (return-from lex> (values t nil)))
|
---|
405 | ((< (monom-elt p i) (monom-elt q i))
|
---|
406 | (return-from lex> (values nil nil)))))))
|
---|
407 |
|
---|
408 | ;; total degree order, ties broken by lexicographic
|
---|
409 | (defgeneric grlex> (p q &optional start end)
|
---|
410 | (:documentation "Return T if P>Q with respect to graded
|
---|
411 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
412 | P=Q, otherwise it is NIL.")
|
---|
413 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
414 | (declare (type monom p q) (type fixnum start end))
|
---|
415 | (let ((d1 (total-degree p start end))
|
---|
416 | (d2 (total-degree q start end)))
|
---|
417 | (declare (type fixnum d1 d2))
|
---|
418 | (cond
|
---|
419 | ((> d1 d2) (values t nil))
|
---|
420 | ((< d1 d2) (values nil nil))
|
---|
421 | (t
|
---|
422 | (lex> p q start end))))))
|
---|
423 |
|
---|
424 | ;; reverse lexicographic
|
---|
425 | (defgeneric revlex> (p q &optional start end)
|
---|
426 | (:documentation "Return T if P>Q with respect to reverse
|
---|
427 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
428 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
429 | because some sets do not have a minimal element. This order is useful
|
---|
430 | in constructing other orders.")
|
---|
431 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
432 | (declare (type fixnum start end))
|
---|
433 | (do ((i (1- end) (1- i)))
|
---|
434 | ((< i start) (values nil t))
|
---|
435 | (declare (type fixnum i))
|
---|
436 | (cond
|
---|
437 | ((< (monom-elt p i) (monom-elt q i))
|
---|
438 | (return-from revlex> (values t nil)))
|
---|
439 | ((> (monom-elt p i) (monom-elt q i))
|
---|
440 | (return-from revlex> (values nil nil)))))))
|
---|
441 |
|
---|
442 |
|
---|
443 | ;; total degree, ties broken by reverse lexicographic
|
---|
444 | (defgeneric grevlex> (p q &optional start end)
|
---|
445 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
446 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
447 | P=Q, otherwise it is NIL.")
|
---|
448 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
449 | (declare (type fixnum start end))
|
---|
450 | (let ((d1 (total-degree p start end))
|
---|
451 | (d2 (total-degree q start end)))
|
---|
452 | (declare (type fixnum d1 d2))
|
---|
453 | (cond
|
---|
454 | ((> d1 d2) (values t nil))
|
---|
455 | ((< d1 d2) (values nil nil))
|
---|
456 | (t
|
---|
457 | (revlex> p q start end))))))
|
---|
458 |
|
---|
459 | (defgeneric invlex> (p q &optional start end)
|
---|
460 | (:documentation "Return T if P>Q with respect to inverse
|
---|
461 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
462 | P=Q, otherwise it is NIL.")
|
---|
463 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
464 | (declare (type fixnum start end))
|
---|
465 | (do ((i (1- end) (1- i)))
|
---|
466 | ((< i start) (values nil t))
|
---|
467 | (declare (type fixnum i))
|
---|
468 | (cond
|
---|
469 | ((> (monom-elt p i) (monom-elt q i))
|
---|
470 | (return-from invlex> (values t nil)))
|
---|
471 | ((< (monom-elt p i) (monom-elt q i))
|
---|
472 | (return-from invlex> (values nil nil)))))))
|
---|
473 |
|
---|
474 | (defun reverse-monomial-order (order)
|
---|
475 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
476 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
477 | (declare (type monom p q) (type fixnum start end))
|
---|
478 | (funcall order q p start end)))
|
---|
479 |
|
---|
480 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
481 | ;;
|
---|
482 | ;; Order making functions
|
---|
483 | ;;
|
---|
484 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
485 |
|
---|
486 | ;; This returns a closure with the same signature
|
---|
487 | ;; as all orders such as #'LEX>.
|
---|
488 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
489 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
490 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
491 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
492 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
493 | (declare (type monom p q) (type fixnum start end))
|
---|
494 | (cond
|
---|
495 | ((> (monom-elt p start) (monom-elt q start))
|
---|
496 | (values t nil))
|
---|
497 | ((< (monom-elt p start) (monom-elt q start))
|
---|
498 | (values nil nil))
|
---|
499 | (t
|
---|
500 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
501 |
|
---|
502 | ;; This returns a closure which is called with an integer argument.
|
---|
503 | ;; The result is *another closure* with the same signature as all
|
---|
504 | ;; orders such as #'LEX>.
|
---|
505 | (defun make-elimination-order-factory (&optional
|
---|
506 | (primary-elimination-order #'lex>)
|
---|
507 | (secondary-elimination-order #'lex>))
|
---|
508 | "Return a function with a single integer argument K. This should be
|
---|
509 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
510 | remaining variables. The call to the closure creates a predicate
|
---|
511 | which compares monomials according to the K-th elimination order. The
|
---|
512 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
513 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
514 | remaining variables, respectively, with ties broken by lexicographical
|
---|
515 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
516 | which indicates that the first K variables appear with identical
|
---|
517 | powers, then the result is that of a call to
|
---|
518 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
519 | X[K],X[K+1],..."
|
---|
520 | #'(lambda (k)
|
---|
521 | (cond
|
---|
522 | ((<= k 0)
|
---|
523 | (error "K must be at least 1"))
|
---|
524 | ((= k 1)
|
---|
525 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
526 | (t
|
---|
527 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
528 | (declare (type monom p q) (type fixnum start end))
|
---|
529 | (multiple-value-bind (primary equal)
|
---|
530 | (funcall primary-elimination-order p q start k)
|
---|
531 | (if equal
|
---|
532 | (funcall secondary-elimination-order p q k end)
|
---|
533 | (values primary nil))))))))
|
---|
534 |
|
---|
535 | (defclass term (monom)
|
---|
536 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
537 | (:default-initargs :coeff nil)
|
---|
538 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
539 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
540 |
|
---|
541 | (defmethod update-instance-for-different-class :before ((old monom) (new term) &key (coeff 1))
|
---|
542 | "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
543 | (reinitialize-instance new :coeff coeff))
|
---|
544 |
|
---|
545 | (defmethod print-object ((self term) stream)
|
---|
546 | (print-unreadable-object (self stream :type t :identity t)
|
---|
547 | (with-accessors ((exponents monom-exponents)
|
---|
548 | (coeff term-coeff))
|
---|
549 | self
|
---|
550 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
551 | exponents coeff))))
|
---|
552 |
|
---|
553 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
554 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
555 | are UNIVERSAL-EQUALP."
|
---|
556 | (and (call-next-method)
|
---|
557 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
558 |
|
---|
559 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
560 | (setf (term-coeff new) 1))
|
---|
561 |
|
---|
562 | (defmethod multiply-by :before ((self term) (other term))
|
---|
563 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
564 | It returns SELF."
|
---|
565 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
566 |
|
---|
567 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
568 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
569 |
|
---|
570 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
571 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
572 |
|
---|
573 | (defmethod divide-by :before ((self term) (other term))
|
---|
574 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
575 |
|
---|
576 | (defgeneric unary-minus (self)
|
---|
577 | (:documentation "Negate object SELF and return it.")
|
---|
578 | (:method ((self number)) (- self))
|
---|
579 | (:method ((self term))
|
---|
580 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
581 | self))
|
---|
582 |
|
---|
583 | (defgeneric universal-zerop (self)
|
---|
584 | (:documentation "Return T iff SELF is zero.")
|
---|
585 | (:method ((self number)) (zerop self))
|
---|
586 | (:method ((self term))
|
---|
587 | (universal-zerop (term-coeff self))))
|
---|