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@ 3474

Last change on this file since 3474 was 3474, checked in by Marek Rychlik, 9 years ago

* empty log message *

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