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

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

* empty log message *

File size: 20.5 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 "UNIVERSAL-EQUALP"
29 "MONOM-ELT"
30 "TOTAL-DEGREE"
31 "SUGAR"
32 "MULTIPLY-BY"
33 "DIVIDE-BY"
34 "MULTIPLY-2"
35 "MULTIPLY"
36 "DIVIDES-P"
37 "DIVIDES-LCM-P"
38 "LCM-DIVIDES-LCM-P"
39 "LCM-EQUAL-LCM-P"
40 "DIVISIBLE-BY-P"
41 "REL-PRIME-P"
42 "UNIVERSAL-LCM"
43 "UNIVERSAL-GCD"
44 "DEPENDS-P"
45 "LEFT-TENSOR-PRODUCT-BY"
46 "RIGHT-TENSOR-PRODUCT-BY"
47 "LEFT-CONTRACT"
48 "MAKE-MONOM-VARIABLE"
49 "MONOM->LIST"
50 "LEX>"
51 "GRLEX>"
52 "REVLEX>"
53 "GREVLEX>"
54 "INVLEX>"
55 "REVERSE-MONOMIAL-ORDER"
56 "MAKE-ELIMINATION-ORDER-FACTORY")
57 (:documentation
58 "This package implements basic operations on monomials, including
59various monomial orders.
60
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 universal-equalp (object1 object2)
139 (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
140 (:method ((m1 monom) (m2 monom))
141 "Returns T iff monomials M1 and M2 have identical EXPONENTS."
142 (equalp (monom-exponents m1) (monom-exponents m2))))
143
144(defgeneric monom-elt (m index)
145 (:documentation "Return the power in the monomial M of variable number INDEX.")
146 (:method ((m monom) index)
147 "Return the power in the monomial M of variable number INDEX."
148 (with-slots (exponents)
149 m
150 (elt exponents index))))
151
152(defgeneric (setf monom-elt) (new-value m index)
153 (:documentation "Set the power in the monomial M of variable number INDEX.")
154 (:method (new-value (m monom) index)
155 (with-slots (exponents)
156 m
157 (setf (elt exponents index) new-value))))
158
159(defgeneric total-degree (m &optional start end)
160 (:documentation "Return the total degree of a monomoal M. Optinally, a range
161of variables may be specified with arguments START and END.")
162 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
163 (declare (type fixnum start end))
164 (with-slots (exponents)
165 m
166 (reduce #'+ exponents :start start :end end))))
167
168(defgeneric sugar (m &optional start end)
169 (:documentation "Return the sugar of a monomial M. Optinally, a range
170of variables may be specified with arguments START and END.")
171 (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
172 (declare (type fixnum start end))
173 (total-degree m start end)))
174
175(defgeneric multiply-by (self other)
176 (:documentation "Multiply SELF by OTHER, return SELF.")
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 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(defun multiply-2 (object1 object2)
210 "Multiply OBJECT1 by OBJECT2"
211 (multiply-by (copy-instance object1) (copy-instance object2)))
212
213(defun multiply (&rest factors)
214 "Non-destructively multiply list FACTORS."
215 (reduce #'multiply-2 factors))
216
217(defun divide (numerator &rest denominators)
218 "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
219 (divide-by (copy-instance numerator) (multiply denominators)))
220
221(defgeneric divides-p (object1 object2)
222 (:documentation "Returns T if OBJECT1 divides OBJECT2.")
223 (:method ((m1 monom) (m2 monom))
224 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
225 (with-slots ((exponents1 exponents))
226 m1
227 (with-slots ((exponents2 exponents))
228 m2
229 (every #'<= exponents1 exponents2)))))
230
231(defgeneric divides-lcm-p (object1 object2 object3)
232 (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
233 (:method ((m1 monom) (m2 monom) (m3 monom))
234 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
235 (with-slots ((exponents1 exponents))
236 m1
237 (with-slots ((exponents2 exponents))
238 m2
239 (with-slots ((exponents3 exponents))
240 m3
241 (every #'(lambda (x y z) (<= x (max y z)))
242 exponents1 exponents2 exponents3))))))
243
244(defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
245 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
246 "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
247 (with-slots ((exponents1 exponents))
248 m1
249 (with-slots ((exponents2 exponents))
250 m2
251 (with-slots ((exponents3 exponents))
252 m3
253 (with-slots ((exponents4 exponents))
254 m4
255 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
256 exponents1 exponents2 exponents3 exponents4)))))))
257
258(defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
259 (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
260 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
261 (with-slots ((exponents1 exponents))
262 m1
263 (with-slots ((exponents2 exponents))
264 m2
265 (with-slots ((exponents3 exponents))
266 m3
267 (with-slots ((exponents4 exponents))
268 m4
269 (every
270 #'(lambda (x y z w) (= (max x y) (max z w)))
271 exponents1 exponents2 exponents3 exponents4)))))))
272
273(defgeneric divisible-by-p (object1 object2)
274 (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
275 (:method ((m1 monom) (m2 monom))
276 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
277 (with-slots ((exponents1 exponents))
278 m1
279 (with-slots ((exponents2 exponents))
280 m2
281 (every #'>= exponents1 exponents2)))))
282
283(defgeneric rel-prime-p (object1 object2)
284 (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
285 (:method ((m1 monom) (m2 monom))
286 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
287 (with-slots ((exponents1 exponents))
288 m1
289 (with-slots ((exponents2 exponents))
290 m2
291 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
292
293(defgeneric universal-lcm (object1 object2)
294 (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
295 (:method ((m1 monom) (m2 monom))
296 "Returns least common multiple of monomials M1 and M2."
297 (with-slots ((exponents1 exponents))
298 m1
299 (with-slots ((exponents2 exponents))
300 m2
301 (let* ((exponents (copy-seq exponents1)))
302 (map-into exponents #'max exponents1 exponents2)
303 (make-instance 'monom :exponents exponents))))))
304
305
306(defgeneric universal-gcd (object1 object2)
307 (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
308 (:method ((m1 monom) (m2 monom))
309 "Returns greatest common divisor of monomials M1 and M2."
310 (with-slots ((exponents1 exponents))
311 m1
312 (with-slots ((exponents2 exponents))
313 m2
314 (let* ((exponents (copy-seq exponents1)))
315 (map-into exponents #'min exponents1 exponents2)
316 (make-instance 'monom :exponents exponents))))))
317
318(defgeneric depends-p (object k)
319 (:documentation "Returns T iff object OBJECT depends on variable K.")
320 (:method ((m monom) k)
321 "Return T if the monomial M depends on variable number K."
322 (declare (type fixnum k))
323 (with-slots (exponents)
324 m
325 (plusp (elt exponents k)))))
326
327(defgeneric left-tensor-product-by (self other)
328 (:documentation "Returns a tensor product SELF by OTHER, stored into
329 SELF. Return SELF.")
330 (:method ((self monom) (other monom))
331 (with-slots ((exponents1 exponents))
332 self
333 (with-slots ((exponents2 exponents))
334 other
335 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
336 self))
337
338(defgeneric right-tensor-product-by (self other)
339 (:documentation "Returns a tensor product of OTHER by SELF, stored
340 into SELF. Returns SELF.")
341 (:method ((self monom) (other monom))
342 (with-slots ((exponents1 exponents))
343 self
344 (with-slots ((exponents2 exponents))
345 other
346 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
347 self))
348
349(defgeneric left-contract (self k)
350 (:documentation "Drop the first K variables in object SELF.")
351 (:method ((self monom) k)
352 "Drop the first K variables in monomial M."
353 (declare (fixnum k))
354 (with-slots (exponents)
355 self
356 (setf exponents (subseq exponents k)))
357 self))
358
359(defun make-monom-variable (nvars pos &optional (power 1)
360 &aux (m (make-instance 'monom :dimension nvars)))
361 "Construct a monomial in the polynomial ring
362RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
363which represents a single variable. It assumes number of variables
364NVARS and the variable is at position POS. Optionally, the variable
365may appear raised to power POWER. "
366 (declare (type fixnum nvars pos power) (type monom m))
367 (with-slots (exponents)
368 m
369 (setf (elt exponents pos) power)
370 m))
371
372(defmethod monom->list ((m monom))
373 "A human-readable representation of a monomial M as a list of exponents."
374 (coerce (monom-exponents m) 'list))
375
376
377;; pure lexicographic
378(defgeneric lex> (p q &optional start end)
379 (:documentation "Return T if P>Q with respect to lexicographic
380order, otherwise NIL. The second returned value is T if P=Q,
381otherwise it is NIL.")
382 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
383 (declare (type fixnum start end))
384 (do ((i start (1+ i)))
385 ((>= i end) (values nil t))
386 (cond
387 ((> (monom-elt p i) (monom-elt q i))
388 (return-from lex> (values t nil)))
389 ((< (monom-elt p i) (monom-elt q i))
390 (return-from lex> (values nil nil)))))))
391
392;; total degree order, ties broken by lexicographic
393(defgeneric grlex> (p q &optional start end)
394 (:documentation "Return T if P>Q with respect to graded
395lexicographic order, otherwise NIL. The second returned value is T if
396P=Q, otherwise it is NIL.")
397 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
398 (declare (type monom p q) (type fixnum start end))
399 (let ((d1 (total-degree p start end))
400 (d2 (total-degree q start end)))
401 (declare (type fixnum d1 d2))
402 (cond
403 ((> d1 d2) (values t nil))
404 ((< d1 d2) (values nil nil))
405 (t
406 (lex> p q start end))))))
407
408;; reverse lexicographic
409(defgeneric revlex> (p q &optional start end)
410 (:documentation "Return T if P>Q with respect to reverse
411lexicographic order, NIL otherwise. The second returned value is T if
412P=Q, otherwise it is NIL. This is not and admissible monomial order
413because some sets do not have a minimal element. This order is useful
414in constructing other orders.")
415 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
416 (declare (type fixnum start end))
417 (do ((i (1- end) (1- i)))
418 ((< i start) (values nil t))
419 (declare (type fixnum i))
420 (cond
421 ((< (monom-elt p i) (monom-elt q i))
422 (return-from revlex> (values t nil)))
423 ((> (monom-elt p i) (monom-elt q i))
424 (return-from revlex> (values nil nil)))))))
425
426
427;; total degree, ties broken by reverse lexicographic
428(defgeneric grevlex> (p q &optional start end)
429 (:documentation "Return T if P>Q with respect to graded reverse
430lexicographic order, NIL otherwise. The second returned value is T if
431P=Q, otherwise it is NIL.")
432 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
433 (declare (type fixnum start end))
434 (let ((d1 (total-degree p start end))
435 (d2 (total-degree q start end)))
436 (declare (type fixnum d1 d2))
437 (cond
438 ((> d1 d2) (values t nil))
439 ((< d1 d2) (values nil nil))
440 (t
441 (revlex> p q start end))))))
442
443(defgeneric invlex> (p q &optional start end)
444 (:documentation "Return T if P>Q with respect to inverse
445lexicographic order, NIL otherwise The second returned value is T if
446P=Q, otherwise it is NIL.")
447 (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
448 (declare (type fixnum start end))
449 (do ((i (1- end) (1- i)))
450 ((< i start) (values nil t))
451 (declare (type fixnum i))
452 (cond
453 ((> (monom-elt p i) (monom-elt q i))
454 (return-from invlex> (values t nil)))
455 ((< (monom-elt p i) (monom-elt q i))
456 (return-from invlex> (values nil nil)))))))
457
458(defun reverse-monomial-order (order)
459 "Create the inverse monomial order to the given monomial order ORDER."
460 #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
461 (declare (type monom p q) (type fixnum start end))
462 (funcall order q p start end)))
463
464;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
465;;
466;; Order making functions
467;;
468;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469
470;; This returns a closure with the same signature
471;; as all orders such as #'LEX>.
472(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
473 "It constructs an elimination order used for the 1-st elimination ideal,
474i.e. for eliminating the first variable. Thus, the order compares the degrees of the
475first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
476 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
477 (declare (type monom p q) (type fixnum start end))
478 (cond
479 ((> (monom-elt p start) (monom-elt q start))
480 (values t nil))
481 ((< (monom-elt p start) (monom-elt q start))
482 (values nil nil))
483 (t
484 (funcall secondary-elimination-order p q (1+ start) end)))))
485
486;; This returns a closure which is called with an integer argument.
487;; The result is *another closure* with the same signature as all
488;; orders such as #'LEX>.
489(defun make-elimination-order-factory (&optional
490 (primary-elimination-order #'lex>)
491 (secondary-elimination-order #'lex>))
492 "Return a function with a single integer argument K. This should be
493the number of initial K variables X[0],X[1],...,X[K-1], which precede
494remaining variables. The call to the closure creates a predicate
495which compares monomials according to the K-th elimination order. The
496monomial orders PRIMARY-ELIMINATION-ORDER and
497SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
498remaining variables, respectively, with ties broken by lexicographical
499order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
500which indicates that the first K variables appear with identical
501powers, then the result is that of a call to
502SECONDARY-ELIMINATION-ORDER applied to the remaining variables
503X[K],X[K+1],..."
504 #'(lambda (k)
505 (cond
506 ((<= k 0)
507 (error "K must be at least 1"))
508 ((= k 1)
509 (make-elimination-order-factory-1 secondary-elimination-order))
510 (t
511 #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
512 (declare (type monom p q) (type fixnum start end))
513 (multiple-value-bind (primary equal)
514 (funcall primary-elimination-order p q start k)
515 (if equal
516 (funcall secondary-elimination-order p q k end)
517 (values primary nil))))))))
518
519(defclass term (monom)
520 ((coeff :initarg :coeff :accessor term-coeff))
521 (:default-initargs :coeff nil)
522 (:documentation "Implements a term, i.e. a product of a scalar
523and powers of some variables, such as 5*X^2*Y^3."))
524
525(defmethod print-object ((self term) stream)
526 (print-unreadable-object (self stream :type t :identity t)
527 (with-accessors ((exponents monom-exponents)
528 (coeff term-coeff))
529 self
530 (format stream "EXPONENTS=~A COEFF=~A"
531 exponents coeff))))
532
533(defmethod universal-equalp ((term1 term) (term2 term))
534 "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
535are UNIVERSAL-EQUALP."
536 (and (call-next-method)
537 (universal-equalp (term-coeff term1) (term-coeff term2))))
538
539(defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
540 (setf (term-coeff new) 1))
541
542(defmethod multiply-by :before ((self term) (other term))
543 "Destructively multiply terms SELF and OTHER and store the result into SELF.
544It returns SELF."
545 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
546
547(defmethod left-tensor-product-by :before ((self term) (other term))
548 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
549
550(defmethod right-tensor-product-by :before ((self term) (other term))
551 (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
552
553(defmethod divide-by :before ((self term) (other term))
554 (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
555
556(defgeneric unary-minus (self)
557 (:method ((self term))
558 (setf (term-coeff self) (unary-minus (term-coeff self)))
559 self))
560
561(defgeneric universal-zerop (self)
562 (:method ((self term))
563 (universal-zerop (term-coeff self))))
Note: See TracBrowser for help on using the repository browser.