[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"
|
---|
[3602] | 25 | "TERM"
|
---|
[423] | 26 | "EXPONENT"
|
---|
[2781] | 27 | "MONOM-DIMENSION"
|
---|
| 28 | "MONOM-EXPONENTS"
|
---|
[3592] | 29 | "UNIVERSAL-EQUALP"
|
---|
[3442] | 30 | "MONOM-ELT"
|
---|
[3592] | 31 | "TOTAL-DEGREE"
|
---|
| 32 | "SUGAR"
|
---|
| 33 | "MULTIPLY-BY"
|
---|
| 34 | "DIVIDE-BY"
|
---|
[3599] | 35 | "DIVIDE"
|
---|
[3592] | 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"
|
---|
[3442] | 50 | "MAKE-MONOM-VARIABLE"
|
---|
[3472] | 51 | "MONOM->LIST"
|
---|
| 52 | "LEX>"
|
---|
| 53 | "GRLEX>"
|
---|
| 54 | "REVLEX>"
|
---|
| 55 | "GREVLEX>"
|
---|
| 56 | "INVLEX>"
|
---|
| 57 | "REVERSE-MONOMIAL-ORDER"
|
---|
[3606] | 58 | "MAKE-ELIMINATION-ORDER-FACTORY"
|
---|
| 59 | "UNARY-MINUS")
|
---|
[2524] | 60 | (:documentation
|
---|
[3477] | 61 | "This package implements basic operations on monomials, including
|
---|
| 62 | various monomial orders.
|
---|
| 63 |
|
---|
[2524] | 64 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 65 |
|
---|
[2524] | 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 |
|
---|
[1610] | 78 | (in-package :monom)
|
---|
[48] | 79 |
|
---|
[1925] | 80 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[1923] | 81 |
|
---|
[48] | 82 | (deftype exponent ()
|
---|
| 83 | "Type of exponent in a monomial."
|
---|
| 84 | 'fixnum)
|
---|
| 85 |
|
---|
[2022] | 86 | (defclass monom ()
|
---|
[3312] | 87 | ((exponents :initarg :exponents :accessor monom-exponents
|
---|
[3054] | 88 | :documentation "The powers of the variables."))
|
---|
[3289] | 89 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
| 90 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
[2779] | 91 | (:documentation
|
---|
| 92 | "Implements a monomial, i.e. a product of powers
|
---|
| 93 | of variables, like X*Y^2."))
|
---|
[880] | 94 |
|
---|
[2245] | 95 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 96 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3313] | 97 | (with-accessors ((exponents monom-exponents))
|
---|
[3216] | 98 | self
|
---|
[3313] | 99 | (format stream "EXPONENTS=~A"
|
---|
| 100 | exponents))))
|
---|
[2027] | 101 |
|
---|
[3299] | 102 | (defmethod initialize-instance :after ((self monom)
|
---|
[3297] | 103 | &key
|
---|
| 104 | (dimension 0 dimension-supplied-p)
|
---|
| 105 | (exponents nil exponents-supplied-p)
|
---|
[3318] | 106 | (exponent 0)
|
---|
[3297] | 107 | &allow-other-keys
|
---|
[2390] | 108 | )
|
---|
[3329] | 109 | "The following INITIALIZE-INSTANCE method allows instance initialization
|
---|
| 110 | of a MONOM in a style similar to MAKE-ARRAY, e.g.:
|
---|
[3328] | 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)>
|
---|
[3329] | 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.
|
---|
[3328] | 120 | "
|
---|
[3315] | 121 | (cond
|
---|
| 122 | (exponents-supplied-p
|
---|
[3327] | 123 | (when (and dimension-supplied-p
|
---|
| 124 | (/= dimension (length exponents)))
|
---|
| 125 | (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
|
---|
| 126 | exponents dimension))
|
---|
[3315] | 127 | (let ((dim (length exponents)))
|
---|
| 128 | (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
[3321] | 129 | (dimension-supplied-p
|
---|
[3315] | 130 | ;; when all exponents are to be identical
|
---|
[3321] | 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."))))
|
---|
[3293] | 136 |
|
---|
[3573] | 137 | (defgeneric monom-dimension (m)
|
---|
[3443] | 138 | (:method ((m monom))
|
---|
| 139 | (length (monom-exponents m))))
|
---|
[3317] | 140 |
|
---|
[3541] | 141 | (defgeneric universal-equalp (object1 object2)
|
---|
| 142 | (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
|
---|
[3607] | 143 | (:method ((object1 cons) (object2 cons)) (equalp object1 object2))
|
---|
[3443] | 144 | (:method ((m1 monom) (m2 monom))
|
---|
[3541] | 145 | "Returns T iff monomials M1 and M2 have identical EXPONENTS."
|
---|
[3535] | 146 | (equalp (monom-exponents m1) (monom-exponents m2))))
|
---|
[2547] | 147 |
|
---|
[3443] | 148 | (defgeneric monom-elt (m index)
|
---|
[3574] | 149 | (:documentation "Return the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 150 | (:method ((m monom) index)
|
---|
[3550] | 151 | "Return the power in the monomial M of variable number INDEX."
|
---|
[3443] | 152 | (with-slots (exponents)
|
---|
| 153 | m
|
---|
| 154 | (elt exponents index))))
|
---|
[48] | 155 |
|
---|
[3443] | 156 | (defgeneric (setf monom-elt) (new-value m index)
|
---|
[3550] | 157 | (:documentation "Set the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 158 | (:method (new-value (m monom) index)
|
---|
| 159 | (with-slots (exponents)
|
---|
| 160 | m
|
---|
[3453] | 161 | (setf (elt exponents index) new-value))))
|
---|
[2023] | 162 |
|
---|
[3551] | 163 | (defgeneric total-degree (m &optional start end)
|
---|
| 164 | (:documentation "Return the total degree of a monomoal M. Optinally, a range
|
---|
[3449] | 165 | of variables may be specified with arguments START and END.")
|
---|
| 166 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 167 | (declare (type fixnum start end))
|
---|
| 168 | (with-slots (exponents)
|
---|
| 169 | m
|
---|
| 170 | (reduce #'+ exponents :start start :end end))))
|
---|
[48] | 171 |
|
---|
[3552] | 172 | (defgeneric sugar (m &optional start end)
|
---|
[3446] | 173 | (:documentation "Return the sugar of a monomial M. Optinally, a range
|
---|
| 174 | of variables may be specified with arguments START and END.")
|
---|
| 175 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 176 | (declare (type fixnum start end))
|
---|
[3552] | 177 | (total-degree m start end)))
|
---|
[48] | 178 |
|
---|
[3553] | 179 | (defgeneric multiply-by (self other)
|
---|
[3549] | 180 | (:documentation "Multiply SELF by OTHER, return SELF.")
|
---|
[3446] | 181 | (:method ((self monom) (other monom))
|
---|
| 182 | (with-slots ((exponents1 exponents))
|
---|
| 183 | self
|
---|
| 184 | (with-slots ((exponents2 exponents))
|
---|
| 185 | other
|
---|
| 186 | (unless (= (length exponents1) (length exponents2))
|
---|
| 187 | (error "Incompatible dimensions"))
|
---|
| 188 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
| 189 | self))
|
---|
[2069] | 190 |
|
---|
[3553] | 191 | (defgeneric divide-by (self other)
|
---|
[3544] | 192 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
[3446] | 193 | (:method ((self monom) (other monom))
|
---|
| 194 | (with-slots ((exponents1 exponents))
|
---|
| 195 | self
|
---|
| 196 | (with-slots ((exponents2 exponents))
|
---|
| 197 | other
|
---|
| 198 | (unless (= (length exponents1) (length exponents2))
|
---|
| 199 | (error "divide-by: Incompatible dimensions."))
|
---|
| 200 | (unless (every #'>= exponents1 exponents2)
|
---|
| 201 | (error "divide-by: Negative power would result."))
|
---|
| 202 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
| 203 | self))
|
---|
[2818] | 204 |
|
---|
[3448] | 205 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
| 206 | "An :AROUND method of COPY-INSTANCE. It replaces
|
---|
| 207 | exponents with a fresh copy of the sequence."
|
---|
[3446] | 208 | (declare (ignore object initargs))
|
---|
| 209 | (let ((copy (call-next-method)))
|
---|
| 210 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
[3453] | 211 | copy))
|
---|
[2950] | 212 |
|
---|
[3560] | 213 | (defun multiply-2 (object1 object2)
|
---|
[3559] | 214 | "Multiply OBJECT1 by OBJECT2"
|
---|
| 215 | (multiply-by (copy-instance object1) (copy-instance object2)))
|
---|
[2816] | 216 |
|
---|
[3557] | 217 | (defun multiply (&rest factors)
|
---|
| 218 | "Non-destructively multiply list FACTORS."
|
---|
| 219 | (reduce #'multiply-2 factors))
|
---|
[3554] | 220 |
|
---|
[3557] | 221 | (defun divide (numerator &rest denominators)
|
---|
| 222 | "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
|
---|
[3558] | 223 | (divide-by (copy-instance numerator) (multiply denominators)))
|
---|
[48] | 224 |
|
---|
[3591] | 225 | (defgeneric divides-p (object1 object2)
|
---|
| 226 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
| 227 | (:method ((m1 monom) (m2 monom))
|
---|
| 228 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
| 229 | (with-slots ((exponents1 exponents))
|
---|
| 230 | m1
|
---|
| 231 | (with-slots ((exponents2 exponents))
|
---|
| 232 | m2
|
---|
| 233 | (every #'<= exponents1 exponents2)))))
|
---|
[48] | 234 |
|
---|
[3585] | 235 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
[3594] | 236 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
[3585] | 237 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
| 238 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[3596] | 239 | (with-slots ((exponents1 exponents))
|
---|
| 240 | m1
|
---|
| 241 | (with-slots ((exponents2 exponents))
|
---|
| 242 | m2
|
---|
| 243 | (with-slots ((exponents3 exponents))
|
---|
| 244 | m3
|
---|
| 245 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
| 246 | exponents1 exponents2 exponents3))))))
|
---|
[48] | 247 |
|
---|
[3588] | 248 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
| 249 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 250 | "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
|
---|
| 251 | (with-slots ((exponents1 exponents))
|
---|
| 252 | m1
|
---|
| 253 | (with-slots ((exponents2 exponents))
|
---|
| 254 | m2
|
---|
| 255 | (with-slots ((exponents3 exponents))
|
---|
| 256 | m3
|
---|
| 257 | (with-slots ((exponents4 exponents))
|
---|
| 258 | m4
|
---|
| 259 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
[3590] | 260 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[869] | 261 |
|
---|
[3589] | 262 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
| 263 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 264 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
| 265 | (with-slots ((exponents1 exponents))
|
---|
| 266 | m1
|
---|
| 267 | (with-slots ((exponents2 exponents))
|
---|
| 268 | m2
|
---|
| 269 | (with-slots ((exponents3 exponents))
|
---|
| 270 | m3
|
---|
| 271 | (with-slots ((exponents4 exponents))
|
---|
| 272 | m4
|
---|
| 273 | (every
|
---|
| 274 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 275 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[48] | 276 |
|
---|
[3563] | 277 | (defgeneric divisible-by-p (object1 object2)
|
---|
| 278 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
| 279 | (:method ((m1 monom) (m2 monom))
|
---|
| 280 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
| 281 | (with-slots ((exponents1 exponents))
|
---|
| 282 | m1
|
---|
| 283 | (with-slots ((exponents2 exponents))
|
---|
| 284 | m2
|
---|
| 285 | (every #'>= exponents1 exponents2)))))
|
---|
[2078] | 286 |
|
---|
[3565] | 287 | (defgeneric rel-prime-p (object1 object2)
|
---|
[3575] | 288 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
[3563] | 289 | (:method ((m1 monom) (m2 monom))
|
---|
| 290 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
| 291 | (with-slots ((exponents1 exponents))
|
---|
| 292 | m1
|
---|
| 293 | (with-slots ((exponents2 exponents))
|
---|
| 294 | m2
|
---|
| 295 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
[48] | 296 |
|
---|
[3595] | 297 | (defgeneric universal-lcm (object1 object2)
|
---|
[3566] | 298 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
| 299 | (:method ((m1 monom) (m2 monom))
|
---|
| 300 | "Returns least common multiple of monomials M1 and M2."
|
---|
| 301 | (with-slots ((exponents1 exponents))
|
---|
| 302 | m1
|
---|
| 303 | (with-slots ((exponents2 exponents))
|
---|
| 304 | m2
|
---|
| 305 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 306 | (map-into exponents #'max exponents1 exponents2)
|
---|
| 307 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 308 |
|
---|
[2080] | 309 |
|
---|
[3567] | 310 | (defgeneric universal-gcd (object1 object2)
|
---|
| 311 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
| 312 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 313 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 314 | (with-slots ((exponents1 exponents))
|
---|
| 315 | m1
|
---|
| 316 | (with-slots ((exponents2 exponents))
|
---|
| 317 | m2
|
---|
| 318 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 319 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 320 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 321 |
|
---|
[3569] | 322 | (defgeneric depends-p (object k)
|
---|
| 323 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 324 | (:method ((m monom) k)
|
---|
| 325 | "Return T if the monomial M depends on variable number K."
|
---|
| 326 | (declare (type fixnum k))
|
---|
| 327 | (with-slots (exponents)
|
---|
| 328 | m
|
---|
| 329 | (plusp (elt exponents k)))))
|
---|
[48] | 330 |
|
---|
[3570] | 331 | (defgeneric left-tensor-product-by (self other)
|
---|
| 332 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 333 | SELF. Return SELF.")
|
---|
| 334 | (:method ((self monom) (other monom))
|
---|
| 335 | (with-slots ((exponents1 exponents))
|
---|
| 336 | self
|
---|
| 337 | (with-slots ((exponents2 exponents))
|
---|
| 338 | other
|
---|
| 339 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 340 | self))
|
---|
[48] | 341 |
|
---|
[3570] | 342 | (defgeneric right-tensor-product-by (self other)
|
---|
| 343 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 344 | into SELF. Returns SELF.")
|
---|
| 345 | (:method ((self monom) (other monom))
|
---|
| 346 | (with-slots ((exponents1 exponents))
|
---|
| 347 | self
|
---|
| 348 | (with-slots ((exponents2 exponents))
|
---|
| 349 | other
|
---|
| 350 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 351 | self))
|
---|
[3026] | 352 |
|
---|
[3571] | 353 | (defgeneric left-contract (self k)
|
---|
| 354 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 355 | (:method ((self monom) k)
|
---|
| 356 | "Drop the first K variables in monomial M."
|
---|
| 357 | (declare (fixnum k))
|
---|
| 358 | (with-slots (exponents)
|
---|
| 359 | self
|
---|
| 360 | (setf exponents (subseq exponents k)))
|
---|
| 361 | self))
|
---|
[886] | 362 |
|
---|
| 363 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 364 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 365 | "Construct a monomial in the polynomial ring
|
---|
| 366 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 367 | which represents a single variable. It assumes number of variables
|
---|
| 368 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 369 | may appear raised to power POWER. "
|
---|
[1924] | 370 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 371 | (with-slots (exponents)
|
---|
| 372 | m
|
---|
[2154] | 373 | (setf (elt exponents pos) power)
|
---|
[2089] | 374 | m))
|
---|
[1151] | 375 |
|
---|
[3605] | 376 | (defgeneric ->list (object)
|
---|
| 377 | (:method ((m monom))
|
---|
| 378 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
| 379 | (coerce (monom-exponents m) 'list)))
|
---|
[3472] | 380 |
|
---|
[3474] | 381 | ;; pure lexicographic
|
---|
[3472] | 382 | (defgeneric lex> (p q &optional start end)
|
---|
| 383 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 384 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 385 | otherwise it is NIL.")
|
---|
[3483] | 386 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 387 | (declare (type fixnum start end))
|
---|
| 388 | (do ((i start (1+ i)))
|
---|
| 389 | ((>= i end) (values nil t))
|
---|
| 390 | (cond
|
---|
[3483] | 391 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 392 | (return-from lex> (values t nil)))
|
---|
[3483] | 393 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 394 | (return-from lex> (values nil nil)))))))
|
---|
| 395 |
|
---|
[3475] | 396 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 397 | (defgeneric grlex> (p q &optional start end)
|
---|
| 398 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 399 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 400 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 401 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 402 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 403 | (let ((d1 (total-degree p start end))
|
---|
| 404 | (d2 (total-degree q start end)))
|
---|
[3472] | 405 | (declare (type fixnum d1 d2))
|
---|
| 406 | (cond
|
---|
| 407 | ((> d1 d2) (values t nil))
|
---|
| 408 | ((< d1 d2) (values nil nil))
|
---|
| 409 | (t
|
---|
| 410 | (lex> p q start end))))))
|
---|
| 411 |
|
---|
| 412 | ;; reverse lexicographic
|
---|
| 413 | (defgeneric revlex> (p q &optional start end)
|
---|
| 414 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 415 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 416 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 417 | because some sets do not have a minimal element. This order is useful
|
---|
| 418 | in constructing other orders.")
|
---|
[3483] | 419 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 420 | (declare (type fixnum start end))
|
---|
| 421 | (do ((i (1- end) (1- i)))
|
---|
| 422 | ((< i start) (values nil t))
|
---|
| 423 | (declare (type fixnum i))
|
---|
| 424 | (cond
|
---|
[3483] | 425 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 426 | (return-from revlex> (values t nil)))
|
---|
[3483] | 427 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 428 | (return-from revlex> (values nil nil)))))))
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 432 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 433 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 434 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 435 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 436 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 437 | (declare (type fixnum start end))
|
---|
[3584] | 438 | (let ((d1 (total-degree p start end))
|
---|
| 439 | (d2 (total-degree q start end)))
|
---|
[3472] | 440 | (declare (type fixnum d1 d2))
|
---|
| 441 | (cond
|
---|
| 442 | ((> d1 d2) (values t nil))
|
---|
| 443 | ((< d1 d2) (values nil nil))
|
---|
| 444 | (t
|
---|
| 445 | (revlex> p q start end))))))
|
---|
| 446 |
|
---|
| 447 | (defgeneric invlex> (p q &optional start end)
|
---|
| 448 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 449 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 450 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 451 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 452 | (declare (type fixnum start end))
|
---|
| 453 | (do ((i (1- end) (1- i)))
|
---|
| 454 | ((< i start) (values nil t))
|
---|
| 455 | (declare (type fixnum i))
|
---|
| 456 | (cond
|
---|
[3483] | 457 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 458 | (return-from invlex> (values t nil)))
|
---|
[3483] | 459 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 460 | (return-from invlex> (values nil nil)))))))
|
---|
| 461 |
|
---|
| 462 | (defun reverse-monomial-order (order)
|
---|
| 463 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 464 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 465 | (declare (type monom p q) (type fixnum start end))
|
---|
| 466 | (funcall order q p start end)))
|
---|
| 467 |
|
---|
| 468 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 469 | ;;
|
---|
| 470 | ;; Order making functions
|
---|
| 471 | ;;
|
---|
| 472 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 473 |
|
---|
| 474 | ;; This returns a closure with the same signature
|
---|
| 475 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 476 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 477 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 478 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 479 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 480 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 481 | (declare (type monom p q) (type fixnum start end))
|
---|
| 482 | (cond
|
---|
[3483] | 483 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 484 | (values t nil))
|
---|
[3483] | 485 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 486 | (values nil nil))
|
---|
| 487 | (t
|
---|
| 488 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 489 |
|
---|
| 490 | ;; This returns a closure which is called with an integer argument.
|
---|
| 491 | ;; The result is *another closure* with the same signature as all
|
---|
| 492 | ;; orders such as #'LEX>.
|
---|
[3486] | 493 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 494 | (primary-elimination-order #'lex>)
|
---|
| 495 | (secondary-elimination-order #'lex>))
|
---|
| 496 | "Return a function with a single integer argument K. This should be
|
---|
| 497 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 498 | remaining variables. The call to the closure creates a predicate
|
---|
| 499 | which compares monomials according to the K-th elimination order. The
|
---|
| 500 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 501 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 502 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 503 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 504 | which indicates that the first K variables appear with identical
|
---|
| 505 | powers, then the result is that of a call to
|
---|
| 506 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 507 | X[K],X[K+1],..."
|
---|
| 508 | #'(lambda (k)
|
---|
| 509 | (cond
|
---|
| 510 | ((<= k 0)
|
---|
| 511 | (error "K must be at least 1"))
|
---|
| 512 | ((= k 1)
|
---|
[3485] | 513 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 514 | (t
|
---|
[3483] | 515 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 516 | (declare (type monom p q) (type fixnum start end))
|
---|
| 517 | (multiple-value-bind (primary equal)
|
---|
| 518 | (funcall primary-elimination-order p q start k)
|
---|
| 519 | (if equal
|
---|
| 520 | (funcall secondary-elimination-order p q k end)
|
---|
| 521 | (values primary nil))))))))
|
---|
| 522 |
|
---|
[3531] | 523 | (defclass term (monom)
|
---|
| 524 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 525 | (:default-initargs :coeff nil)
|
---|
| 526 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 527 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 528 |
|
---|
| 529 | (defmethod print-object ((self term) stream)
|
---|
| 530 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 531 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 532 | (coeff term-coeff))
|
---|
[3531] | 533 | self
|
---|
| 534 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 535 | exponents coeff))))
|
---|
| 536 |
|
---|
[3542] | 537 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 538 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 539 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 540 | (and (call-next-method)
|
---|
| 541 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 542 |
|
---|
[3533] | 543 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
| 544 | (setf (term-coeff new) 1))
|
---|
[3531] | 545 |
|
---|
[3556] | 546 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 547 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 548 | It returns SELF."
|
---|
[3580] | 549 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 550 |
|
---|
[3581] | 551 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 552 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 553 |
|
---|
[3581] | 554 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 555 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 556 |
|
---|
[3556] | 557 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 558 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 559 |
|
---|
[3582] | 560 | (defgeneric unary-minus (self)
|
---|
| 561 | (:method ((self term))
|
---|
| 562 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 563 | self))
|
---|
[3531] | 564 |
|
---|
[3578] | 565 | (defgeneric universal-zerop (self)
|
---|
| 566 | (:method ((self term))
|
---|
| 567 | (universal-zerop (term-coeff self))))
|
---|