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@ 1948

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

* empty log message *

File size: 7.9 KB
Line 
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(defpackage "TERMLIST"
23 (:use :cl :monom :ring :ring-and-order :term)
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"))
44
45(in-package :termlist)
46
47(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
48
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."
56 (mapcar #'(lambda (term) (make-term :monom (monom-contract (term-monom term) k)
57 :coeff (term-coeff term)))
58 p))
59
60(defun termlist-extend (p &optional (m (make-monom :dimension 1)))
61 "Extend every monomial in a polynomial P by inserting at the
62beginning of every monomial the list of powers M."
63 (mapcar #'(lambda (term) (make-term :monom (monom-append m (term-monom term))
64 :coeff (term-coeff term)))
65 p))
66
67(defun termlist-add-variables (p n)
68 "Add N variables to a polynomial P by inserting zero powers
69at the beginning of each monomial."
70 (declare (fixnum n))
71 (mapcar #'(lambda (term)
72 (make-term :monom (monom-append (make-monom :dimension n)
73 (term-monom term))
74 :coeff (term-coeff term)))
75 p))
76
77
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
93even if there are divisors of 0."
94 (declare (ring ring))
95 (mapcan
96 #'(lambda (term)
97 (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
98 (unless (funcall (ring-zerop ring) c1)
99 (list (make-term :monom (term-monom term) :coeff c1)))))
100 p))
101
102
103(defun term-mul-lst (ring term1 term2)
104 "A special version of term multiplication. Returns (LIST TERM) where
105TERM is the product of the terms TERM1 TERM2, or NIL when the product
106is 0. This definition takes care of divisors of 0 in the coefficient
107ring."
108 (declare (ring ring))
109 (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
110 (unless (funcall (ring-zerop ring) c)
111 (list (make-term :monom (monom-mul (term-monom term1) (term-monom term2))
112 :coeff c)))))
113
114(defun term-times-termlist (ring term f)
115 (declare (type ring ring) (type term term))
116 (mapcan #'(lambda (term-f) (term-mul-lst ring term term-f)) f))
117
118(defun termlist-times-term (ring f term)
119 (declare (ring ring) (type term term))
120 (mapcan #'(lambda (term-f) (term-mul-lst ring term-f term)) f))
121
122(defun monom-times-term (m term)
123 (declare (type monom m) (type term term))
124 (make-term :monom (monom-mul m (term-monom term)) :coeff (term-coeff term)))
125
126(defun monom-times-termlist (m f)
127 (declare (type monom m))
128 (cond
129 ((null f) nil)
130 (t
131 (mapcar #'(lambda (x) (monom-times-term m x)) f))))
132
133(defun termlist-uminus (ring f)
134 (declare (ring ring))
135 (mapcar #'(lambda (x)
136 (make-term :monom (term-monom x)
137 :coeff (funcall (ring-uminus ring) (term-coeff x))))
138 f))
139
140(defun termlist-add (ring-and-order p q
141 &aux
142 (ring (ro-ring ring-and-order))
143 (order (ro-order ring-and-order)))
144 (declare (ring-and-order ring-and-order) (type list p q))
145 (do (r)
146 ((cond
147 ((endp p)
148 (setf r (revappend r q)) t)
149 ((endp q)
150 (setf r (revappend r p)) t)
151 (t
152 (multiple-value-bind
153 (lm-greater lm-equal)
154 (funcall order (termlist-lm p) (termlist-lm q))
155 (cond
156 (lm-equal
157 (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
158 (unless (funcall (ring-zerop ring) s) ;check for cancellation
159 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
160 (setf p (cdr p) q (cdr q))))
161 (lm-greater
162 (setf r (cons (car p) r)
163 p (cdr p)))
164 (t (setf r (cons (car q) r)
165 q (cdr q)))))
166 nil))
167 r)))
168
169(defun termlist-sub (ring-and-order p q
170 &aux
171 (ring (ro-ring ring-and-order))
172 (order (ro-order ring-and-order)))
173 (declare (ring-and-order ring-and-order) (type list p q))
174 (do (r)
175 ((cond
176 ((endp p)
177 (setf r (revappend r (termlist-uminus ring q)))
178 t)
179 ((endp q)
180 (setf r (revappend r p))
181 t)
182 (t
183 (multiple-value-bind
184 (mgreater mequal)
185 (funcall order (termlist-lm p) (termlist-lm q))
186 (cond
187 (mequal
188 (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
189 (unless (funcall (ring-zerop ring) s) ;check for cancellation
190 (setf r (cons (make-term :monom (termlist-lm p) :coeff s) r)))
191 (setf p (cdr p) q (cdr q))))
192 (mgreater
193 (setf r (cons (car p) r)
194 p (cdr p)))
195 (t (setf r (cons (make-term :monom (termlist-lm q)
196 :coeff (funcall (ring-uminus ring) (termlist-lc q))) r)
197 q (cdr q)))))
198 nil))
199 r)))
200
201;; Multiplication of polynomials
202;; Non-destructive version
203(defun termlist-mul (ring-and-order p q
204 &aux (ring (ro-ring ring-and-order)))
205 (declare (ring-and-order ring-and-order))
206 (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
207 ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
208 ((endp (cdr p))
209 (term-times-termlist ring (car p) q))
210 ((endp (cdr q))
211 (termlist-times-term ring p (car q)))
212 (t
213 (let ((head (term-mul-lst ring (termlist-lt p) (termlist-lt q)))
214 (tail (termlist-add ring-and-order
215 (term-times-termlist ring (car p) (cdr q))
216 (termlist-mul ring-and-order (cdr p) q))))
217 (cond ((null head) tail)
218 ((null tail) head)
219 (t (nconc head tail)))))))
220
221(defun termlist-unit (ring dim)
222 (declare (ring ring) (fixnum dim))
223 (list (make-term :monom (make-monom :dimension dim)
224 :coeff (funcall (ring-unit ring)))))
225
226
227(defun termlist-expt (ring-and-order poly n
228 &aux
229 (ring (ro-ring ring-and-order))
230 (dim (monom-dimension (termlist-lm poly))))
231 (declare (ring-and-order ring-and-order) (type fixnum n dim))
232 (cond
233 ((minusp n) (error "termlist-expt: Negative exponent."))
234 ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
235 (t
236 (do ((k 1 (ash k 1))
237 (q poly (termlist-mul ring-and-order q q)) ;keep squaring
238 (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring-and-order p q) p)))
239 ((> k n) p)
240 (declare (fixnum k))))))
Note: See TracBrowser for help on using the repository browser.