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