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

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