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