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

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

* empty log message *

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