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

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

* empty log message *

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