[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"
|
---|
[3592] | 28 | "UNIVERSAL-EQUALP"
|
---|
[3442] | 29 | "MONOM-ELT"
|
---|
[3592] | 30 | "TOTAL-DEGREE"
|
---|
| 31 | "SUGAR"
|
---|
| 32 | "MULTIPLY-BY"
|
---|
| 33 | "DIVIDE-BY"
|
---|
| 34 | "MULTIPLY-2"
|
---|
| 35 | "MULTIPLY"
|
---|
| 36 | "DIVIDES-P"
|
---|
| 37 | "DIVIDES-LCM-P"
|
---|
| 38 | "LCM-DIVIDES-LCM-P"
|
---|
| 39 | "LCM-EQUAL-LCM-P"
|
---|
| 40 | "DIVISIBLE-BY-P"
|
---|
| 41 | "REL-PRIME-P"
|
---|
| 42 | "UNIVERSAL-LCM"
|
---|
| 43 | "UNIVERSAL-GCD"
|
---|
| 44 | "DEPENDS-P"
|
---|
| 45 | "LEFT-TENSOR-PRODUCT-BY"
|
---|
| 46 | "RIGHT-TENSOR-PRODUCT-BY"
|
---|
| 47 | "LEFT-CONTRACT"
|
---|
[3442] | 48 | "MAKE-MONOM-VARIABLE"
|
---|
[3472] | 49 | "MONOM->LIST"
|
---|
| 50 | "LEX>"
|
---|
| 51 | "GRLEX>"
|
---|
| 52 | "REVLEX>"
|
---|
| 53 | "GREVLEX>"
|
---|
| 54 | "INVLEX>"
|
---|
| 55 | "REVERSE-MONOMIAL-ORDER"
|
---|
[3482] | 56 | "MAKE-ELIMINATION-ORDER-FACTORY")
|
---|
[2524] | 57 | (:documentation
|
---|
[3477] | 58 | "This package implements basic operations on monomials, including
|
---|
| 59 | various monomial orders.
|
---|
| 60 |
|
---|
[2524] | 61 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 62 |
|
---|
[2524] | 63 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 64 |
|
---|
| 65 | However, lists may be implemented as other sequence types, so the
|
---|
| 66 | flexibility to change the representation should be maintained in the
|
---|
| 67 | code to use general operations on sequences whenever possible. The
|
---|
| 68 | optimization for the actual representation should be left to
|
---|
| 69 | declarations and the compiler.
|
---|
| 70 |
|
---|
| 71 | EXAMPLES: 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
|
---|
| 90 | of 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
|
---|
| 107 | of 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 |
|
---|
| 113 | If both DIMENSION and EXPONENTS are supplied, they must be compatible,
|
---|
| 114 | i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
|
---|
| 115 | is not supplied, a monom with repeated value EXPONENT is created.
|
---|
| 116 | By 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 |
|
---|
[3573] | 134 | (defgeneric monom-dimension (m)
|
---|
[3443] | 135 | (:method ((m monom))
|
---|
| 136 | (length (monom-exponents m))))
|
---|
[3317] | 137 |
|
---|
[3541] | 138 | (defgeneric universal-equalp (object1 object2)
|
---|
| 139 | (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
|
---|
[3443] | 140 | (:method ((m1 monom) (m2 monom))
|
---|
[3541] | 141 | "Returns T iff monomials M1 and M2 have identical EXPONENTS."
|
---|
[3535] | 142 | (equalp (monom-exponents m1) (monom-exponents m2))))
|
---|
[2547] | 143 |
|
---|
[3443] | 144 | (defgeneric monom-elt (m index)
|
---|
[3574] | 145 | (:documentation "Return the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 146 | (:method ((m monom) index)
|
---|
[3550] | 147 | "Return the power in the monomial M of variable number INDEX."
|
---|
[3443] | 148 | (with-slots (exponents)
|
---|
| 149 | m
|
---|
| 150 | (elt exponents index))))
|
---|
[48] | 151 |
|
---|
[3443] | 152 | (defgeneric (setf monom-elt) (new-value m index)
|
---|
[3550] | 153 | (:documentation "Set the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 154 | (:method (new-value (m monom) index)
|
---|
| 155 | (with-slots (exponents)
|
---|
| 156 | m
|
---|
[3453] | 157 | (setf (elt exponents index) new-value))))
|
---|
[2023] | 158 |
|
---|
[3551] | 159 | (defgeneric total-degree (m &optional start end)
|
---|
| 160 | (:documentation "Return the total degree of a monomoal M. Optinally, a range
|
---|
[3449] | 161 | of variables may be specified with arguments START and END.")
|
---|
| 162 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 163 | (declare (type fixnum start end))
|
---|
| 164 | (with-slots (exponents)
|
---|
| 165 | m
|
---|
| 166 | (reduce #'+ exponents :start start :end end))))
|
---|
[48] | 167 |
|
---|
[3552] | 168 | (defgeneric sugar (m &optional start end)
|
---|
[3446] | 169 | (:documentation "Return the sugar of a monomial M. Optinally, a range
|
---|
| 170 | of variables may be specified with arguments START and END.")
|
---|
| 171 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 172 | (declare (type fixnum start end))
|
---|
[3552] | 173 | (total-degree m start end)))
|
---|
[48] | 174 |
|
---|
[3553] | 175 | (defgeneric multiply-by (self other)
|
---|
[3549] | 176 | (:documentation "Multiply SELF by OTHER, return SELF.")
|
---|
[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 |
|
---|
[3553] | 187 | (defgeneric divide-by (self other)
|
---|
[3544] | 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 |
|
---|
[3560] | 209 | (defun multiply-2 (object1 object2)
|
---|
[3559] | 210 | "Multiply OBJECT1 by OBJECT2"
|
---|
| 211 | (multiply-by (copy-instance object1) (copy-instance object2)))
|
---|
[2816] | 212 |
|
---|
[3557] | 213 | (defun multiply (&rest factors)
|
---|
| 214 | "Non-destructively multiply list FACTORS."
|
---|
| 215 | (reduce #'multiply-2 factors))
|
---|
[3554] | 216 |
|
---|
[3557] | 217 | (defun divide (numerator &rest denominators)
|
---|
| 218 | "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
|
---|
[3558] | 219 | (divide-by (copy-instance numerator) (multiply denominators)))
|
---|
[48] | 220 |
|
---|
[3591] | 221 | (defgeneric divides-p (object1 object2)
|
---|
| 222 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
| 223 | (:method ((m1 monom) (m2 monom))
|
---|
| 224 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
| 225 | (with-slots ((exponents1 exponents))
|
---|
| 226 | m1
|
---|
| 227 | (with-slots ((exponents2 exponents))
|
---|
| 228 | m2
|
---|
| 229 | (every #'<= exponents1 exponents2)))))
|
---|
[48] | 230 |
|
---|
[3585] | 231 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
[3594] | 232 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
[3585] | 233 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
| 234 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[3596] | 235 | (with-slots ((exponents1 exponents))
|
---|
| 236 | m1
|
---|
| 237 | (with-slots ((exponents2 exponents))
|
---|
| 238 | m2
|
---|
| 239 | (with-slots ((exponents3 exponents))
|
---|
| 240 | m3
|
---|
| 241 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
| 242 | exponents1 exponents2 exponents3))))))
|
---|
[48] | 243 |
|
---|
[3588] | 244 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
| 245 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 246 | "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
|
---|
| 247 | (with-slots ((exponents1 exponents))
|
---|
| 248 | m1
|
---|
| 249 | (with-slots ((exponents2 exponents))
|
---|
| 250 | m2
|
---|
| 251 | (with-slots ((exponents3 exponents))
|
---|
| 252 | m3
|
---|
| 253 | (with-slots ((exponents4 exponents))
|
---|
| 254 | m4
|
---|
| 255 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
[3590] | 256 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[869] | 257 |
|
---|
[3589] | 258 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
| 259 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 260 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
| 261 | (with-slots ((exponents1 exponents))
|
---|
| 262 | m1
|
---|
| 263 | (with-slots ((exponents2 exponents))
|
---|
| 264 | m2
|
---|
| 265 | (with-slots ((exponents3 exponents))
|
---|
| 266 | m3
|
---|
| 267 | (with-slots ((exponents4 exponents))
|
---|
| 268 | m4
|
---|
| 269 | (every
|
---|
| 270 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 271 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[48] | 272 |
|
---|
[3563] | 273 | (defgeneric divisible-by-p (object1 object2)
|
---|
| 274 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
| 275 | (:method ((m1 monom) (m2 monom))
|
---|
| 276 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
| 277 | (with-slots ((exponents1 exponents))
|
---|
| 278 | m1
|
---|
| 279 | (with-slots ((exponents2 exponents))
|
---|
| 280 | m2
|
---|
| 281 | (every #'>= exponents1 exponents2)))))
|
---|
[2078] | 282 |
|
---|
[3565] | 283 | (defgeneric rel-prime-p (object1 object2)
|
---|
[3575] | 284 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
[3563] | 285 | (:method ((m1 monom) (m2 monom))
|
---|
| 286 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
| 287 | (with-slots ((exponents1 exponents))
|
---|
| 288 | m1
|
---|
| 289 | (with-slots ((exponents2 exponents))
|
---|
| 290 | m2
|
---|
| 291 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
[48] | 292 |
|
---|
[3595] | 293 | (defgeneric universal-lcm (object1 object2)
|
---|
[3566] | 294 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
| 295 | (:method ((m1 monom) (m2 monom))
|
---|
| 296 | "Returns least common multiple of monomials M1 and M2."
|
---|
| 297 | (with-slots ((exponents1 exponents))
|
---|
| 298 | m1
|
---|
| 299 | (with-slots ((exponents2 exponents))
|
---|
| 300 | m2
|
---|
| 301 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 302 | (map-into exponents #'max exponents1 exponents2)
|
---|
| 303 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 304 |
|
---|
[2080] | 305 |
|
---|
[3567] | 306 | (defgeneric universal-gcd (object1 object2)
|
---|
| 307 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
| 308 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 309 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 310 | (with-slots ((exponents1 exponents))
|
---|
| 311 | m1
|
---|
| 312 | (with-slots ((exponents2 exponents))
|
---|
| 313 | m2
|
---|
| 314 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 315 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 316 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 317 |
|
---|
[3569] | 318 | (defgeneric depends-p (object k)
|
---|
| 319 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 320 | (:method ((m monom) k)
|
---|
| 321 | "Return T if the monomial M depends on variable number K."
|
---|
| 322 | (declare (type fixnum k))
|
---|
| 323 | (with-slots (exponents)
|
---|
| 324 | m
|
---|
| 325 | (plusp (elt exponents k)))))
|
---|
[48] | 326 |
|
---|
[3570] | 327 | (defgeneric left-tensor-product-by (self other)
|
---|
| 328 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 329 | SELF. Return SELF.")
|
---|
| 330 | (:method ((self monom) (other monom))
|
---|
| 331 | (with-slots ((exponents1 exponents))
|
---|
| 332 | self
|
---|
| 333 | (with-slots ((exponents2 exponents))
|
---|
| 334 | other
|
---|
| 335 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 336 | self))
|
---|
[48] | 337 |
|
---|
[3570] | 338 | (defgeneric right-tensor-product-by (self other)
|
---|
| 339 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 340 | into SELF. Returns SELF.")
|
---|
| 341 | (:method ((self monom) (other monom))
|
---|
| 342 | (with-slots ((exponents1 exponents))
|
---|
| 343 | self
|
---|
| 344 | (with-slots ((exponents2 exponents))
|
---|
| 345 | other
|
---|
| 346 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 347 | self))
|
---|
[3026] | 348 |
|
---|
[3571] | 349 | (defgeneric left-contract (self k)
|
---|
| 350 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 351 | (:method ((self monom) k)
|
---|
| 352 | "Drop the first K variables in monomial M."
|
---|
| 353 | (declare (fixnum k))
|
---|
| 354 | (with-slots (exponents)
|
---|
| 355 | self
|
---|
| 356 | (setf exponents (subseq exponents k)))
|
---|
| 357 | self))
|
---|
[886] | 358 |
|
---|
| 359 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 360 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 361 | "Construct a monomial in the polynomial ring
|
---|
| 362 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 363 | which represents a single variable. It assumes number of variables
|
---|
| 364 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 365 | may appear raised to power POWER. "
|
---|
[1924] | 366 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 367 | (with-slots (exponents)
|
---|
| 368 | m
|
---|
[2154] | 369 | (setf (elt exponents pos) power)
|
---|
[2089] | 370 | m))
|
---|
[1151] | 371 |
|
---|
[3441] | 372 | (defmethod monom->list ((m monom))
|
---|
[1152] | 373 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2779] | 374 | (coerce (monom-exponents m) 'list))
|
---|
[3472] | 375 |
|
---|
| 376 |
|
---|
[3474] | 377 | ;; pure lexicographic
|
---|
[3472] | 378 | (defgeneric lex> (p q &optional start end)
|
---|
| 379 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 380 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 381 | otherwise it is NIL.")
|
---|
[3483] | 382 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 383 | (declare (type fixnum start end))
|
---|
| 384 | (do ((i start (1+ i)))
|
---|
| 385 | ((>= i end) (values nil t))
|
---|
| 386 | (cond
|
---|
[3483] | 387 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 388 | (return-from lex> (values t nil)))
|
---|
[3483] | 389 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 390 | (return-from lex> (values nil nil)))))))
|
---|
| 391 |
|
---|
[3475] | 392 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 393 | (defgeneric grlex> (p q &optional start end)
|
---|
| 394 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 395 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 396 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 397 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 398 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 399 | (let ((d1 (total-degree p start end))
|
---|
| 400 | (d2 (total-degree q start end)))
|
---|
[3472] | 401 | (declare (type fixnum d1 d2))
|
---|
| 402 | (cond
|
---|
| 403 | ((> d1 d2) (values t nil))
|
---|
| 404 | ((< d1 d2) (values nil nil))
|
---|
| 405 | (t
|
---|
| 406 | (lex> p q start end))))))
|
---|
| 407 |
|
---|
| 408 | ;; reverse lexicographic
|
---|
| 409 | (defgeneric revlex> (p q &optional start end)
|
---|
| 410 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 411 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 412 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 413 | because some sets do not have a minimal element. This order is useful
|
---|
| 414 | in constructing other orders.")
|
---|
[3483] | 415 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 416 | (declare (type fixnum start end))
|
---|
| 417 | (do ((i (1- end) (1- i)))
|
---|
| 418 | ((< i start) (values nil t))
|
---|
| 419 | (declare (type fixnum i))
|
---|
| 420 | (cond
|
---|
[3483] | 421 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 422 | (return-from revlex> (values t nil)))
|
---|
[3483] | 423 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 424 | (return-from revlex> (values nil nil)))))))
|
---|
| 425 |
|
---|
| 426 |
|
---|
| 427 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 428 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 429 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 430 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 431 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 432 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 433 | (declare (type fixnum start end))
|
---|
[3584] | 434 | (let ((d1 (total-degree p start end))
|
---|
| 435 | (d2 (total-degree q start end)))
|
---|
[3472] | 436 | (declare (type fixnum d1 d2))
|
---|
| 437 | (cond
|
---|
| 438 | ((> d1 d2) (values t nil))
|
---|
| 439 | ((< d1 d2) (values nil nil))
|
---|
| 440 | (t
|
---|
| 441 | (revlex> p q start end))))))
|
---|
| 442 |
|
---|
| 443 | (defgeneric invlex> (p q &optional start end)
|
---|
| 444 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 445 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 446 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 447 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 448 | (declare (type fixnum start end))
|
---|
| 449 | (do ((i (1- end) (1- i)))
|
---|
| 450 | ((< i start) (values nil t))
|
---|
| 451 | (declare (type fixnum i))
|
---|
| 452 | (cond
|
---|
[3483] | 453 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 454 | (return-from invlex> (values t nil)))
|
---|
[3483] | 455 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 456 | (return-from invlex> (values nil nil)))))))
|
---|
| 457 |
|
---|
| 458 | (defun reverse-monomial-order (order)
|
---|
| 459 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 460 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 461 | (declare (type monom p q) (type fixnum start end))
|
---|
| 462 | (funcall order q p start end)))
|
---|
| 463 |
|
---|
| 464 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 465 | ;;
|
---|
| 466 | ;; Order making functions
|
---|
| 467 | ;;
|
---|
| 468 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 469 |
|
---|
| 470 | ;; This returns a closure with the same signature
|
---|
| 471 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 472 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 473 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 474 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 475 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 476 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 477 | (declare (type monom p q) (type fixnum start end))
|
---|
| 478 | (cond
|
---|
[3483] | 479 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 480 | (values t nil))
|
---|
[3483] | 481 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 482 | (values nil nil))
|
---|
| 483 | (t
|
---|
| 484 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 485 |
|
---|
| 486 | ;; This returns a closure which is called with an integer argument.
|
---|
| 487 | ;; The result is *another closure* with the same signature as all
|
---|
| 488 | ;; orders such as #'LEX>.
|
---|
[3486] | 489 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 490 | (primary-elimination-order #'lex>)
|
---|
| 491 | (secondary-elimination-order #'lex>))
|
---|
| 492 | "Return a function with a single integer argument K. This should be
|
---|
| 493 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 494 | remaining variables. The call to the closure creates a predicate
|
---|
| 495 | which compares monomials according to the K-th elimination order. The
|
---|
| 496 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 497 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 498 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 499 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 500 | which indicates that the first K variables appear with identical
|
---|
| 501 | powers, then the result is that of a call to
|
---|
| 502 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 503 | X[K],X[K+1],..."
|
---|
| 504 | #'(lambda (k)
|
---|
| 505 | (cond
|
---|
| 506 | ((<= k 0)
|
---|
| 507 | (error "K must be at least 1"))
|
---|
| 508 | ((= k 1)
|
---|
[3485] | 509 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 510 | (t
|
---|
[3483] | 511 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 512 | (declare (type monom p q) (type fixnum start end))
|
---|
| 513 | (multiple-value-bind (primary equal)
|
---|
| 514 | (funcall primary-elimination-order p q start k)
|
---|
| 515 | (if equal
|
---|
| 516 | (funcall secondary-elimination-order p q k end)
|
---|
| 517 | (values primary nil))))))))
|
---|
| 518 |
|
---|
[3531] | 519 | (defclass term (monom)
|
---|
| 520 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 521 | (:default-initargs :coeff nil)
|
---|
| 522 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 523 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 524 |
|
---|
| 525 | (defmethod print-object ((self term) stream)
|
---|
| 526 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 527 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 528 | (coeff term-coeff))
|
---|
[3531] | 529 | self
|
---|
| 530 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 531 | exponents coeff))))
|
---|
| 532 |
|
---|
[3542] | 533 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 534 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 535 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 536 | (and (call-next-method)
|
---|
| 537 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 538 |
|
---|
[3533] | 539 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
| 540 | (setf (term-coeff new) 1))
|
---|
[3531] | 541 |
|
---|
[3556] | 542 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 543 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 544 | It returns SELF."
|
---|
[3580] | 545 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 546 |
|
---|
[3581] | 547 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 548 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 549 |
|
---|
[3581] | 550 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 551 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 552 |
|
---|
[3556] | 553 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 554 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 555 |
|
---|
[3582] | 556 | (defgeneric unary-minus (self)
|
---|
| 557 | (:method ((self term))
|
---|
| 558 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 559 | self))
|
---|
[3531] | 560 |
|
---|
[3578] | 561 | (defgeneric universal-zerop (self)
|
---|
| 562 | (:method ((self term))
|
---|
| 563 | (universal-zerop (term-coeff self))))
|
---|