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