| 1 | ;;; -*- Mode: Lisp -*-
|
---|
| 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 |
|
---|
| 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 | ;;
|
---|
| 37 | ;; Monom x*y^2 ---> (1 2)
|
---|
| 38 | ;;
|
---|
| 39 | ;;----------------------------------------------------------------
|
---|
| 40 |
|
---|
| 41 | (defpackage "MONOM"
|
---|
| 42 | (:use :cl :ring)
|
---|
| 43 | (:export "MONOM"
|
---|
| 44 | "EXPONENT"
|
---|
| 45 | "MONOM-DIMENSION"
|
---|
| 46 | "MONOM-EXPONENTS"
|
---|
| 47 | "MAKE-MONOM-VARIABLE"))
|
---|
| 48 |
|
---|
| 49 | (in-package :monom)
|
---|
| 50 |
|
---|
| 51 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
| 52 |
|
---|
| 53 | (deftype exponent ()
|
---|
| 54 | "Type of exponent in a monomial."
|
---|
| 55 | 'fixnum)
|
---|
| 56 |
|
---|
| 57 | (defclass monom ()
|
---|
| 58 | ((dimension :initarg :dimension :accessor monom-dimension)
|
---|
| 59 | (exponents :initarg :exponents :accessor monom-exponents))
|
---|
| 60 | (:default-initargs :dimension nil :exponents nil :exponent nil))
|
---|
| 61 |
|
---|
| 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)))
|
---|
| 66 |
|
---|
| 67 | #|
|
---|
| 68 | ;; Debug calls to initialize-instance
|
---|
| 69 | (defmethod initialize-instance :around ((self monom)
|
---|
| 70 | &rest
|
---|
| 71 | args
|
---|
| 72 | &key
|
---|
| 73 | &allow-other-keys)
|
---|
| 74 | (format t "MONOM::INITIALIZE-INSTANCE called with:~&ARGS: ~W.~%" args)
|
---|
| 75 | (call-next-method)
|
---|
| 76 | )
|
---|
| 77 | |#
|
---|
| 78 |
|
---|
| 79 | (defmethod shared-initialize :after ((self monom) slot-names
|
---|
| 80 | &rest initargs
|
---|
| 81 | &key
|
---|
| 82 | dimension
|
---|
| 83 | exponents
|
---|
| 84 | exponent
|
---|
| 85 | &allow-other-keys
|
---|
| 86 | )
|
---|
| 87 | (format t "MONOM::SHARED-INITIALIZE called with:~&SLOT-NAMES: ~W~&INITARGS: ~W.~%" slot-names initargs)
|
---|
| 88 | (let* ((new-dimension (cond (dimension dimension)
|
---|
| 89 | (exponents
|
---|
| 90 | (length exponents))
|
---|
| 91 | (t
|
---|
| 92 | (error "DIMENSION or EXPONENTS must not be NIL"))))
|
---|
| 93 | (new-exponents (cond
|
---|
| 94 | ;; when exponents are supplied
|
---|
| 95 | (exponents
|
---|
| 96 | (make-array (list new-dimension) :initial-contents exponents))
|
---|
| 97 | ;; when all exponents are to be identical
|
---|
| 98 | (exponent
|
---|
| 99 | (make-array (list new-dimension) :initial-element exponent
|
---|
| 100 | :element-type 'exponent))
|
---|
| 101 | ;; otherwise, all exponents are zero
|
---|
| 102 | (t
|
---|
| 103 | (make-array (list new-dimension) :element-type 'exponent :initial-element 0)))))
|
---|
| 104 | (setf (slot-value self 'dimension) new-dimension
|
---|
| 105 | (slot-value self 'exponents) new-exponents)))
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 110 | ;;
|
---|
| 111 | ;; Operations on monomials
|
---|
| 112 | ;;
|
---|
| 113 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 114 |
|
---|
| 115 | (defmethod r-dimension ((m monom))
|
---|
| 116 | (monom-dimension m))
|
---|
| 117 |
|
---|
| 118 | (defmethod r-elt ((m monom) index)
|
---|
| 119 | "Return the power in the monomial M of variable number INDEX."
|
---|
| 120 | (with-slots (exponents)
|
---|
| 121 | m
|
---|
| 122 | (elt exponents index)))
|
---|
| 123 |
|
---|
| 124 | (defmethod (setf r-elt) (new-value (m monom) index)
|
---|
| 125 | "Return the power in the monomial M of variable number INDEX."
|
---|
| 126 | (with-slots (exponents)
|
---|
| 127 | m
|
---|
| 128 | (setf (elt exponents index) new-value)))
|
---|
| 129 |
|
---|
| 130 | (defmethod r-total-degree ((m monom) &optional (start 0) (end (r-dimension m)))
|
---|
| 131 | "Return the todal degree of a monomoal M. Optinally, a range
|
---|
| 132 | of variables may be specified with arguments START and END."
|
---|
| 133 | (declare (type fixnum start end))
|
---|
| 134 | (with-slots (exponents)
|
---|
| 135 | m
|
---|
| 136 | (reduce #'+ exponents :start start :end end)))
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | (defmethod r-sugar ((m monom) &aux (start 0) (end (r-dimension m)))
|
---|
| 140 | "Return the sugar of a monomial M. Optinally, a range
|
---|
| 141 | of variables may be specified with arguments START and END."
|
---|
| 142 | (declare (type fixnum start end))
|
---|
| 143 | (r-total-degree m start end))
|
---|
| 144 |
|
---|
| 145 | (defmethod r* ((m1 monom) (m2 monom))
|
---|
| 146 | "Multiply monomial M1 by monomial M2."
|
---|
| 147 | (with-slots ((exponents1 exponents) dimension)
|
---|
| 148 | m1
|
---|
| 149 | (with-slots ((exponents2 exponents))
|
---|
| 150 | m2
|
---|
| 151 | (let* ((exponents (copy-seq exponents1)))
|
---|
| 152 | (map-into exponents #'+ exponents1 exponents2)
|
---|
| 153 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | (defmethod r/ ((m1 monom) (m2 monom))
|
---|
| 158 | "Divide monomial M1 by monomial M2."
|
---|
| 159 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 160 | m1
|
---|
| 161 | (with-slots ((exponents2 exponents))
|
---|
| 162 | m2
|
---|
| 163 | (let* ((exponents (copy-seq exponents1))
|
---|
| 164 | (dimension dimension1))
|
---|
| 165 | (map-into exponents #'- exponents1 exponents2)
|
---|
| 166 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
| 167 |
|
---|
| 168 | (defmethod r-divides-p ((m1 monom) (m2 monom))
|
---|
| 169 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
| 170 | (with-slots ((exponents1 exponents))
|
---|
| 171 | m1
|
---|
| 172 | (with-slots ((exponents2 exponents))
|
---|
| 173 | m2
|
---|
| 174 | (every #'<= exponents1 exponents2))))
|
---|
| 175 |
|
---|
| 176 |
|
---|
| 177 | (defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
|
---|
| 178 | "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
|
---|
| 179 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
| 180 | m1 m2 m3))
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 | (defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
|
---|
| 184 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
| 185 | (declare (type monom m1 m2 m3 m4))
|
---|
| 186 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
| 187 | m1 m2 m3 m4))
|
---|
| 188 |
|
---|
| 189 | (defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
|
---|
| 190 | "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
|
---|
| 191 | (with-slots ((exponents1 exponents))
|
---|
| 192 | m1
|
---|
| 193 | (with-slots ((exponents2 exponents))
|
---|
| 194 | m2
|
---|
| 195 | (with-slots ((exponents3 exponents))
|
---|
| 196 | m3
|
---|
| 197 | (with-slots ((exponents4 exponents))
|
---|
| 198 | m4
|
---|
| 199 | (every
|
---|
| 200 | #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
| 201 | exponents1 exponents2 exponents3 exponents4))))))
|
---|
| 202 |
|
---|
| 203 | (defmethod r-divisible-by-p ((m1 monom) (m2 monom))
|
---|
| 204 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
| 205 | (with-slots ((exponents1 exponents))
|
---|
| 206 | m1
|
---|
| 207 | (with-slots ((exponents2 exponents))
|
---|
| 208 | m2
|
---|
| 209 | (every #'>= exponents1 exponents2))))
|
---|
| 210 |
|
---|
| 211 | (defmethod r-rel-prime-p ((m1 monom) (m2 monom))
|
---|
| 212 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
| 213 | (with-slots ((exponents1 exponents))
|
---|
| 214 | m1
|
---|
| 215 | (with-slots ((exponents2 exponents))
|
---|
| 216 | m2
|
---|
| 217 | (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | (defmethod r-equalp ((m1 monom) (m2 monom))
|
---|
| 221 | "Returns T if two monomials M1 and M2 are equal."
|
---|
| 222 | (with-slots ((exponents1 exponents))
|
---|
| 223 | m1
|
---|
| 224 | (with-slots ((exponents2 exponents))
|
---|
| 225 | m2
|
---|
| 226 | (every #'= exponents1 exponents2))))
|
---|
| 227 |
|
---|
| 228 | (defmethod r-lcm ((m1 monom) (m2 monom))
|
---|
| 229 | "Returns least common multiple of monomials M1 and M2."
|
---|
| 230 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 231 | m1
|
---|
| 232 | (with-slots ((exponents2 exponents))
|
---|
| 233 | m2
|
---|
| 234 | (let* ((exponents (copy-seq exponents1))
|
---|
| 235 | (dimension dimension1))
|
---|
| 236 | (map-into exponents #'max exponents1 exponents2)
|
---|
| 237 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | (defmethod r-gcd ((m1 monom) (m2 monom))
|
---|
| 241 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
| 242 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 243 | m1
|
---|
| 244 | (with-slots ((exponents2 exponents))
|
---|
| 245 | m2
|
---|
| 246 | (let* ((exponents (copy-seq exponents1))
|
---|
| 247 | (dimension dimension1))
|
---|
| 248 | (map-into exponents #'min exponents1 exponents2)
|
---|
| 249 | (make-instance 'monom :dimension dimension :exponents exponents)))))
|
---|
| 250 |
|
---|
| 251 | (defmethod r-depends-p ((m monom) k)
|
---|
| 252 | "Return T if the monomial M depends on variable number K."
|
---|
| 253 | (declare (type fixnum k))
|
---|
| 254 | (with-slots (exponents)
|
---|
| 255 | m
|
---|
| 256 | (plusp (elt exponents k))))
|
---|
| 257 |
|
---|
| 258 | (defmethod r-tensor-product ((m1 monom) (m2 monom))
|
---|
| 259 | (with-slots ((exponents1 exponents) (dimension1 dimension))
|
---|
| 260 | m1
|
---|
| 261 | (with-slots ((exponents2 exponents) (dimension2 dimension))
|
---|
| 262 | m2
|
---|
| 263 | (make-instance 'monom
|
---|
| 264 | :dimension (+ dimension1 dimension2)
|
---|
| 265 | :exponents (concatenate 'vector exponents1 exponents2)))))
|
---|
| 266 |
|
---|
| 267 | (defmethod r-contract ((m monom) k)
|
---|
| 268 | "Drop the first K variables in monomial M."
|
---|
| 269 | (declare (fixnum k))
|
---|
| 270 | (with-slots (dimension exponents)
|
---|
| 271 | m
|
---|
| 272 | (setf dimension (- dimension k)
|
---|
| 273 | exponents (subseq exponents k))))
|
---|
| 274 |
|
---|
| 275 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
| 276 | &aux (m (make-instance 'monom :dimension nvars)))
|
---|
| 277 | "Construct a monomial in the polynomial ring
|
---|
| 278 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
| 279 | which represents a single variable. It assumes number of variables
|
---|
| 280 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
| 281 | may appear raised to power POWER. "
|
---|
| 282 | (declare (type fixnum nvars pos power) (type monom m))
|
---|
| 283 | (with-slots (exponents)
|
---|
| 284 | m
|
---|
| 285 | (setf (elt exponents pos) power)
|
---|
| 286 | m))
|
---|
| 287 |
|
---|
| 288 | (defmethod r->list ((m monom))
|
---|
| 289 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
| 290 | (coerce (monom-exponents m) 'list))
|
---|