[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"
|
---|
[3827] | 23 | (:use :cl :utils :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"
|
---|
| 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 |
|
---|
[3541] | 146 | (defgeneric universal-equalp (object1 object2)
|
---|
| 147 | (:documentation "Returns T iff OBJECT1 and OBJECT2 are equal.")
|
---|
[3650] | 148 | (:method ((object1 cons) (object2 cons)) (every #'universal-equalp object1 object2))
|
---|
[3611] | 149 | (:method ((object1 number) (object2 number)) (= object1 object2))
|
---|
[3443] | 150 | (:method ((m1 monom) (m2 monom))
|
---|
[3541] | 151 | "Returns T iff monomials M1 and M2 have identical EXPONENTS."
|
---|
[3535] | 152 | (equalp (monom-exponents m1) (monom-exponents m2))))
|
---|
[2547] | 153 |
|
---|
[3443] | 154 | (defgeneric monom-elt (m index)
|
---|
[3574] | 155 | (:documentation "Return the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 156 | (:method ((m monom) index)
|
---|
[3550] | 157 | "Return the power in the monomial M of variable number INDEX."
|
---|
[3443] | 158 | (with-slots (exponents)
|
---|
| 159 | m
|
---|
| 160 | (elt exponents index))))
|
---|
[48] | 161 |
|
---|
[3443] | 162 | (defgeneric (setf monom-elt) (new-value m index)
|
---|
[3550] | 163 | (:documentation "Set the power in the monomial M of variable number INDEX.")
|
---|
[3443] | 164 | (:method (new-value (m monom) index)
|
---|
| 165 | (with-slots (exponents)
|
---|
| 166 | m
|
---|
[3453] | 167 | (setf (elt exponents index) new-value))))
|
---|
[2023] | 168 |
|
---|
[3551] | 169 | (defgeneric total-degree (m &optional start end)
|
---|
| 170 | (:documentation "Return the total degree of a monomoal M. Optinally, a range
|
---|
[3449] | 171 | of variables may be specified with arguments START and END.")
|
---|
| 172 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 173 | (declare (type fixnum start end))
|
---|
| 174 | (with-slots (exponents)
|
---|
| 175 | m
|
---|
| 176 | (reduce #'+ exponents :start start :end end))))
|
---|
[48] | 177 |
|
---|
[3552] | 178 | (defgeneric sugar (m &optional start end)
|
---|
[3446] | 179 | (:documentation "Return the sugar of a monomial M. Optinally, a range
|
---|
| 180 | of variables may be specified with arguments START and END.")
|
---|
| 181 | (:method ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
| 182 | (declare (type fixnum start end))
|
---|
[3552] | 183 | (total-degree m start end)))
|
---|
[48] | 184 |
|
---|
[3553] | 185 | (defgeneric multiply-by (self other)
|
---|
[4098] | 186 | (:documentation "Multiply SELF by OTHER, return SELF. The object SELF
|
---|
| 187 | may be destructively modified in the process, while OTHER should remain
|
---|
| 188 | unmodified.")
|
---|
[3612] | 189 | (:method ((self number) (other number)) (* self other))
|
---|
[3446] | 190 | (:method ((self monom) (other monom))
|
---|
| 191 | (with-slots ((exponents1 exponents))
|
---|
| 192 | self
|
---|
| 193 | (with-slots ((exponents2 exponents))
|
---|
| 194 | other
|
---|
| 195 | (unless (= (length exponents1) (length exponents2))
|
---|
| 196 | (error "Incompatible dimensions"))
|
---|
| 197 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
[3846] | 198 | self))
|
---|
[2069] | 199 |
|
---|
[4093] | 200 | (defun multiply (factor &rest more-factors)
|
---|
| 201 | "Successively multiplies factor FACTOR by the remaining arguments
|
---|
[4097] | 202 | MORE-FACTORS, using MULTIPLY-BY to multiply two factors. Thus
|
---|
| 203 | FACTOR may be destructively modified."
|
---|
[4099] | 204 | (reduce #'multiply-by more-factors :initial-value (copy-instance factor)))
|
---|
[4093] | 205 |
|
---|
[3553] | 206 | (defgeneric divide-by (self other)
|
---|
[3544] | 207 | (:documentation "Divide SELF by OTHER, return SELF.")
|
---|
[3613] | 208 | (:method ((self number) (other number)) (/ self other))
|
---|
[3446] | 209 | (:method ((self monom) (other monom))
|
---|
| 210 | (with-slots ((exponents1 exponents))
|
---|
| 211 | self
|
---|
| 212 | (with-slots ((exponents2 exponents))
|
---|
| 213 | other
|
---|
| 214 | (unless (= (length exponents1) (length exponents2))
|
---|
| 215 | (error "divide-by: Incompatible dimensions."))
|
---|
| 216 | (unless (every #'>= exponents1 exponents2)
|
---|
| 217 | (error "divide-by: Negative power would result."))
|
---|
| 218 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
[3845] | 219 | self))
|
---|
[2818] | 220 |
|
---|
[3448] | 221 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
[4129] | 222 | "An :AROUND method of COPY-INSTANCE. It replaces exponents with a fresh copy of the sequence."
|
---|
[4130] | 223 | (declare (ignore object initargs))
|
---|
| 224 | (let ((copy (call-next-method)))
|
---|
| 225 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
| 226 | copy))
|
---|
[2950] | 227 |
|
---|
[4032] | 228 | (defgeneric unary-inverse (self)
|
---|
| 229 | (:documentation "Returns the unary inverse of SELF.")
|
---|
[4033] | 230 | (:method ((self number)) (/ self))
|
---|
| 231 | (:method :before ((self monom))
|
---|
[4037] | 232 | (assert (zerop (total-degree self))
|
---|
| 233 | nil
|
---|
| 234 | "Monom ~A must have total degree 0 to be invertible." self))
|
---|
[4033] | 235 | (:method ((self monom)) self))
|
---|
[4032] | 236 |
|
---|
[3849] | 237 | (defun divide (numerator &rest denominators)
|
---|
[4096] | 238 | "Successively divides NUMERATOR by elements of DENOMINATORS. The operation
|
---|
| 239 | destructively modifies NUMERATOR."
|
---|
[3849] | 240 | (cond ((endp denominators)
|
---|
[4030] | 241 | (unary-inverse numerator))
|
---|
[4100] | 242 | (t (reduce #'divide-by denominators :initial-value (copy-instance numerator)))))
|
---|
[48] | 243 |
|
---|
[3591] | 244 | (defgeneric divides-p (object1 object2)
|
---|
| 245 | (:documentation "Returns T if OBJECT1 divides OBJECT2.")
|
---|
| 246 | (:method ((m1 monom) (m2 monom))
|
---|
| 247 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
| 248 | (with-slots ((exponents1 exponents))
|
---|
| 249 | m1
|
---|
| 250 | (with-slots ((exponents2 exponents))
|
---|
| 251 | m2
|
---|
| 252 | (every #'<= exponents1 exponents2)))))
|
---|
[48] | 253 |
|
---|
[3585] | 254 | (defgeneric divides-lcm-p (object1 object2 object3)
|
---|
[3594] | 255 | (:documentation "Returns T if OBJECT1 divides LCM(OBJECT2,OBJECT3), NIL otherwise.")
|
---|
[3585] | 256 | (:method ((m1 monom) (m2 monom) (m3 monom))
|
---|
| 257 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[3596] | 258 | (with-slots ((exponents1 exponents))
|
---|
| 259 | m1
|
---|
| 260 | (with-slots ((exponents2 exponents))
|
---|
| 261 | m2
|
---|
| 262 | (with-slots ((exponents3 exponents))
|
---|
| 263 | m3
|
---|
| 264 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
| 265 | exponents1 exponents2 exponents3))))))
|
---|
[48] | 266 |
|
---|
[3588] | 267 | (defgeneric lcm-divides-lcm-p (object1 object2 object3 object4)
|
---|
| 268 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 269 | "Returns T if monomial LCM(M1,M2) divides 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 #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
[3590] | 279 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[869] | 280 |
|
---|
[3589] | 281 | (defgeneric monom-lcm-equal-lcm-p (object1 object2 object3 object4)
|
---|
| 282 | (:method ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 283 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
| 284 | (with-slots ((exponents1 exponents))
|
---|
| 285 | m1
|
---|
| 286 | (with-slots ((exponents2 exponents))
|
---|
| 287 | m2
|
---|
| 288 | (with-slots ((exponents3 exponents))
|
---|
| 289 | m3
|
---|
| 290 | (with-slots ((exponents4 exponents))
|
---|
| 291 | m4
|
---|
| 292 | (every
|
---|
| 293 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 294 | exponents1 exponents2 exponents3 exponents4)))))))
|
---|
[48] | 295 |
|
---|
[3563] | 296 | (defgeneric divisible-by-p (object1 object2)
|
---|
| 297 | (:documentation "Return T if OBJECT1 is divisible by OBJECT2.")
|
---|
| 298 | (:method ((m1 monom) (m2 monom))
|
---|
| 299 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
| 300 | (with-slots ((exponents1 exponents))
|
---|
| 301 | m1
|
---|
| 302 | (with-slots ((exponents2 exponents))
|
---|
| 303 | m2
|
---|
| 304 | (every #'>= exponents1 exponents2)))))
|
---|
[2078] | 305 |
|
---|
[3565] | 306 | (defgeneric rel-prime-p (object1 object2)
|
---|
[3575] | 307 | (:documentation "Returns T if objects OBJECT1 and OBJECT2 are relatively prime.")
|
---|
[3563] | 308 | (:method ((m1 monom) (m2 monom))
|
---|
| 309 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
| 310 | (with-slots ((exponents1 exponents))
|
---|
| 311 | m1
|
---|
| 312 | (with-slots ((exponents2 exponents))
|
---|
| 313 | m2
|
---|
| 314 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2)))))
|
---|
[48] | 315 |
|
---|
[3595] | 316 | (defgeneric universal-lcm (object1 object2)
|
---|
[3566] | 317 | (:documentation "Returns the multiple of objects OBJECT1 and OBJECT2.")
|
---|
| 318 | (:method ((m1 monom) (m2 monom))
|
---|
| 319 | "Returns least common multiple 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 #'max exponents1 exponents2)
|
---|
| 326 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 327 |
|
---|
[2080] | 328 |
|
---|
[3567] | 329 | (defgeneric universal-gcd (object1 object2)
|
---|
| 330 | (:documentation "Returns GCD of objects OBJECT1 and OBJECT2")
|
---|
[3681] | 331 | (:method ((object1 number) (object2 number)) (gcd object1 object2))
|
---|
[3567] | 332 | (:method ((m1 monom) (m2 monom))
|
---|
[3568] | 333 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 334 | (with-slots ((exponents1 exponents))
|
---|
| 335 | m1
|
---|
| 336 | (with-slots ((exponents2 exponents))
|
---|
| 337 | m2
|
---|
| 338 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 339 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 340 | (make-instance 'monom :exponents exponents))))))
|
---|
[48] | 341 |
|
---|
[3569] | 342 | (defgeneric depends-p (object k)
|
---|
| 343 | (:documentation "Returns T iff object OBJECT depends on variable K.")
|
---|
| 344 | (:method ((m monom) k)
|
---|
| 345 | "Return T if the monomial M depends on variable number K."
|
---|
| 346 | (declare (type fixnum k))
|
---|
| 347 | (with-slots (exponents)
|
---|
| 348 | m
|
---|
| 349 | (plusp (elt exponents k)))))
|
---|
[48] | 350 |
|
---|
[3570] | 351 | (defgeneric left-tensor-product-by (self other)
|
---|
| 352 | (:documentation "Returns a tensor product SELF by OTHER, stored into
|
---|
| 353 | SELF. Return SELF.")
|
---|
| 354 | (:method ((self monom) (other monom))
|
---|
| 355 | (with-slots ((exponents1 exponents))
|
---|
| 356 | self
|
---|
| 357 | (with-slots ((exponents2 exponents))
|
---|
| 358 | other
|
---|
| 359 | (setf exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 360 | self))
|
---|
[48] | 361 |
|
---|
[3570] | 362 | (defgeneric right-tensor-product-by (self other)
|
---|
| 363 | (:documentation "Returns a tensor product of OTHER by SELF, stored
|
---|
| 364 | into SELF. Returns SELF.")
|
---|
| 365 | (:method ((self monom) (other monom))
|
---|
| 366 | (with-slots ((exponents1 exponents))
|
---|
| 367 | self
|
---|
| 368 | (with-slots ((exponents2 exponents))
|
---|
| 369 | other
|
---|
| 370 | (setf exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 371 | self))
|
---|
[3026] | 372 |
|
---|
[3571] | 373 | (defgeneric left-contract (self k)
|
---|
| 374 | (:documentation "Drop the first K variables in object SELF.")
|
---|
| 375 | (:method ((self monom) k)
|
---|
| 376 | "Drop the first K variables in monomial M."
|
---|
| 377 | (declare (fixnum k))
|
---|
| 378 | (with-slots (exponents)
|
---|
| 379 | self
|
---|
| 380 | (setf exponents (subseq exponents k)))
|
---|
| 381 | self))
|
---|
[886] | 382 |
|
---|
| 383 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 384 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 385 | "Construct a monomial in the polynomial ring
|
---|
| 386 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 387 | which represents a single variable. It assumes number of variables
|
---|
| 388 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 389 | may appear raised to power POWER. "
|
---|
[1924] | 390 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 391 | (with-slots (exponents)
|
---|
| 392 | m
|
---|
[2154] | 393 | (setf (elt exponents pos) power)
|
---|
[2089] | 394 | m))
|
---|
[1151] | 395 |
|
---|
[3811] | 396 | (defun make-monom-constant (dimension)
|
---|
| 397 | (make-instance 'monom :dimension dimension))
|
---|
| 398 |
|
---|
[3474] | 399 | ;; pure lexicographic
|
---|
[3472] | 400 | (defgeneric lex> (p q &optional start end)
|
---|
| 401 | (:documentation "Return T if P>Q with respect to lexicographic
|
---|
| 402 | order, otherwise NIL. The second returned value is T if P=Q,
|
---|
| 403 | otherwise it is NIL.")
|
---|
[3483] | 404 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 405 | (declare (type fixnum start end))
|
---|
| 406 | (do ((i start (1+ i)))
|
---|
| 407 | ((>= i end) (values nil t))
|
---|
| 408 | (cond
|
---|
[3483] | 409 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 410 | (return-from lex> (values t nil)))
|
---|
[3483] | 411 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 412 | (return-from lex> (values nil nil)))))))
|
---|
| 413 |
|
---|
[3475] | 414 | ;; total degree order, ties broken by lexicographic
|
---|
[3472] | 415 | (defgeneric grlex> (p q &optional start end)
|
---|
| 416 | (:documentation "Return T if P>Q with respect to graded
|
---|
| 417 | lexicographic order, otherwise NIL. The second returned value is T if
|
---|
| 418 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 419 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 420 | (declare (type monom p q) (type fixnum start end))
|
---|
[3583] | 421 | (let ((d1 (total-degree p start end))
|
---|
| 422 | (d2 (total-degree q start end)))
|
---|
[3472] | 423 | (declare (type fixnum d1 d2))
|
---|
| 424 | (cond
|
---|
| 425 | ((> d1 d2) (values t nil))
|
---|
| 426 | ((< d1 d2) (values nil nil))
|
---|
| 427 | (t
|
---|
| 428 | (lex> p q start end))))))
|
---|
| 429 |
|
---|
| 430 | ;; reverse lexicographic
|
---|
| 431 | (defgeneric revlex> (p q &optional start end)
|
---|
| 432 | (:documentation "Return T if P>Q with respect to reverse
|
---|
| 433 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 434 | P=Q, otherwise it is NIL. This is not and admissible monomial order
|
---|
| 435 | because some sets do not have a minimal element. This order is useful
|
---|
| 436 | in constructing other orders.")
|
---|
[3483] | 437 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 438 | (declare (type fixnum start end))
|
---|
| 439 | (do ((i (1- end) (1- i)))
|
---|
| 440 | ((< i start) (values nil t))
|
---|
| 441 | (declare (type fixnum i))
|
---|
| 442 | (cond
|
---|
[3483] | 443 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 444 | (return-from revlex> (values t nil)))
|
---|
[3483] | 445 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 446 | (return-from revlex> (values nil nil)))))))
|
---|
| 447 |
|
---|
| 448 |
|
---|
| 449 | ;; total degree, ties broken by reverse lexicographic
|
---|
| 450 | (defgeneric grevlex> (p q &optional start end)
|
---|
| 451 | (:documentation "Return T if P>Q with respect to graded reverse
|
---|
| 452 | lexicographic order, NIL otherwise. The second returned value is T if
|
---|
| 453 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 454 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 455 | (declare (type fixnum start end))
|
---|
[3584] | 456 | (let ((d1 (total-degree p start end))
|
---|
| 457 | (d2 (total-degree q start end)))
|
---|
[3472] | 458 | (declare (type fixnum d1 d2))
|
---|
| 459 | (cond
|
---|
| 460 | ((> d1 d2) (values t nil))
|
---|
| 461 | ((< d1 d2) (values nil nil))
|
---|
| 462 | (t
|
---|
| 463 | (revlex> p q start end))))))
|
---|
| 464 |
|
---|
| 465 | (defgeneric invlex> (p q &optional start end)
|
---|
| 466 | (:documentation "Return T if P>Q with respect to inverse
|
---|
| 467 | lexicographic order, NIL otherwise The second returned value is T if
|
---|
| 468 | P=Q, otherwise it is NIL.")
|
---|
[3483] | 469 | (:method ((p monom) (q monom) &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 470 | (declare (type fixnum start end))
|
---|
| 471 | (do ((i (1- end) (1- i)))
|
---|
| 472 | ((< i start) (values nil t))
|
---|
| 473 | (declare (type fixnum i))
|
---|
| 474 | (cond
|
---|
[3483] | 475 | ((> (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 476 | (return-from invlex> (values t nil)))
|
---|
[3483] | 477 | ((< (monom-elt p i) (monom-elt q i))
|
---|
[3472] | 478 | (return-from invlex> (values nil nil)))))))
|
---|
| 479 |
|
---|
| 480 | (defun reverse-monomial-order (order)
|
---|
| 481 | "Create the inverse monomial order to the given monomial order ORDER."
|
---|
[3483] | 482 | #'(lambda (p q &optional (start 0) (end (monom-dimension q)))
|
---|
[3472] | 483 | (declare (type monom p q) (type fixnum start end))
|
---|
| 484 | (funcall order q p start end)))
|
---|
| 485 |
|
---|
| 486 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 487 | ;;
|
---|
| 488 | ;; Order making functions
|
---|
| 489 | ;;
|
---|
| 490 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 491 |
|
---|
| 492 | ;; This returns a closure with the same signature
|
---|
| 493 | ;; as all orders such as #'LEX>.
|
---|
[3487] | 494 | (defun make-elimination-order-factory-1 (&optional (secondary-elimination-order #'lex>))
|
---|
[3472] | 495 | "It constructs an elimination order used for the 1-st elimination ideal,
|
---|
| 496 | i.e. for eliminating the first variable. Thus, the order compares the degrees of the
|
---|
| 497 | first variable in P and Q first, with ties broken by SECONDARY-ELIMINATION-ORDER."
|
---|
[3483] | 498 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 499 | (declare (type monom p q) (type fixnum start end))
|
---|
| 500 | (cond
|
---|
[3483] | 501 | ((> (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 502 | (values t nil))
|
---|
[3483] | 503 | ((< (monom-elt p start) (monom-elt q start))
|
---|
[3472] | 504 | (values nil nil))
|
---|
| 505 | (t
|
---|
| 506 | (funcall secondary-elimination-order p q (1+ start) end)))))
|
---|
| 507 |
|
---|
| 508 | ;; This returns a closure which is called with an integer argument.
|
---|
| 509 | ;; The result is *another closure* with the same signature as all
|
---|
| 510 | ;; orders such as #'LEX>.
|
---|
[3486] | 511 | (defun make-elimination-order-factory (&optional
|
---|
[3472] | 512 | (primary-elimination-order #'lex>)
|
---|
| 513 | (secondary-elimination-order #'lex>))
|
---|
| 514 | "Return a function with a single integer argument K. This should be
|
---|
| 515 | the number of initial K variables X[0],X[1],...,X[K-1], which precede
|
---|
| 516 | remaining variables. The call to the closure creates a predicate
|
---|
| 517 | which compares monomials according to the K-th elimination order. The
|
---|
| 518 | monomial orders PRIMARY-ELIMINATION-ORDER and
|
---|
| 519 | SECONDARY-ELIMINATION-ORDER are used to compare the first K and the
|
---|
| 520 | remaining variables, respectively, with ties broken by lexicographical
|
---|
| 521 | order. That is, if PRIMARY-ELIMINATION-ORDER yields (VALUES NIL T),
|
---|
| 522 | which indicates that the first K variables appear with identical
|
---|
| 523 | powers, then the result is that of a call to
|
---|
| 524 | SECONDARY-ELIMINATION-ORDER applied to the remaining variables
|
---|
| 525 | X[K],X[K+1],..."
|
---|
| 526 | #'(lambda (k)
|
---|
| 527 | (cond
|
---|
| 528 | ((<= k 0)
|
---|
| 529 | (error "K must be at least 1"))
|
---|
| 530 | ((= k 1)
|
---|
[3485] | 531 | (make-elimination-order-factory-1 secondary-elimination-order))
|
---|
[3472] | 532 | (t
|
---|
[3483] | 533 | #'(lambda (p q &optional (start 0) (end (monom-dimension p)))
|
---|
[3472] | 534 | (declare (type monom p q) (type fixnum start end))
|
---|
| 535 | (multiple-value-bind (primary equal)
|
---|
| 536 | (funcall primary-elimination-order p q start k)
|
---|
| 537 | (if equal
|
---|
| 538 | (funcall secondary-elimination-order p q k end)
|
---|
| 539 | (values primary nil))))))))
|
---|
| 540 |
|
---|
[3531] | 541 | (defclass term (monom)
|
---|
| 542 | ((coeff :initarg :coeff :accessor term-coeff))
|
---|
| 543 | (:default-initargs :coeff nil)
|
---|
| 544 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
| 545 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
| 546 |
|
---|
[3875] | 547 | (defmethod update-instance-for-different-class :after ((old monom) (new term) &key (coeff 1))
|
---|
[3794] | 548 | "Converts OLD of class MONOM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
[3792] | 549 | (reinitialize-instance new :coeff coeff))
|
---|
[3785] | 550 |
|
---|
[3876] | 551 | (defmethod update-instance-for-different-class :after ((old term) (new term) &key (coeff (term-coeff old)))
|
---|
| 552 | "Converts OLD of class TERM to a NEW of class TERM, initializing coefficient to COEFF."
|
---|
| 553 | (reinitialize-instance new :coeff coeff))
|
---|
[3875] | 554 |
|
---|
[3876] | 555 |
|
---|
[3531] | 556 | (defmethod print-object ((self term) stream)
|
---|
| 557 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 558 | (with-accessors ((exponents monom-exponents)
|
---|
[3532] | 559 | (coeff term-coeff))
|
---|
[3531] | 560 | self
|
---|
| 561 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
| 562 | exponents coeff))))
|
---|
| 563 |
|
---|
[4127] | 564 | (defmethod copy-instance :around ((object term) &rest initargs &key &allow-other-keys)
|
---|
| 565 | "An :AROUND method of COPY-INSTANCE. It replaces the coefficient with a fresh copy."
|
---|
| 566 | (declare (ignore object initargs))
|
---|
| 567 | (let ((copy (call-next-method)))
|
---|
[4128] | 568 | (setf (term-coeff copy) (copy-instance (term-coeff object)))
|
---|
[4127] | 569 | copy))
|
---|
| 570 |
|
---|
[3846] | 571 | (defmethod multiply-by ((self term) (other number))
|
---|
[4091] | 572 | (reinitialize-instance self :coeff (multiply-by (term-coeff self) other)))
|
---|
[3846] | 573 |
|
---|
[3845] | 574 | (defmethod divide-by ((self term) (other number))
|
---|
[4091] | 575 | (reinitialize-instance self :coeff (divide-by (term-coeff self) other)))
|
---|
[3845] | 576 |
|
---|
[4037] | 577 | (defmethod unary-inverse :after ((self term))
|
---|
| 578 | (with-slots (coeff)
|
---|
| 579 | self
|
---|
| 580 | (setf coeff (unary-inverse coeff))))
|
---|
| 581 |
|
---|
[3812] | 582 | (defun make-term-constant (dimension &optional (coeff 1))
|
---|
| 583 | (make-instance 'term :dimension dimension :coeff coeff))
|
---|
| 584 |
|
---|
[3542] | 585 | (defmethod universal-equalp ((term1 term) (term2 term))
|
---|
| 586 | "Returns T if TERM1 and TERM2 are equal as MONOM, and coefficients
|
---|
| 587 | are UNIVERSAL-EQUALP."
|
---|
[3540] | 588 | (and (call-next-method)
|
---|
| 589 | (universal-equalp (term-coeff term1) (term-coeff term2))))
|
---|
[3531] | 590 |
|
---|
[3556] | 591 | (defmethod multiply-by :before ((self term) (other term))
|
---|
[3531] | 592 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
| 593 | It returns SELF."
|
---|
[3580] | 594 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 595 |
|
---|
[4093] | 596 |
|
---|
[3581] | 597 | (defmethod left-tensor-product-by :before ((self term) (other term))
|
---|
[3579] | 598 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 599 |
|
---|
[3581] | 600 | (defmethod right-tensor-product-by :before ((self term) (other term))
|
---|
[3556] | 601 | (setf (term-coeff self) (multiply-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 602 |
|
---|
[3556] | 603 | (defmethod divide-by :before ((self term) (other term))
|
---|
| 604 | (setf (term-coeff self) (divide-by (term-coeff self) (term-coeff other))))
|
---|
[3531] | 605 |
|
---|
[3582] | 606 | (defgeneric unary-minus (self)
|
---|
[3615] | 607 | (:documentation "Negate object SELF and return it.")
|
---|
| 608 | (:method ((self number)) (- self))
|
---|
[3582] | 609 | (:method ((self term))
|
---|
| 610 | (setf (term-coeff self) (unary-minus (term-coeff self)))
|
---|
| 611 | self))
|
---|
[3531] | 612 |
|
---|
[3578] | 613 | (defgeneric universal-zerop (self)
|
---|
[3617] | 614 | (:documentation "Return T iff SELF is zero.")
|
---|
[3618] | 615 | (:method ((self number)) (zerop self))
|
---|
[3578] | 616 | (:method ((self term))
|
---|
| 617 | (universal-zerop (term-coeff self))))
|
---|
[3823] | 618 |
|
---|
| 619 | (defgeneric ->list (self)
|
---|
| 620 | (:method ((self monom))
|
---|
| 621 | "A human-readable representation of a monomial SELF as a list of exponents."
|
---|
| 622 | (coerce (monom-exponents self) 'list))
|
---|
| 623 | (:method ((self term))
|
---|
| 624 | "A human-readable representation of a term SELF as a cons of the list of exponents and the coefficient."
|
---|
[3824] | 625 | (cons (coerce (monom-exponents self) 'list) (term-coeff self))))
|
---|
[3826] | 626 |
|
---|
[4168] | 627 | (defgeneric ->sexp (object &optional vars)
|
---|
| 628 | (:documentation "Convert a polynomial OBJECT to an S-expression, using variables VARS.")
|
---|
| 629 | (:method :before ((object monom) &optional vars)
|
---|
| 630 | "Check the length of variables VARS against the length of exponents in OBJECT."
|
---|
[4024] | 631 | (with-slots (exponents)
|
---|
[4168] | 632 | object
|
---|
[4024] | 633 | (assert (= (length vars) (length exponents))
|
---|
[4026] | 634 | nil
|
---|
[4024] | 635 | "Variables ~A and exponents ~A must have the same length." vars exponents)))
|
---|
[4168] | 636 | (:method ((object monom) &optional vars)
|
---|
| 637 | "Convert a monomial OBJECT to infix form, using variable VARS to build the representation."
|
---|
[3828] | 638 | (with-slots (exponents)
|
---|
[4168] | 639 | object
|
---|
[4010] | 640 | (let ((m (mapcan #'(lambda (var power)
|
---|
| 641 | (cond ((= power 0) nil)
|
---|
| 642 | ((= power 1) (list var))
|
---|
| 643 | (t (list `(expt ,var ,power)))))
|
---|
| 644 | vars (coerce exponents 'list))))
|
---|
[4024] | 645 | (cond ((endp m) 1)
|
---|
| 646 | ((endp (cdr m)) (car m))
|
---|
[4010] | 647 | (t
|
---|
| 648 | (cons '* m))))))
|
---|
[4168] | 649 | (:method :around ((object term) &optional vars)
|
---|
| 650 | "Convert a term OBJECT to infix form, using variable VARS to build the representation."
|
---|
[4009] | 651 | (declare (ignore vars))
|
---|
[4168] | 652 | (with-slots (coeff)
|
---|
| 653 | object
|
---|
| 654 | (let ((sexp (call-next-method)))
|
---|
| 655 | (cond ((eql coeff 1) sexp)
|
---|
| 656 | ((atom sexp)
|
---|
| 657 | (cond ((eql sexp 1) coeff)
|
---|
| 658 | (t (list '* coeff sexp))))
|
---|
| 659 | ((eql (car sexp) '*)
|
---|
| 660 | (list* '* coeff (cdr sexp)))
|
---|
[4010] | 661 | (t
|
---|
[4168] | 662 | (list '* coeff sexp)))))))
|
---|