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

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

* empty log message *

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