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

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

* empty log message *

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