[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"
|
---|
[3482] | 57 | "MAKE-ELIMINATION-ORDER-FACTORY")
|
---|
[2524] | 58 | (:documentation
|
---|
[3477] | 59 | "This package implements basic operations on monomials, including
|
---|
| 60 | various monomial orders.
|
---|
| 61 |
|
---|
[2524] | 62 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 63 |
|
---|
[2524] | 64 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 65 |
|
---|
| 66 | However, lists may be implemented as other sequence types, so the
|
---|
| 67 | flexibility to change the representation should be maintained in the
|
---|
| 68 | code to use general operations on sequences whenever possible. The
|
---|
| 69 | optimization for the actual representation should be left to
|
---|
| 70 | declarations and the compiler.
|
---|
| 71 |
|
---|
| 72 | EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 73 |
|
---|
| 74 | Monom x*y^2 ---> (1 2) "))
|
---|
| 75 |
|
---|
[1610] | 76 | (in-package :monom)
|
---|
[48] | 77 |
|
---|
[1925] | 78 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[1923] | 79 |
|
---|
[48] | 80 | (deftype exponent ()
|
---|
| 81 | "Type of exponent in a monomial."
|
---|
| 82 | 'fixnum)
|
---|
| 83 |
|
---|
[2022] | 84 | (defclass monom ()
|
---|
[3312] | 85 | ((exponents :initarg :exponents :accessor monom-exponents
|
---|
[3054] | 86 | :documentation "The powers of the variables."))
|
---|
[3289] | 87 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
| 88 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
[2779] | 89 | (:documentation
|
---|
| 90 | "Implements a monomial, i.e. a product of powers
|
---|
| 91 | of variables, like X*Y^2."))
|
---|
[880] | 92 |
|
---|
[2245] | 93 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 94 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3313] | 95 | (with-accessors ((exponents monom-exponents))
|
---|
[3216] | 96 | self
|
---|
[3313] | 97 | (format stream "EXPONENTS=~A"
|
---|
| 98 | exponents))))
|
---|
[2027] | 99 |
|
---|
[3299] | 100 | (defmethod initialize-instance :after ((self monom)
|
---|
[3297] | 101 | &key
|
---|
| 102 | (dimension 0 dimension-supplied-p)
|
---|
| 103 | (exponents nil exponents-supplied-p)
|
---|
[3318] | 104 | (exponent 0)
|
---|
[3297] | 105 | &allow-other-keys
|
---|
[2390] | 106 | )
|
---|
[3329] | 107 | "The following INITIALIZE-INSTANCE method allows instance initialization
|
---|
| 108 | of a MONOM in a style similar to MAKE-ARRAY, e.g.:
|
---|
[3328] | 109 |
|
---|
| 110 | (MAKE-INSTANCE :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
| 111 | (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
| 112 | (MAKE-INSTANCE :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
|
---|
[3329] | 113 |
|
---|
| 114 | If both DIMENSION and EXPONENTS are supplied, they must be compatible,
|
---|
| 115 | i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
|
---|
| 116 | is not supplied, a monom with repeated value EXPONENT is created.
|
---|
| 117 | By default EXPONENT is 0, which results in a constant monomial.
|
---|
[3328] | 118 | "
|
---|
[3315] | 119 | (cond
|
---|
| 120 | (exponents-supplied-p
|
---|
[3327] | 121 | (when (and dimension-supplied-p
|
---|
| 122 | (/= dimension (length exponents)))
|
---|
| 123 | (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
|
---|
| 124 | exponents dimension))
|
---|
[3315] | 125 | (let ((dim (length exponents)))
|
---|
| 126 | (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
[3321] | 127 | (dimension-supplied-p
|
---|
[3315] | 128 | ;; when all exponents are to be identical
|
---|
[3321] | 129 | (setf (slot-value self 'exponents) (make-array (list dimension)
|
---|
| 130 | :initial-element exponent
|
---|
| 131 | :element-type 'exponent)))
|
---|
| 132 | (t
|
---|
| 133 | (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
|
---|
[3293] | 134 |
|
---|
[3443] | 135 | (defgeneric monom-dimension (m)
|
---|
| 136 | (:method ((m monom))
|
---|
| 137 | (length (monom-exponents m))))
|
---|
[3317] | 138 |
|
---|
[3541] | 139 | (defgeneric universal-equalp (object1 object2)
|
---|
| 140 | (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
|
---|
[3443] | 141 | (:method ((m1 monom) (m2 monom))
|
---|
[3541] | 142 | "Returns T iff monomials M1 and M2 have identical EXPONENTS."
|
---|
[3535] | 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 |
|
---|
[3545] | 160 | (defgeneric universal-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 |
|
---|
[3545] | 169 | (defgeneric universal-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))
|
---|
[3546] | 174 | (universal-total-degree m start end)))
|
---|
[48] | 175 |
|
---|
[3545] | 176 | (defgeneric universal-multiply-by (self other)
|
---|
[3446] | 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 |
|
---|
[3544] | 187 | (defgeneric universal-divide-by (self other)
|
---|
| 188 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
[3446] | 189 | (:method ((self monom) (other monom))
|
---|
| 190 | (with-slots ((exponents1 exponents))
|
---|
| 191 | self
|
---|
| 192 | (with-slots ((exponents2 exponents))
|
---|
| 193 | other
|
---|
| 194 | (unless (= (length exponents1) (length exponents2))
|
---|
| 195 | (error "divide-by: Incompatible dimensions."))
|
---|
| 196 | (unless (every #'>= exponents1 exponents2)
|
---|
| 197 | (error "divide-by: Negative power would result."))
|
---|
| 198 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
| 199 | self))
|
---|
[2818] | 200 |
|
---|
[3448] | 201 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
| 202 | "An :AROUND method of COPY-INSTANCE. It replaces
|
---|
| 203 | exponents with a fresh copy of the sequence."
|
---|
[3446] | 204 | (declare (ignore object initargs))
|
---|
| 205 | (let ((copy (call-next-method)))
|
---|
| 206 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
[3453] | 207 | copy))
|
---|
[2950] | 208 |
|
---|
[3543] | 209 | (defgeneric universal-multiply-2 (object1 object2)
|
---|
| 210 | (:documentation "Multiply OBJECT1 by OBJECT2")
|
---|
| 211 | (:method (object1 object2)
|
---|
| 212 | (universal-multiply-by (copy-instance m1) (copy-instance m2))))
|
---|
[2816] | 213 |
|
---|
[3543] | 214 | (defgeneric universal-divide (numerator &rest denominators)
|
---|
| 215 | (:documentation "Non-destructively divide object NUMERATOR by product of DENOMINATORS.")
|
---|
| 216 | (:method ((numerator monom) &rest denominators)
|
---|
| 217 | (monom-divide-by (copy-instance numerator) (reduce #'universal-multiply-2 denominators))))
|
---|
[48] | 218 |
|
---|
[3441] | 219 | (defmethod monom-divides-p ((m1 monom) (m2 monom))
|
---|
[48] | 220 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
[2039] | 221 | (with-slots ((exponents1 exponents))
|
---|
| 222 | m1
|
---|
| 223 | (with-slots ((exponents2 exponents))
|
---|
| 224 | m2
|
---|
| 225 | (every #'<= exponents1 exponents2))))
|
---|
[48] | 226 |
|
---|
[2075] | 227 |
|
---|
[3441] | 228 | (defmethod monom-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
[2055] | 229 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[875] | 230 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
[869] | 231 | m1 m2 m3))
|
---|
[48] | 232 |
|
---|
[2049] | 233 |
|
---|
[3441] | 234 | (defmethod monom-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
[48] | 235 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
[1890] | 236 | (declare (type monom m1 m2 m3 m4))
|
---|
[869] | 237 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 238 | m1 m2 m3 m4))
|
---|
| 239 |
|
---|
[3441] | 240 | (defmethod monom-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
[2075] | 241 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
[2171] | 242 | (with-slots ((exponents1 exponents))
|
---|
[2076] | 243 | m1
|
---|
[2171] | 244 | (with-slots ((exponents2 exponents))
|
---|
[2076] | 245 | m2
|
---|
[2171] | 246 | (with-slots ((exponents3 exponents))
|
---|
[2076] | 247 | m3
|
---|
[2171] | 248 | (with-slots ((exponents4 exponents))
|
---|
[2076] | 249 | m4
|
---|
[2077] | 250 | (every
|
---|
| 251 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 252 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
[48] | 253 |
|
---|
[3441] | 254 | (defmethod monom-divisible-by-p ((m1 monom) (m2 monom))
|
---|
[48] | 255 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
[2171] | 256 | (with-slots ((exponents1 exponents))
|
---|
[2144] | 257 | m1
|
---|
[2171] | 258 | (with-slots ((exponents2 exponents))
|
---|
[2144] | 259 | m2
|
---|
| 260 | (every #'>= exponents1 exponents2))))
|
---|
[2078] | 261 |
|
---|
[3441] | 262 | (defmethod monom-rel-prime-p ((m1 monom) (m2 monom))
|
---|
[48] | 263 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
[2171] | 264 | (with-slots ((exponents1 exponents))
|
---|
[2078] | 265 | m1
|
---|
[2171] | 266 | (with-slots ((exponents2 exponents))
|
---|
[2078] | 267 | m2
|
---|
[2154] | 268 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
[48] | 269 |
|
---|
[2076] | 270 |
|
---|
[3441] | 271 | (defmethod monom-lcm ((m1 monom) (m2 monom))
|
---|
[48] | 272 | "Returns least common multiple of monomials M1 and M2."
|
---|
[3322] | 273 | (with-slots ((exponents1 exponents))
|
---|
[2082] | 274 | m1
|
---|
[2171] | 275 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 276 | m2
|
---|
[3324] | 277 | (let* ((exponents (copy-seq exponents1)))
|
---|
[2082] | 278 | (map-into exponents #'max exponents1 exponents2)
|
---|
[3322] | 279 | (make-instance 'monom :exponents exponents)))))
|
---|
[48] | 280 |
|
---|
[2080] | 281 |
|
---|
[3441] | 282 | (defmethod monom-gcd ((m1 monom) (m2 monom))
|
---|
[48] | 283 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
[3322] | 284 | (with-slots ((exponents1 exponents))
|
---|
[2082] | 285 | m1
|
---|
[2171] | 286 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 287 | m2
|
---|
[3322] | 288 | (let* ((exponents (copy-seq exponents1)))
|
---|
[2082] | 289 | (map-into exponents #'min exponents1 exponents2)
|
---|
[3322] | 290 | (make-instance 'monom :exponents exponents)))))
|
---|
[48] | 291 |
|
---|
[3441] | 292 | (defmethod monom-depends-p ((m monom) k)
|
---|
[48] | 293 | "Return T if the monomial M depends on variable number K."
|
---|
[2083] | 294 | (declare (type fixnum k))
|
---|
| 295 | (with-slots (exponents)
|
---|
| 296 | m
|
---|
[2154] | 297 | (plusp (elt exponents k))))
|
---|
[48] | 298 |
|
---|
[3441] | 299 | (defmethod monom-left-tensor-product-by ((self monom) (other monom))
|
---|
[3323] | 300 | (with-slots ((exponents1 exponents))
|
---|
[3020] | 301 | self
|
---|
[3323] | 302 | (with-slots ((exponents2 exponents))
|
---|
[3020] | 303 | other
|
---|
[3323] | 304 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
[3036] | 305 | self)
|
---|
[48] | 306 |
|
---|
[3441] | 307 | (defmethod monom-right-tensor-product-by ((self monom) (other monom))
|
---|
[3323] | 308 | (with-slots ((exponents1 exponents))
|
---|
[3026] | 309 | self
|
---|
[3323] | 310 | (with-slots ((exponents2 exponents))
|
---|
[3026] | 311 | other
|
---|
[3323] | 312 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
[3036] | 313 | self)
|
---|
[3026] | 314 |
|
---|
[3441] | 315 | (defmethod monom-left-contract ((self monom) k)
|
---|
[1638] | 316 | "Drop the first K variables in monomial M."
|
---|
[2085] | 317 | (declare (fixnum k))
|
---|
[3323] | 318 | (with-slots (exponents)
|
---|
[3040] | 319 | self
|
---|
[3323] | 320 | (setf exponents (subseq exponents k)))
|
---|
[3039] | 321 | self)
|
---|
[886] | 322 |
|
---|
| 323 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 324 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 325 | "Construct a monomial in the polynomial ring
|
---|
| 326 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 327 | which represents a single variable. It assumes number of variables
|
---|
| 328 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 329 | may appear raised to power POWER. "
|
---|
[1924] | 330 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 331 | (with-slots (exponents)
|
---|
| 332 | m
|
---|
[2154] | 333 | (setf (elt exponents pos) power)
|
---|
[2089] | 334 | m))
|
---|
[1151] | 335 |
|
---|
[3441] | 336 | (defmethod monom->list ((m monom))
|
---|
[1152] | 337 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2779] | 338 | (coerce (monom-exponents m) 'list))
|
---|
[3472] | 339 |
|
---|
| 340 |
|
---|
[3474] | 341 | ;; pure lexicographic
|
---|
[3472] | 342 | (defgeneric lex> (p q &optional start end)
|
---|
| 343 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 344 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 345 | otherwise it is NIL.")
|
---|
[3483] | 346 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 347 | (declare (type fixnum start end))
|
---|
| 348 | (do ((i start (1+ i)))
|
---|
| 349 | ((>= i end) (values nil t))
|
---|
| 350 | (cond
|
---|
[3483] | 351 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 352 | (return-from lex> (values t nil)))
|
---|
[3483] | 353 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 354 | (return-from lex> (values nil nil)))))))
|
---|
| 355 |
|
---|
[3475] | 356 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 357 | (defgeneric grlex> (p q &optional start end)
|
---|
| 358 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 359 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 360 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 361 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 362 | (declare (type monom p q) (type fixnum start end))
|
---|
[3483] | 363 | (let ((d1 (monom-total-degree p start end))
|
---|
| 364 | (d2 (monom-total-degree q start end)))
|
---|
[3472] | 365 | (declare (type fixnum d1 d2))
|
---|
| 366 | (cond
|
---|
| 367 | ((> d1 d2) (values t nil))
|
---|
| 368 | ((< d1 d2) (values nil nil))
|
---|
| 369 | (t
|
---|
| 370 | (lex> p q start end))))))
|
---|
| 371 |
|
---|
| 372 | ;; reverse lexicographic
|
---|
| 373 | (defgeneric revlex> (p q &optional start end)
|
---|
| 374 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 375 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 376 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 377 | because some sets do not have a minimal element. This order is useful
|
---|
| 378 | in constructing other orders.")
|
---|
[3483] | 379 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 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
|
---|
[3483] | 385 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 386 | (return-from revlex> (values t nil)))
|
---|
[3483] | 387 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 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
|
---|
| 394 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 395 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 396 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 397 | (declare (type fixnum start end))
|
---|
[3483] | 398 | (let ((d1 (monom-total-degree p start end))
|
---|
| 399 | (d2 (monom-total-degree q start end)))
|
---|
[3472] | 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
|
---|
| 409 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 410 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 411 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 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
|
---|
[3483] | 417 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 418 | (return-from invlex> (values t nil)))
|
---|
[3483] | 419 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 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."
|
---|
[3483] | 424 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 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>.
|
---|
[3487] | 436 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 437 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 438 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 439 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 440 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 441 | (declare (type monom p q) (type fixnum start end))
|
---|
| 442 | (cond
|
---|
[3483] | 443 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 444 | (values t nil))
|
---|
[3483] | 445 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 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>.
|
---|
[3486] | 453 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 454 | (primary-elimination-order #'lex>)
|
---|
| 455 | (secondary-elimination-order #'lex>))
|
---|
| 456 | "Return a function with a single integer argument K. This should be
|
---|
| 457 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 458 | remaining variables. The call to the closure creates a predicate
|
---|
| 459 | which compares monomials according to the K-th elimination order. The
|
---|
| 460 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 461 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 462 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 463 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 464 | which indicates that the first K variables appear with identical
|
---|
| 465 | powers, then the result is that of a call to
|
---|
| 466 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 467 | X[K],X[K+1],..."
|
---|
| 468 | #'(lambda (k)
|
---|
| 469 | (cond
|
---|
| 470 | ((<= k 0)
|
---|
| 471 | (error "K must be at least 1"))
|
---|
| 472 | ((= k 1)
|
---|
[3485] | 473 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 474 | (t
|
---|
[3483] | 475 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 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 |
|
---|
[3531] | 483 | (defclass term (monom)
|
---|
| 484 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 485 | (:default-initargs :coeff nil)
|
---|
| 486 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 487 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 488 |
|
---|
| 489 | (defmethod print-object ((self term) stream)
|
---|
| 490 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 491 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 492 | (coeff term-coeff))
|
---|
[3531] | 493 | self
|
---|
| 494 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 495 | exponents coeff))))
|
---|
| 496 |
|
---|
[3542] | 497 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 498 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 499 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 500 | (and (call-next-method)
|
---|
| 501 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 502 |
|
---|
[3533] | 503 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
| 504 | (setf (term-coeff new) 1))
|
---|
[3531] | 505 |
|
---|
[3538] | 506 | (defmethod term-multiply-by ((self term) (other term))
|
---|
[3531] | 507 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 508 | It returns SELF."
|
---|
[3538] | 509 | (setf (term-coeff self) (universal-multiply-by (term-coeff self) (scalar-coeff other))))
|
---|
[3531] | 510 |
|
---|
[3539] | 511 | (defmethod term-left-tensor-product-by ((self term) (other term))
|
---|
| 512 | (setf (term-coeff self) (universal-multiply-by (term-coeff self) (term-coeff other)))
|
---|
[3531] | 513 | (call-next-method))
|
---|
| 514 |
|
---|
[3540] | 515 | (defmethod term-right-tensor-product-by ((self term) (other term))
|
---|
[3533] | 516 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other)))
|
---|
[3531] | 517 | (call-next-method))
|
---|
| 518 |
|
---|
[3533] | 519 | (defmethod monom-divide-by ((self term) (other term))
|
---|
[3531] | 520 | "Destructively divide term SELF by OTHER and store the result into SELF.
|
---|
| 521 | It returns SELF."
|
---|
[3533] | 522 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other)))
|
---|
[3531] | 523 | (call-next-method))
|
---|
| 524 |
|
---|
[3533] | 525 | (defmethod monom-unary-minus ((self term))
|
---|
| 526 | (setf (term-coeff self) (monom-unary-minus (term-coeff self)))
|
---|
[3531] | 527 | self)
|
---|
| 528 |
|
---|
[3533] | 529 | (defmethod monom-multiply ((term1 term) (term2 term))
|
---|
[3531] | 530 | "Non-destructively multiply TERM1 by TERM2."
|
---|
[3533] | 531 | (monom-multiply-by (copy-instance term1) (copy-instance term2)))
|
---|
[3531] | 532 |
|
---|
[3533] | 533 | (defmethod monom-multiply ((term1 number) (term2 monom))
|
---|
[3531] | 534 | "Non-destructively multiply TERM1 by TERM2."
|
---|
[3533] | 535 | (monom-multiply term1 (change-class (copy-instance term2) 'term)))
|
---|
[3531] | 536 |
|
---|
[3533] | 537 | (defmethod monom-zerop ((self term))
|
---|
| 538 | (zerop (term-coeff self)))
|
---|