close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/termlist.lisp@ 4444

Last change on this file since 4444 was 1964, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 8.3 KB
RevLine 
[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."
[1952]56 (mapcar #'(lambda (term)
57 (declare (type term term))
58 (make-term :monom (monom-contract (term-monom term) k)
59 :coeff (term-coeff term)))
[401]60 p))
61
[1961]62(defun termlist-ncontract (p &optional (k 1))
63 "Eliminate first K variables from a polynomial P. Destructive version."
64 (mapc #'(lambda (term)
65 (declare (type term term))
[1964]66 (setf (term-monom term) (monom-contract (term-monom term) k)))
67 p))
[1961]68
[894]69(defun termlist-extend (p &optional (m (make-monom :dimension 1)))
[401]70 "Extend every monomial in a polynomial P by inserting at the
71beginning of every monomial the list of powers M."
[1951]72 (mapcar #'(lambda (term)
73 (declare (type term term))
74 (make-term :monom (monom-append m (term-monom term))
75 :coeff (term-coeff term)))
[401]76 p))
77
78(defun termlist-add-variables (p n)
79 "Add N variables to a polynomial P by inserting zero powers
80at the beginning of each monomial."
81 (declare (fixnum n))
82 (mapcar #'(lambda (term)
[1950]83 (declare (type term term))
[1831]84 (make-term :monom (monom-append (make-monom :dimension n)
85 (term-monom term))
86 :coeff (term-coeff term)))
[401]87 p))
88
89
[150]90;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91;;
92;; Low-level polynomial arithmetic done on
93;; lists of terms
94;;
95;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96
97(defmacro termlist-lt (p) `(car ,p))
98(defun termlist-lm (p) (term-monom (termlist-lt p)))
99(defun termlist-lc (p) (term-coeff (termlist-lt p)))
100
101(define-modify-macro scalar-mul (c) coeff-mul)
102
103(defun scalar-times-termlist (ring c p)
104 "Multiply scalar C by a polynomial P. This function works
105even if there are divisors of 0."
[706]106 (declare (ring ring))
[150]107 (mapcan
108 #'(lambda (term)
109 (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
110 (unless (funcall (ring-zerop ring) c1)
[1832]111 (list (make-term :monom (term-monom term) :coeff c1)))))
[150]112 p))
113
114
[379]115(defun term-mul-lst (ring term1 term2)
[380]116 "A special version of term multiplication. Returns (LIST TERM) where
117TERM is the product of the terms TERM1 TERM2, or NIL when the product
118is 0. This definition takes care of divisors of 0 in the coefficient
119ring."
[1953]120 (declare (ring ring) (type term term1 term2))
[150]121 (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
122 (unless (funcall (ring-zerop ring) c)
[1833]123 (list (make-term :monom (monom-mul (term-monom term1) (term-monom term2))
124 :coeff c)))))
[150]125
126(defun term-times-termlist (ring term f)
[1948]127 (declare (type ring ring) (type term term))
[379]128 (mapcan #'(lambda (term-f) (term-mul-lst ring term term-f)) f))
[150]129
130(defun termlist-times-term (ring f term)
[1947]131 (declare (ring ring) (type term term))
[379]132 (mapcan #'(lambda (term-f) (term-mul-lst ring term-f term)) f))
[150]133
134(defun monom-times-term (m term)
[1946]135 (declare (type monom m) (type term term))
[1834]136 (make-term :monom (monom-mul m (term-monom term)) :coeff (term-coeff term)))
[150]137
138(defun monom-times-termlist (m f)
[1945]139 (declare (type monom m))
[150]140 (cond
[1227]141 ((null f) nil)
142 (t
143 (mapcar #'(lambda (x) (monom-times-term m x)) f))))
[150]144
145(defun termlist-uminus (ring f)
[705]146 (declare (ring ring))
[150]147 (mapcar #'(lambda (x)
[1835]148 (make-term :monom (term-monom x)
149 :coeff (funcall (ring-uminus ring) (term-coeff x))))
[150]150 f))
151
[935]152(defun termlist-add (ring-and-order p q
153 &aux
154 (ring (ro-ring ring-and-order))
155 (order (ro-order ring-and-order)))
[1944]156 (declare (ring-and-order ring-and-order) (type list p q))
[150]157 (do (r)
158 ((cond
[935]159 ((endp p)
160 (setf r (revappend r q)) t)
161 ((endp q)
162 (setf r (revappend r p)) t)
163 (t
164 (multiple-value-bind
165 (lm-greater lm-equal)
166 (funcall order (termlist-lm p) (termlist-lm q))
167 (cond
168 (lm-equal
169 (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
170 (unless (funcall (ring-zerop ring) s) ;check for cancellation
[1836]171 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
[935]172 (setf p (cdr p) q (cdr q))))
173 (lm-greater
174 (setf r (cons (car p) r)
175 p (cdr p)))
176 (t (setf r (cons (car q) r)
177 q (cdr q)))))
178 nil))
[150]179 r)))
180
[942]181(defun termlist-sub (ring-and-order p q
[935]182 &aux
183 (ring (ro-ring ring-and-order))
184 (order (ro-order ring-and-order)))
[1943]185 (declare (ring-and-order ring-and-order) (type list p q))
[150]186 (do (r)
187 ((cond
[935]188 ((endp p)
189 (setf r (revappend r (termlist-uminus ring q)))
190 t)
191 ((endp q)
192 (setf r (revappend r p))
193 t)
194 (t
195 (multiple-value-bind
196 (mgreater mequal)
197 (funcall order (termlist-lm p) (termlist-lm q))
198 (cond
199 (mequal
200 (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
201 (unless (funcall (ring-zerop ring) s) ;check for cancellation
[1837]202 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
[935]203 (setf p (cdr p) q (cdr q))))
204 (mgreater
205 (setf r (cons (car p) r)
206 p (cdr p)))
[1838]207 (t (setf r (cons (make-term :monom (termlist-lm q)
208 :coeff (funcall (ring-uminus ring) (termlist-lc q))) r)
[935]209 q (cdr q)))))
210 nil))
[150]211 r)))
212
213;; Multiplication of polynomials
214;; Non-destructive version
[936]215(defun termlist-mul (ring-and-order p q
[941]216 &aux (ring (ro-ring ring-and-order)))
[936]217 (declare (ring-and-order ring-and-order))
[150]218 (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
219 ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
220 ((endp (cdr p))
221 (term-times-termlist ring (car p) q))
222 ((endp (cdr q))
223 (termlist-times-term ring p (car q)))
224 (t
[379]225 (let ((head (term-mul-lst ring (termlist-lt p) (termlist-lt q)))
[936]226 (tail (termlist-add ring-and-order
227 (term-times-termlist ring (car p) (cdr q))
228 (termlist-mul ring-and-order (cdr p) q))))
[150]229 (cond ((null head) tail)
230 ((null tail) head)
231 (t (nconc head tail)))))))
[1227]232
[815]233(defun termlist-unit (ring dim)
[1942]234 (declare (ring ring) (fixnum dim))
[1839]235 (list (make-term :monom (make-monom :dimension dim)
236 :coeff (funcall (ring-unit ring)))))
[150]237
[1227]238
[937]239(defun termlist-expt (ring-and-order poly n
240 &aux
241 (ring (ro-ring ring-and-order))
242 (dim (monom-dimension (termlist-lm poly))))
[1942]243 (declare (ring-and-order ring-and-order) (type fixnum n dim))
[150]244 (cond
[1227]245 ((minusp n) (error "termlist-expt: Negative exponent."))
246 ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
247 (t
248 (do ((k 1 (ash k 1))
249 (q poly (termlist-mul ring-and-order q q)) ;keep squaring
250 (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring-and-order p q) p)))
251 ((> k n) p)
252 (declare (fixnum k))))))
Note: See TracBrowser for help on using the repository browser.