[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")
|
---|
[3681] | 317 | (:method ((object1 number) (object2 number)) (gcd object1 object2))
|
---|
[3567] | 318 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 319 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 320 | (with-slots ((exponents1 exponents))
|
---|
| 321 | m1
|
---|
| 322 | (with-slots ((exponents2 exponents))
|
---|
| 323 | m2
|
---|
| 324 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 325 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 326 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 327 |
|
---|
[3569] | 328 | (defgeneric depends-p (object k)
|
---|
| 329 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 330 | (:method ((m monom) k)
|
---|
| 331 | "Return T if the monomial M depends on variable number K."
|
---|
| 332 | (declare (type fixnum k))
|
---|
| 333 | (with-slots (exponents)
|
---|
| 334 | m
|
---|
| 335 | (plusp (elt exponents k)))))
|
---|
[48] | 336 |
|
---|
[3570] | 337 | (defgeneric left-tensor-product-by (self other)
|
---|
| 338 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 339 | SELF. Return SELF.")
|
---|
| 340 | (:method ((self monom) (other monom))
|
---|
| 341 | (with-slots ((exponents1 exponents))
|
---|
| 342 | self
|
---|
| 343 | (with-slots ((exponents2 exponents))
|
---|
| 344 | other
|
---|
| 345 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 346 | self))
|
---|
[48] | 347 |
|
---|
[3570] | 348 | (defgeneric right-tensor-product-by (self other)
|
---|
| 349 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 350 | into SELF. Returns SELF.")
|
---|
| 351 | (:method ((self monom) (other monom))
|
---|
| 352 | (with-slots ((exponents1 exponents))
|
---|
| 353 | self
|
---|
| 354 | (with-slots ((exponents2 exponents))
|
---|
| 355 | other
|
---|
| 356 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 357 | self))
|
---|
[3026] | 358 |
|
---|
[3571] | 359 | (defgeneric left-contract (self k)
|
---|
| 360 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 361 | (:method ((self monom) k)
|
---|
| 362 | "Drop the first K variables in monomial M."
|
---|
| 363 | (declare (fixnum k))
|
---|
| 364 | (with-slots (exponents)
|
---|
| 365 | self
|
---|
| 366 | (setf exponents (subseq exponents k)))
|
---|
| 367 | self))
|
---|
[886] | 368 |
|
---|
| 369 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 370 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 371 | "Construct a monomial in the polynomial ring
|
---|
| 372 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 373 | which represents a single variable. It assumes number of variables
|
---|
| 374 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 375 | may appear raised to power POWER. "
|
---|
[1924] | 376 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 377 | (with-slots (exponents)
|
---|
| 378 | m
|
---|
[2154] | 379 | (setf (elt exponents pos) power)
|
---|
[2089] | 380 | m))
|
---|
[1151] | 381 |
|
---|
[3605] | 382 | (defgeneric ->list (object)
|
---|
| 383 | (:method ((m monom))
|
---|
| 384 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
| 385 | (coerce (monom-exponents m) 'list)))
|
---|
[3472] | 386 |
|
---|
[3474] | 387 | ;; pure lexicographic
|
---|
[3472] | 388 | (defgeneric lex> (p q &optional start end)
|
---|
| 389 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 390 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 391 | otherwise it is NIL.")
|
---|
[3483] | 392 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 393 | (declare (type fixnum start end))
|
---|
| 394 | (do ((i start (1+ i)))
|
---|
| 395 | ((>= i end) (values nil t))
|
---|
| 396 | (cond
|
---|
[3483] | 397 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 398 | (return-from lex> (values t nil)))
|
---|
[3483] | 399 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 400 | (return-from lex> (values nil nil)))))))
|
---|
| 401 |
|
---|
[3475] | 402 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 403 | (defgeneric grlex> (p q &optional start end)
|
---|
| 404 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 405 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 406 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 407 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 408 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 409 | (let ((d1 (total-degree p start end))
|
---|
| 410 | (d2 (total-degree q start end)))
|
---|
[3472] | 411 | (declare (type fixnum d1 d2))
|
---|
| 412 | (cond
|
---|
| 413 | ((> d1 d2) (values t nil))
|
---|
| 414 | ((< d1 d2) (values nil nil))
|
---|
| 415 | (t
|
---|
| 416 | (lex> p q start end))))))
|
---|
| 417 |
|
---|
| 418 | ;; reverse lexicographic
|
---|
| 419 | (defgeneric revlex> (p q &optional start end)
|
---|
| 420 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 421 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 422 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 423 | because some sets do not have a minimal element. This order is useful
|
---|
| 424 | in constructing other orders.")
|
---|
[3483] | 425 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 426 | (declare (type fixnum start end))
|
---|
| 427 | (do ((i (1- end) (1- i)))
|
---|
| 428 | ((< i start) (values nil t))
|
---|
| 429 | (declare (type fixnum i))
|
---|
| 430 | (cond
|
---|
[3483] | 431 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 432 | (return-from revlex> (values t nil)))
|
---|
[3483] | 433 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 434 | (return-from revlex> (values nil nil)))))))
|
---|
| 435 |
|
---|
| 436 |
|
---|
| 437 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 438 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 439 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 440 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 441 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 442 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 443 | (declare (type fixnum start end))
|
---|
[3584] | 444 | (let ((d1 (total-degree p start end))
|
---|
| 445 | (d2 (total-degree q start end)))
|
---|
[3472] | 446 | (declare (type fixnum d1 d2))
|
---|
| 447 | (cond
|
---|
| 448 | ((> d1 d2) (values t nil))
|
---|
| 449 | ((< d1 d2) (values nil nil))
|
---|
| 450 | (t
|
---|
| 451 | (revlex> p q start end))))))
|
---|
| 452 |
|
---|
| 453 | (defgeneric invlex> (p q &optional start end)
|
---|
| 454 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 455 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 456 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 457 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 458 | (declare (type fixnum start end))
|
---|
| 459 | (do ((i (1- end) (1- i)))
|
---|
| 460 | ((< i start) (values nil t))
|
---|
| 461 | (declare (type fixnum i))
|
---|
| 462 | (cond
|
---|
[3483] | 463 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 464 | (return-from invlex> (values t nil)))
|
---|
[3483] | 465 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 466 | (return-from invlex> (values nil nil)))))))
|
---|
| 467 |
|
---|
| 468 | (defun reverse-monomial-order (order)
|
---|
| 469 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 470 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 471 | (declare (type monom p q) (type fixnum start end))
|
---|
| 472 | (funcall order q p start end)))
|
---|
| 473 |
|
---|
| 474 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 475 | ;;
|
---|
| 476 | ;; Order making functions
|
---|
| 477 | ;;
|
---|
| 478 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 479 |
|
---|
| 480 | ;; This returns a closure with the same signature
|
---|
| 481 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 482 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 483 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 484 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 485 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 486 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 487 | (declare (type monom p q) (type fixnum start end))
|
---|
| 488 | (cond
|
---|
[3483] | 489 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 490 | (values t nil))
|
---|
[3483] | 491 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 492 | (values nil nil))
|
---|
| 493 | (t
|
---|
| 494 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 495 |
|
---|
| 496 | ;; This returns a closure which is called with an integer argument.
|
---|
| 497 | ;; The result is *another closure* with the same signature as all
|
---|
| 498 | ;; orders such as #'LEX>.
|
---|
[3486] | 499 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 500 | (primary-elimination-order #'lex>)
|
---|
| 501 | (secondary-elimination-order #'lex>))
|
---|
| 502 | "Return a function with a single integer argument K. This should be
|
---|
| 503 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 504 | remaining variables. The call to the closure creates a predicate
|
---|
| 505 | which compares monomials according to the K-th elimination order. The
|
---|
| 506 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 507 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 508 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 509 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 510 | which indicates that the first K variables appear with identical
|
---|
| 511 | powers, then the result is that of a call to
|
---|
| 512 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 513 | X[K],X[K+1],..."
|
---|
| 514 | #'(lambda (k)
|
---|
| 515 | (cond
|
---|
| 516 | ((<= k 0)
|
---|
| 517 | (error "K must be at least 1"))
|
---|
| 518 | ((= k 1)
|
---|
[3485] | 519 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 520 | (t
|
---|
[3483] | 521 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 522 | (declare (type monom p q) (type fixnum start end))
|
---|
| 523 | (multiple-value-bind (primary equal)
|
---|
| 524 | (funcall primary-elimination-order p q start k)
|
---|
| 525 | (if equal
|
---|
| 526 | (funcall secondary-elimination-order p q k end)
|
---|
| 527 | (values primary nil))))))))
|
---|
| 528 |
|
---|
[3531] | 529 | (defclass term (monom)
|
---|
| 530 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 531 | (:default-initargs :coeff nil)
|
---|
| 532 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 533 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 534 |
|
---|
| 535 | (defmethod print-object ((self term) stream)
|
---|
| 536 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 537 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 538 | (coeff term-coeff))
|
---|
[3531] | 539 | self
|
---|
| 540 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 541 | exponents coeff))))
|
---|
| 542 |
|
---|
[3542] | 543 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 544 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 545 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 546 | (and (call-next-method)
|
---|
| 547 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 548 |
|
---|
[3533] | 549 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key)
|
---|
| 550 | (setf (term-coeff new) 1))
|
---|
[3531] | 551 |
|
---|
[3556] | 552 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 553 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 554 | It returns SELF."
|
---|
[3580] | 555 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 556 |
|
---|
[3581] | 557 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 558 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 559 |
|
---|
[3581] | 560 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 561 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 562 |
|
---|
[3556] | 563 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 564 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 565 |
|
---|
[3582] | 566 | (defgeneric unary-minus (self)
|
---|
[3615] | 567 | (:documentation "Negate object SELF and return it.")
|
---|
| 568 | (:method ((self number)) (- self))
|
---|
[3582] | 569 | (:method ((self term))
|
---|
| 570 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 571 | self))
|
---|
[3531] | 572 |
|
---|
[3578] | 573 | (defgeneric universal-zerop (self)
|
---|
[3617] | 574 | (:documentation "Return T iff SELF is zero.")
|
---|
[3618] | 575 | (:method ((self number)) (zerop self))
|
---|
[3578] | 576 | (:method ((self term))
|
---|
| 577 | (universal-zerop (term-coeff self))))
|
---|