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