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/polynomial.lisp@ 3126

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

* empty log message *

File size: 14.3 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 "POLYNOMIAL"
23 (:use :cl :utils :ring :monom :order :term #| :infix |# )
24 (:export "POLY"
25 "POLY-TERMLIST"
26 "POLY-TERM-ORDER"
27 "CHANGE-TERM-ORDER"
28 "STANDARD-EXTENSION"
29 "STANDARD-EXTENSION-1"
30 "STANDARD-SUM"
31 "SATURATION-EXTENSION"
32 "ALIST->POLY")
33 (:documentation "Implements polynomials"))
34
35(in-package :polynomial)
36
37(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
38
39(defclass poly ()
40 ((termlist :initarg :termlist :accessor poly-termlist
41 :documentation "List of terms.")
42 (order :initarg :order :accessor poly-term-order
43 :documentation "Monomial/term order."))
44 (:default-initargs :termlist nil :order #'lex>)
45 (:documentation "A polynomial with a list of terms TERMLIST, ordered
46according to term order ORDER, which defaults to LEX>."))
47
48(defmethod print-object ((self poly) stream)
49 (format stream "#<POLY TERMLIST=~A ORDER=~A>"
50 (poly-termlist self)
51 (poly-term-order self)))
52
53(defgeneric change-term-order (self other)
54 (:documentation "Change term order of SELF to the term order of OTHER.")
55 (:method ((self poly) (other poly))
56 (unless (eq (poly-term-order self) (poly-term-order other))
57 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
58 (poly-term-order self) (poly-term-order other)))
59 self))
60
61(defun alist->poly (alist &aux (poly (make-instance 'poly)))
62 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
63It can be used to enter simple polynomials by hand, e.g the polynomial
64in two variables, X and Y, given in standard notation as:
65
66 3*X^2*Y^3+2*Y+7
67
68can be entered as
69(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
70
71NOTE: The primary use is for low-level debugging of the package."
72 (dolist (x alist poly)
73 (insert-item poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
74
75
76(defmethod r-equalp ((self poly) (other poly))
77 "POLY instances are R-EQUALP if they have the same
78order and if all terms are R-EQUALP."
79 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
80 (eq (poly-term-order self) (poly-term-order other))))
81
82(defmethod insert-item ((self poly) (item term))
83 (push item (poly-termlist self))
84 self)
85
86(defmethod append-item ((self poly) (item term))
87 (setf (cdr (last (poly-termlist self))) (list item))
88 self)
89
90;; Leading term
91(defgeneric leading-term (object)
92 (:method ((self poly))
93 (car (poly-termlist self)))
94 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
95
96;; Second term
97(defgeneric second-leading-term (object)
98 (:method ((self poly))
99 (cadar (poly-termlist self)))
100 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
101
102;; Leading coefficient
103(defgeneric leading-coefficient (object)
104 (:method ((self poly))
105 (r-coeff (leading-term self)))
106 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
107
108;; Second coefficient
109(defgeneric second-leading-coefficient (object)
110 (:method ((self poly))
111 (r-coeff (second-leading-term self)))
112 (:documentation "The second leading coefficient of a polynomial. It
113 signals error for a polynomial with at most one term."))
114
115;; Testing for a zero polynomial
116(defmethod r-zerop ((self poly))
117 (null (poly-termlist self)))
118
119;; The number of terms
120(defmethod r-length ((self poly))
121 (length (poly-termlist self)))
122
123(defmethod multiply-by ((self poly) (other monom))
124 (mapc #'(lambda (term) (multiply-by term other))
125 (poly-termlist self))
126 self)
127
128(defmethod multiply-by ((self poly) (other term))
129 (mapc #'(lambda (term) (multiply-by term other))
130 (poly-termlist self))
131 self)
132
133(defmethod multiply-by ((self poly) (other scalar))
134 (mapc #'(lambda (term) (multiply-by term other))
135 (poly-termlist self))
136 self)
137
138
139(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
140 "Return an expression which will efficiently adds/subtracts two
141polynomials, P and Q. The addition/subtraction of coefficients is
142performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
143is supplied, it is used to negate the coefficients of Q which do not
144have a corresponding coefficient in P. The code implements an
145efficient algorithm to add two polynomials represented as sorted lists
146of terms. The code destroys both arguments, reusing the terms to build
147the result."
148 `(macrolet ((lc (x) `(r-coeff (car ,x))))
149 (do ((p ,p)
150 (q ,q)
151 r)
152 ((or (endp p) (endp q))
153 ;; NOTE: R contains the result in reverse order. Can it
154 ;; be more efficient to produce the terms in correct order?
155 (unless (endp q)
156 ;; Upon subtraction, we must change the sign of
157 ;; all coefficients in q
158 ,@(when uminus-fn
159 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
160 (setf r (nreconc r q)))
161 r)
162 (multiple-value-bind
163 (greater-p equal-p)
164 (funcall ,order-fn (car p) (car q))
165 (cond
166 (greater-p
167 (rotatef (cdr p) r p)
168 )
169 (equal-p
170 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
171 (cond
172 ((r-zerop s)
173 (setf p (cdr p))
174 )
175 (t
176 (setf (lc p) s)
177 (rotatef (cdr p) r p))))
178 (setf q (cdr q))
179 )
180 (t
181 ;;Negate the term of Q if UMINUS provided, signallig
182 ;;that we are doing subtraction
183 ,(when uminus-fn
184 `(setf (lc q) (funcall ,uminus-fn (lc q))))
185 (rotatef (cdr q) r q)))))))
186
187
188(defmacro def-add/subtract-method (add/subtract-method-name
189 uminus-method-name
190 &optional
191 (doc-string nil doc-string-supplied-p))
192 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
193 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
194 ,@(when doc-string-supplied-p `(,doc-string))
195 ;; Ensure orders are compatible
196 (change-term-order other self)
197 (setf (poly-termlist self) (fast-add/subtract
198 (poly-termlist self) (poly-termlist other)
199 (poly-term-order self)
200 #',add/subtract-method-name
201 ,(when uminus-method-name `(function ,uminus-method-name))))
202 self))
203
204(eval-when (:compile-toplevel :load-toplevel :execute)
205
206 (def-add/subtract-method add-to nil
207 "Adds to polynomial SELF another polynomial OTHER.
208This operation destructively modifies both polynomials.
209The result is stored in SELF. This implementation does
210no consing, entirely reusing the sells of SELF and OTHER.")
211
212 (def-add/subtract-method subtract-from unary-minus
213 "Subtracts from polynomial SELF another polynomial OTHER.
214This operation destructively modifies both polynomials.
215The result is stored in SELF. This implementation does
216no consing, entirely reusing the sells of SELF and OTHER.")
217 )
218
219(defmethod unary-minus ((self poly))
220 "Destructively modifies the coefficients of the polynomial SELF,
221by changing their sign."
222 (mapc #'unary-minus (poly-termlist self))
223 self)
224
225(defun add-termlists (p q order-fn)
226 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
227 (fast-add/subtract p q order-fn #'add-to nil))
228
229(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
230 &optional (reverse-arg-order-P nil))
231 "Multiplies term TERM by a list of term, TERMLIST.
232Takes into accound divisors of zero in the ring, by
233deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
234is T, change the order of arguments; this may be important
235if we extend the package to non-commutative rings."
236 `(mapcan #'(lambda (other-term)
237 (let ((prod (r*
238 ,@(cond
239 (reverse-arg-order-p
240 `(other-term ,term))
241 (t
242 `(,term other-term))))))
243 (cond
244 ((r-zerop prod) nil)
245 (t (list prod)))))
246 ,termlist))
247
248(defun multiply-termlists (p q order-fn)
249 (cond
250 ((or (endp p) (endp q))
251 ;;p or q is 0 (represented by NIL)
252 nil)
253 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
254 ((endp (cdr p))
255 (multiply-term-by-termlist-dropping-zeros (car p) q))
256 ((endp (cdr q))
257 (multiply-term-by-termlist-dropping-zeros (car q) p t))
258 (t
259 (cons (r* (car p) (car q))
260 (add-termlists
261 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
262 (multiply-termlists (cdr p) q order-fn)
263 order-fn)))))
264
265(defmethod multiply-by ((self poly) (other poly))
266 (change-term-order other self)
267 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
268 (poly-termlist other)
269 (poly-term-order self)))
270 self)
271
272(defmethod r* ((poly1 poly) (poly2 poly))
273 "Non-destructively multiply POLY1 by POLY2."
274 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
275
276(defmethod left-tensor-product-by ((self poly) (other term))
277 (setf (poly-termlist self)
278 (mapcan #'(lambda (term)
279 (let ((prod (left-tensor-product-by term other)))
280 (cond
281 ((r-zerop prod) nil)
282 (t (list prod)))))
283 (poly-termlist self)))
284 self)
285
286(defmethod right-tensor-product-by ((self poly) (other term))
287 (setf (poly-termlist self)
288 (mapcan #'(lambda (term)
289 (let ((prod (right-tensor-product-by term other)))
290 (cond
291 ((r-zerop prod) nil)
292 (t (list prod)))))
293 (poly-termlist self)))
294 self)
295
296(defmethod left-tensor-product-by ((self poly) (other monom))
297 (setf (poly-termlist self)
298 (mapcan #'(lambda (term)
299 (let ((prod (left-tensor-product-by term other)))
300 (cond
301 ((r-zerop prod) nil)
302 (t (list prod)))))
303 (poly-termlist self)))
304 self)
305
306(defmethod right-tensor-product-by ((self poly) (other monom))
307 (setf (poly-termlist self)
308 (mapcan #'(lambda (term)
309 (let ((prod (right-tensor-product-by term other)))
310 (cond
311 ((r-zerop prod) nil)
312 (t (list prod)))))
313 (poly-termlist self)))
314 self)
315
316
317(defun standard-extension (plist &aux (k (length plist)) (i 0))
318 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
319is a list of polynomials. Destructively modifies PLIST elements."
320 (mapc #'(lambda (poly)
321 (left-tensor-product-by
322 poly
323 (prog1
324 (make-monom-variable k i)
325 (incf i))))
326 plist))
327
328(defmethod poly-dimension ((poly poly))
329 (cond ((r-zerop poly) -1)
330 (t (monom-dimension (leading-term poly)))))
331
332(defun standard-extension-1 (plist
333 &aux
334 (plist (standard-extension plist))
335 (nvars (poly-dimension (car plist))))
336 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
337Firstly, new K variables U1, U2, ..., UK, are inserted into each
338polynomial. Subsequently, P1, P2, ..., PK are destructively modified
339tantamount to replacing PI with UI*PI-1. It assumes that all
340polynomials have the same dimension, and only the first polynomial
341is examined to determine this dimension."
342 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
343 ;; 1 from each polynomial; since UI*PI has no constant term,
344 ;; we just need to append the constant term at the end
345 ;; of each termlist.
346 (flet ((subtract-1 (p)
347 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
348 (setf plist (mapc #'subtract-1 plist)))
349 plist)
350
351
352(defun standard-sum (plist
353 &aux
354 (plist (standard-extension plist))
355 (nvars (poly-dimension (car plist))))
356 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
357Firstly, new K variables, U1, U2, ..., UK, are inserted into each
358polynomial. Subsequently, P1, P2, ..., PK are destructively modified
359tantamount to replacing PI with UI*PI, and the resulting polynomials
360are added. Finally, 1 is subtracted. It should be noted that the term
361order is not modified, which is equivalent to using a lexicographic
362order on the first K variables."
363 (flet ((subtract-1 (p)
364 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
365 (subtract-1
366 (make-instance
367 'poly
368 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
369
370#|
371
372(defun saturation-extension-1 (ring f p)
373 "Calculate [F, U*P-1]. It destructively modifies F."
374 (declare (type ring ring))
375 (polysaturation-extension ring f (list p)))
376
377
378
379
380(defun spoly (ring-and-order f g
381 &aux
382 (ring (ro-ring ring-and-order)))
383 "It yields the S-polynomial of polynomials F and G."
384 (declare (type ring-and-order ring-and-order) (type poly f g))
385 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
386 (mf (monom-div lcm (poly-lm f)))
387 (mg (monom-div lcm (poly-lm g))))
388 (declare (type monom mf mg))
389 (multiple-value-bind (c cf cg)
390 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
391 (declare (ignore c))
392 (poly-sub
393 ring-and-order
394 (scalar-times-poly ring cg (monom-times-poly mf f))
395 (scalar-times-poly ring cf (monom-times-poly mg g))))))
396
397
398(defun poly-primitive-part (ring p)
399 "Divide polynomial P with integer coefficients by gcd of its
400coefficients and return the result."
401 (declare (type ring ring) (type poly p))
402 (if (poly-zerop p)
403 (values p 1)
404 (let ((c (poly-content ring p)))
405 (values (make-poly-from-termlist
406 (mapcar
407 #'(lambda (x)
408 (make-term :monom (term-monom x)
409 :coeff (funcall (ring-div ring) (term-coeff x) c)))
410 (poly-termlist p))
411 (poly-sugar p))
412 c))))
413
414(defun poly-content (ring p)
415 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
416to compute the greatest common divisor."
417 (declare (type ring ring) (type poly p))
418 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
419
420|#
Note: See TracBrowser for help on using the repository browser.