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

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

* empty log message *

File size: 17.4 KB
RevLine 
[3400]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;;----------------------------------------------------------------
[1201]10;;; -*- Mode: Lisp -*-
[77]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
[431]31(defpackage "POLYNOMIAL"
[3478]32 (:use :cl :utils :monom)
[2596]33 (:export "POLY"
[3270]34 "POLY-DIMENSION"
[2596]35 "POLY-TERMLIST"
[3016]36 "POLY-TERM-ORDER"
[3071]37 "CHANGE-TERM-ORDER"
[3099]38 "STANDARD-EXTENSION"
[3101]39 "STANDARD-EXTENSION-1"
[3109]40 "STANDARD-SUM"
[3094]41 "SATURATION-EXTENSION"
42 "ALIST->POLY")
[3489]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."))
[143]46
[431]47(in-package :polynomial)
48
[1927]49(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]50
[2442]51(defclass poly ()
[3253]52 ((dimension :initform nil
[3250]53 :initarg :dimension
54 :accessor poly-dimension
[3242]55 :documentation "Shared dimension of all terms, the number of variables")
[3250]56 (termlist :initform nil :initarg :termlist :accessor poly-termlist
[2697]57 :documentation "List of terms.")
[3250]58 (order :initform #'lex> :initarg :order :accessor poly-term-order
[2697]59 :documentation "Monomial/term order."))
[3262]60 (:default-initargs :dimension nil :termlist nil :order #'lex>)
[2695]61 (:documentation "A polynomial with a list of terms TERMLIST, ordered
[2696]62according to term order ORDER, which defaults to LEX>."))
[2442]63
[2471]64(defmethod print-object ((self poly) stream)
[3241]65 (print-unreadable-object (self stream :type t :identity t)
[3243]66 (with-accessors ((dimension poly-dimension)
67 (termlist poly-termlist)
68 (order poly-term-order))
[3237]69 self
[3244]70 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
71 dimension termlist order))))
[2469]72
[3015]73(defgeneric change-term-order (self other)
[3012]74 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]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)))
[3012]79 self))
[3010]80
[3095]81(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3126]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."
[3099]92 (dolist (x alist poly)
[3499]93 (poly-insert-term poly (make-instance 'monom :exponents (car x)) (cdr x))))
[3092]94
95
[3402]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
[3401]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)
[3501]106 :termlist (list (cons monom 1))))
[3403]107
[2650]108(defmethod r-equalp ((self poly) (other poly))
[2680]109 "POLY instances are R-EQUALP if they have the same
110order and if all terms are R-EQUALP."
[2651]111 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
112 (eq (poly-term-order self) (poly-term-order other))))
[2650]113
[3500]114(defgeneric poly-insert-term (self monom coeff)
115 (:method ((self poly) (monom monom) coeff)
116 (cond ((null (poly-dimension self))
117 (setf (poly-dimension self) (monom-dimension monom)))
118 (t (assert (= (poly-dimension self) (monom-dimension monom)))))
119 (push (cons monom coeff) (poly-termlist self))
120 self))
[2464]121
[3502]122(defgeneric poly-append-term (self monom coeff)
123 (:method ((self poly) (monom monom) coeff)
124 (cond ((null (poly-dimension self))
125 (setf (poly-dimension self) (monom-dimension monom)))
126 (t (assert (= (poly-dimension self) (monom-dimension monom)))))
127 (setf (cdr (last (poly-termlist self))) (list (cons monom coeff)))
128 self))
[2466]129
[52]130;; Leading term
[2442]131(defgeneric leading-term (object)
132 (:method ((self poly))
[2525]133 (car (poly-termlist self)))
134 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]135
136;; Second term
[2442]137(defgeneric second-leading-term (object)
138 (:method ((self poly))
[2525]139 (cadar (poly-termlist self)))
140 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]141
142;; Leading coefficient
[2442]143(defgeneric leading-coefficient (object)
144 (:method ((self poly))
[3221]145 (scalar-coeff (leading-term self)))
[2545]146 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]147
148;; Second coefficient
[2442]149(defgeneric second-leading-coefficient (object)
150 (:method ((self poly))
[3221]151 (scalar-coeff (second-leading-term self)))
[2906]152 (:documentation "The second leading coefficient of a polynomial. It
153 signals error for a polynomial with at most one term."))
[52]154
155;; Testing for a zero polynomial
[2445]156(defmethod r-zerop ((self poly))
157 (null (poly-termlist self)))
[52]158
159;; The number of terms
[2445]160(defmethod r-length ((self poly))
161 (length (poly-termlist self)))
[52]162
[2483]163(defmethod multiply-by ((self poly) (other monom))
[2501]164 (mapc #'(lambda (term) (multiply-by term other))
165 (poly-termlist self))
[2483]166 self)
[2469]167
[3120]168(defmethod multiply-by ((self poly) (other term))
169 (mapc #'(lambda (term) (multiply-by term other))
170 (poly-termlist self))
171 self)
172
[2501]173(defmethod multiply-by ((self poly) (other scalar))
[2502]174 (mapc #'(lambda (term) (multiply-by term other))
[2501]175 (poly-termlist self))
[2487]176 self)
177
[2607]178
[2761]179(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]180 "Return an expression which will efficiently adds/subtracts two
181polynomials, P and Q. The addition/subtraction of coefficients is
182performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
183is supplied, it is used to negate the coefficients of Q which do not
[2756]184have a corresponding coefficient in P. The code implements an
185efficient algorithm to add two polynomials represented as sorted lists
186of terms. The code destroys both arguments, reusing the terms to build
187the result."
[3221]188 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
[2742]189 (do ((p ,p)
190 (q ,q)
191 r)
192 ((or (endp p) (endp q))
193 ;; NOTE: R contains the result in reverse order. Can it
194 ;; be more efficient to produce the terms in correct order?
[2774]195 (unless (endp q)
[2776]196 ;; Upon subtraction, we must change the sign of
197 ;; all coefficients in q
[2774]198 ,@(when uminus-fn
[2775]199 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]200 (setf r (nreconc r q)))
[2742]201 r)
202 (multiple-value-bind
203 (greater-p equal-p)
[2766]204 (funcall ,order-fn (car p) (car q))
[2742]205 (cond
206 (greater-p
207 (rotatef (cdr p) r p)
208 )
209 (equal-p
[2766]210 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]211 (cond
212 ((r-zerop s)
213 (setf p (cdr p))
214 )
215 (t
216 (setf (lc p) s)
217 (rotatef (cdr p) r p))))
218 (setf q (cdr q))
219 )
220 (t
[2743]221 ;;Negate the term of Q if UMINUS provided, signallig
222 ;;that we are doing subtraction
[2908]223 ,(when uminus-fn
224 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[2743]225 (rotatef (cdr q) r q)))))))
[2585]226
[2655]227
[2763]228(defmacro def-add/subtract-method (add/subtract-method-name
[2752]229 uminus-method-name
230 &optional
[2913]231 (doc-string nil doc-string-supplied-p))
[2615]232 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]233 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]234 ,@(when doc-string-supplied-p `(,doc-string))
[2769]235 ;; Ensure orders are compatible
[3015]236 (change-term-order other self)
[2772]237 (setf (poly-termlist self) (fast-add/subtract
238 (poly-termlist self) (poly-termlist other)
239 (poly-term-order self)
240 #',add/subtract-method-name
241 ,(when uminus-method-name `(function ,uminus-method-name))))
[2609]242 self))
[2487]243
[2916]244(eval-when (:compile-toplevel :load-toplevel :execute)
[2777]245
246 (def-add/subtract-method add-to nil
247 "Adds to polynomial SELF another polynomial OTHER.
[2610]248This operation destructively modifies both polynomials.
249The result is stored in SELF. This implementation does
[2752]250no consing, entirely reusing the sells of SELF and OTHER.")
[2609]251
[2777]252 (def-add/subtract-method subtract-from unary-minus
[2753]253 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]254This operation destructively modifies both polynomials.
255The result is stored in SELF. This implementation does
[2752]256no consing, entirely reusing the sells of SELF and OTHER.")
[2916]257 )
[2777]258
[2691]259(defmethod unary-minus ((self poly))
[2694]260 "Destructively modifies the coefficients of the polynomial SELF,
261by changing their sign."
[2692]262 (mapc #'unary-minus (poly-termlist self))
[2683]263 self)
[52]264
[2795]265(defun add-termlists (p q order-fn)
[2794]266 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
[2917]267 (fast-add/subtract p q order-fn #'add-to nil))
[2794]268
[2800]269(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]270 &optional (reverse-arg-order-P nil))
[2799]271 "Multiplies term TERM by a list of term, TERMLIST.
[2792]272Takes into accound divisors of zero in the ring, by
[2927]273deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]274is T, change the order of arguments; this may be important
[2927]275if we extend the package to non-commutative rings."
[2800]276 `(mapcan #'(lambda (other-term)
[2907]277 (let ((prod (r*
[2923]278 ,@(cond
[2930]279 (reverse-arg-order-p
[2925]280 `(other-term ,term))
281 (t
282 `(,term other-term))))))
[2800]283 (cond
284 ((r-zerop prod) nil)
285 (t (list prod)))))
286 ,termlist))
[2790]287
[2796]288(defun multiply-termlists (p q order-fn)
[3127]289 "A version of polynomial multiplication, operating
290directly on termlists."
[2787]291 (cond
[2917]292 ((or (endp p) (endp q))
293 ;;p or q is 0 (represented by NIL)
294 nil)
[2789]295 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]296 ((endp (cdr p))
[2918]297 (multiply-term-by-termlist-dropping-zeros (car p) q))
298 ((endp (cdr q))
[2919]299 (multiply-term-by-termlist-dropping-zeros (car q) p t))
300 (t
[2948]301 (cons (r* (car p) (car q))
[2949]302 (add-termlists
303 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
304 (multiply-termlists (cdr p) q order-fn)
305 order-fn)))))
[2793]306
[2803]307(defmethod multiply-by ((self poly) (other poly))
[3014]308 (change-term-order other self)
[2803]309 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
310 (poly-termlist other)
311 (poly-term-order self)))
312 self)
313
[3405]314(defmethod r+ ((poly1 poly) poly2)
[3374]315 "Non-destructively add POLY1 by POLY2."
[3405]316 (add-to (copy-instance POLY1) (change-class (copy-instance POLY2) 'poly)))
[3374]317
[3430]318(defmethod r- ((minuend poly) &rest subtrahends)
[3427]319 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3433]320 (subtract-from (copy-instance minuend)
321 (change-class (reduce #'r+ subtrahends) 'poly)))
[3374]322
[3406]323(defmethod r+ ((poly1 term) poly2)
324 "Non-destructively add POLY1 by POLY2."
[3432]325 (add-to (change-class (copy-instance poly1) 'poly)
326 (change-class (copy-instance poly2) 'poly)))
[3406]327
[3426]328(defmethod r- ((minuend term) &rest subtrahends)
329 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3428]330 (subtract-from (change-class (copy-instance minuend) 'poly)
[3435]331 (change-class (reduce #'r+ subtrahends) 'poly)))
[3406]332
[3407]333(defmethod r+ ((poly1 monom) poly2)
334 "Non-destructively add POLY1 by POLY2."
[3431]335 (add-to (change-class (copy-instance poly1) 'poly)
336 (change-class (copy-instance poly2) 'poly)))
[3407]337
[3425]338(defmethod r- ((minuend monom) &rest subtrahends)
339 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3428]340 (subtract-from (change-class (copy-instance minuend) 'poly)
[3434]341 (change-class (reduce #'r+ subtrahends) 'poly)))
[3407]342
[3374]343(defmethod r* ((poly1 poly) (poly2 poly))
[2939]344 "Non-destructively multiply POLY1 by POLY2."
[3432]345 (multiply-by (copy-instance poly1) (copy-instance poly2)))
[2916]346
[3044]347(defmethod left-tensor-product-by ((self poly) (other term))
348 (setf (poly-termlist self)
349 (mapcan #'(lambda (term)
[3047]350 (let ((prod (left-tensor-product-by term other)))
[3044]351 (cond
352 ((r-zerop prod) nil)
353 (t (list prod)))))
[3048]354 (poly-termlist self)))
[3044]355 self)
356
357(defmethod right-tensor-product-by ((self poly) (other term))
[3045]358 (setf (poly-termlist self)
359 (mapcan #'(lambda (term)
[3046]360 (let ((prod (right-tensor-product-by term other)))
[3045]361 (cond
362 ((r-zerop prod) nil)
363 (t (list prod)))))
[3048]364 (poly-termlist self)))
[3045]365 self)
[3044]366
[3062]367(defmethod left-tensor-product-by ((self poly) (other monom))
368 (setf (poly-termlist self)
369 (mapcan #'(lambda (term)
370 (let ((prod (left-tensor-product-by term other)))
371 (cond
372 ((r-zerop prod) nil)
373 (t (list prod)))))
374 (poly-termlist self)))
[3249]375 (incf (poly-dimension self) (monom-dimension other))
[3062]376 self)
[3044]377
[3062]378(defmethod right-tensor-product-by ((self poly) (other monom))
379 (setf (poly-termlist self)
380 (mapcan #'(lambda (term)
381 (let ((prod (right-tensor-product-by term other)))
382 (cond
383 ((r-zerop prod) nil)
384 (t (list prod)))))
385 (poly-termlist self)))
[3249]386 (incf (poly-dimension self) (monom-dimension other))
[3062]387 self)
388
389
[3084]390(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]391 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]392is a list of polynomials. Destructively modifies PLIST elements."
[3061]393 (mapc #'(lambda (poly)
[3085]394 (left-tensor-product-by
395 poly
396 (prog1
397 (make-monom-variable k i)
398 (incf i))))
[3061]399 plist))
[52]400
[3087]401(defun standard-extension-1 (plist
402 &aux
[3096]403 (plist (standard-extension plist))
[3087]404 (nvars (poly-dimension (car plist))))
[3081]405 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]406Firstly, new K variables U1, U2, ..., UK, are inserted into each
407polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]408tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]409polynomials have the same dimension, and only the first polynomial
410is examined to determine this dimension."
[3089]411 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
412 ;; 1 from each polynomial; since UI*PI has no constant term,
413 ;; we just need to append the constant term at the end
414 ;; of each termlist.
[3064]415 (flet ((subtract-1 (p)
[3503]416 (poly-append-term p (make-instance 'monom :dimension nvars) -1)))
[3083]417 (setf plist (mapc #'subtract-1 plist)))
[3077]418 plist)
[52]419
420
[3107]421(defun standard-sum (plist
422 &aux
423 (plist (standard-extension plist))
424 (nvars (poly-dimension (car plist))))
[3087]425 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
426Firstly, new K variables, U1, U2, ..., UK, are inserted into each
427polynomial. Subsequently, P1, P2, ..., PK are destructively modified
428tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]429are added. Finally, 1 is subtracted. It should be noted that the term
430order is not modified, which is equivalent to using a lexicographic
431order on the first K variables."
[3107]432 (flet ((subtract-1 (p)
433 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
[3108]434 (subtract-1
435 (make-instance
436 'poly
[3115]437 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]438
[3122]439#|
440
[1477]441(defun saturation-extension-1 (ring f p)
[1497]442 "Calculate [F, U*P-1]. It destructively modifies F."
[1908]443 (declare (type ring ring))
[1477]444 (polysaturation-extension ring f (list p)))
[53]445
[3122]446
[53]447
448
[1189]449(defun spoly (ring-and-order f g
450 &aux
451 (ring (ro-ring ring-and-order)))
[55]452 "It yields the S-polynomial of polynomials F and G."
[1911]453 (declare (type ring-and-order ring-and-order) (type poly f g))
[55]454 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
[2913]455 (mf (monom-div lcm (poly-lm f)))
456 (mg (monom-div lcm (poly-lm g))))
[55]457 (declare (type monom mf mg))
458 (multiple-value-bind (c cf cg)
459 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
460 (declare (ignore c))
461 (poly-sub
[1189]462 ring-and-order
[55]463 (scalar-times-poly ring cg (monom-times-poly mf f))
464 (scalar-times-poly ring cf (monom-times-poly mg g))))))
[53]465
466
[55]467(defun poly-primitive-part (ring p)
468 "Divide polynomial P with integer coefficients by gcd of its
469coefficients and return the result."
[1912]470 (declare (type ring ring) (type poly p))
[55]471 (if (poly-zerop p)
472 (values p 1)
[2913]473 (let ((c (poly-content ring p)))
474 (values (make-poly-from-termlist
475 (mapcar
476 #'(lambda (x)
477 (make-term :monom (term-monom x)
478 :coeff (funcall (ring-div ring) (term-coeff x) c)))
479 (poly-termlist p))
480 (poly-sugar p))
481 c))))
[55]482
483(defun poly-content (ring p)
484 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
485to compute the greatest common divisor."
[1913]486 (declare (type ring ring) (type poly p))
[55]487 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
[1066]488
[2456]489|#
Note: See TracBrowser for help on using the repository browser.