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