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

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

* empty log message *

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