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

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

* empty log message *

File size: 15.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 "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 (assert (= (monom-dimension self) (poly-dimension other)))
133 (mapc #'(lambda (term) (multiply-by term other))
134 (poly-termlist self))
135 self)
136
137(defmethod multiply-by ((self poly) (other term))
138 (assert (= (monom-dimension self) (monom-dimension other)))
139 (mapc #'(lambda (term) (multiply-by term other))
140 (poly-termlist self))
141 self)
142
143(defmethod multiply-by ((self poly) (other scalar))
144 (assert (= (monom-dimension self) (monom-dimension other)))
145 (mapc #'(lambda (term) (multiply-by term other))
146 (poly-termlist self))
147 self)
148
149
150(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
151 "Return an expression which will efficiently adds/subtracts two
152polynomials, P and Q. The addition/subtraction of coefficients is
153performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
154is supplied, it is used to negate the coefficients of Q which do not
155have a corresponding coefficient in P. The code implements an
156efficient algorithm to add two polynomials represented as sorted lists
157of terms. The code destroys both arguments, reusing the terms to build
158the result."
159 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
160 (do ((p ,p)
161 (q ,q)
162 r)
163 ((or (endp p) (endp q))
164 ;; NOTE: R contains the result in reverse order. Can it
165 ;; be more efficient to produce the terms in correct order?
166 (unless (endp q)
167 ;; Upon subtraction, we must change the sign of
168 ;; all coefficients in q
169 ,@(when uminus-fn
170 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
171 (setf r (nreconc r q)))
172 r)
173 (multiple-value-bind
174 (greater-p equal-p)
175 (funcall ,order-fn (car p) (car q))
176 (cond
177 (greater-p
178 (rotatef (cdr p) r p)
179 )
180 (equal-p
181 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
182 (cond
183 ((r-zerop s)
184 (setf p (cdr p))
185 )
186 (t
187 (setf (lc p) s)
188 (rotatef (cdr p) r p))))
189 (setf q (cdr q))
190 )
191 (t
192 ;;Negate the term of Q if UMINUS provided, signallig
193 ;;that we are doing subtraction
194 ,(when uminus-fn
195 `(setf (lc q) (funcall ,uminus-fn (lc q))))
196 (rotatef (cdr q) r q)))))))
197
198
199(defmacro def-add/subtract-method (add/subtract-method-name
200 uminus-method-name
201 &optional
202 (doc-string nil doc-string-supplied-p))
203 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
204 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
205 ,@(when doc-string-supplied-p `(,doc-string))
206 ;; Ensure orders are compatible
207 (change-term-order other self)
208 (setf (poly-termlist self) (fast-add/subtract
209 (poly-termlist self) (poly-termlist other)
210 (poly-term-order self)
211 #',add/subtract-method-name
212 ,(when uminus-method-name `(function ,uminus-method-name))))
213 self))
214
215(eval-when (:compile-toplevel :load-toplevel :execute)
216
217 (def-add/subtract-method add-to nil
218 "Adds to polynomial SELF another polynomial OTHER.
219This operation destructively modifies both polynomials.
220The result is stored in SELF. This implementation does
221no consing, entirely reusing the sells of SELF and OTHER.")
222
223 (def-add/subtract-method subtract-from unary-minus
224 "Subtracts from polynomial SELF another polynomial OTHER.
225This operation destructively modifies both polynomials.
226The result is stored in SELF. This implementation does
227no consing, entirely reusing the sells of SELF and OTHER.")
228 )
229
230(defmethod unary-minus ((self poly))
231 "Destructively modifies the coefficients of the polynomial SELF,
232by changing their sign."
233 (mapc #'unary-minus (poly-termlist self))
234 self)
235
236(defun add-termlists (p q order-fn)
237 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
238 (fast-add/subtract p q order-fn #'add-to nil))
239
240(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
241 &optional (reverse-arg-order-P nil))
242 "Multiplies term TERM by a list of term, TERMLIST.
243Takes into accound divisors of zero in the ring, by
244deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
245is T, change the order of arguments; this may be important
246if we extend the package to non-commutative rings."
247 `(mapcan #'(lambda (other-term)
248 (let ((prod (r*
249 ,@(cond
250 (reverse-arg-order-p
251 `(other-term ,term))
252 (t
253 `(,term other-term))))))
254 (cond
255 ((r-zerop prod) nil)
256 (t (list prod)))))
257 ,termlist))
258
259(defun multiply-termlists (p q order-fn)
260 "A version of polynomial multiplication, operating
261directly on termlists."
262 (cond
263 ((or (endp p) (endp q))
264 ;;p or q is 0 (represented by NIL)
265 nil)
266 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
267 ((endp (cdr p))
268 (multiply-term-by-termlist-dropping-zeros (car p) q))
269 ((endp (cdr q))
270 (multiply-term-by-termlist-dropping-zeros (car q) p t))
271 (t
272 (cons (r* (car p) (car q))
273 (add-termlists
274 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
275 (multiply-termlists (cdr p) q order-fn)
276 order-fn)))))
277
278(defmethod multiply-by ((self poly) (other poly))
279 (assert (= (monom-dimension self) (monom-dimension other)))
280 (change-term-order other self)
281 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
282 (poly-termlist other)
283 (poly-term-order self)))
284 self)
285
286(defmethod r* ((poly1 poly) (poly2 poly))
287 "Non-destructively multiply POLY1 by POLY2."
288 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
289
290(defmethod left-tensor-product-by ((self poly) (other term))
291 (setf (poly-termlist self)
292 (mapcan #'(lambda (term)
293 (let ((prod (left-tensor-product-by term other)))
294 (cond
295 ((r-zerop prod) nil)
296 (t (list prod)))))
297 (poly-termlist self)))
298 self)
299
300(defmethod right-tensor-product-by ((self poly) (other term))
301 (setf (poly-termlist self)
302 (mapcan #'(lambda (term)
303 (let ((prod (right-tensor-product-by term other)))
304 (cond
305 ((r-zerop prod) nil)
306 (t (list prod)))))
307 (poly-termlist self)))
308 self)
309
310(defmethod left-tensor-product-by ((self poly) (other monom))
311 (setf (poly-termlist self)
312 (mapcan #'(lambda (term)
313 (let ((prod (left-tensor-product-by term other)))
314 (cond
315 ((r-zerop prod) nil)
316 (t (list prod)))))
317 (poly-termlist self)))
318 (incf (poly-dimension self) (monom-dimension other))
319 self)
320
321(defmethod right-tensor-product-by ((self poly) (other monom))
322 (setf (poly-termlist self)
323 (mapcan #'(lambda (term)
324 (let ((prod (right-tensor-product-by term other)))
325 (cond
326 ((r-zerop prod) nil)
327 (t (list prod)))))
328 (poly-termlist self)))
329 (incf (poly-dimension self) (monom-dimension other))
330 self)
331
332
333(defun standard-extension (plist &aux (k (length plist)) (i 0))
334 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
335is a list of polynomials. Destructively modifies PLIST elements."
336 (mapc #'(lambda (poly)
337 (left-tensor-product-by
338 poly
339 (prog1
340 (make-monom-variable k i)
341 (incf i))))
342 plist))
343
344(defun standard-extension-1 (plist
345 &aux
346 (plist (standard-extension plist))
347 (nvars (poly-dimension (car plist))))
348 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
349Firstly, new K variables U1, U2, ..., UK, are inserted into each
350polynomial. Subsequently, P1, P2, ..., PK are destructively modified
351tantamount to replacing PI with UI*PI-1. It assumes that all
352polynomials have the same dimension, and only the first polynomial
353is examined to determine this dimension."
354 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
355 ;; 1 from each polynomial; since UI*PI has no constant term,
356 ;; we just need to append the constant term at the end
357 ;; of each termlist.
358 (flet ((subtract-1 (p)
359 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
360 (setf plist (mapc #'subtract-1 plist)))
361 plist)
362
363
364(defun standard-sum (plist
365 &aux
366 (plist (standard-extension plist))
367 (nvars (poly-dimension (car plist))))
368 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
369Firstly, new K variables, U1, U2, ..., UK, are inserted into each
370polynomial. Subsequently, P1, P2, ..., PK are destructively modified
371tantamount to replacing PI with UI*PI, and the resulting polynomials
372are added. Finally, 1 is subtracted. It should be noted that the term
373order is not modified, which is equivalent to using a lexicographic
374order on the first K variables."
375 (flet ((subtract-1 (p)
376 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
377 (subtract-1
378 (make-instance
379 'poly
380 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
381
382#|
383
384(defun saturation-extension-1 (ring f p)
385 "Calculate [F, U*P-1]. It destructively modifies F."
386 (declare (type ring ring))
387 (polysaturation-extension ring f (list p)))
388
389
390
391
392(defun spoly (ring-and-order f g
393 &aux
394 (ring (ro-ring ring-and-order)))
395 "It yields the S-polynomial of polynomials F and G."
396 (declare (type ring-and-order ring-and-order) (type poly f g))
397 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
398 (mf (monom-div lcm (poly-lm f)))
399 (mg (monom-div lcm (poly-lm g))))
400 (declare (type monom mf mg))
401 (multiple-value-bind (c cf cg)
402 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
403 (declare (ignore c))
404 (poly-sub
405 ring-and-order
406 (scalar-times-poly ring cg (monom-times-poly mf f))
407 (scalar-times-poly ring cf (monom-times-poly mg g))))))
408
409
410(defun poly-primitive-part (ring p)
411 "Divide polynomial P with integer coefficients by gcd of its
412coefficients and return the result."
413 (declare (type ring ring) (type poly p))
414 (if (poly-zerop p)
415 (values p 1)
416 (let ((c (poly-content ring p)))
417 (values (make-poly-from-termlist
418 (mapcar
419 #'(lambda (x)
420 (make-term :monom (term-monom x)
421 :coeff (funcall (ring-div ring) (term-coeff x) c)))
422 (poly-termlist p))
423 (poly-sugar p))
424 c))))
425
426(defun poly-content (ring p)
427 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
428to compute the greatest common divisor."
429 (declare (type ring ring) (type poly p))
430 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
431
432|#
Note: See TracBrowser for help on using the repository browser.