[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"
|
---|
[4225] | 23 | (:use :cl :utils :copy :ring :integer-ring)
|
---|
[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"
|
---|
| 37 | "DIVIDES-P"
|
---|
| 38 | "DIVIDES-LCM-P"
|
---|
| 39 | "LCM-DIVIDES-LCM-P"
|
---|
| 40 | "LCM-EQUAL-LCM-P"
|
---|
| 41 | "DIVISIBLE-BY-P"
|
---|
| 42 | "REL-PRIME-P"
|
---|
| 43 | "UNIVERSAL-LCM"
|
---|
| 44 | "UNIVERSAL-GCD"
|
---|
| 45 | "DEPENDS-P"
|
---|
| 46 | "LEFT-TENSOR-PRODUCT-BY"
|
---|
| 47 | "RIGHT-TENSOR-PRODUCT-BY"
|
---|
| 48 | "LEFT-CONTRACT"
|
---|
[3442] | 49 | "MAKE-MONOM-VARIABLE"
|
---|
[3811] | 50 | "MAKE-MONOM-CONSTANT"
|
---|
[3812] | 51 | "MAKE-TERM-CONSTANT"
|
---|
[3610] | 52 | "->LIST"
|
---|
[4023] | 53 | "->SEXP"
|
---|
[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"
|
---|
[4031] | 63 | "UNARY-INVERSE"
|
---|
[3616] | 64 | "UNIVERSAL-ZEROP")
|
---|
[2524] | 65 | (:documentation
|
---|
[3477] | 66 | "This package implements basic operations on monomials, including
|
---|
| 67 | various monomial orders.
|
---|
| 68 |
|
---|
[2524] | 69 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 70 |
|
---|
[2524] | 71 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 72 |
|
---|
| 73 | However, lists may be implemented as other sequence types, so the
|
---|
| 74 | flexibility to change the representation should be maintained in the
|
---|
| 75 | code to use general operations on sequences whenever possible. The
|
---|
| 76 | optimization for the actual representation should be left to
|
---|
| 77 | declarations and the compiler.
|
---|
| 78 |
|
---|
| 79 | EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 80 |
|
---|
| 81 | Monom x*y^2 ---> (1 2) "))
|
---|
| 82 |
|
---|
[1610] | 83 | (in-package :monom)
|
---|
[48] | 84 |
|
---|
[3802] | 85 | (proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 0)))
|
---|
[1923] | 86 |
|
---|
[48] | 87 | (deftype exponent ()
|
---|
| 88 | "Type of exponent in a monomial."
|
---|
| 89 | 'fixnum)
|
---|
| 90 |
|
---|
[2022] | 91 | (defclass monom ()
|
---|
[3312] | 92 | ((exponents :initarg :exponents :accessor monom-exponents
|
---|
[3054] | 93 | :documentation "The powers of the variables."))
|
---|
[3289] | 94 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
| 95 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
[2779] | 96 | (:documentation
|
---|
| 97 | "Implements a monomial, i.e. a product of powers
|
---|
| 98 | of variables, like X*Y^2."))
|
---|
[880] | 99 |
|
---|
[2245] | 100 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 101 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3313] | 102 | (with-accessors ((exponents monom-exponents))
|
---|
[3216] | 103 | self
|
---|
[3313] | 104 | (format stream "EXPONENTS=~A"
|
---|
| 105 | exponents))))
|
---|
[2027] | 106 |
|
---|
[3299] | 107 | (defmethod initialize-instance :after ((self monom)
|
---|
[3297] | 108 | &key
|
---|
| 109 | (dimension 0 dimension-supplied-p)
|
---|
| 110 | (exponents nil exponents-supplied-p)
|
---|
[3318] | 111 | (exponent 0)
|
---|
[3297] | 112 | &allow-other-keys
|
---|
[2390] | 113 | )
|
---|
[3329] | 114 | "The following INITIALIZE-INSTANCE method allows instance initialization
|
---|
| 115 | of a MONOM in a style similar to MAKE-ARRAY, e.g.:
|
---|
[3328] | 116 |
|
---|
[3788] | 117 | (MAKE-INSTANCE 'MONOM :EXPONENTS '(1 2 3)) --> #<MONOM EXPONENTS=#(1 2 3)>
|
---|
| 118 | (MAKE-INSTANCE 'MONOM :DIMENSION 3) --> #<MONOM EXPONENTS=#(0 0 0)>
|
---|
| 119 | (MAKE-INSTANCE 'MONOM :DIMENSION 3 :EXPONENT 7) --> #<MONOM EXPONENTS=#(7 7 7)>
|
---|
[3329] | 120 |
|
---|
| 121 | If both DIMENSION and EXPONENTS are supplied, they must be compatible,
|
---|
| 122 | i.e. the length of EXPONENTS must be equal DIMENSION. If EXPONENTS
|
---|
| 123 | is not supplied, a monom with repeated value EXPONENT is created.
|
---|
| 124 | By default EXPONENT is 0, which results in a constant monomial.
|
---|
[3328] | 125 | "
|
---|
[3315] | 126 | (cond
|
---|
| 127 | (exponents-supplied-p
|
---|
[3327] | 128 | (when (and dimension-supplied-p
|
---|
| 129 | (/= dimension (length exponents)))
|
---|
| 130 | (error "EXPONENTS (~A) must have supplied length DIMENSION (~A)"
|
---|
| 131 | exponents dimension))
|
---|
[3315] | 132 | (let ((dim (length exponents)))
|
---|
| 133 | (setf (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
[3321] | 134 | (dimension-supplied-p
|
---|
[3315] | 135 | ;; when all exponents are to be identical
|
---|
[3321] | 136 | (setf (slot-value self 'exponents) (make-array (list dimension)
|
---|
| 137 | :initial-element exponent
|
---|
| 138 | :element-type 'exponent)))
|
---|
| 139 | (t
|
---|
| 140 | (error "Initarg DIMENSION or EXPONENTS must be supplied."))))
|
---|
[3293] | 141 |
|
---|
[3807] | 142 | (defgeneric monom-dimension (self)
|
---|
| 143 | (:method ((self monom))
|
---|
| 144 | (length (monom-exponents self))))
|
---|
[3317] | 145 |
|
---|
[4235] | 146 | (defmethod universal-equalp ((self monom) (other monom))
|
---|
| 147 | "Returns T iff monomials SELF and OTHER have identical EXPONENTS."
|
---|
[4236] | 148 | (equalp (monom-exponents self) (monom-exponents other)))
|
---|
[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)
|
---|
[4098] | 182 | (:documentation "Multiply SELF by OTHER, return SELF. The object SELF
|
---|
| 183 | may be destructively modified in the process, while OTHER should remain
|
---|
| 184 | unmodified.")
|
---|
[3612] | 185 | (:method ((self number) (other number)) (* self other))
|
---|
[3446] | 186 | (:method ((self monom) (other monom))
|
---|
| 187 | (with-slots ((exponents1 exponents))
|
---|
| 188 | self
|
---|
| 189 | (with-slots ((exponents2 exponents))
|
---|
| 190 | other
|
---|
| 191 | (unless (= (length exponents1) (length exponents2))
|
---|
| 192 | (error "Incompatible dimensions"))
|
---|
| 193 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
[3846] | 194 | self))
|
---|
[2069] | 195 |
|
---|
[4093] | 196 | (defun multiply (factor &rest more-factors)
|
---|
| 197 | "Successively multiplies factor FACTOR by the remaining arguments
|
---|
[4097] | 198 | MORE-FACTORS, using MULTIPLY-BY to multiply two factors. Thus
|
---|
| 199 | FACTOR may be destructively modified."
|
---|
[4099] | 200 | (reduce #'multiply-by more-factors :initial-value (copy-instance factor)))
|
---|
[4093] | 201 |
|
---|
[3553] | 202 | (defgeneric divide-by (self other)
|
---|
[3544] | 203 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
[3613] | 204 | (:method ((self number) (other number)) (/ self other))
|
---|
[3446] | 205 | (:method ((self monom) (other monom))
|
---|
| 206 | (with-slots ((exponents1 exponents))
|
---|
| 207 | self
|
---|
| 208 | (with-slots ((exponents2 exponents))
|
---|
| 209 | other
|
---|
| 210 | (unless (= (length exponents1) (length exponents2))
|
---|
| 211 | (error "divide-by: Incompatible dimensions."))
|
---|
| 212 | (unless (every #'>= exponents1 exponents2)
|
---|
| 213 | (error "divide-by: Negative power would result."))
|
---|
| 214 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
[3845] | 215 | self))
|
---|
[2818] | 216 |
|
---|
[3448] | 217 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
[4129] | 218 | "An :AROUND method of COPY-INSTANCE. It replaces exponents with a fresh copy of the sequence."
|
---|
[4130] | 219 | (declare (ignore object initargs))
|
---|
| 220 | (let ((copy (call-next-method)))
|
---|
| 221 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
| 222 | copy))
|
---|
[2950] | 223 |
|
---|
[4032] | 224 | (defgeneric unary-inverse (self)
|
---|
| 225 | (:documentation "Returns the unary inverse of SELF.")
|
---|
[4033] | 226 | (:method ((self number)) (/ self))
|
---|
| 227 | (:method :before ((self monom))
|
---|
[4037] | 228 | (assert (zerop (total-degree self))
|
---|
| 229 | nil
|
---|
| 230 | "Monom ~A must have total degree 0 to be invertible." self))
|
---|
[4033] | 231 | (:method ((self monom)) self))
|
---|
[4032] | 232 |
|
---|
[3849] | 233 | (defun divide (numerator &rest denominators)
|
---|
[4096] | 234 | "Successively divides NUMERATOR by elements of DENOMINATORS. The operation
|
---|
| 235 | destructively modifies NUMERATOR."
|
---|
[3849] | 236 | (cond ((endp denominators)
|
---|
[4030] | 237 | (unary-inverse numerator))
|
---|
[4100] | 238 | (t (reduce #'divide-by denominators :initial-value (copy-instance numerator)))))
|
---|
[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")
|
---|
| 327 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 328 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 329 | (with-slots ((exponents1 exponents))
|
---|
| 330 | m1
|
---|
| 331 | (with-slots ((exponents2 exponents))
|
---|
| 332 | m2
|
---|
| 333 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 334 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 335 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 336 |
|
---|
[3569] | 337 | (defgeneric depends-p (object k)
|
---|
| 338 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 339 | (:method ((m monom) k)
|
---|
| 340 | "Return T if the monomial M depends on variable number K."
|
---|
| 341 | (declare (type fixnum k))
|
---|
| 342 | (with-slots (exponents)
|
---|
| 343 | m
|
---|
| 344 | (plusp (elt exponents k)))))
|
---|
[48] | 345 |
|
---|
[3570] | 346 | (defgeneric left-tensor-product-by (self other)
|
---|
| 347 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 348 | SELF. Return 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 exponents2 exponents1))))
|
---|
| 355 | self))
|
---|
[48] | 356 |
|
---|
[3570] | 357 | (defgeneric right-tensor-product-by (self other)
|
---|
| 358 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 359 | into SELF. Returns SELF.")
|
---|
| 360 | (:method ((self monom) (other monom))
|
---|
| 361 | (with-slots ((exponents1 exponents))
|
---|
| 362 | self
|
---|
| 363 | (with-slots ((exponents2 exponents))
|
---|
| 364 | other
|
---|
| 365 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 366 | self))
|
---|
[3026] | 367 |
|
---|
[3571] | 368 | (defgeneric left-contract (self k)
|
---|
| 369 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 370 | (:method ((self monom) k)
|
---|
| 371 | "Drop the first K variables in monomial M."
|
---|
| 372 | (declare (fixnum k))
|
---|
| 373 | (with-slots (exponents)
|
---|
| 374 | self
|
---|
| 375 | (setf exponents (subseq exponents k)))
|
---|
| 376 | self))
|
---|
[886] | 377 |
|
---|
| 378 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 379 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 380 | "Construct a monomial in the polynomial ring
|
---|
| 381 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 382 | which represents a single variable. It assumes number of variables
|
---|
| 383 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 384 | may appear raised to power POWER. "
|
---|
[1924] | 385 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 386 | (with-slots (exponents)
|
---|
| 387 | m
|
---|
[2154] | 388 | (setf (elt exponents pos) power)
|
---|
[2089] | 389 | m))
|
---|
[1151] | 390 |
|
---|
[3811] | 391 | (defun make-monom-constant (dimension)
|
---|
| 392 | (make-instance 'monom :dimension dimension))
|
---|
| 393 |
|
---|
[3474] | 394 | ;; pure lexicographic
|
---|
[3472] | 395 | (defgeneric lex> (p q &optional start end)
|
---|
| 396 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 397 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 398 | otherwise it is NIL.")
|
---|
[3483] | 399 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 400 | (declare (type fixnum start end))
|
---|
| 401 | (do ((i start (1+ i)))
|
---|
| 402 | ((>= i end) (values nil t))
|
---|
| 403 | (cond
|
---|
[3483] | 404 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 405 | (return-from lex> (values t nil)))
|
---|
[3483] | 406 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 407 | (return-from lex> (values nil nil)))))))
|
---|
| 408 |
|
---|
[3475] | 409 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 410 | (defgeneric grlex> (p q &optional start end)
|
---|
| 411 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 412 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 413 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 414 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 415 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 416 | (let ((d1 (total-degree p start end))
|
---|
| 417 | (d2 (total-degree q start end)))
|
---|
[3472] | 418 | (declare (type fixnum d1 d2))
|
---|
| 419 | (cond
|
---|
| 420 | ((> d1 d2) (values t nil))
|
---|
| 421 | ((< d1 d2) (values nil nil))
|
---|
| 422 | (t
|
---|
| 423 | (lex> p q start end))))))
|
---|
| 424 |
|
---|
| 425 | ;; reverse lexicographic
|
---|
| 426 | (defgeneric revlex> (p q &optional start end)
|
---|
| 427 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 428 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 429 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 430 | because some sets do not have a minimal element. This order is useful
|
---|
| 431 | in constructing other orders.")
|
---|
[3483] | 432 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 433 | (declare (type fixnum start end))
|
---|
| 434 | (do ((i (1- end) (1- i)))
|
---|
| 435 | ((< i start) (values nil t))
|
---|
| 436 | (declare (type fixnum i))
|
---|
| 437 | (cond
|
---|
[3483] | 438 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 439 | (return-from revlex> (values t nil)))
|
---|
[3483] | 440 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 441 | (return-from revlex> (values nil nil)))))))
|
---|
| 442 |
|
---|
| 443 |
|
---|
| 444 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 445 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 446 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 447 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 448 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 449 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 450 | (declare (type fixnum start end))
|
---|
[3584] | 451 | (let ((d1 (total-degree p start end))
|
---|
| 452 | (d2 (total-degree q start end)))
|
---|
[3472] | 453 | (declare (type fixnum d1 d2))
|
---|
| 454 | (cond
|
---|
| 455 | ((> d1 d2) (values t nil))
|
---|
| 456 | ((< d1 d2) (values nil nil))
|
---|
| 457 | (t
|
---|
| 458 | (revlex> p q start end))))))
|
---|
| 459 |
|
---|
| 460 | (defgeneric invlex> (p q &optional start end)
|
---|
| 461 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 462 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 463 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 464 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 465 | (declare (type fixnum start end))
|
---|
| 466 | (do ((i (1- end) (1- i)))
|
---|
| 467 | ((< i start) (values nil t))
|
---|
| 468 | (declare (type fixnum i))
|
---|
| 469 | (cond
|
---|
[3483] | 470 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 471 | (return-from invlex> (values t nil)))
|
---|
[3483] | 472 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 473 | (return-from invlex> (values nil nil)))))))
|
---|
| 474 |
|
---|
| 475 | (defun reverse-monomial-order (order)
|
---|
| 476 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 477 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 478 | (declare (type monom p q) (type fixnum start end))
|
---|
| 479 | (funcall order q p start end)))
|
---|
| 480 |
|
---|
| 481 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 482 | ;;
|
---|
| 483 | ;; Order making functions
|
---|
| 484 | ;;
|
---|
| 485 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 486 |
|
---|
| 487 | ;; This returns a closure with the same signature
|
---|
| 488 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 489 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 490 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 491 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 492 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 493 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 494 | (declare (type monom p q) (type fixnum start end))
|
---|
| 495 | (cond
|
---|
[3483] | 496 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 497 | (values t nil))
|
---|
[3483] | 498 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 499 | (values nil nil))
|
---|
| 500 | (t
|
---|
| 501 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 502 |
|
---|
| 503 | ;; This returns a closure which is called with an integer argument.
|
---|
| 504 | ;; The result is *another closure* with the same signature as all
|
---|
| 505 | ;; orders such as #'LEX>.
|
---|
[3486] | 506 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 507 | (primary-elimination-order #'lex>)
|
---|
| 508 | (secondary-elimination-order #'lex>))
|
---|
| 509 | "Return a function with a single integer argument K. This should be
|
---|
| 510 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 511 | remaining variables. The call to the closure creates a predicate
|
---|
| 512 | which compares monomials according to the K-th elimination order. The
|
---|
| 513 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 514 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 515 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 516 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 517 | which indicates that the first K variables appear with identical
|
---|
| 518 | powers, then the result is that of a call to
|
---|
| 519 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 520 | X[K],X[K+1],..."
|
---|
| 521 | #'(lambda (k)
|
---|
| 522 | (cond
|
---|
| 523 | ((<= k 0)
|
---|
| 524 | (error "K must be at least 1"))
|
---|
| 525 | ((= k 1)
|
---|
[3485] | 526 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 527 | (t
|
---|
[3483] | 528 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 529 | (declare (type monom p q) (type fixnum start end))
|
---|
| 530 | (multiple-value-bind (primary equal)
|
---|
| 531 | (funcall primary-elimination-order p q start k)
|
---|
| 532 | (if equal
|
---|
| 533 | (funcall secondary-elimination-order p q k end)
|
---|
| 534 | (values primary nil))))))))
|
---|
| 535 |
|
---|
[3531] | 536 | (defclass term (monom)
|
---|
[4226] | 537 | ((coeff :initarg :coeff :accessor term-coeff :type ring))
|
---|
| 538 | (:default-initargs :coeff 1)
|
---|
[3531] | 539 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 540 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 541 |
|
---|
[4226] | 542 | (defmethod initialize-instance :around ((self term) &rest initargs &key coeff)
|
---|
| 543 | "A convenience method. If a coefficient is an integer, wrap it in the INTEGER-RING object"
|
---|
| 544 | ;; Dispatch on supplied type of coefficient
|
---|
| 545 | (typecase coeff
|
---|
| 546 | (integer
|
---|
| 547 | (setf (getf initargs :coeff) (make-instance 'integer-ring :value coeff))))
|
---|
| 548 | ;; Now pass new initargs to the next method
|
---|
| 549 | (apply #'call-next-method (cons self initargs)))
|
---|
| 550 |
|
---|
[3875] | 551 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
|
---|
[3794] | 552 | "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
[3792] | 553 | (reinitialize-instance new :coeff coeff))
|
---|
[3785] | 554 |
|
---|
[3876] | 555 | (defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
|
---|
| 556 | "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
| 557 | (reinitialize-instance new :coeff coeff))
|
---|
[3875] | 558 |
|
---|
[3876] | 559 |
|
---|
[3531] | 560 | (defmethod print-object ((self term) stream)
|
---|
| 561 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 562 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 563 | (coeff term-coeff))
|
---|
[3531] | 564 | self
|
---|
| 565 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 566 | exponents coeff))))
|
---|
| 567 |
|
---|
[4127] | 568 | (defmethod copy-instance :around ((object term) &rest initargs &key &allow-other-keys)
|
---|
| 569 | "An :AROUND method of COPY-INSTANCE. It replaces the coefficient with a fresh copy."
|
---|
| 570 | (declare (ignore object initargs))
|
---|
| 571 | (let ((copy (call-next-method)))
|
---|
[4128] | 572 | (setf (term-coeff copy) (copy-instance (term-coeff object)))
|
---|
[4127] | 573 | copy))
|
---|
| 574 |
|
---|
[3846] | 575 | (defmethod multiply-by ((self term) (other number))
|
---|
[4091] | 576 | (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
|
---|
[3846] | 577 |
|
---|
[3845] | 578 | (defmethod divide-by ((self term) (other number))
|
---|
[4091] | 579 | (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
|
---|
[3845] | 580 |
|
---|
[4037] | 581 | (defmethod unary-inverse :after ((self term))
|
---|
| 582 | (with-slots (coeff)
|
---|
| 583 | self
|
---|
| 584 | (setf coeff (unary-inverse coeff))))
|
---|
| 585 |
|
---|
[3812] | 586 | (defun make-term-constant (dimension &optional (coeff 1))
|
---|
| 587 | (make-instance 'term :dimension dimension :coeff coeff))
|
---|
| 588 |
|
---|
[3542] | 589 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 590 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 591 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 592 | (and (call-next-method)
|
---|
| 593 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 594 |
|
---|
[3556] | 595 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 596 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 597 | It returns SELF."
|
---|
[3580] | 598 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 599 |
|
---|
[4093] | 600 |
|
---|
[3581] | 601 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 602 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 603 |
|
---|
[3581] | 604 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 605 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 606 |
|
---|
[3556] | 607 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 608 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 609 |
|
---|
[3582] | 610 | (defgeneric unary-minus (self)
|
---|
[3615] | 611 | (:documentation "Negate object SELF and return it.")
|
---|
| 612 | (:method ((self number)) (- self))
|
---|
[3582] | 613 | (:method ((self term))
|
---|
| 614 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 615 | self))
|
---|
[3531] | 616 |
|
---|
[3578] | 617 | (defgeneric universal-zerop (self)
|
---|
[3617] | 618 | (:documentation "Return T iff SELF is zero.")
|
---|
[3618] | 619 | (:method ((self number)) (zerop self))
|
---|
[3578] | 620 | (:method ((self term))
|
---|
| 621 | (universal-zerop (term-coeff self))))
|
---|
[3823] | 622 |
|
---|
| 623 | (defgeneric ->list (self)
|
---|
| 624 | (:method ((self monom))
|
---|
| 625 | "A human-readable representation of a monomial SELF as a list of exponents."
|
---|
| 626 | (coerce (monom-exponents self) 'list))
|
---|
| 627 | (:method ((self term))
|
---|
| 628 | "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
|
---|
[4236] | 629 | (cons (coerce (monom-exponents self) 'list) (->list (term-coeff self)))))
|
---|
[3826] | 630 |
|
---|
[4168] | 631 | (defgeneric ->sexp (object &optional vars)
|
---|
| 632 | (:documentation "Convert a polynomial OBJECT to an S-expression, using variables VARS.")
|
---|
| 633 | (:method :before ((object monom) &optional vars)
|
---|
| 634 | "Check the length of variables VARS against the length of exponents in OBJECT."
|
---|
[4024] | 635 | (with-slots (exponents)
|
---|
[4168] | 636 | object
|
---|
[4024] | 637 | (assert (= (length vars) (length exponents))
|
---|
[4026] | 638 | nil
|
---|
[4024] | 639 | "Variables ~A and exponents ~A must have the same length." vars exponents)))
|
---|
[4168] | 640 | (:method ((object monom) &optional vars)
|
---|
| 641 | "Convert a monomial OBJECT to infix form, using variable VARS to build the representation."
|
---|
[3828] | 642 | (with-slots (exponents)
|
---|
[4168] | 643 | object
|
---|
[4010] | 644 | (let ((m (mapcan #'(lambda (var power)
|
---|
| 645 | (cond ((= power 0) nil)
|
---|
| 646 | ((= power 1) (list var))
|
---|
| 647 | (t (list `(expt ,var ,power)))))
|
---|
| 648 | vars (coerce exponents 'list))))
|
---|
[4024] | 649 | (cond ((endp m) 1)
|
---|
| 650 | ((endp (cdr m)) (car m))
|
---|
[4010] | 651 | (t
|
---|
| 652 | (cons '* m))))))
|
---|
[4168] | 653 | (:method :around ((object term) &optional vars)
|
---|
| 654 | "Convert a term OBJECT to infix form, using variable VARS to build the representation."
|
---|
[4009] | 655 | (declare (ignore vars))
|
---|
[4168] | 656 | (with-slots (coeff)
|
---|
| 657 | object
|
---|
[4235] | 658 | (let ((monom-sexp (call-next-method))
|
---|
| 659 | (coeff-sexp (->sexp coeff)))
|
---|
| 660 | (cond ((eql coeff-sexp 1) monom-sexp)
|
---|
| 661 | ((atom monom-sexp)
|
---|
| 662 | (cond ((eql monom-sexp 1) coeff-sexp)
|
---|
| 663 | (t (list '* coeff-sexp monom-sexp))))
|
---|
| 664 | ((eql (car monom-sexp) '*)
|
---|
| 665 | (list* '* coeff-sexp (cdr monom-sexp)))
|
---|
[4010] | 666 | (t
|
---|
[4235] | 667 | (list '* coeff-sexp monom-sexp)))))))
|
---|