[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 |
|
---|
[3802] | 82 | (proclaim '(optimize (speed 0) (space 0) (safety 3) (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 |
|
---|
[3788] | 114 | (MAKE-INSTANCE 'MONOM :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
| 115 | (MAKE-INSTANCE 'MONOM :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
| 116 | (MAKE-INSTANCE 'MONOM :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)))
|
---|
[3798] | 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."
|
---|
[3800] | 224 | (cond ((endp factors) 1)
|
---|
| 225 | ((endp (rest factors)) (first factors))
|
---|
| 226 | (t (reduce #'multiply-2 factors :initial-value 1))))
|
---|
[3554] | 227 |
|
---|
[3557] | 228 | (defun divide (numerator &rest denominators)
|
---|
| 229 | "Non-destructively divide object NUMERATOR by product of DENOMINATORS."
|
---|
[3614] | 230 | (divide-by (copy-instance numerator) (apply #'multiply denominators)))
|
---|
[48] | 231 |
|
---|
[3591] | 232 | (defgeneric divides-p (object1 object2)
|
---|
| 233 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
| 234 | (:method ((m1 monom) (m2 monom))
|
---|
| 235 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
| 236 | (with-slots ((exponents1 exponents))
|
---|
| 237 | m1
|
---|
| 238 | (with-slots ((exponents2 exponents))
|
---|
| 239 | m2
|
---|
| 240 | (every #'<= exponents1 exponents2)))))
|
---|
[48] | 241 |
|
---|
[3585] | 242 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
[3594] | 243 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
[3585] | 244 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
| 245 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[3596] | 246 | (with-slots ((exponents1 exponents))
|
---|
| 247 | m1
|
---|
| 248 | (with-slots ((exponents2 exponents))
|
---|
| 249 | m2
|
---|
| 250 | (with-slots ((exponents3 exponents))
|
---|
| 251 | m3
|
---|
| 252 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
| 253 | exponents1 exponents2 exponents3))))))
|
---|
[48] | 254 |
|
---|
[3588] | 255 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
| 256 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 257 | "Returns T if monomial LCM(M1,M2) divides LCM(M3,M4), NIL otherwise."
|
---|
| 258 | (with-slots ((exponents1 exponents))
|
---|
| 259 | m1
|
---|
| 260 | (with-slots ((exponents2 exponents))
|
---|
| 261 | m2
|
---|
| 262 | (with-slots ((exponents3 exponents))
|
---|
| 263 | m3
|
---|
| 264 | (with-slots ((exponents4 exponents))
|
---|
| 265 | m4
|
---|
| 266 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
[3590] | 267 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[869] | 268 |
|
---|
[3589] | 269 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
| 270 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 271 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
| 272 | (with-slots ((exponents1 exponents))
|
---|
| 273 | m1
|
---|
| 274 | (with-slots ((exponents2 exponents))
|
---|
| 275 | m2
|
---|
| 276 | (with-slots ((exponents3 exponents))
|
---|
| 277 | m3
|
---|
| 278 | (with-slots ((exponents4 exponents))
|
---|
| 279 | m4
|
---|
| 280 | (every
|
---|
| 281 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 282 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[48] | 283 |
|
---|
[3563] | 284 | (defgeneric divisible-by-p (object1 object2)
|
---|
| 285 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
| 286 | (:method ((m1 monom) (m2 monom))
|
---|
| 287 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
| 288 | (with-slots ((exponents1 exponents))
|
---|
| 289 | m1
|
---|
| 290 | (with-slots ((exponents2 exponents))
|
---|
| 291 | m2
|
---|
| 292 | (every #'>= exponents1 exponents2)))))
|
---|
[2078] | 293 |
|
---|
[3565] | 294 | (defgeneric rel-prime-p (object1 object2)
|
---|
[3575] | 295 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
[3563] | 296 | (:method ((m1 monom) (m2 monom))
|
---|
| 297 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
| 298 | (with-slots ((exponents1 exponents))
|
---|
| 299 | m1
|
---|
| 300 | (with-slots ((exponents2 exponents))
|
---|
| 301 | m2
|
---|
| 302 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
[48] | 303 |
|
---|
[3595] | 304 | (defgeneric universal-lcm (object1 object2)
|
---|
[3566] | 305 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
| 306 | (:method ((m1 monom) (m2 monom))
|
---|
| 307 | "Returns least common multiple of monomials M1 and M2."
|
---|
| 308 | (with-slots ((exponents1 exponents))
|
---|
| 309 | m1
|
---|
| 310 | (with-slots ((exponents2 exponents))
|
---|
| 311 | m2
|
---|
| 312 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 313 | (map-into exponents #'max exponents1 exponents2)
|
---|
| 314 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 315 |
|
---|
[2080] | 316 |
|
---|
[3567] | 317 | (defgeneric universal-gcd (object1 object2)
|
---|
| 318 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
[3681] | 319 | (:method ((object1 number) (object2 number)) (gcd object1 object2))
|
---|
[3567] | 320 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 321 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 322 | (with-slots ((exponents1 exponents))
|
---|
| 323 | m1
|
---|
| 324 | (with-slots ((exponents2 exponents))
|
---|
| 325 | m2
|
---|
| 326 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 327 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 328 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 329 |
|
---|
[3569] | 330 | (defgeneric depends-p (object k)
|
---|
| 331 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 332 | (:method ((m monom) k)
|
---|
| 333 | "Return T if the monomial M depends on variable number K."
|
---|
| 334 | (declare (type fixnum k))
|
---|
| 335 | (with-slots (exponents)
|
---|
| 336 | m
|
---|
| 337 | (plusp (elt exponents k)))))
|
---|
[48] | 338 |
|
---|
[3570] | 339 | (defgeneric left-tensor-product-by (self other)
|
---|
| 340 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 341 | SELF. Return SELF.")
|
---|
| 342 | (:method ((self monom) (other monom))
|
---|
| 343 | (with-slots ((exponents1 exponents))
|
---|
| 344 | self
|
---|
| 345 | (with-slots ((exponents2 exponents))
|
---|
| 346 | other
|
---|
| 347 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 348 | self))
|
---|
[48] | 349 |
|
---|
[3570] | 350 | (defgeneric right-tensor-product-by (self other)
|
---|
| 351 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 352 | into SELF. Returns SELF.")
|
---|
| 353 | (:method ((self monom) (other monom))
|
---|
| 354 | (with-slots ((exponents1 exponents))
|
---|
| 355 | self
|
---|
| 356 | (with-slots ((exponents2 exponents))
|
---|
| 357 | other
|
---|
| 358 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 359 | self))
|
---|
[3026] | 360 |
|
---|
[3571] | 361 | (defgeneric left-contract (self k)
|
---|
| 362 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 363 | (:method ((self monom) k)
|
---|
| 364 | "Drop the first K variables in monomial M."
|
---|
| 365 | (declare (fixnum k))
|
---|
| 366 | (with-slots (exponents)
|
---|
| 367 | self
|
---|
| 368 | (setf exponents (subseq exponents k)))
|
---|
| 369 | self))
|
---|
[886] | 370 |
|
---|
| 371 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 372 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 373 | "Construct a monomial in the polynomial ring
|
---|
| 374 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 375 | which represents a single variable. It assumes number of variables
|
---|
| 376 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 377 | may appear raised to power POWER. "
|
---|
[1924] | 378 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 379 | (with-slots (exponents)
|
---|
| 380 | m
|
---|
[2154] | 381 | (setf (elt exponents pos) power)
|
---|
[2089] | 382 | m))
|
---|
[1151] | 383 |
|
---|
[3605] | 384 | (defgeneric ->list (object)
|
---|
| 385 | (:method ((m monom))
|
---|
| 386 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
| 387 | (coerce (monom-exponents m) 'list)))
|
---|
[3472] | 388 |
|
---|
[3474] | 389 | ;; pure lexicographic
|
---|
[3472] | 390 | (defgeneric lex> (p q &optional start end)
|
---|
| 391 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 392 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 393 | otherwise it is NIL.")
|
---|
[3483] | 394 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 395 | (declare (type fixnum start end))
|
---|
| 396 | (do ((i start (1+ i)))
|
---|
| 397 | ((>= i end) (values nil t))
|
---|
| 398 | (cond
|
---|
[3483] | 399 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 400 | (return-from lex> (values t nil)))
|
---|
[3483] | 401 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 402 | (return-from lex> (values nil nil)))))))
|
---|
| 403 |
|
---|
[3475] | 404 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 405 | (defgeneric grlex> (p q &optional start end)
|
---|
| 406 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 407 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 408 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 409 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 410 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 411 | (let ((d1 (total-degree p start end))
|
---|
| 412 | (d2 (total-degree q start end)))
|
---|
[3472] | 413 | (declare (type fixnum d1 d2))
|
---|
| 414 | (cond
|
---|
| 415 | ((> d1 d2) (values t nil))
|
---|
| 416 | ((< d1 d2) (values nil nil))
|
---|
| 417 | (t
|
---|
| 418 | (lex> p q start end))))))
|
---|
| 419 |
|
---|
| 420 | ;; reverse lexicographic
|
---|
| 421 | (defgeneric revlex> (p q &optional start end)
|
---|
| 422 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 423 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 424 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 425 | because some sets do not have a minimal element. This order is useful
|
---|
| 426 | in constructing other orders.")
|
---|
[3483] | 427 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 428 | (declare (type fixnum start end))
|
---|
| 429 | (do ((i (1- end) (1- i)))
|
---|
| 430 | ((< i start) (values nil t))
|
---|
| 431 | (declare (type fixnum i))
|
---|
| 432 | (cond
|
---|
[3483] | 433 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 434 | (return-from revlex> (values t nil)))
|
---|
[3483] | 435 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 436 | (return-from revlex> (values nil nil)))))))
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 440 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 441 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 442 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 443 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 444 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 445 | (declare (type fixnum start end))
|
---|
[3584] | 446 | (let ((d1 (total-degree p start end))
|
---|
| 447 | (d2 (total-degree q start end)))
|
---|
[3472] | 448 | (declare (type fixnum d1 d2))
|
---|
| 449 | (cond
|
---|
| 450 | ((> d1 d2) (values t nil))
|
---|
| 451 | ((< d1 d2) (values nil nil))
|
---|
| 452 | (t
|
---|
| 453 | (revlex> p q start end))))))
|
---|
| 454 |
|
---|
| 455 | (defgeneric invlex> (p q &optional start end)
|
---|
| 456 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 457 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 458 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 459 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 460 | (declare (type fixnum start end))
|
---|
| 461 | (do ((i (1- end) (1- i)))
|
---|
| 462 | ((< i start) (values nil t))
|
---|
| 463 | (declare (type fixnum i))
|
---|
| 464 | (cond
|
---|
[3483] | 465 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 466 | (return-from invlex> (values t nil)))
|
---|
[3483] | 467 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 468 | (return-from invlex> (values nil nil)))))))
|
---|
| 469 |
|
---|
| 470 | (defun reverse-monomial-order (order)
|
---|
| 471 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 472 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 473 | (declare (type monom p q) (type fixnum start end))
|
---|
| 474 | (funcall order q p start end)))
|
---|
| 475 |
|
---|
| 476 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 477 | ;;
|
---|
| 478 | ;; Order making functions
|
---|
| 479 | ;;
|
---|
| 480 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 481 |
|
---|
| 482 | ;; This returns a closure with the same signature
|
---|
| 483 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 484 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 485 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 486 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 487 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 488 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 489 | (declare (type monom p q) (type fixnum start end))
|
---|
| 490 | (cond
|
---|
[3483] | 491 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 492 | (values t nil))
|
---|
[3483] | 493 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 494 | (values nil nil))
|
---|
| 495 | (t
|
---|
| 496 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 497 |
|
---|
| 498 | ;; This returns a closure which is called with an integer argument.
|
---|
| 499 | ;; The result is *another closure* with the same signature as all
|
---|
| 500 | ;; orders such as #'LEX>.
|
---|
[3486] | 501 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 502 | (primary-elimination-order #'lex>)
|
---|
| 503 | (secondary-elimination-order #'lex>))
|
---|
| 504 | "Return a function with a single integer argument K. This should be
|
---|
| 505 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 506 | remaining variables. The call to the closure creates a predicate
|
---|
| 507 | which compares monomials according to the K-th elimination order. The
|
---|
| 508 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 509 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 510 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 511 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 512 | which indicates that the first K variables appear with identical
|
---|
| 513 | powers, then the result is that of a call to
|
---|
| 514 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 515 | X[K],X[K+1],..."
|
---|
| 516 | #'(lambda (k)
|
---|
| 517 | (cond
|
---|
| 518 | ((<= k 0)
|
---|
| 519 | (error "K must be at least 1"))
|
---|
| 520 | ((= k 1)
|
---|
[3485] | 521 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 522 | (t
|
---|
[3483] | 523 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 524 | (declare (type monom p q) (type fixnum start end))
|
---|
| 525 | (multiple-value-bind (primary equal)
|
---|
| 526 | (funcall primary-elimination-order p q start k)
|
---|
| 527 | (if equal
|
---|
| 528 | (funcall secondary-elimination-order p q k end)
|
---|
| 529 | (values primary nil))))))))
|
---|
| 530 |
|
---|
[3531] | 531 | (defclass term (monom)
|
---|
| 532 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 533 | (:default-initargs :coeff nil)
|
---|
| 534 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 535 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 536 |
|
---|
[3793] | 537 | (defmethod update-instance-for-different-class :before ((old monom) (new term) &key (coeff 1))
|
---|
[3794] | 538 | "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
[3792] | 539 | (reinitialize-instance new :coeff coeff))
|
---|
[3785] | 540 |
|
---|
[3531] | 541 | (defmethod print-object ((self term) stream)
|
---|
| 542 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 543 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 544 | (coeff term-coeff))
|
---|
[3531] | 545 | self
|
---|
| 546 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 547 | exponents coeff))))
|
---|
| 548 |
|
---|
[3542] | 549 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 550 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 551 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 552 | (and (call-next-method)
|
---|
| 553 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 554 |
|
---|
[3533] | 555 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
| 556 | (setf (term-coeff new) 1))
|
---|
[3531] | 557 |
|
---|
[3556] | 558 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 559 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 560 | It returns SELF."
|
---|
[3580] | 561 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 562 |
|
---|
[3581] | 563 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 564 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 565 |
|
---|
[3581] | 566 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 567 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 568 |
|
---|
[3556] | 569 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 570 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 571 |
|
---|
[3582] | 572 | (defgeneric unary-minus (self)
|
---|
[3615] | 573 | (:documentation "Negate object SELF and return it.")
|
---|
| 574 | (:method ((self number)) (- self))
|
---|
[3582] | 575 | (:method ((self term))
|
---|
| 576 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 577 | self))
|
---|
[3531] | 578 |
|
---|
[3578] | 579 | (defgeneric universal-zerop (self)
|
---|
[3617] | 580 | (:documentation "Return T iff SELF is zero.")
|
---|
[3618] | 581 | (:method ((self number)) (zerop self))
|
---|
[3578] | 582 | (:method ((self term))
|
---|
| 583 | (universal-zerop (term-coeff self))))
|
---|