[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"
|
---|
[2025] | 23 | (:use :cl :ring)
|
---|
[422] | 24 | (:export "MONOM"
|
---|
[423] | 25 | "EXPONENT"
|
---|
[2781] | 26 | "MONOM-DIMENSION"
|
---|
| 27 | "MONOM-EXPONENTS"
|
---|
[2524] | 28 | "MAKE-MONOM-VARIABLE")
|
---|
| 29 | (:documentation
|
---|
| 30 | "This package implements basic operations on monomials.
|
---|
| 31 | DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
[81] | 32 |
|
---|
[2524] | 33 | monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 34 |
|
---|
| 35 | However, lists may be implemented as other sequence types, so the
|
---|
| 36 | flexibility to change the representation should be maintained in the
|
---|
| 37 | code to use general operations on sequences whenever possible. The
|
---|
| 38 | optimization for the actual representation should be left to
|
---|
| 39 | declarations and the compiler.
|
---|
| 40 |
|
---|
| 41 | EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 42 |
|
---|
| 43 | Monom x*y^2 ---> (1 2) "))
|
---|
| 44 |
|
---|
[1610] | 45 | (in-package :monom)
|
---|
[48] | 46 |
|
---|
[1925] | 47 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[1923] | 48 |
|
---|
[48] | 49 | (deftype exponent ()
|
---|
| 50 | "Type of exponent in a monomial."
|
---|
| 51 | 'fixnum)
|
---|
| 52 |
|
---|
[2022] | 53 | (defclass monom ()
|
---|
[3054] | 54 | ((dimension :initarg :dimension :accessor monom-dimension
|
---|
| 55 | :documentation "The number of variables.")
|
---|
| 56 | (exponents :initarg :exponents :accessor monom-exponents
|
---|
| 57 | :documentation "The powers of the variables."))
|
---|
[3213] | 58 | (:extra-initargs :exponent nil)
|
---|
[2779] | 59 | (:documentation
|
---|
| 60 | "Implements a monomial, i.e. a product of powers
|
---|
| 61 | of variables, like X*Y^2."))
|
---|
[880] | 62 |
|
---|
[2245] | 63 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 64 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 65 | (format stream "DIMENSION=~A EXPONENTS=~A"
|
---|
| 66 | (monom-dimension self)
|
---|
| 67 | (monom-exponents self))))
|
---|
[2027] | 68 |
|
---|
[2390] | 69 | (defmethod shared-initialize :after ((self monom) slot-names
|
---|
| 70 | &key
|
---|
| 71 | dimension
|
---|
| 72 | exponents
|
---|
| 73 | exponent
|
---|
| 74 | &allow-other-keys
|
---|
| 75 | )
|
---|
[2354] | 76 | (if (eq slot-names t) (setf slot-names '(dimension exponents)))
|
---|
| 77 | (dolist (slot-name slot-names)
|
---|
[2357] | 78 | (case slot-name
|
---|
[2354] | 79 | (dimension
|
---|
[2355] | 80 | (cond (dimension
|
---|
| 81 | (setf (slot-value self 'dimension) dimension))
|
---|
[2354] | 82 | (exponents
|
---|
| 83 | (setf (slot-value self 'dimension) (length exponents)))
|
---|
| 84 | (t
|
---|
| 85 | (error "DIMENSION or EXPONENTS must not be NIL"))))
|
---|
| 86 | (exponents
|
---|
| 87 | (cond
|
---|
| 88 | ;; when exponents are supplied
|
---|
| 89 | (exponents
|
---|
[2356] | 90 | (let ((dim (length exponents)))
|
---|
[2405] | 91 | (when (and dimension (/= dimension dim))
|
---|
| 92 | (error "EXPONENTS must have length DIMENSION"))
|
---|
[2356] | 93 | (setf (slot-value self 'dimension) dim
|
---|
| 94 | (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
|
---|
[2354] | 95 | ;; when all exponents are to be identical
|
---|
[2356] | 96 | (t
|
---|
| 97 | (let ((dim (slot-value self 'dimension)))
|
---|
| 98 | (setf (slot-value self 'exponents)
|
---|
| 99 | (make-array (list dim) :initial-element (or exponent 0)
|
---|
| 100 | :element-type 'exponent)))))))))
|
---|
[717] | 101 |
|
---|
[2850] | 102 | (defmethod r-equalp ((m1 monom) (m2 monom))
|
---|
[2778] | 103 | "Returns T iff monomials M1 and M2 have identical
|
---|
| 104 | EXPONENTS."
|
---|
[2779] | 105 | (equalp (monom-exponents m1) (monom-exponents m2)))
|
---|
[2547] | 106 |
|
---|
[2398] | 107 | (defmethod r-coeff ((m monom))
|
---|
| 108 | "A MONOM can be treated as a special case of TERM,
|
---|
| 109 | where the coefficient is 1."
|
---|
| 110 | 1)
|
---|
[2397] | 111 |
|
---|
[2143] | 112 | (defmethod r-elt ((m monom) index)
|
---|
[48] | 113 | "Return the power in the monomial M of variable number INDEX."
|
---|
[2023] | 114 | (with-slots (exponents)
|
---|
| 115 | m
|
---|
[2154] | 116 | (elt exponents index)))
|
---|
[48] | 117 |
|
---|
[2160] | 118 | (defmethod (setf r-elt) (new-value (m monom) index)
|
---|
[2023] | 119 | "Return the power in the monomial M of variable number INDEX."
|
---|
| 120 | (with-slots (exponents)
|
---|
| 121 | m
|
---|
[2154] | 122 | (setf (elt exponents index) new-value)))
|
---|
[2023] | 123 |
|
---|
[2779] | 124 | (defmethod r-total-degree ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
[48] | 125 | "Return the todal degree of a monomoal M. Optinally, a range
|
---|
| 126 | of variables may be specified with arguments START and END."
|
---|
[2023] | 127 | (declare (type fixnum start end))
|
---|
| 128 | (with-slots (exponents)
|
---|
| 129 | m
|
---|
[2154] | 130 | (reduce #'+ exponents :start start :end end)))
|
---|
[48] | 131 |
|
---|
[2064] | 132 |
|
---|
[2779] | 133 | (defmethod r-sugar ((m monom) &aux (start 0) (end (monom-dimension m)))
|
---|
[48] | 134 | "Return the sugar of a monomial M. Optinally, a range
|
---|
| 135 | of variables may be specified with arguments START and END."
|
---|
[2032] | 136 | (declare (type fixnum start end))
|
---|
[2155] | 137 | (r-total-degree m start end))
|
---|
[48] | 138 |
|
---|
[2478] | 139 | (defmethod multiply-by ((self monom) (other monom))
|
---|
[2807] | 140 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2478] | 141 | self
|
---|
[2811] | 142 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
[2478] | 143 | other
|
---|
[2811] | 144 | (unless (= dimension1 dimension2)
|
---|
[2813] | 145 | (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
|
---|
[2811] | 146 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
[2480] | 147 | self)
|
---|
[2069] | 148 |
|
---|
[2818] | 149 | (defmethod divide-by ((self monom) (other monom))
|
---|
| 150 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 151 | self
|
---|
| 152 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
| 153 | other
|
---|
| 154 | (unless (= dimension1 dimension2)
|
---|
| 155 | (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
|
---|
| 156 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
| 157 | self)
|
---|
| 158 |
|
---|
[3004] | 159 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
[3018] | 160 | "An :AROUNT method for COPY-INSTANCE. The primary method is a shallow copy,
|
---|
| 161 | while for monomials we typically need a fresh copy of the
|
---|
| 162 | exponents."
|
---|
[3004] | 163 | (declare (ignore object initargs))
|
---|
[3002] | 164 | (let ((copy (call-next-method)))
|
---|
[3003] | 165 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
[3002] | 166 | copy))
|
---|
[2950] | 167 |
|
---|
[2816] | 168 | (defmethod r* ((m1 monom) (m2 monom))
|
---|
| 169 | "Non-destructively multiply monomial M1 by M2."
|
---|
[3032] | 170 | (multiply-by (copy-instance m1) (copy-instance m2)))
|
---|
[2816] | 171 |
|
---|
[2144] | 172 | (defmethod r/ ((m1 monom) (m2 monom))
|
---|
[2819] | 173 | "Non-destructively divide monomial M1 by monomial M2."
|
---|
[3032] | 174 | (divide-by (copy-instance m1) (copy-instance m2)))
|
---|
[48] | 175 |
|
---|
[2144] | 176 | (defmethod r-divides-p ((m1 monom) (m2 monom))
|
---|
[48] | 177 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
[2039] | 178 | (with-slots ((exponents1 exponents))
|
---|
| 179 | m1
|
---|
| 180 | (with-slots ((exponents2 exponents))
|
---|
| 181 | m2
|
---|
| 182 | (every #'<= exponents1 exponents2))))
|
---|
[48] | 183 |
|
---|
[2075] | 184 |
|
---|
[2144] | 185 | (defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
[2055] | 186 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[875] | 187 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
[869] | 188 | m1 m2 m3))
|
---|
[48] | 189 |
|
---|
[2049] | 190 |
|
---|
[2144] | 191 | (defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
[48] | 192 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
[1890] | 193 | (declare (type monom m1 m2 m3 m4))
|
---|
[869] | 194 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 195 | m1 m2 m3 m4))
|
---|
| 196 |
|
---|
[2144] | 197 | (defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
[2075] | 198 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
[2171] | 199 | (with-slots ((exponents1 exponents))
|
---|
[2076] | 200 | m1
|
---|
[2171] | 201 | (with-slots ((exponents2 exponents))
|
---|
[2076] | 202 | m2
|
---|
[2171] | 203 | (with-slots ((exponents3 exponents))
|
---|
[2076] | 204 | m3
|
---|
[2171] | 205 | (with-slots ((exponents4 exponents))
|
---|
[2076] | 206 | m4
|
---|
[2077] | 207 | (every
|
---|
| 208 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 209 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
[48] | 210 |
|
---|
[2144] | 211 | (defmethod r-divisible-by-p ((m1 monom) (m2 monom))
|
---|
[48] | 212 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
[2171] | 213 | (with-slots ((exponents1 exponents))
|
---|
[2144] | 214 | m1
|
---|
[2171] | 215 | (with-slots ((exponents2 exponents))
|
---|
[2144] | 216 | m2
|
---|
| 217 | (every #'>= exponents1 exponents2))))
|
---|
[2078] | 218 |
|
---|
[2146] | 219 | (defmethod r-rel-prime-p ((m1 monom) (m2 monom))
|
---|
[48] | 220 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
[2171] | 221 | (with-slots ((exponents1 exponents))
|
---|
[2078] | 222 | m1
|
---|
[2171] | 223 | (with-slots ((exponents2 exponents))
|
---|
[2078] | 224 | m2
|
---|
[2154] | 225 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
[48] | 226 |
|
---|
[2076] | 227 |
|
---|
[2146] | 228 | (defmethod r-lcm ((m1 monom) (m2 monom))
|
---|
[48] | 229 | "Returns least common multiple of monomials M1 and M2."
|
---|
[2319] | 230 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 231 | m1
|
---|
[2171] | 232 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 233 | m2
|
---|
| 234 | (let* ((exponents (copy-seq exponents1))
|
---|
[2319] | 235 | (dimension dimension1))
|
---|
[2082] | 236 | (map-into exponents #'max exponents1 exponents2)
|
---|
[2200] | 237 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 238 |
|
---|
[2080] | 239 |
|
---|
[2146] | 240 | (defmethod r-gcd ((m1 monom) (m2 monom))
|
---|
[48] | 241 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
[2320] | 242 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 243 | m1
|
---|
[2171] | 244 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 245 | m2
|
---|
| 246 | (let* ((exponents (copy-seq exponents1))
|
---|
[2320] | 247 | (dimension dimension1))
|
---|
[2082] | 248 | (map-into exponents #'min exponents1 exponents2)
|
---|
[2197] | 249 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 250 |
|
---|
[2146] | 251 | (defmethod r-depends-p ((m monom) k)
|
---|
[48] | 252 | "Return T if the monomial M depends on variable number K."
|
---|
[2083] | 253 | (declare (type fixnum k))
|
---|
| 254 | (with-slots (exponents)
|
---|
| 255 | m
|
---|
[2154] | 256 | (plusp (elt exponents k))))
|
---|
[48] | 257 |
|
---|
[3020] | 258 | (defmethod left-tensor-product-by ((self monom) (other monom))
|
---|
[2321] | 259 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[3020] | 260 | self
|
---|
[2321] | 261 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
[3020] | 262 | other
|
---|
| 263 | (setf dimension1 (+ dimension1 dimension2)
|
---|
[3036] | 264 | exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 265 | self)
|
---|
[48] | 266 |
|
---|
[3026] | 267 | (defmethod right-tensor-product-by ((self monom) (other monom))
|
---|
| 268 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 269 | self
|
---|
| 270 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
| 271 | other
|
---|
| 272 | (setf dimension1 (+ dimension1 dimension2)
|
---|
[3036] | 273 | exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 274 | self)
|
---|
[3026] | 275 |
|
---|
[3039] | 276 | (defmethod left-contract ((self monom) k)
|
---|
[1638] | 277 | "Drop the first K variables in monomial M."
|
---|
[2085] | 278 | (declare (fixnum k))
|
---|
[2196] | 279 | (with-slots (dimension exponents)
|
---|
[3040] | 280 | self
|
---|
[2197] | 281 | (setf dimension (- dimension k)
|
---|
[3039] | 282 | exponents (subseq exponents k)))
|
---|
| 283 | self)
|
---|
[886] | 284 |
|
---|
| 285 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 286 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 287 | "Construct a monomial in the polynomial ring
|
---|
| 288 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 289 | which represents a single variable. It assumes number of variables
|
---|
| 290 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 291 | may appear raised to power POWER. "
|
---|
[1924] | 292 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 293 | (with-slots (exponents)
|
---|
| 294 | m
|
---|
[2154] | 295 | (setf (elt exponents pos) power)
|
---|
[2089] | 296 | m))
|
---|
[1151] | 297 |
|
---|
[2150] | 298 | (defmethod r->list ((m monom))
|
---|
[1152] | 299 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2779] | 300 | (coerce (monom-exponents m) 'list))
|
---|
[2780] | 301 |
|
---|
[2783] | 302 | (defmethod r-dimension ((self monom))
|
---|
| 303 | (monom-dimension self))
|
---|
| 304 |
|
---|
[2780] | 305 | (defmethod r-exponents ((self monom))
|
---|
| 306 | (monom-exponents self))
|
---|