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

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

* empty log message *

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