[1201] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
[150] | 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 |
|
---|
[411] | 22 | (defpackage "TERMLIST"
|
---|
[1604] | 23 | (:use :cl :monom :ring :ring-and-order :term)
|
---|
[411] | 24 | (:export "TERMLIST-SUGAR"
|
---|
| 25 | "TERMLIST-CONTRACT"
|
---|
| 26 | "TERMLIST-EXTEND"
|
---|
| 27 | "TERMLIST-ADD-VARIABLES"
|
---|
| 28 | "TERMLIST-LT"
|
---|
| 29 | "TERMLIST-LM"
|
---|
| 30 | "TERMLIST-LC"
|
---|
| 31 | "SCALAR-MUL"
|
---|
| 32 | "SCALAR-TIMES-TERMLIST"
|
---|
| 33 | "TERM-MUL-LST"
|
---|
| 34 | "TERMLIST-TIMES-TERM"
|
---|
| 35 | "TERM-TIMES-TERMLIST"
|
---|
| 36 | "MONOM-TIMES-TERM"
|
---|
| 37 | "MONOM-TIMES-TERMLIST"
|
---|
| 38 | "TERMLIST-UMINUS"
|
---|
| 39 | "TERMLIST-ADD"
|
---|
| 40 | "TERMLIST-SUB"
|
---|
| 41 | "TERMLIST-MUL"
|
---|
| 42 | "TERMLIST-UNIT"
|
---|
| 43 | "TERMLIST-EXPT"))
|
---|
[150] | 44 |
|
---|
[436] | 45 | (in-package :termlist)
|
---|
| 46 |
|
---|
[1928] | 47 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
| 48 |
|
---|
[401] | 49 | (defun termlist-sugar (p &aux (sugar -1))
|
---|
| 50 | (declare (fixnum sugar))
|
---|
| 51 | (dolist (term p sugar)
|
---|
| 52 | (setf sugar (max sugar (term-sugar term)))))
|
---|
| 53 |
|
---|
| 54 | (defun termlist-contract (p &optional (k 1))
|
---|
| 55 | "Eliminate first K variables from a polynomial P."
|
---|
[1829] | 56 | (mapcar #'(lambda (term) (make-term :monom (monom-contract (term-monom term) k)
|
---|
| 57 | :coeff (term-coeff term)))
|
---|
[401] | 58 | p))
|
---|
| 59 |
|
---|
[894] | 60 | (defun termlist-extend (p &optional (m (make-monom :dimension 1)))
|
---|
[401] | 61 | "Extend every monomial in a polynomial P by inserting at the
|
---|
| 62 | beginning of every monomial the list of powers M."
|
---|
[1830] | 63 | (mapcar #'(lambda (term) (make-term :monom (monom-append m (term-monom term))
|
---|
| 64 | :coeff (term-coeff term)))
|
---|
[401] | 65 | p))
|
---|
| 66 |
|
---|
| 67 | (defun termlist-add-variables (p n)
|
---|
| 68 | "Add N variables to a polynomial P by inserting zero powers
|
---|
| 69 | at the beginning of each monomial."
|
---|
| 70 | (declare (fixnum n))
|
---|
| 71 | (mapcar #'(lambda (term)
|
---|
[1831] | 72 | (make-term :monom (monom-append (make-monom :dimension n)
|
---|
| 73 | (term-monom term))
|
---|
| 74 | :coeff (term-coeff term)))
|
---|
[401] | 75 | p))
|
---|
| 76 |
|
---|
| 77 |
|
---|
[150] | 78 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 79 | ;;
|
---|
| 80 | ;; Low-level polynomial arithmetic done on
|
---|
| 81 | ;; lists of terms
|
---|
| 82 | ;;
|
---|
| 83 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 84 |
|
---|
| 85 | (defmacro termlist-lt (p) `(car ,p))
|
---|
| 86 | (defun termlist-lm (p) (term-monom (termlist-lt p)))
|
---|
| 87 | (defun termlist-lc (p) (term-coeff (termlist-lt p)))
|
---|
| 88 |
|
---|
| 89 | (define-modify-macro scalar-mul (c) coeff-mul)
|
---|
| 90 |
|
---|
| 91 | (defun scalar-times-termlist (ring c p)
|
---|
| 92 | "Multiply scalar C by a polynomial P. This function works
|
---|
| 93 | even if there are divisors of 0."
|
---|
[706] | 94 | (declare (ring ring))
|
---|
[150] | 95 | (mapcan
|
---|
| 96 | #'(lambda (term)
|
---|
| 97 | (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
|
---|
| 98 | (unless (funcall (ring-zerop ring) c1)
|
---|
[1832] | 99 | (list (make-term :monom (term-monom term) :coeff c1)))))
|
---|
[150] | 100 | p))
|
---|
| 101 |
|
---|
| 102 |
|
---|
[379] | 103 | (defun term-mul-lst (ring term1 term2)
|
---|
[380] | 104 | "A special version of term multiplication. Returns (LIST TERM) where
|
---|
| 105 | TERM is the product of the terms TERM1 TERM2, or NIL when the product
|
---|
| 106 | is 0. This definition takes care of divisors of 0 in the coefficient
|
---|
| 107 | ring."
|
---|
[705] | 108 | (declare (ring ring))
|
---|
[150] | 109 | (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
|
---|
| 110 | (unless (funcall (ring-zerop ring) c)
|
---|
[1833] | 111 | (list (make-term :monom (monom-mul (term-monom term1) (term-monom term2))
|
---|
| 112 | :coeff c)))))
|
---|
[150] | 113 |
|
---|
| 114 | (defun term-times-termlist (ring term f)
|
---|
| 115 | (declare (type ring ring))
|
---|
[379] | 116 | (mapcan #'(lambda (term-f) (term-mul-lst ring term term-f)) f))
|
---|
[150] | 117 |
|
---|
| 118 | (defun termlist-times-term (ring f term)
|
---|
[705] | 119 | (declare (ring ring))
|
---|
[379] | 120 | (mapcan #'(lambda (term-f) (term-mul-lst ring term-f term)) f))
|
---|
[150] | 121 |
|
---|
| 122 | (defun monom-times-term (m term)
|
---|
[1834] | 123 | (make-term :monom (monom-mul m (term-monom term)) :coeff (term-coeff term)))
|
---|
[150] | 124 |
|
---|
| 125 | (defun monom-times-termlist (m f)
|
---|
| 126 | (cond
|
---|
[1227] | 127 | ((null f) nil)
|
---|
| 128 | (t
|
---|
| 129 | (mapcar #'(lambda (x) (monom-times-term m x)) f))))
|
---|
[150] | 130 |
|
---|
| 131 | (defun termlist-uminus (ring f)
|
---|
[705] | 132 | (declare (ring ring))
|
---|
[150] | 133 | (mapcar #'(lambda (x)
|
---|
[1835] | 134 | (make-term :monom (term-monom x)
|
---|
| 135 | :coeff (funcall (ring-uminus ring) (term-coeff x))))
|
---|
[150] | 136 | f))
|
---|
| 137 |
|
---|
[935] | 138 | (defun termlist-add (ring-and-order p q
|
---|
| 139 | &aux
|
---|
| 140 | (ring (ro-ring ring-and-order))
|
---|
| 141 | (order (ro-order ring-and-order)))
|
---|
| 142 | (declare (type list p q) (ring-and-order ring-and-order))
|
---|
[150] | 143 | (do (r)
|
---|
| 144 | ((cond
|
---|
[935] | 145 | ((endp p)
|
---|
| 146 | (setf r (revappend r q)) t)
|
---|
| 147 | ((endp q)
|
---|
| 148 | (setf r (revappend r p)) t)
|
---|
| 149 | (t
|
---|
| 150 | (multiple-value-bind
|
---|
| 151 | (lm-greater lm-equal)
|
---|
| 152 | (funcall order (termlist-lm p) (termlist-lm q))
|
---|
| 153 | (cond
|
---|
| 154 | (lm-equal
|
---|
| 155 | (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
|
---|
| 156 | (unless (funcall (ring-zerop ring) s) ;check for cancellation
|
---|
[1836] | 157 | (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
|
---|
[935] | 158 | (setf p (cdr p) q (cdr q))))
|
---|
| 159 | (lm-greater
|
---|
| 160 | (setf r (cons (car p) r)
|
---|
| 161 | p (cdr p)))
|
---|
| 162 | (t (setf r (cons (car q) r)
|
---|
| 163 | q (cdr q)))))
|
---|
| 164 | nil))
|
---|
[150] | 165 | r)))
|
---|
| 166 |
|
---|
[942] | 167 | (defun termlist-sub (ring-and-order p q
|
---|
[935] | 168 | &aux
|
---|
| 169 | (ring (ro-ring ring-and-order))
|
---|
| 170 | (order (ro-order ring-and-order)))
|
---|
[943] | 171 | (declare (type list p q) (ring-and-order ring-and-order))
|
---|
[150] | 172 | (do (r)
|
---|
| 173 | ((cond
|
---|
[935] | 174 | ((endp p)
|
---|
| 175 | (setf r (revappend r (termlist-uminus ring q)))
|
---|
| 176 | t)
|
---|
| 177 | ((endp q)
|
---|
| 178 | (setf r (revappend r p))
|
---|
| 179 | t)
|
---|
| 180 | (t
|
---|
| 181 | (multiple-value-bind
|
---|
| 182 | (mgreater mequal)
|
---|
| 183 | (funcall order (termlist-lm p) (termlist-lm q))
|
---|
| 184 | (cond
|
---|
| 185 | (mequal
|
---|
| 186 | (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
|
---|
| 187 | (unless (funcall (ring-zerop ring) s) ;check for cancellation
|
---|
[1837] | 188 | (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
|
---|
[935] | 189 | (setf p (cdr p) q (cdr q))))
|
---|
| 190 | (mgreater
|
---|
| 191 | (setf r (cons (car p) r)
|
---|
| 192 | p (cdr p)))
|
---|
[1838] | 193 | (t (setf r (cons (make-term :monom (termlist-lm q)
|
---|
| 194 | :coeff (funcall (ring-uminus ring) (termlist-lc q))) r)
|
---|
[935] | 195 | q (cdr q)))))
|
---|
| 196 | nil))
|
---|
[150] | 197 | r)))
|
---|
| 198 |
|
---|
| 199 | ;; Multiplication of polynomials
|
---|
| 200 | ;; Non-destructive version
|
---|
[936] | 201 | (defun termlist-mul (ring-and-order p q
|
---|
[941] | 202 | &aux (ring (ro-ring ring-and-order)))
|
---|
[936] | 203 | (declare (ring-and-order ring-and-order))
|
---|
[150] | 204 | (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
|
---|
| 205 | ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
|
---|
| 206 | ((endp (cdr p))
|
---|
| 207 | (term-times-termlist ring (car p) q))
|
---|
| 208 | ((endp (cdr q))
|
---|
| 209 | (termlist-times-term ring p (car q)))
|
---|
| 210 | (t
|
---|
[379] | 211 | (let ((head (term-mul-lst ring (termlist-lt p) (termlist-lt q)))
|
---|
[936] | 212 | (tail (termlist-add ring-and-order
|
---|
| 213 | (term-times-termlist ring (car p) (cdr q))
|
---|
| 214 | (termlist-mul ring-and-order (cdr p) q))))
|
---|
[150] | 215 | (cond ((null head) tail)
|
---|
| 216 | ((null tail) head)
|
---|
| 217 | (t (nconc head tail)))))))
|
---|
[1227] | 218 |
|
---|
[815] | 219 | (defun termlist-unit (ring dim)
|
---|
| 220 | (declare (fixnum dim) (ring ring))
|
---|
[1839] | 221 | (list (make-term :monom (make-monom :dimension dim)
|
---|
| 222 | :coeff (funcall (ring-unit ring)))))
|
---|
[150] | 223 |
|
---|
[1227] | 224 |
|
---|
[937] | 225 | (defun termlist-expt (ring-and-order poly n
|
---|
| 226 | &aux
|
---|
| 227 | (ring (ro-ring ring-and-order))
|
---|
| 228 | (dim (monom-dimension (termlist-lm poly))))
|
---|
| 229 | (declare (type fixnum n dim) (ring-and-order ring-and-order))
|
---|
[150] | 230 | (cond
|
---|
[1227] | 231 | ((minusp n) (error "termlist-expt: Negative exponent."))
|
---|
| 232 | ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
|
---|
| 233 | (t
|
---|
| 234 | (do ((k 1 (ash k 1))
|
---|
| 235 | (q poly (termlist-mul ring-and-order q q)) ;keep squaring
|
---|
| 236 | (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring-and-order p q) p)))
|
---|
| 237 | ((> k n) p)
|
---|
| 238 | (declare (fixnum k))))))
|
---|