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

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

* empty log message *

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