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

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

* empty log message *

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