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