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