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

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

* empty log message *

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