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