[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
|
---|
[3477] | 60 | "This package implements basic operations on monomials, including
|
---|
| 61 | various monomial orders.
|
---|
| 62 |
|
---|
[2524] | 63 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 64 |
|
---|
[2524] | 65 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 66 |
|
---|
| 67 | However, lists may be implemented as other sequence types, so the
|
---|
| 68 | flexibility to change the representation should be maintained in the
|
---|
| 69 | code to use general operations on sequences whenever possible. The
|
---|
| 70 | optimization for the actual representation should be left to
|
---|
| 71 | declarations and the compiler.
|
---|
| 72 |
|
---|
| 73 | EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 74 |
|
---|
| 75 | Monom x*y^2 ---> (1 2) "))
|
---|
| 76 |
|
---|
[1610] | 77 | (in-package :monom)
|
---|
[48] | 78 |
|
---|
[1925] | 79 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[1923] | 80 |
|
---|
[48] | 81 | (deftype exponent ()
|
---|
| 82 | "Type of exponent in a monomial."
|
---|
| 83 | 'fixnum)
|
---|
| 84 |
|
---|
[2022] | 85 | (defclass monom ()
|
---|
[3312] | 86 | ((exponents :initarg :exponents :accessor monom-exponents
|
---|
[3054] | 87 | :documentation "The powers of the variables."))
|
---|
[3289] | 88 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
| 89 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
[2779] | 90 | (:documentation
|
---|
| 91 | "Implements a monomial, i.e. a product of powers
|
---|
| 92 | of variables, like X*Y^2."))
|
---|
[880] | 93 |
|
---|
[2245] | 94 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 95 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3313] | 96 | (with-accessors ((exponents monom-exponents))
|
---|
[3216] | 97 | self
|
---|
[3313] | 98 | (format stream "EXPONENTS=~A"
|
---|
| 99 | exponents))))
|
---|
[2027] | 100 |
|
---|
[3299] | 101 | (defmethod initialize-instance :after ((self monom)
|
---|
[3297] | 102 | &key
|
---|
| 103 | (dimension 0 dimension-supplied-p)
|
---|
| 104 | (exponents nil exponents-supplied-p)
|
---|
[3318] | 105 | (exponent 0)
|
---|
[3297] | 106 | &allow-other-keys
|
---|
[2390] | 107 | )
|
---|
[3329] | 108 | "The following INITIALIZE-INSTANCE method allows instance initialization
|
---|
| 109 | of a MONOM in a style similar to MAKE-ARRAY, e.g.:
|
---|
[3328] | 110 |
|
---|
| 111 | (MAKE-INSTANCE :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
| 112 | (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
| 113 | (MAKE-INSTANCE :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
|
---|
[3329] | 114 |
|
---|
| 115 | If both DIMENSION and EXPONENTS are supplied, they must be compatible,
|
---|
| 116 | i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
|
---|
| 117 | is not supplied, a monom with repeated value EXPONENT is created.
|
---|
| 118 | By default EXPONENT is 0, which results in a constant monomial.
|
---|
[3328] | 119 | "
|
---|
[3315] | 120 | (cond
|
---|
| 121 | (exponents-supplied-p
|
---|
[3327] | 122 | (when (and dimension-supplied-p
|
---|
| 123 | (/= dimension (length exponents)))
|
---|
| 124 | (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
|
---|
| 125 | exponents dimension))
|
---|
[3315] | 126 | (let ((dim (length exponents)))
|
---|
| 127 | (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
[3321] | 128 | (dimension-supplied-p
|
---|
[3315] | 129 | ;; when all exponents are to be identical
|
---|
[3321] | 130 | (setf (slot-value self 'exponents) (make-array (list dimension)
|
---|
| 131 | :initial-element exponent
|
---|
| 132 | :element-type 'exponent)))
|
---|
| 133 | (t
|
---|
| 134 | (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
|
---|
[3293] | 135 |
|
---|
[3443] | 136 | (defgeneric monom-dimension (m)
|
---|
| 137 | (:method ((m monom))
|
---|
| 138 | (length (monom-exponents m))))
|
---|
[3317] | 139 |
|
---|
[3443] | 140 | (defgeneric monom-equalp (m1 m2)
|
---|
| 141 | (:documentation "Returns T iff monomials M1 and M2 have identical EXPONENTS.")
|
---|
| 142 | (:method ((m1 monom) (m2 monom))
|
---|
| 143 | `(equalp (monom-exponents ,m1) (monom-exponents ,m2))))
|
---|
[2547] | 144 |
|
---|
[3443] | 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))))
|
---|
[48] | 152 |
|
---|
[3443] | 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
|
---|
[3453] | 158 | (setf (elt exponents index) new-value))))
|
---|
[2023] | 159 |
|
---|
[3450] | 160 | (defgeneric monom-total-degree (m &optional start end)
|
---|
[3449] | 161 | (:documentation "Return the todal degree of a monomoal M. Optinally, a range
|
---|
| 162 | of 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))))
|
---|
[48] | 168 |
|
---|
[3451] | 169 | (defgeneric monom-sugar (m &optional start end)
|
---|
[3446] | 170 | (:documentation "Return the sugar of a monomial M. Optinally, a range
|
---|
| 171 | of 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 | (monom-total-degree m start end)))
|
---|
[48] | 175 |
|
---|
[3446] | 176 | (defgeneric monom-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))
|
---|
[2069] | 186 |
|
---|
[3456] | 187 | (defgeneric monom-divide-by (self other)
|
---|
[3446] | 188 | (:method ((self monom) (other monom))
|
---|
| 189 | (with-slots ((exponents1 exponents))
|
---|
| 190 | self
|
---|
| 191 | (with-slots ((exponents2 exponents))
|
---|
| 192 | other
|
---|
| 193 | (unless (= (length exponents1) (length exponents2))
|
---|
| 194 | (error "divide-by: Incompatible dimensions."))
|
---|
| 195 | (unless (every #'>= exponents1 exponents2)
|
---|
| 196 | (error "divide-by: Negative power would result."))
|
---|
| 197 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
| 198 | self))
|
---|
[2818] | 199 |
|
---|
[3448] | 200 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
| 201 | "An :AROUND method of COPY-INSTANCE. It replaces
|
---|
| 202 | exponents with a fresh copy of the sequence."
|
---|
[3446] | 203 | (declare (ignore object initargs))
|
---|
| 204 | (let ((copy (call-next-method)))
|
---|
| 205 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
[3453] | 206 | copy))
|
---|
[2950] | 207 |
|
---|
[3442] | 208 | (defmethod monom-multiply-2 ((m1 monom) (m2 monom))
|
---|
[2816] | 209 | "Non-destructively multiply monomial M1 by M2."
|
---|
[3454] | 210 | (monom-multiply-by (copy-instance m1) (copy-instance m2)))
|
---|
[2816] | 211 |
|
---|
[3442] | 212 | (defmethod monom-multiply ((numerator monom) &rest denominators)
|
---|
[3416] | 213 | "Non-destructively divide monomial NUMERATOR by product of DENOMINATORS."
|
---|
[3455] | 214 | (monom-divide-by (copy-instance numerator) (reduce #'monom-multiply-2 denominators)))
|
---|
[48] | 215 |
|
---|
[3441] | 216 | (defmethod monom-divides-p ((m1 monom) (m2 monom))
|
---|
[48] | 217 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
[2039] | 218 | (with-slots ((exponents1 exponents))
|
---|
| 219 | m1
|
---|
| 220 | (with-slots ((exponents2 exponents))
|
---|
| 221 | m2
|
---|
| 222 | (every #'<= exponents1 exponents2))))
|
---|
[48] | 223 |
|
---|
[2075] | 224 |
|
---|
[3441] | 225 | (defmethod monom-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
[2055] | 226 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[875] | 227 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
[869] | 228 | m1 m2 m3))
|
---|
[48] | 229 |
|
---|
[2049] | 230 |
|
---|
[3441] | 231 | (defmethod monom-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
[48] | 232 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
[1890] | 233 | (declare (type monom m1 m2 m3 m4))
|
---|
[869] | 234 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 235 | m1 m2 m3 m4))
|
---|
| 236 |
|
---|
[3441] | 237 | (defmethod monom-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
[2075] | 238 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
[2171] | 239 | (with-slots ((exponents1 exponents))
|
---|
[2076] | 240 | m1
|
---|
[2171] | 241 | (with-slots ((exponents2 exponents))
|
---|
[2076] | 242 | m2
|
---|
[2171] | 243 | (with-slots ((exponents3 exponents))
|
---|
[2076] | 244 | m3
|
---|
[2171] | 245 | (with-slots ((exponents4 exponents))
|
---|
[2076] | 246 | m4
|
---|
[2077] | 247 | (every
|
---|
| 248 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 249 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
[48] | 250 |
|
---|
[3441] | 251 | (defmethod monom-divisible-by-p ((m1 monom) (m2 monom))
|
---|
[48] | 252 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
[2171] | 253 | (with-slots ((exponents1 exponents))
|
---|
[2144] | 254 | m1
|
---|
[2171] | 255 | (with-slots ((exponents2 exponents))
|
---|
[2144] | 256 | m2
|
---|
| 257 | (every #'>= exponents1 exponents2))))
|
---|
[2078] | 258 |
|
---|
[3441] | 259 | (defmethod monom-rel-prime-p ((m1 monom) (m2 monom))
|
---|
[48] | 260 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
[2171] | 261 | (with-slots ((exponents1 exponents))
|
---|
[2078] | 262 | m1
|
---|
[2171] | 263 | (with-slots ((exponents2 exponents))
|
---|
[2078] | 264 | m2
|
---|
[2154] | 265 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
[48] | 266 |
|
---|
[2076] | 267 |
|
---|
[3441] | 268 | (defmethod monom-lcm ((m1 monom) (m2 monom))
|
---|
[48] | 269 | "Returns least common multiple of monomials M1 and M2."
|
---|
[3322] | 270 | (with-slots ((exponents1 exponents))
|
---|
[2082] | 271 | m1
|
---|
[2171] | 272 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 273 | m2
|
---|
[3324] | 274 | (let* ((exponents (copy-seq exponents1)))
|
---|
[2082] | 275 | (map-into exponents #'max exponents1 exponents2)
|
---|
[3322] | 276 | (make-instance 'monom :exponents exponents)))))
|
---|
[48] | 277 |
|
---|
[2080] | 278 |
|
---|
[3441] | 279 | (defmethod monom-gcd ((m1 monom) (m2 monom))
|
---|
[48] | 280 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
[3322] | 281 | (with-slots ((exponents1 exponents))
|
---|
[2082] | 282 | m1
|
---|
[2171] | 283 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 284 | m2
|
---|
[3322] | 285 | (let* ((exponents (copy-seq exponents1)))
|
---|
[2082] | 286 | (map-into exponents #'min exponents1 exponents2)
|
---|
[3322] | 287 | (make-instance 'monom :exponents exponents)))))
|
---|
[48] | 288 |
|
---|
[3441] | 289 | (defmethod monom-depends-p ((m monom) k)
|
---|
[48] | 290 | "Return T if the monomial M depends on variable number K."
|
---|
[2083] | 291 | (declare (type fixnum k))
|
---|
| 292 | (with-slots (exponents)
|
---|
| 293 | m
|
---|
[2154] | 294 | (plusp (elt exponents k))))
|
---|
[48] | 295 |
|
---|
[3441] | 296 | (defmethod monom-left-tensor-product-by ((self monom) (other monom))
|
---|
[3323] | 297 | (with-slots ((exponents1 exponents))
|
---|
[3020] | 298 | self
|
---|
[3323] | 299 | (with-slots ((exponents2 exponents))
|
---|
[3020] | 300 | other
|
---|
[3323] | 301 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
[3036] | 302 | self)
|
---|
[48] | 303 |
|
---|
[3441] | 304 | (defmethod monom-right-tensor-product-by ((self monom) (other monom))
|
---|
[3323] | 305 | (with-slots ((exponents1 exponents))
|
---|
[3026] | 306 | self
|
---|
[3323] | 307 | (with-slots ((exponents2 exponents))
|
---|
[3026] | 308 | other
|
---|
[3323] | 309 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
[3036] | 310 | self)
|
---|
[3026] | 311 |
|
---|
[3441] | 312 | (defmethod monom-left-contract ((self monom) k)
|
---|
[1638] | 313 | "Drop the first K variables in monomial M."
|
---|
[2085] | 314 | (declare (fixnum k))
|
---|
[3323] | 315 | (with-slots (exponents)
|
---|
[3040] | 316 | self
|
---|
[3323] | 317 | (setf exponents (subseq exponents k)))
|
---|
[3039] | 318 | self)
|
---|
[886] | 319 |
|
---|
| 320 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 321 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 322 | "Construct a monomial in the polynomial ring
|
---|
| 323 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 324 | which represents a single variable. It assumes number of variables
|
---|
| 325 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 326 | may appear raised to power POWER. "
|
---|
[1924] | 327 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 328 | (with-slots (exponents)
|
---|
| 329 | m
|
---|
[2154] | 330 | (setf (elt exponents pos) power)
|
---|
[2089] | 331 | m))
|
---|
[1151] | 332 |
|
---|
[3441] | 333 | (defmethod monom->list ((m monom))
|
---|
[1152] | 334 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2779] | 335 | (coerce (monom-exponents m) 'list))
|
---|
[3472] | 336 |
|
---|
| 337 |
|
---|
[3474] | 338 | ;; pure lexicographic
|
---|
[3472] | 339 | (defgeneric lex> (p q &optional start end)
|
---|
| 340 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 341 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 342 | otherwise it is NIL.")
|
---|
| 343 | (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
|
---|
| 344 | (declare (type fixnum start end))
|
---|
| 345 | (do ((i start (1+ i)))
|
---|
| 346 | ((>= i end) (values nil t))
|
---|
| 347 | (cond
|
---|
| 348 | ((> (r-elt p i) (r-elt q i))
|
---|
| 349 | (return-from lex> (values t nil)))
|
---|
| 350 | ((< (r-elt p i) (r-elt q i))
|
---|
| 351 | (return-from lex> (values nil nil)))))))
|
---|
| 352 |
|
---|
[3475] | 353 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 354 | (defgeneric grlex> (p q &optional start end)
|
---|
| 355 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 356 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 357 | P=Q, otherwise it is NIL.")
|
---|
| 358 | (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
|
---|
| 359 | (declare (type monom p q) (type fixnum start end))
|
---|
| 360 | (let ((d1 (r-total-degree p start end))
|
---|
| 361 | (d2 (r-total-degree q start end)))
|
---|
| 362 | (declare (type fixnum d1 d2))
|
---|
| 363 | (cond
|
---|
| 364 | ((> d1 d2) (values t nil))
|
---|
| 365 | ((< d1 d2) (values nil nil))
|
---|
| 366 | (t
|
---|
| 367 | (lex> p q start end))))))
|
---|
| 368 |
|
---|
| 369 | ;; reverse lexicographic
|
---|
| 370 | (defgeneric revlex> (p q &optional start end)
|
---|
| 371 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 372 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 373 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 374 | because some sets do not have a minimal element. This order is useful
|
---|
| 375 | in constructing other orders.")
|
---|
| 376 | (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
|
---|
| 377 | (declare (type fixnum start end))
|
---|
| 378 | (do ((i (1- end) (1- i)))
|
---|
| 379 | ((< i start) (values nil t))
|
---|
| 380 | (declare (type fixnum i))
|
---|
| 381 | (cond
|
---|
| 382 | ((< (r-elt p i) (r-elt q i))
|
---|
| 383 | (return-from revlex> (values t nil)))
|
---|
| 384 | ((> (r-elt p i) (r-elt q i))
|
---|
| 385 | (return-from revlex> (values nil nil)))))))
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 389 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 390 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 391 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 392 | P=Q, otherwise it is NIL.")
|
---|
| 393 | (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
|
---|
| 394 | (declare (type fixnum start end))
|
---|
| 395 | (let ((d1 (r-total-degree p start end))
|
---|
| 396 | (d2 (r-total-degree q start end)))
|
---|
| 397 | (declare (type fixnum d1 d2))
|
---|
| 398 | (cond
|
---|
| 399 | ((> d1 d2) (values t nil))
|
---|
| 400 | ((< d1 d2) (values nil nil))
|
---|
| 401 | (t
|
---|
| 402 | (revlex> p q start end))))))
|
---|
| 403 |
|
---|
| 404 | (defgeneric invlex> (p q &optional start end)
|
---|
| 405 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 406 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 407 | P=Q, otherwise it is NIL.")
|
---|
| 408 | (:method ((p monom) (q monom) &optional (start 0) (end (r-dimension p)))
|
---|
| 409 | (declare (type fixnum start end))
|
---|
| 410 | (do ((i (1- end) (1- i)))
|
---|
| 411 | ((< i start) (values nil t))
|
---|
| 412 | (declare (type fixnum i))
|
---|
| 413 | (cond
|
---|
| 414 | ((> (r-elt p i) (r-elt q i))
|
---|
| 415 | (return-from invlex> (values t nil)))
|
---|
| 416 | ((< (r-elt p i) (r-elt q i))
|
---|
| 417 | (return-from invlex> (values nil nil)))))))
|
---|
| 418 |
|
---|
| 419 | (defun reverse-monomial-order (order)
|
---|
| 420 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
| 421 | #'(lambda (p q &optional (start 0) (end (r-dimension q)))
|
---|
| 422 | (declare (type monom p q) (type fixnum start end))
|
---|
| 423 | (funcall order q p start end)))
|
---|
| 424 |
|
---|
| 425 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 426 | ;;
|
---|
| 427 | ;; Order making functions
|
---|
| 428 | ;;
|
---|
| 429 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 430 |
|
---|
| 431 | ;; This returns a closure with the same signature
|
---|
| 432 | ;; as all orders such as #'LEX>.
|
---|
| 433 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
| 434 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 435 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 436 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
| 437 | #'(lambda (p q &optional (start 0) (end (r-dimension p)))
|
---|
| 438 | (declare (type monom p q) (type fixnum start end))
|
---|
| 439 | (cond
|
---|
| 440 | ((> (r-elt p start) (r-elt q start))
|
---|
| 441 | (values t nil))
|
---|
| 442 | ((< (r-elt p start) (r-elt q start))
|
---|
| 443 | (values nil nil))
|
---|
| 444 | (t
|
---|
| 445 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 446 |
|
---|
| 447 | ;; This returns a closure which is called with an integer argument.
|
---|
| 448 | ;; The result is *another closure* with the same signature as all
|
---|
| 449 | ;; orders such as #'LEX>.
|
---|
| 450 | (defun make-elimination-order-factory (&optional
|
---|
| 451 | (primary-elimination-order #'lex>)
|
---|
| 452 | (secondary-elimination-order #'lex>))
|
---|
| 453 | "Return a function with a single integer argument K. This should be
|
---|
| 454 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 455 | remaining variables. The call to the closure creates a predicate
|
---|
| 456 | which compares monomials according to the K-th elimination order. The
|
---|
| 457 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 458 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 459 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 460 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 461 | which indicates that the first K variables appear with identical
|
---|
| 462 | powers, then the result is that of a call to
|
---|
| 463 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 464 | X[K],X[K+1],..."
|
---|
| 465 | #'(lambda (k)
|
---|
| 466 | (cond
|
---|
| 467 | ((<= k 0)
|
---|
| 468 | (error "K must be at least 1"))
|
---|
| 469 | ((= k 1)
|
---|
| 470 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
| 471 | (t
|
---|
| 472 | #'(lambda (p q &optional (start 0) (end (r-dimension p)))
|
---|
| 473 | (declare (type monom p q) (type fixnum start end))
|
---|
| 474 | (multiple-value-bind (primary equal)
|
---|
| 475 | (funcall primary-elimination-order p q start k)
|
---|
| 476 | (if equal
|
---|
| 477 | (funcall secondary-elimination-order p q k end)
|
---|
| 478 | (values primary nil))))))))
|
---|
| 479 |
|
---|