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

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

* empty log message *

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