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