[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."))
|
---|
[3289] | 58 | ;; default-initargs are not needed, they are handled by SHARED-INITIALIZE
|
---|
| 59 | ;;(:default-initargs :dimension 'foo :exponents 'bar :exponent 'baz)
|
---|
[2779] | 60 | (:documentation
|
---|
| 61 | "Implements a monomial, i.e. a product of powers
|
---|
| 62 | of variables, like X*Y^2."))
|
---|
[880] | 63 |
|
---|
[2245] | 64 | (defmethod print-object ((self monom) stream)
|
---|
[3196] | 65 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3216] | 66 | (with-accessors ((dimension monom-dimension) (exponents monom-exponents))
|
---|
| 67 | self
|
---|
| 68 | (format stream "DIMENSION=~A EXPONENTS=~A"
|
---|
| 69 | dimension exponents))))
|
---|
[2027] | 70 |
|
---|
[3300] | 71 | ;; The following INITIALIZE-INSTANCE method allows instance
|
---|
| 72 | ;; initialization in a style similar to MAKE-ARRAY, e.g.
|
---|
[3291] | 73 | ;;
|
---|
[3292] | 74 | ;; (MAKE-INSTANCE :EXPONENTS '(1 2 3)) --> #<MONOM DIMENSION=3 EXPONENTS=#(1 2 3)>
|
---|
| 75 | ;; (MAKE-INSTANCE :DIMENSION 3) --> #<MONOM DIMENSION=3 EXPONENTS=#(0 0 0)>
|
---|
[3291] | 76 | ;; (MAKE-INSTANCE :DIMENSION 3 :EXPONENT 7) --> #<MONOM DIMENSION=3 EXPONENTS=#(7 7 7)>
|
---|
| 77 | ;;
|
---|
[3299] | 78 | (defmethod initialize-instance :after ((self monom)
|
---|
[3297] | 79 | &key
|
---|
| 80 | (dimension 0 dimension-supplied-p)
|
---|
| 81 | (exponents nil exponents-supplied-p)
|
---|
| 82 | (exponent 0 exponent-supplied-p)
|
---|
| 83 | &allow-other-keys
|
---|
[2390] | 84 | )
|
---|
[3298] | 85 | (when dimension-supplied-p
|
---|
| 86 | (setf (slot-value self 'dimension) dimension))
|
---|
[3293] | 87 |
|
---|
[3298] | 88 | (when exponents-supplied-p
|
---|
| 89 | (let ((dim (length exponents)))
|
---|
[3301] | 90 | (when (/= (slot-value self 'dimension) dim)
|
---|
[3298] | 91 | (error "EXPONENTS must have length DIMENSION"))
|
---|
| 92 | (setf (slot-value self 'dimension) dim
|
---|
| 93 | (slot-value self 'exponents) (make-array dim :initial-contents exponents))
|
---|
| 94 | (setf (slot-value self 'dimension) (length exponents))))
|
---|
[3293] | 95 |
|
---|
[3298] | 96 | ;; when all exponents are to be identical
|
---|
[3299] | 97 | (when exponent-supplied-p
|
---|
[3298] | 98 | (unless (slot-boundp self 'dimension)
|
---|
| 99 | (error "Slot DIMENSION is unbound, but must be known if EXPONENT is supplied."))
|
---|
| 100 | (let ((dim (slot-value self 'dimension)))
|
---|
| 101 | (setf (slot-value self 'exponents)
|
---|
| 102 | (make-array (list dim) :initial-element exponent
|
---|
| 103 | :element-type 'exponent)))))
|
---|
[3293] | 104 |
|
---|
[2850] | 105 | (defmethod r-equalp ((m1 monom) (m2 monom))
|
---|
[2778] | 106 | "Returns T iff monomials M1 and M2 have identical
|
---|
| 107 | EXPONENTS."
|
---|
[2779] | 108 | (equalp (monom-exponents m1) (monom-exponents m2)))
|
---|
[2547] | 109 |
|
---|
[2398] | 110 | (defmethod r-coeff ((m monom))
|
---|
| 111 | "A MONOM can be treated as a special case of TERM,
|
---|
| 112 | where the coefficient is 1."
|
---|
| 113 | 1)
|
---|
[2397] | 114 |
|
---|
[2143] | 115 | (defmethod r-elt ((m monom) index)
|
---|
[48] | 116 | "Return the power in the monomial M of variable number INDEX."
|
---|
[2023] | 117 | (with-slots (exponents)
|
---|
| 118 | m
|
---|
[2154] | 119 | (elt exponents index)))
|
---|
[48] | 120 |
|
---|
[2160] | 121 | (defmethod (setf r-elt) (new-value (m monom) index)
|
---|
[2023] | 122 | "Return the power in the monomial M of variable number INDEX."
|
---|
| 123 | (with-slots (exponents)
|
---|
| 124 | m
|
---|
[2154] | 125 | (setf (elt exponents index) new-value)))
|
---|
[2023] | 126 |
|
---|
[2779] | 127 | (defmethod r-total-degree ((m monom) &optional (start 0) (end (monom-dimension m)))
|
---|
[48] | 128 | "Return the todal degree of a monomoal M. Optinally, a range
|
---|
| 129 | of variables may be specified with arguments START and END."
|
---|
[2023] | 130 | (declare (type fixnum start end))
|
---|
| 131 | (with-slots (exponents)
|
---|
| 132 | m
|
---|
[2154] | 133 | (reduce #'+ exponents :start start :end end)))
|
---|
[48] | 134 |
|
---|
[2064] | 135 |
|
---|
[2779] | 136 | (defmethod r-sugar ((m monom) &aux (start 0) (end (monom-dimension m)))
|
---|
[48] | 137 | "Return the sugar of a monomial M. Optinally, a range
|
---|
| 138 | of variables may be specified with arguments START and END."
|
---|
[2032] | 139 | (declare (type fixnum start end))
|
---|
[2155] | 140 | (r-total-degree m start end))
|
---|
[48] | 141 |
|
---|
[2478] | 142 | (defmethod multiply-by ((self monom) (other monom))
|
---|
[2807] | 143 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2478] | 144 | self
|
---|
[2811] | 145 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
[2478] | 146 | other
|
---|
[2811] | 147 | (unless (= dimension1 dimension2)
|
---|
[2813] | 148 | (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
|
---|
[2811] | 149 | (map-into exponents1 #'+ exponents1 exponents2)))
|
---|
[2480] | 150 | self)
|
---|
[2069] | 151 |
|
---|
[2818] | 152 | (defmethod divide-by ((self monom) (other monom))
|
---|
| 153 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 154 | self
|
---|
| 155 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
| 156 | other
|
---|
| 157 | (unless (= dimension1 dimension2)
|
---|
| 158 | (error "Incompatible dimensions: ~A and ~A.~%" dimension1 dimension2))
|
---|
| 159 | (map-into exponents1 #'- exponents1 exponents2)))
|
---|
| 160 | self)
|
---|
| 161 |
|
---|
[3004] | 162 | (defmethod copy-instance :around ((object monom) &rest initargs &key &allow-other-keys)
|
---|
[3018] | 163 | "An :AROUNT method for COPY-INSTANCE. The primary method is a shallow copy,
|
---|
| 164 | while for monomials we typically need a fresh copy of the
|
---|
| 165 | exponents."
|
---|
[3004] | 166 | (declare (ignore object initargs))
|
---|
[3002] | 167 | (let ((copy (call-next-method)))
|
---|
[3003] | 168 | (setf (monom-exponents copy) (copy-seq (monom-exponents copy)))
|
---|
[3002] | 169 | copy))
|
---|
[2950] | 170 |
|
---|
[2816] | 171 | (defmethod r* ((m1 monom) (m2 monom))
|
---|
| 172 | "Non-destructively multiply monomial M1 by M2."
|
---|
[3032] | 173 | (multiply-by (copy-instance m1) (copy-instance m2)))
|
---|
[2816] | 174 |
|
---|
[2144] | 175 | (defmethod r/ ((m1 monom) (m2 monom))
|
---|
[2819] | 176 | "Non-destructively divide monomial M1 by monomial M2."
|
---|
[3032] | 177 | (divide-by (copy-instance m1) (copy-instance m2)))
|
---|
[48] | 178 |
|
---|
[2144] | 179 | (defmethod r-divides-p ((m1 monom) (m2 monom))
|
---|
[48] | 180 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
[2039] | 181 | (with-slots ((exponents1 exponents))
|
---|
| 182 | m1
|
---|
| 183 | (with-slots ((exponents2 exponents))
|
---|
| 184 | m2
|
---|
| 185 | (every #'<= exponents1 exponents2))))
|
---|
[48] | 186 |
|
---|
[2075] | 187 |
|
---|
[2144] | 188 | (defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
[2055] | 189 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[875] | 190 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
[869] | 191 | m1 m2 m3))
|
---|
[48] | 192 |
|
---|
[2049] | 193 |
|
---|
[2144] | 194 | (defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
[48] | 195 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
[1890] | 196 | (declare (type monom m1 m2 m3 m4))
|
---|
[869] | 197 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 198 | m1 m2 m3 m4))
|
---|
| 199 |
|
---|
[2144] | 200 | (defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
[2075] | 201 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
[2171] | 202 | (with-slots ((exponents1 exponents))
|
---|
[2076] | 203 | m1
|
---|
[2171] | 204 | (with-slots ((exponents2 exponents))
|
---|
[2076] | 205 | m2
|
---|
[2171] | 206 | (with-slots ((exponents3 exponents))
|
---|
[2076] | 207 | m3
|
---|
[2171] | 208 | (with-slots ((exponents4 exponents))
|
---|
[2076] | 209 | m4
|
---|
[2077] | 210 | (every
|
---|
| 211 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 212 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
[48] | 213 |
|
---|
[2144] | 214 | (defmethod r-divisible-by-p ((m1 monom) (m2 monom))
|
---|
[48] | 215 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
[2171] | 216 | (with-slots ((exponents1 exponents))
|
---|
[2144] | 217 | m1
|
---|
[2171] | 218 | (with-slots ((exponents2 exponents))
|
---|
[2144] | 219 | m2
|
---|
| 220 | (every #'>= exponents1 exponents2))))
|
---|
[2078] | 221 |
|
---|
[2146] | 222 | (defmethod r-rel-prime-p ((m1 monom) (m2 monom))
|
---|
[48] | 223 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
[2171] | 224 | (with-slots ((exponents1 exponents))
|
---|
[2078] | 225 | m1
|
---|
[2171] | 226 | (with-slots ((exponents2 exponents))
|
---|
[2078] | 227 | m2
|
---|
[2154] | 228 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
[48] | 229 |
|
---|
[2076] | 230 |
|
---|
[2146] | 231 | (defmethod r-lcm ((m1 monom) (m2 monom))
|
---|
[48] | 232 | "Returns least common multiple of monomials M1 and M2."
|
---|
[2319] | 233 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 234 | m1
|
---|
[2171] | 235 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 236 | m2
|
---|
| 237 | (let* ((exponents (copy-seq exponents1))
|
---|
[2319] | 238 | (dimension dimension1))
|
---|
[2082] | 239 | (map-into exponents #'max exponents1 exponents2)
|
---|
[2200] | 240 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 241 |
|
---|
[2080] | 242 |
|
---|
[2146] | 243 | (defmethod r-gcd ((m1 monom) (m2 monom))
|
---|
[48] | 244 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
[2320] | 245 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 246 | m1
|
---|
[2171] | 247 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 248 | m2
|
---|
| 249 | (let* ((exponents (copy-seq exponents1))
|
---|
[2320] | 250 | (dimension dimension1))
|
---|
[2082] | 251 | (map-into exponents #'min exponents1 exponents2)
|
---|
[2197] | 252 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 253 |
|
---|
[2146] | 254 | (defmethod r-depends-p ((m monom) k)
|
---|
[48] | 255 | "Return T if the monomial M depends on variable number K."
|
---|
[2083] | 256 | (declare (type fixnum k))
|
---|
| 257 | (with-slots (exponents)
|
---|
| 258 | m
|
---|
[2154] | 259 | (plusp (elt exponents k))))
|
---|
[48] | 260 |
|
---|
[3020] | 261 | (defmethod left-tensor-product-by ((self monom) (other monom))
|
---|
[2321] | 262 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[3020] | 263 | self
|
---|
[2321] | 264 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
[3020] | 265 | other
|
---|
| 266 | (setf dimension1 (+ dimension1 dimension2)
|
---|
[3036] | 267 | exponents1 (concatenate 'vector exponents2 exponents1))))
|
---|
| 268 | self)
|
---|
[48] | 269 |
|
---|
[3026] | 270 | (defmethod right-tensor-product-by ((self monom) (other monom))
|
---|
| 271 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 272 | self
|
---|
| 273 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
| 274 | other
|
---|
| 275 | (setf dimension1 (+ dimension1 dimension2)
|
---|
[3036] | 276 | exponents1 (concatenate 'vector exponents1 exponents2))))
|
---|
| 277 | self)
|
---|
[3026] | 278 |
|
---|
[3039] | 279 | (defmethod left-contract ((self monom) k)
|
---|
[1638] | 280 | "Drop the first K variables in monomial M."
|
---|
[2085] | 281 | (declare (fixnum k))
|
---|
[2196] | 282 | (with-slots (dimension exponents)
|
---|
[3040] | 283 | self
|
---|
[2197] | 284 | (setf dimension (- dimension k)
|
---|
[3039] | 285 | exponents (subseq exponents k)))
|
---|
| 286 | self)
|
---|
[886] | 287 |
|
---|
| 288 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 289 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 290 | "Construct a monomial in the polynomial ring
|
---|
| 291 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 292 | which represents a single variable. It assumes number of variables
|
---|
| 293 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 294 | may appear raised to power POWER. "
|
---|
[1924] | 295 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 296 | (with-slots (exponents)
|
---|
| 297 | m
|
---|
[2154] | 298 | (setf (elt exponents pos) power)
|
---|
[2089] | 299 | m))
|
---|
[1151] | 300 |
|
---|
[2150] | 301 | (defmethod r->list ((m monom))
|
---|
[1152] | 302 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2779] | 303 | (coerce (monom-exponents m) 'list))
|
---|
[2780] | 304 |
|
---|
[2783] | 305 | (defmethod r-dimension ((self monom))
|
---|
| 306 | (monom-dimension self))
|
---|
| 307 |
|
---|
[2780] | 308 | (defmethod r-exponents ((self monom))
|
---|
| 309 | (monom-exponents self))
|
---|