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

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

* empty log message *

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