[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 |
|
---|
[418] | 22 | ;;----------------------------------------------------------------
|
---|
| 23 | ;; This package implements BASIC OPERATIONS ON MONOMIALS
|
---|
| 24 | ;;----------------------------------------------------------------
|
---|
| 25 | ;; DATA STRUCTURES: Conceptually, monomials can be represented as lists:
|
---|
| 26 | ;;
|
---|
| 27 | ;; monom: (n1 n2 ... nk) where ni are non-negative integers
|
---|
| 28 | ;;
|
---|
| 29 | ;; However, lists may be implemented as other sequence types,
|
---|
| 30 | ;; so the flexibility to change the representation should be
|
---|
| 31 | ;; maintained in the code to use general operations on sequences
|
---|
| 32 | ;; whenever possible. The optimization for the actual representation
|
---|
| 33 | ;; should be left to declarations and the compiler.
|
---|
| 34 | ;;----------------------------------------------------------------
|
---|
| 35 | ;; EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 36 | ;;
|
---|
[714] | 37 | ;; Monom x*y^2 ---> (1 2)
|
---|
[418] | 38 | ;;
|
---|
| 39 | ;;----------------------------------------------------------------
|
---|
| 40 |
|
---|
[1610] | 41 | (defpackage "MONOM"
|
---|
[2025] | 42 | (:use :cl :ring)
|
---|
[422] | 43 | (:export "MONOM"
|
---|
[423] | 44 | "EXPONENT"
|
---|
[2125] | 45 | "MONOM-DIMENSION"
|
---|
[2124] | 46 | "MONOM-EXPONENTS"
|
---|
| 47 | "MAKE-MONOM-VARIABLE"))
|
---|
[81] | 48 |
|
---|
[1610] | 49 | (in-package :monom)
|
---|
[48] | 50 |
|
---|
[1925] | 51 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[1923] | 52 |
|
---|
[48] | 53 | (deftype exponent ()
|
---|
| 54 | "Type of exponent in a monomial."
|
---|
| 55 | 'fixnum)
|
---|
| 56 |
|
---|
[2022] | 57 | (defclass monom ()
|
---|
[2193] | 58 | ((dimension :initarg :dimension :accessor monom-dimension)
|
---|
[2125] | 59 | (exponents :initarg :exponents :accessor monom-exponents))
|
---|
[2268] | 60 | (:default-initargs :dimension nil :exponents nil :exponent nil))
|
---|
[880] | 61 |
|
---|
[2245] | 62 | (defmethod print-object ((self monom) stream)
|
---|
| 63 | (format stream "#<MONOM DIMENSION=~A EXPONENTS=~A>"
|
---|
| 64 | (slot-value self 'dimension)
|
---|
| 65 | (slot-value self 'exponents)))
|
---|
[2027] | 66 |
|
---|
[2309] | 67 | #|
|
---|
| 68 | ;; Debug calls to initialize-instance
|
---|
[2298] | 69 | (defmethod initialize-instance :around ((self monom)
|
---|
[2281] | 70 | &rest
|
---|
| 71 | args
|
---|
| 72 | &key
|
---|
| 73 | &allow-other-keys)
|
---|
[2296] | 74 | (format t "MONOM::INITIALIZE-INSTANCE called with:~&ARGS: ~W.~%" args)
|
---|
[2297] | 75 | (call-next-method)
|
---|
| 76 | )
|
---|
[2309] | 77 | |#
|
---|
[2278] | 78 |
|
---|
[2282] | 79 | (defmethod initialize-instance ((self monom)
|
---|
| 80 | ;;&rest args
|
---|
| 81 | &key
|
---|
| 82 | dimension
|
---|
| 83 | exponents
|
---|
| 84 | exponent
|
---|
| 85 | &allow-other-keys
|
---|
| 86 | )
|
---|
[2269] | 87 | (let* ((new-dimension (cond (dimension dimension)
|
---|
| 88 | (exponents
|
---|
[2261] | 89 | (length exponents))
|
---|
| 90 | (t
|
---|
[2269] | 91 | (error "DIMENSION or EXPONENTS must not be NIL"))))
|
---|
[2261] | 92 | (new-exponents (cond
|
---|
| 93 | ;; when exponents are supplied
|
---|
[2269] | 94 | (exponents
|
---|
| 95 | (make-array (list new-dimension) :initial-contents exponents))
|
---|
[2261] | 96 | ;; when all exponents are to be identical
|
---|
[2269] | 97 | (exponent
|
---|
[2261] | 98 | (make-array (list new-dimension) :initial-element exponent
|
---|
| 99 | :element-type 'exponent))
|
---|
| 100 | ;; otherwise, all exponents are zero
|
---|
| 101 | (t
|
---|
[2262] | 102 | (make-array (list new-dimension) :element-type 'exponent :initial-element 0)))))
|
---|
[2267] | 103 | (setf (slot-value self 'dimension) new-dimension
|
---|
[2294] | 104 | (slot-value self 'exponents) new-exponents)))
|
---|
[717] | 105 |
|
---|
[2221] | 106 |
|
---|
[2225] | 107 |
|
---|
[48] | 108 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 109 | ;;
|
---|
| 110 | ;; Operations on monomials
|
---|
| 111 | ;;
|
---|
| 112 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 113 |
|
---|
[2143] | 114 | (defmethod r-dimension ((m monom))
|
---|
[2126] | 115 | (monom-dimension m))
|
---|
[745] | 116 |
|
---|
[2143] | 117 | (defmethod r-elt ((m monom) index)
|
---|
[48] | 118 | "Return the power in the monomial M of variable number INDEX."
|
---|
[2023] | 119 | (with-slots (exponents)
|
---|
| 120 | m
|
---|
[2154] | 121 | (elt exponents index)))
|
---|
[48] | 122 |
|
---|
[2160] | 123 | (defmethod (setf r-elt) (new-value (m monom) index)
|
---|
[2023] | 124 | "Return the power in the monomial M of variable number INDEX."
|
---|
| 125 | (with-slots (exponents)
|
---|
| 126 | m
|
---|
[2154] | 127 | (setf (elt exponents index) new-value)))
|
---|
[2023] | 128 |
|
---|
[2149] | 129 | (defmethod r-total-degree ((m monom) &optional (start 0) (end (r-dimension m)))
|
---|
[48] | 130 | "Return the todal degree of a monomoal M. Optinally, a range
|
---|
| 131 | of variables may be specified with arguments START and END."
|
---|
[2023] | 132 | (declare (type fixnum start end))
|
---|
| 133 | (with-slots (exponents)
|
---|
| 134 | m
|
---|
[2154] | 135 | (reduce #'+ exponents :start start :end end)))
|
---|
[48] | 136 |
|
---|
[2064] | 137 |
|
---|
[2149] | 138 | (defmethod r-sugar ((m monom) &aux (start 0) (end (r-dimension m)))
|
---|
[48] | 139 | "Return the sugar of a monomial M. Optinally, a range
|
---|
| 140 | of variables may be specified with arguments START and END."
|
---|
[2032] | 141 | (declare (type fixnum start end))
|
---|
[2155] | 142 | (r-total-degree m start end))
|
---|
[48] | 143 |
|
---|
[2144] | 144 | (defmethod r* ((m1 monom) (m2 monom))
|
---|
[2072] | 145 | "Multiply monomial M1 by monomial M2."
|
---|
[2195] | 146 | (with-slots ((exponents1 exponents) dimension)
|
---|
[2038] | 147 | m1
|
---|
[2170] | 148 | (with-slots ((exponents2 exponents))
|
---|
[2038] | 149 | m2
|
---|
[2167] | 150 | (let* ((exponents (copy-seq exponents1)))
|
---|
[2154] | 151 | (map-into exponents #'+ exponents1 exponents2)
|
---|
[2195] | 152 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[2038] | 153 |
|
---|
[2069] | 154 |
|
---|
| 155 |
|
---|
[2144] | 156 | (defmethod r/ ((m1 monom) (m2 monom))
|
---|
[1896] | 157 | "Divide monomial M1 by monomial M2."
|
---|
[2313] | 158 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2034] | 159 | m1
|
---|
[2037] | 160 | (with-slots ((exponents2 exponents))
|
---|
[2034] | 161 | m2
|
---|
| 162 | (let* ((exponents (copy-seq exponents1))
|
---|
[2314] | 163 | (dimension dimension1))
|
---|
[2154] | 164 | (map-into exponents #'- exponents1 exponents2)
|
---|
[2195] | 165 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 166 |
|
---|
[2144] | 167 | (defmethod r-divides-p ((m1 monom) (m2 monom))
|
---|
[48] | 168 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
[2039] | 169 | (with-slots ((exponents1 exponents))
|
---|
| 170 | m1
|
---|
| 171 | (with-slots ((exponents2 exponents))
|
---|
| 172 | m2
|
---|
| 173 | (every #'<= exponents1 exponents2))))
|
---|
[48] | 174 |
|
---|
[2075] | 175 |
|
---|
[2144] | 176 | (defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
[2055] | 177 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
[875] | 178 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
[869] | 179 | m1 m2 m3))
|
---|
[48] | 180 |
|
---|
[2049] | 181 |
|
---|
[2144] | 182 | (defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
[48] | 183 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
[1890] | 184 | (declare (type monom m1 m2 m3 m4))
|
---|
[869] | 185 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 186 | m1 m2 m3 m4))
|
---|
| 187 |
|
---|
[2144] | 188 | (defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
[2075] | 189 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
[2171] | 190 | (with-slots ((exponents1 exponents))
|
---|
[2076] | 191 | m1
|
---|
[2171] | 192 | (with-slots ((exponents2 exponents))
|
---|
[2076] | 193 | m2
|
---|
[2171] | 194 | (with-slots ((exponents3 exponents))
|
---|
[2076] | 195 | m3
|
---|
[2171] | 196 | (with-slots ((exponents4 exponents))
|
---|
[2076] | 197 | m4
|
---|
[2077] | 198 | (every
|
---|
| 199 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 200 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
[48] | 201 |
|
---|
[2144] | 202 | (defmethod r-divisible-by-p ((m1 monom) (m2 monom))
|
---|
[48] | 203 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
[2171] | 204 | (with-slots ((exponents1 exponents))
|
---|
[2144] | 205 | m1
|
---|
[2171] | 206 | (with-slots ((exponents2 exponents))
|
---|
[2144] | 207 | m2
|
---|
| 208 | (every #'>= exponents1 exponents2))))
|
---|
[2078] | 209 |
|
---|
[2146] | 210 | (defmethod r-rel-prime-p ((m1 monom) (m2 monom))
|
---|
[48] | 211 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
[2171] | 212 | (with-slots ((exponents1 exponents))
|
---|
[2078] | 213 | m1
|
---|
[2171] | 214 | (with-slots ((exponents2 exponents))
|
---|
[2078] | 215 | m2
|
---|
[2154] | 216 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
[48] | 217 |
|
---|
[2076] | 218 |
|
---|
[2163] | 219 | (defmethod r-equalp ((m1 monom) (m2 monom))
|
---|
[48] | 220 | "Returns T if two monomials M1 and M2 are equal."
|
---|
[2171] | 221 | (with-slots ((exponents1 exponents))
|
---|
[2079] | 222 | m1
|
---|
[2171] | 223 | (with-slots ((exponents2 exponents))
|
---|
[2079] | 224 | m2
|
---|
| 225 | (every #'= exponents1 exponents2))))
|
---|
[48] | 226 |
|
---|
[2146] | 227 | (defmethod r-lcm ((m1 monom) (m2 monom))
|
---|
[48] | 228 | "Returns least common multiple of monomials M1 and M2."
|
---|
[2319] | 229 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 230 | m1
|
---|
[2171] | 231 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 232 | m2
|
---|
| 233 | (let* ((exponents (copy-seq exponents1))
|
---|
[2319] | 234 | (dimension dimension1))
|
---|
[2082] | 235 | (map-into exponents #'max exponents1 exponents2)
|
---|
[2200] | 236 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 237 |
|
---|
[2080] | 238 |
|
---|
[2146] | 239 | (defmethod r-gcd ((m1 monom) (m2 monom))
|
---|
[48] | 240 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
[2320] | 241 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2082] | 242 | m1
|
---|
[2171] | 243 | (with-slots ((exponents2 exponents))
|
---|
[2082] | 244 | m2
|
---|
| 245 | (let* ((exponents (copy-seq exponents1))
|
---|
[2320] | 246 | (dimension dimension1))
|
---|
[2082] | 247 | (map-into exponents #'min exponents1 exponents2)
|
---|
[2197] | 248 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
[48] | 249 |
|
---|
[2146] | 250 | (defmethod r-depends-p ((m monom) k)
|
---|
[48] | 251 | "Return T if the monomial M depends on variable number K."
|
---|
[2083] | 252 | (declare (type fixnum k))
|
---|
| 253 | (with-slots (exponents)
|
---|
| 254 | m
|
---|
[2154] | 255 | (plusp (elt exponents k))))
|
---|
[48] | 256 |
|
---|
[2321] | 257 | (defmethod r-tensor-product ((m1 monom) (m2 monom))
|
---|
| 258 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
[2087] | 259 | m1
|
---|
[2321] | 260 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
[2087] | 261 | m2
|
---|
[2147] | 262 | (make-instance 'monom
|
---|
[2321] | 263 | :dimension (+ dimension1 dimension2)
|
---|
[2147] | 264 | :exponents (concatenate 'vector exponents1 exponents2)))))
|
---|
[48] | 265 |
|
---|
[2148] | 266 | (defmethod r-contract ((m monom) k)
|
---|
[1638] | 267 | "Drop the first K variables in monomial M."
|
---|
[2085] | 268 | (declare (fixnum k))
|
---|
[2196] | 269 | (with-slots (dimension exponents)
|
---|
[2085] | 270 | m
|
---|
[2197] | 271 | (setf dimension (- dimension k)
|
---|
[2085] | 272 | exponents (subseq exponents k))))
|
---|
[886] | 273 |
|
---|
| 274 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
[2218] | 275 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
[886] | 276 | "Construct a monomial in the polynomial ring
|
---|
| 277 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 278 | which represents a single variable. It assumes number of variables
|
---|
| 279 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 280 | may appear raised to power POWER. "
|
---|
[1924] | 281 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
[2089] | 282 | (with-slots (exponents)
|
---|
| 283 | m
|
---|
[2154] | 284 | (setf (elt exponents pos) power)
|
---|
[2089] | 285 | m))
|
---|
[1151] | 286 |
|
---|
[2150] | 287 | (defmethod r->list ((m monom))
|
---|
[1152] | 288 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
[2148] | 289 | (coerce (monom-exponents m) 'list))
|
---|