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 3) (space 0) (safety 0) (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 :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
115 | (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
116 | (MAKE-INSTANCE :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 (m)
|
---|
140 | (:method ((m monom))
|
---|
141 | (length (monom-exponents m))))
|
---|
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 |
|
---|
195 | (defgeneric divide-by (self other)
|
---|
196 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
197 | (:method ((self number) (other number)) (/ self other))
|
---|
198 | (:method ((self monom) (other monom))
|
---|
199 | (with-slots ((exponents1 exponents))
|
---|
200 | self
|
---|
201 | (with-slots ((exponents2 exponents))
|
---|
202 | other
|
---|
203 | (unless (= (length exponents1) (length exponents2))
|
---|
204 | (error "divide-by: Incompatible dimensions."))
|
---|
205 | (unless (every #'>= exponents1 exponents2)
|
---|
206 | (error "divide-by: Negative power would result."))
|
---|
207 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
208 | self))
|
---|
209 |
|
---|
210 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
211 | "An :AROUND method of COPY-INSTANCE. It replaces
|
---|
212 | exponents with a fresh copy of the sequence."
|
---|
213 | (declare (ignore object initargs))
|
---|
214 | (let ((copy (call-next-method)))
|
---|
215 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
216 | copy))
|
---|
217 |
|
---|
218 | (defun multiply-2 (object1 object2)
|
---|
219 | "Multiply OBJECT1 by OBJECT2"
|
---|
220 | (multiply-by (copy-instance object1) (copy-instance object2)))
|
---|
221 |
|
---|
222 | (defun multiply (&rest factors)
|
---|
223 | "Non-destructively multiply list FACTORS."
|
---|
224 | (reduce #'multiply-2 factors))
|
---|
225 |
|
---|
226 | (defun divide (numerator &rest denominators)
|
---|
227 | "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
|
---|
228 | (divide-by (copy-instance numerator) (apply #'multiply denominators)))
|
---|
229 |
|
---|
230 | (defgeneric divides-p (object1 object2)
|
---|
231 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
232 | (:method ((m1 monom) (m2 monom))
|
---|
233 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
234 | (with-slots ((exponents1 exponents))
|
---|
235 | m1
|
---|
236 | (with-slots ((exponents2 exponents))
|
---|
237 | m2
|
---|
238 | (every #'<= exponents1 exponents2)))))
|
---|
239 |
|
---|
240 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
241 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
242 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
243 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
244 | (with-slots ((exponents1 exponents))
|
---|
245 | m1
|
---|
246 | (with-slots ((exponents2 exponents))
|
---|
247 | m2
|
---|
248 | (with-slots ((exponents3 exponents))
|
---|
249 | m3
|
---|
250 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
251 | exponents1 exponents2 exponents3))))))
|
---|
252 |
|
---|
253 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
254 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
255 | "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
|
---|
256 | (with-slots ((exponents1 exponents))
|
---|
257 | m1
|
---|
258 | (with-slots ((exponents2 exponents))
|
---|
259 | m2
|
---|
260 | (with-slots ((exponents3 exponents))
|
---|
261 | m3
|
---|
262 | (with-slots ((exponents4 exponents))
|
---|
263 | m4
|
---|
264 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
265 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
266 |
|
---|
267 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
268 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
269 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
270 | (with-slots ((exponents1 exponents))
|
---|
271 | m1
|
---|
272 | (with-slots ((exponents2 exponents))
|
---|
273 | m2
|
---|
274 | (with-slots ((exponents3 exponents))
|
---|
275 | m3
|
---|
276 | (with-slots ((exponents4 exponents))
|
---|
277 | m4
|
---|
278 | (every
|
---|
279 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
280 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
281 |
|
---|
282 | (defgeneric divisible-by-p (object1 object2)
|
---|
283 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
284 | (:method ((m1 monom) (m2 monom))
|
---|
285 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
286 | (with-slots ((exponents1 exponents))
|
---|
287 | m1
|
---|
288 | (with-slots ((exponents2 exponents))
|
---|
289 | m2
|
---|
290 | (every #'>= exponents1 exponents2)))))
|
---|
291 |
|
---|
292 | (defgeneric rel-prime-p (object1 object2)
|
---|
293 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
294 | (:method ((m1 monom) (m2 monom))
|
---|
295 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
296 | (with-slots ((exponents1 exponents))
|
---|
297 | m1
|
---|
298 | (with-slots ((exponents2 exponents))
|
---|
299 | m2
|
---|
300 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
301 |
|
---|
302 | (defgeneric universal-lcm (object1 object2)
|
---|
303 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
304 | (:method ((m1 monom) (m2 monom))
|
---|
305 | "Returns least common multiple of monomials M1 and M2."
|
---|
306 | (with-slots ((exponents1 exponents))
|
---|
307 | m1
|
---|
308 | (with-slots ((exponents2 exponents))
|
---|
309 | m2
|
---|
310 | (let* ((exponents (copy-seq exponents1)))
|
---|
311 | (map-into exponents #'max exponents1 exponents2)
|
---|
312 | (make-instance 'monom :exponents exponents))))))
|
---|
313 |
|
---|
314 |
|
---|
315 | (defgeneric universal-gcd (object1 object2)
|
---|
316 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
317 | (:method ((object1 number) (object2 number)) (gcd object1 object2))
|
---|
318 | (:method ((m1 monom) (m2 monom))
|
---|
319 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
320 | (with-slots ((exponents1 exponents))
|
---|
321 | m1
|
---|
322 | (with-slots ((exponents2 exponents))
|
---|
323 | m2
|
---|
324 | (let* ((exponents (copy-seq exponents1)))
|
---|
325 | (map-into exponents #'min exponents1 exponents2)
|
---|
326 | (make-instance 'monom :exponents exponents))))))
|
---|
327 |
|
---|
328 | (defgeneric depends-p (object k)
|
---|
329 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
330 | (:method ((m monom) k)
|
---|
331 | "Return T if the monomial M depends on variable number K."
|
---|
332 | (declare (type fixnum k))
|
---|
333 | (with-slots (exponents)
|
---|
334 | m
|
---|
335 | (plusp (elt exponents k)))))
|
---|
336 |
|
---|
337 | (defgeneric left-tensor-product-by (self other)
|
---|
338 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
339 | SELF. Return SELF.")
|
---|
340 | (:method ((self monom) (other monom))
|
---|
341 | (with-slots ((exponents1 exponents))
|
---|
342 | self
|
---|
343 | (with-slots ((exponents2 exponents))
|
---|
344 | other
|
---|
345 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
346 | self))
|
---|
347 |
|
---|
348 | (defgeneric right-tensor-product-by (self other)
|
---|
349 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
350 | into SELF. Returns SELF.")
|
---|
351 | (:method ((self monom) (other monom))
|
---|
352 | (with-slots ((exponents1 exponents))
|
---|
353 | self
|
---|
354 | (with-slots ((exponents2 exponents))
|
---|
355 | other
|
---|
356 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
357 | self))
|
---|
358 |
|
---|
359 | (defgeneric left-contract (self k)
|
---|
360 | (:documentation "Drop the first K variables in object SELF.")
|
---|
361 | (:method ((self monom) k)
|
---|
362 | "Drop the first K variables in monomial M."
|
---|
363 | (declare (fixnum k))
|
---|
364 | (with-slots (exponents)
|
---|
365 | self
|
---|
366 | (setf exponents (subseq exponents k)))
|
---|
367 | self))
|
---|
368 |
|
---|
369 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
370 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
371 | "Construct a monomial in the polynomial ring
|
---|
372 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
373 | which represents a single variable. It assumes number of variables
|
---|
374 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
375 | may appear raised to power POWER. "
|
---|
376 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
377 | (with-slots (exponents)
|
---|
378 | m
|
---|
379 | (setf (elt exponents pos) power)
|
---|
380 | m))
|
---|
381 |
|
---|
382 | (defgeneric ->list (object)
|
---|
383 | (:method ((m monom))
|
---|
384 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
385 | (coerce (monom-exponents m) 'list)))
|
---|
386 |
|
---|
387 | ;; pure lexicographic
|
---|
388 | (defgeneric lex> (p q &optional start end)
|
---|
389 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
390 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
391 | otherwise it is NIL.")
|
---|
392 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
393 | (declare (type fixnum start end))
|
---|
394 | (do ((i start (1+ i)))
|
---|
395 | ((>= i end) (values nil t))
|
---|
396 | (cond
|
---|
397 | ((> (monom-elt p i) (monom-elt q i))
|
---|
398 | (return-from lex> (values t nil)))
|
---|
399 | ((< (monom-elt p i) (monom-elt q i))
|
---|
400 | (return-from lex> (values nil nil)))))))
|
---|
401 |
|
---|
402 | ;; total degree order, ties broken by lexicographic
|
---|
403 | (defgeneric grlex> (p q &optional start end)
|
---|
404 | (:documentation "Return T if P>Q with respect to graded
|
---|
405 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
406 | P=Q, otherwise it is NIL.")
|
---|
407 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
408 | (declare (type monom p q) (type fixnum start end))
|
---|
409 | (let ((d1 (total-degree p start end))
|
---|
410 | (d2 (total-degree q start end)))
|
---|
411 | (declare (type fixnum d1 d2))
|
---|
412 | (cond
|
---|
413 | ((> d1 d2) (values t nil))
|
---|
414 | ((< d1 d2) (values nil nil))
|
---|
415 | (t
|
---|
416 | (lex> p q start end))))))
|
---|
417 |
|
---|
418 | ;; reverse lexicographic
|
---|
419 | (defgeneric revlex> (p q &optional start end)
|
---|
420 | (:documentation "Return T if P>Q with respect to reverse
|
---|
421 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
422 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
423 | because some sets do not have a minimal element. This order is useful
|
---|
424 | in constructing other orders.")
|
---|
425 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
426 | (declare (type fixnum start end))
|
---|
427 | (do ((i (1- end) (1- i)))
|
---|
428 | ((< i start) (values nil t))
|
---|
429 | (declare (type fixnum i))
|
---|
430 | (cond
|
---|
431 | ((< (monom-elt p i) (monom-elt q i))
|
---|
432 | (return-from revlex> (values t nil)))
|
---|
433 | ((> (monom-elt p i) (monom-elt q i))
|
---|
434 | (return-from revlex> (values nil nil)))))))
|
---|
435 |
|
---|
436 |
|
---|
437 | ;; total degree, ties broken by reverse lexicographic
|
---|
438 | (defgeneric grevlex> (p q &optional start end)
|
---|
439 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
440 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
441 | P=Q, otherwise it is NIL.")
|
---|
442 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
443 | (declare (type fixnum start end))
|
---|
444 | (let ((d1 (total-degree p start end))
|
---|
445 | (d2 (total-degree q start end)))
|
---|
446 | (declare (type fixnum d1 d2))
|
---|
447 | (cond
|
---|
448 | ((> d1 d2) (values t nil))
|
---|
449 | ((< d1 d2) (values nil nil))
|
---|
450 | (t
|
---|
451 | (revlex> p q start end))))))
|
---|
452 |
|
---|
453 | (defgeneric invlex> (p q &optional start end)
|
---|
454 | (:documentation "Return T if P>Q with respect to inverse
|
---|
455 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
456 | P=Q, otherwise it is NIL.")
|
---|
457 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
458 | (declare (type fixnum start end))
|
---|
459 | (do ((i (1- end) (1- i)))
|
---|
460 | ((< i start) (values nil t))
|
---|
461 | (declare (type fixnum i))
|
---|
462 | (cond
|
---|
463 | ((> (monom-elt p i) (monom-elt q i))
|
---|
464 | (return-from invlex> (values t nil)))
|
---|
465 | ((< (monom-elt p i) (monom-elt q i))
|
---|
466 | (return-from invlex> (values nil nil)))))))
|
---|
467 |
|
---|
468 | (defun reverse-monomial-order (order)
|
---|
469 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
470 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
471 | (declare (type monom p q) (type fixnum start end))
|
---|
472 | (funcall order q p start end)))
|
---|
473 |
|
---|
474 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
475 | ;;
|
---|
476 | ;; Order making functions
|
---|
477 | ;;
|
---|
478 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
479 |
|
---|
480 | ;; This returns a closure with the same signature
|
---|
481 | ;; as all orders such as #'LEX>.
|
---|
482 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
483 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
484 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
485 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
486 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
487 | (declare (type monom p q) (type fixnum start end))
|
---|
488 | (cond
|
---|
489 | ((> (monom-elt p start) (monom-elt q start))
|
---|
490 | (values t nil))
|
---|
491 | ((< (monom-elt p start) (monom-elt q start))
|
---|
492 | (values nil nil))
|
---|
493 | (t
|
---|
494 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
495 |
|
---|
496 | ;; This returns a closure which is called with an integer argument.
|
---|
497 | ;; The result is *another closure* with the same signature as all
|
---|
498 | ;; orders such as #'LEX>.
|
---|
499 | (defun make-elimination-order-factory (&optional
|
---|
500 | (primary-elimination-order #'lex>)
|
---|
501 | (secondary-elimination-order #'lex>))
|
---|
502 | "Return a function with a single integer argument K. This should be
|
---|
503 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
504 | remaining variables. The call to the closure creates a predicate
|
---|
505 | which compares monomials according to the K-th elimination order. The
|
---|
506 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
507 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
508 | remaining variables, respectively, with ties broken by lexicographical
|
---|
509 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
510 | which indicates that the first K variables appear with identical
|
---|
511 | powers, then the result is that of a call to
|
---|
512 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
513 | X[K],X[K+1],..."
|
---|
514 | #'(lambda (k)
|
---|
515 | (cond
|
---|
516 | ((<= k 0)
|
---|
517 | (error "K must be at least 1"))
|
---|
518 | ((= k 1)
|
---|
519 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
520 | (t
|
---|
521 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
522 | (declare (type monom p q) (type fixnum start end))
|
---|
523 | (multiple-value-bind (primary equal)
|
---|
524 | (funcall primary-elimination-order p q start k)
|
---|
525 | (if equal
|
---|
526 | (funcall secondary-elimination-order p q k end)
|
---|
527 | (values primary nil))))))))
|
---|
528 |
|
---|
529 | (defclass term (monom)
|
---|
530 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
531 | (:default-initargs :coeff nil)
|
---|
532 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
533 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
534 |
|
---|
535 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
|
---|
536 | "Converts OLD of class MONOM to a NEW of class TERM."
|
---|
537 | (reinitialize-instance new
|
---|
538 | :exponents (monom-exponents old)
|
---|
539 | :coeff coeff))
|
---|
540 |
|
---|
541 |
|
---|
542 | (defmethod print-object ((self term) stream)
|
---|
543 | (print-unreadable-object (self stream :type t :identity t)
|
---|
544 | (with-accessors ((exponents monom-exponents)
|
---|
545 | (coeff term-coeff))
|
---|
546 | self
|
---|
547 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
548 | exponents coeff))))
|
---|
549 |
|
---|
550 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
551 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
552 | are UNIVERSAL-EQUALP."
|
---|
553 | (and (call-next-method)
|
---|
554 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
555 |
|
---|
556 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
557 | (setf (term-coeff new) 1))
|
---|
558 |
|
---|
559 | (defmethod multiply-by :before ((self term) (other term))
|
---|
560 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
561 | It returns SELF."
|
---|
562 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
563 |
|
---|
564 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
565 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
566 |
|
---|
567 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
568 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
569 |
|
---|
570 | (defmethod divide-by :before ((self term) (other term))
|
---|
571 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
572 |
|
---|
573 | (defgeneric unary-minus (self)
|
---|
574 | (:documentation "Negate object SELF and return it.")
|
---|
575 | (:method ((self number)) (- self))
|
---|
576 | (:method ((self term))
|
---|
577 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
578 | self))
|
---|
579 |
|
---|
580 | (defgeneric universal-zerop (self)
|
---|
581 | (:documentation "Return T iff SELF is zero.")
|
---|
582 | (:method ((self number)) (zerop self))
|
---|
583 | (:method ((self term))
|
---|
584 | (universal-zerop (term-coeff self))))
|
---|