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

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

* empty log message *

File size: 17.2 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[81]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
[1610]22(defpackage "MONOM"
[3446]23 (:use :cl :copy)
[422]24 (:export "MONOM"
[423]25 "EXPONENT"
[2781]26 "MONOM-DIMENSION"
27 "MONOM-EXPONENTS"
[3442]28 "MONOM-EQUALP"
29 "MONOM-ELT"
30 "MONOM-TOTAL-DEGREE"
[3466]31 "MONOM-SUGAR"
[3442]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"
[3472]50 "MONOM->LIST"
51 "LEX>"
52 "GRLEX>"
53 "REVLEX>"
54 "GREVLEX>"
55 "INVLEX>"
56 "REVERSE-MONOMIAL-ORDER"
57 "MAKE-ELIMINATION-ORDER-FACTORY"))
[3442]58
[2524]59 (:documentation
60 "This package implements basic operations on monomials.
61DATA STRUCTURES: Conceptually, monomials can be represented as lists:
[81]62
[2524]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
[1610]75(in-package :monom)
[48]76
[1925]77(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[1923]78
[48]79(deftype exponent ()
80 "Type of exponent in a monomial."
81 'fixnum)
82
[2022]83(defclass monom ()
[3312]84 ((exponents :initarg :exponents :accessor monom-exponents
[3054]85 :documentation "The powers of the variables."))
[3289]86 ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
87 ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
[2779]88 (:documentation
89 "Implements a monomial, i.e. a product of powers
90of variables, like X*Y^2."))
[880]91
[2245]92(defmethod print-object ((self monom) stream)
[3196]93 (print-unreadable-object (self stream :type t :identity t)
[3313]94 (with-accessors ((exponents monom-exponents))
[3216]95 self
[3313]96 (format stream "EXPONENTS=~A"
97 exponents))))
[2027]98
[3299]99(defmethod initialize-instance :after ((self monom)
[3297]100 &key
101 (dimension 0 dimension-supplied-p)
102 (exponents nil exponents-supplied-p)
[3318]103 (exponent 0)
[3297]104 &allow-other-keys
[2390]105 )
[3329]106 "The following INITIALIZE-INSTANCE method allows instance initialization
107of a MONOM in a style similar to MAKE-ARRAY, e.g.:
[3328]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)>
[3329]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.
[3328]117"
[3315]118 (cond
119 (exponents-supplied-p
[3327]120 (when (and dimension-supplied-p
121 (/= dimension (length exponents)))
122 (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
123 exponents dimension))
[3315]124 (let ((dim (length exponents)))
125 (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
[3321]126 (dimension-supplied-p
[3315]127 ;; when all exponents are to be identical
[3321]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."))))
[3293]133
[3443]134(defgeneric monom-dimension (m)
135 (:method ((m monom))
136 (length (monom-exponents m))))
[3317]137
[3443]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))))
[2547]142
[3443]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))))
[48]150
[3443]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
[3453]156 (setf (elt exponents index) new-value))))
[2023]157
[3450]158(defgeneric monom-total-degree (m &optional start end)
[3449]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))))
[48]166
[3451]167(defgeneric monom-sugar (m &optional start end)
[3446]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)))
[48]173
[3446]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))
[2069]184
[3456]185(defgeneric monom-divide-by (self other)
[3446]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))
[2818]197
[3448]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."
[3446]201 (declare (ignore object initargs))
202 (let ((copy (call-next-method)))
203 (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
[3453]204 copy))
[2950]205
[3442]206(defmethod monom-multiply-2 ((m1 monom) (m2 monom))
[2816]207 "Non-destructively multiply monomial M1 by M2."
[3454]208 (monom-multiply-by (copy-instance m1) (copy-instance m2)))
[2816]209
[3442]210(defmethod monom-multiply ((numerator monom) &rest denominators)
[3416]211 "Non-destructively divide monomial NUMERATOR by product of DENOMINATORS."
[3455]212 (monom-divide-by (copy-instance numerator) (reduce #'monom-multiply-2 denominators)))
[48]213
[3441]214(defmethod monom-divides-p ((m1 monom) (m2 monom))
[48]215 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
[2039]216 (with-slots ((exponents1 exponents))
217 m1
218 (with-slots ((exponents2 exponents))
219 m2
220 (every #'<= exponents1 exponents2))))
[48]221
[2075]222
[3441]223(defmethod monom-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
[2055]224 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[875]225 (every #'(lambda (x y z) (<= x (max y z)))
[869]226 m1 m2 m3))
[48]227
[2049]228
[3441]229(defmethod monom-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
[48]230 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
[1890]231 (declare (type monom m1 m2 m3 m4))
[869]232 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
233 m1 m2 m3 m4))
234
[3441]235(defmethod monom-lcm-equal-lcm-p (m1 m2 m3 m4)
[2075]236 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
[2171]237 (with-slots ((exponents1 exponents))
[2076]238 m1
[2171]239 (with-slots ((exponents2 exponents))
[2076]240 m2
[2171]241 (with-slots ((exponents3 exponents))
[2076]242 m3
[2171]243 (with-slots ((exponents4 exponents))
[2076]244 m4
[2077]245 (every
246 #'(lambda (x y z w) (= (max x y) (max z w)))
247 exponents1 exponents2 exponents3 exponents4))))))
[48]248
[3441]249(defmethod monom-divisible-by-p ((m1 monom) (m2 monom))
[48]250 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
[2171]251 (with-slots ((exponents1 exponents))
[2144]252 m1
[2171]253 (with-slots ((exponents2 exponents))
[2144]254 m2
255 (every #'>= exponents1 exponents2))))
[2078]256
[3441]257(defmethod monom-rel-prime-p ((m1 monom) (m2 monom))
[48]258 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
[2171]259 (with-slots ((exponents1 exponents))
[2078]260 m1
[2171]261 (with-slots ((exponents2 exponents))
[2078]262 m2
[2154]263 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
[48]264
[2076]265
[3441]266(defmethod monom-lcm ((m1 monom) (m2 monom))
[48]267 "Returns least common multiple of monomials M1 and M2."
[3322]268 (with-slots ((exponents1 exponents))
[2082]269 m1
[2171]270 (with-slots ((exponents2 exponents))
[2082]271 m2
[3324]272 (let* ((exponents (copy-seq exponents1)))
[2082]273 (map-into exponents #'max exponents1 exponents2)
[3322]274 (make-instance 'monom :exponents exponents)))))
[48]275
[2080]276
[3441]277(defmethod monom-gcd ((m1 monom) (m2 monom))
[48]278 "Returns greatest common divisor of monomials M1 and M2."
[3322]279 (with-slots ((exponents1 exponents))
[2082]280 m1
[2171]281 (with-slots ((exponents2 exponents))
[2082]282 m2
[3322]283 (let* ((exponents (copy-seq exponents1)))
[2082]284 (map-into exponents #'min exponents1 exponents2)
[3322]285 (make-instance 'monom :exponents exponents)))))
[48]286
[3441]287(defmethod monom-depends-p ((m monom) k)
[48]288 "Return T if the monomial M depends on variable number K."
[2083]289 (declare (type fixnum k))
290 (with-slots (exponents)
291 m
[2154]292 (plusp (elt exponents k))))
[48]293
[3441]294(defmethod monom-left-tensor-product-by ((self monom) (other monom))
[3323]295 (with-slots ((exponents1 exponents))
[3020]296 self
[3323]297 (with-slots ((exponents2 exponents))
[3020]298 other
[3323]299 (setf exponents1 (concatenate 'vector exponents2 exponents1))))
[3036]300 self)
[48]301
[3441]302(defmethod monom-right-tensor-product-by ((self monom) (other monom))
[3323]303 (with-slots ((exponents1 exponents))
[3026]304 self
[3323]305 (with-slots ((exponents2 exponents))
[3026]306 other
[3323]307 (setf exponents1 (concatenate 'vector exponents1 exponents2))))
[3036]308 self)
[3026]309
[3441]310(defmethod monom-left-contract ((self monom) k)
[1638]311 "Drop the first K variables in monomial M."
[2085]312 (declare (fixnum k))
[3323]313 (with-slots (exponents)
[3040]314 self
[3323]315 (setf exponents (subseq exponents k)))
[3039]316 self)
[886]317
318(defun make-monom-variable (nvars pos &optional (power 1)
[2218]319 &aux (m (make-instance 'monom :dimension nvars)))
[886]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. "
[1924]325 (declare (type fixnum nvars pos power) (type monom m))
[2089]326 (with-slots (exponents)
327 m
[2154]328 (setf (elt exponents pos) power)
[2089]329 m))
[1151]330
[3441]331(defmethod monom->list ((m monom))
[1152]332 "A human-readable representation of a monomial M as a list of exponents."
[2779]333 (coerce (monom-exponents m) 'list))
[3472]334
335
336(in-package :order)
337
338(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
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 (r-dimension p)))
346 (declare (type fixnum start end))
347 (do ((i start (1+ i)))
348 ((>= i end) (values nil t))
349 (cond
350 ((> (r-elt p i) (r-elt q i))
351 (return-from lex> (values t nil)))
352 ((< (r-elt p i) (r-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 (r-dimension p)))
361 (declare (type monom p q) (type fixnum start end))
362 (let ((d1 (r-total-degree p start end))
363 (d2 (r-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
372;; reverse lexicographic
373(defgeneric revlex> (p q &optional start end)
374 (:documentation "Return T if P>Q with respect to reverse
375lexicographic order, NIL otherwise. The second returned value is T if
376P=Q, otherwise it is NIL. This is not and admissible monomial order
377because some sets do not have a minimal element. This order is useful
378in constructing other orders.")
379 (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
380 (declare (type fixnum start end))
381 (do ((i (1- end) (1- i)))
382 ((< i start) (values nil t))
383 (declare (type fixnum i))
384 (cond
385 ((< (r-elt p i) (r-elt q i))
386 (return-from revlex> (values t nil)))
387 ((> (r-elt p i) (r-elt q i))
388 (return-from revlex> (values nil nil)))))))
389
390
391;; total degree, ties broken by reverse lexicographic
392(defgeneric grevlex> (p q &optional start end)
393 (:documentation "Return T if P>Q with respect to graded reverse
394lexicographic order, NIL otherwise. The second returned value is T if
395P=Q, otherwise it is NIL.")
396 (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
397 (declare (type fixnum start end))
398 (let ((d1 (r-total-degree p start end))
399 (d2 (r-total-degree q start end)))
400 (declare (type fixnum d1 d2))
401 (cond
402 ((> d1 d2) (values t nil))
403 ((< d1 d2) (values nil nil))
404 (t
405 (revlex> p q start end))))))
406
407(defgeneric invlex> (p q &optional start end)
408 (:documentation "Return T if P>Q with respect to inverse
409lexicographic order, NIL otherwise The second returned value is T if
410P=Q, otherwise it is NIL.")
411 (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
412 (declare (type fixnum start end))
413 (do ((i (1- end) (1- i)))
414 ((< i start) (values nil t))
415 (declare (type fixnum i))
416 (cond
417 ((> (r-elt p i) (r-elt q i))
418 (return-from invlex> (values t nil)))
419 ((< (r-elt p i) (r-elt q i))
420 (return-from invlex> (values nil nil)))))))
421
422(defun reverse-monomial-order (order)
423 "Create the inverse monomial order to the given monomial order ORDER."
424 #'(lambda (p q &optional (start 0) (end (r-dimension q)))
425 (declare (type monom p q) (type fixnum start end))
426 (funcall order q p start end)))
427
428;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
429;;
430;; Order making functions
431;;
432;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
433
434;; This returns a closure with the same signature
435;; as all orders such as #'LEX>.
436(defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
437 "It constructs an elimination order used for the 1-st elimination ideal,
438i.e. for eliminating the first variable. Thus, the order compares the degrees of the
439first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
440 #'(lambda (p q &optional (start 0) (end (r-dimension p)))
441 (declare (type monom p q) (type fixnum start end))
442 (cond
443 ((> (r-elt p start) (r-elt q start))
444 (values t nil))
445 ((< (r-elt p start) (r-elt q start))
446 (values nil nil))
447 (t
448 (funcall secondary-elimination-order p q (1+ start) end)))))
449
450;; This returns a closure which is called with an integer argument.
451;; The result is *another closure* with the same signature as all
452;; orders such as #'LEX>.
453(defun make-elimination-order-factory (&optional
454 (primary-elimination-order #'lex>)
455 (secondary-elimination-order #'lex>))
456 "Return a function with a single integer argument K. This should be
457the number of initial K variables X[0],X[1],...,X[K-1], which precede
458remaining variables. The call to the closure creates a predicate
459which compares monomials according to the K-th elimination order. The
460monomial orders PRIMARY-ELIMINATION-ORDER and
461SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
462remaining variables, respectively, with ties broken by lexicographical
463order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
464which indicates that the first K variables appear with identical
465powers, then the result is that of a call to
466SECONDARY-ELIMINATION-ORDER applied to the remaining variables
467X[K],X[K+1],..."
468 #'(lambda (k)
469 (cond
470 ((<= k 0)
471 (error "K must be at least 1"))
472 ((= k 1)
473 (make-elimination-order-factory-1 secondary-elimination-order))
474 (t
475 #'(lambda (p q &optional (start 0) (end (r-dimension p)))
476 (declare (type monom p q) (type fixnum start end))
477 (multiple-value-bind (primary equal)
478 (funcall primary-elimination-order p q start k)
479 (if equal
480 (funcall secondary-elimination-order p q k end)
481 (values primary nil))))))))
482
Note: See TracBrowser for help on using the repository browser.