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

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

* empty log message *

File size: 16.6 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"
[3509]37 "POLY-INSERT-TERM"
[3520]38 "POLY-ADD-TO"
[3071]39 "CHANGE-TERM-ORDER"
[3099]40 "STANDARD-EXTENSION"
[3101]41 "STANDARD-EXTENSION-1"
[3109]42 "STANDARD-SUM"
[3094]43 "SATURATION-EXTENSION"
44 "ALIST->POLY")
[3489]45 (:documentation "Implements polynomials. A polynomial is essentially
46a mapping of monomials of the same degree to coefficients. The
47momomials are ordered according to a monomial order."))
[143]48
[431]49(in-package :polynomial)
50
[1927]51(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]52
[2442]53(defclass poly ()
[3253]54 ((dimension :initform nil
[3250]55 :initarg :dimension
56 :accessor poly-dimension
[3242]57 :documentation "Shared dimension of all terms, the number of variables")
[3250]58 (termlist :initform nil :initarg :termlist :accessor poly-termlist
[3512]59 :documentation "List of terms. This is an association
60list mapping monomials to coefficients, ordered by this polynomial's
61monomial order.")
[3250]62 (order :initform #'lex> :initarg :order :accessor poly-term-order
[2697]63 :documentation "Monomial/term order."))
[3262]64 (:default-initargs :dimension nil :termlist nil :order #'lex>)
[2695]65 (:documentation "A polynomial with a list of terms TERMLIST, ordered
[2696]66according to term order ORDER, which defaults to LEX>."))
[2442]67
[2471]68(defmethod print-object ((self poly) stream)
[3241]69 (print-unreadable-object (self stream :type t :identity t)
[3243]70 (with-accessors ((dimension poly-dimension)
71 (termlist poly-termlist)
72 (order poly-term-order))
[3237]73 self
[3244]74 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
75 dimension termlist order))))
[2469]76
[3015]77(defgeneric change-term-order (self other)
[3012]78 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]79 (:method ((self poly) (other poly))
80 (unless (eq (poly-term-order self) (poly-term-order other))
[3521]81 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other) :key #'car)
[3010]82 (poly-term-order self) (poly-term-order other)))
[3012]83 self))
[3010]84
[3510]85(defgeneric poly-insert-term (self monom coeff)
[3515]86 (:documentation "Insert a term with monomial MONOM and coefficient COEFF
[3516]87before all other terms. Order is not enforced.")
[3510]88 (:method ((self poly) (monom monom) coeff)
89 (cond ((null (poly-dimension self))
90 (setf (poly-dimension self) (monom-dimension monom)))
91 (t (assert (= (poly-dimension self) (monom-dimension monom)))))
92 (push (cons monom coeff) (poly-termlist self))
93 self))
94
95(defgeneric poly-append-term (self monom coeff)
[3515]96 (:documentation "Append a term with monomial MONOM and coefficient COEFF
[3516]97after all other terms. Order is not enforced.")
[3510]98 (:method ((self poly) (monom monom) coeff)
99 (cond ((null (poly-dimension self))
100 (setf (poly-dimension self) (monom-dimension monom)))
101 (t (assert (= (poly-dimension self) (monom-dimension monom)))))
102 (setf (cdr (last (poly-termlist self))) (list (cons monom coeff)))
103 self))
104
[3095]105(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3126]106 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
107It can be used to enter simple polynomials by hand, e.g the polynomial
108in two variables, X and Y, given in standard notation as:
109
110 3*X^2*Y^3+2*Y+7
111
112can be entered as
113(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
114
115NOTE: The primary use is for low-level debugging of the package."
[3099]116 (dolist (x alist poly)
[3499]117 (poly-insert-term poly (make-instance 'monom :exponents (car x)) (cdr x))))
[3092]118
[3401]119(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
120 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
121 (reinitialize-instance new
122 :dimension (monom-dimension old)
[3511]123 :termlist (list (cons old 1))))
[3403]124
[3513]125(defgeneric poly-equalp (self other)
[3514]126 (:documentation "Implements equality of polynomials.")
[3513]127 (:method ((self poly) (other poly))
[3514]128 (and (eql (poly-dimension self) (poly-dimension other))
129 (every #'r-equalp (poly-termlist self) (poly-termlist other))
130 (eq (poly-term-order self) (poly-term-order other)))))
[2650]131
[52]132;; Leading term
[2442]133(defgeneric leading-term (object)
134 (:method ((self poly))
[2525]135 (car (poly-termlist self)))
136 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]137
138;; Second term
[2442]139(defgeneric second-leading-term (object)
140 (:method ((self poly))
[2525]141 (cadar (poly-termlist self)))
142 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]143
144;; Leading coefficient
[2442]145(defgeneric leading-coefficient (object)
146 (:method ((self poly))
[3221]147 (scalar-coeff (leading-term self)))
[2545]148 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]149
150;; Second coefficient
[2442]151(defgeneric second-leading-coefficient (object)
152 (:method ((self poly))
[3221]153 (scalar-coeff (second-leading-term self)))
[2906]154 (:documentation "The second leading coefficient of a polynomial. It
155 signals error for a polynomial with at most one term."))
[52]156
157;; Testing for a zero polynomial
[3518]158(defgeneric poly-zerop (self)
159 (:method ((self poly))
160 (null (poly-termlist self))))
[52]161
162;; The number of terms
[3518]163(defgeneric poly-length (self)
164 (:method ((self poly))
165 (length (poly-termlist self))))
[52]166
[3518]167(defgeneric poly-multiply-by (self other)
[3519]168 (:documentation "Multiply a polynomial SELF by OTHER.")
[3518]169 (:method ((self poly) (other monom))
[3519]170 "Multiply a polynomial SELF by monomial OTHER"
171 (mapc #'(lambda (term) (cons (monom-multiply-by (car term) other) (cdr other)))
[3518]172 (poly-termlist self))
173 self))
[2469]174
[2761]175(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]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
[2756]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."
[3221]184 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
[2742]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?
[2774]191 (unless (endp q)
[2776]192 ;; Upon subtraction, we must change the sign of
193 ;; all coefficients in q
[2774]194 ,@(when uminus-fn
[2775]195 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]196 (setf r (nreconc r q)))
[2742]197 r)
198 (multiple-value-bind
199 (greater-p equal-p)
[3522]200 (funcall ,order-fn (caar p) (caar q))
[2742]201 (cond
202 (greater-p
203 (rotatef (cdr p) r p)
204 )
205 (equal-p
[2766]206 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]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
[2743]217 ;;Negate the term of Q if UMINUS provided, signallig
218 ;;that we are doing subtraction
[2908]219 ,(when uminus-fn
220 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[2743]221 (rotatef (cdr q) r q)))))))
[2585]222
[2655]223
[2763]224(defmacro def-add/subtract-method (add/subtract-method-name
[2752]225 uminus-method-name
226 &optional
[2913]227 (doc-string nil doc-string-supplied-p))
[3520]228 "This macro avoids code duplication for two similar operations: POLY-ADD-TO and POLY-SUBTRACT-FROM."
[2749]229 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]230 ,@(when doc-string-supplied-p `(,doc-string))
[2769]231 ;; Ensure orders are compatible
[3015]232 (change-term-order other self)
[2772]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))))
[2609]238 self))
[2487]239
[2916]240(eval-when (:compile-toplevel :load-toplevel :execute)
[2777]241
[3520]242 (def-add/subtract-method poly-add-to nil
[2777]243 "Adds to polynomial SELF another polynomial OTHER.
[2610]244This operation destructively modifies both polynomials.
245The result is stored in SELF. This implementation does
[2752]246no consing, entirely reusing the sells of SELF and OTHER.")
[2609]247
[3520]248 (def-add/subtract-method poly-subtract-from unary-minus
[2753]249 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]250This operation destructively modifies both polynomials.
251The result is stored in SELF. This implementation does
[2752]252no consing, entirely reusing the sells of SELF and OTHER.")
[2916]253 )
[2777]254
[2691]255(defmethod unary-minus ((self poly))
[2694]256 "Destructively modifies the coefficients of the polynomial SELF,
257by changing their sign."
[2692]258 (mapc #'unary-minus (poly-termlist self))
[2683]259 self)
[52]260
[2795]261(defun add-termlists (p q order-fn)
[2794]262 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
[3520]263 (fast-add/subtract p q order-fn #'poly-add-to nil))
[2794]264
[2800]265(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]266 &optional (reverse-arg-order-P nil))
[2799]267 "Multiplies term TERM by a list of term, TERMLIST.
[2792]268Takes into accound divisors of zero in the ring, by
[2927]269deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]270is T, change the order of arguments; this may be important
[2927]271if we extend the package to non-commutative rings."
[2800]272 `(mapcan #'(lambda (other-term)
[2907]273 (let ((prod (r*
[2923]274 ,@(cond
[2930]275 (reverse-arg-order-p
[2925]276 `(other-term ,term))
277 (t
278 `(,term other-term))))))
[2800]279 (cond
280 ((r-zerop prod) nil)
281 (t (list prod)))))
282 ,termlist))
[2790]283
[2796]284(defun multiply-termlists (p q order-fn)
[3127]285 "A version of polynomial multiplication, operating
286directly on termlists."
[2787]287 (cond
[2917]288 ((or (endp p) (endp q))
289 ;;p or q is 0 (represented by NIL)
290 nil)
[2789]291 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]292 ((endp (cdr p))
[2918]293 (multiply-term-by-termlist-dropping-zeros (car p) q))
294 ((endp (cdr q))
[2919]295 (multiply-term-by-termlist-dropping-zeros (car q) p t))
296 (t
[2948]297 (cons (r* (car p) (car q))
[2949]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)))))
[2793]302
[2803]303(defmethod multiply-by ((self poly) (other poly))
[3014]304 (change-term-order other self)
[2803]305 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
306 (poly-termlist other)
307 (poly-term-order self)))
308 self)
309
[3405]310(defmethod r+ ((poly1 poly) poly2)
[3374]311 "Non-destructively add POLY1 by POLY2."
[3520]312 (poly-add-to (copy-instance POLY1) (change-class (copy-instance POLY2) 'poly)))
[3374]313
[3430]314(defmethod r- ((minuend poly) &rest subtrahends)
[3427]315 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3520]316 (poly-subtract-from (copy-instance minuend)
[3433]317 (change-class (reduce #'r+ subtrahends) 'poly)))
[3374]318
[3407]319(defmethod r+ ((poly1 monom) poly2)
320 "Non-destructively add POLY1 by POLY2."
[3520]321 (poly-add-to (change-class (copy-instance poly1) 'poly)
[3431]322 (change-class (copy-instance poly2) 'poly)))
[3407]323
[3425]324(defmethod r- ((minuend monom) &rest subtrahends)
325 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3520]326 (poly-subtract-from (change-class (copy-instance minuend) 'poly)
[3434]327 (change-class (reduce #'r+ subtrahends) 'poly)))
[3407]328
[3374]329(defmethod r* ((poly1 poly) (poly2 poly))
[2939]330 "Non-destructively multiply POLY1 by POLY2."
[3432]331 (multiply-by (copy-instance poly1) (copy-instance poly2)))
[2916]332
[3062]333(defmethod left-tensor-product-by ((self poly) (other monom))
334 (setf (poly-termlist self)
335 (mapcan #'(lambda (term)
336 (let ((prod (left-tensor-product-by term other)))
337 (cond
338 ((r-zerop prod) nil)
339 (t (list prod)))))
340 (poly-termlist self)))
[3249]341 (incf (poly-dimension self) (monom-dimension other))
[3062]342 self)
[3044]343
[3062]344(defmethod right-tensor-product-by ((self poly) (other monom))
345 (setf (poly-termlist self)
346 (mapcan #'(lambda (term)
347 (let ((prod (right-tensor-product-by term other)))
348 (cond
349 ((r-zerop prod) nil)
350 (t (list prod)))))
351 (poly-termlist self)))
[3249]352 (incf (poly-dimension self) (monom-dimension other))
[3062]353 self)
354
355
[3084]356(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]357 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]358is a list of polynomials. Destructively modifies PLIST elements."
[3061]359 (mapc #'(lambda (poly)
[3085]360 (left-tensor-product-by
361 poly
362 (prog1
363 (make-monom-variable k i)
364 (incf i))))
[3061]365 plist))
[52]366
[3087]367(defun standard-extension-1 (plist
368 &aux
[3096]369 (plist (standard-extension plist))
[3087]370 (nvars (poly-dimension (car plist))))
[3081]371 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]372Firstly, new K variables U1, U2, ..., UK, are inserted into each
373polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]374tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]375polynomials have the same dimension, and only the first polynomial
376is examined to determine this dimension."
[3089]377 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
378 ;; 1 from each polynomial; since UI*PI has no constant term,
379 ;; we just need to append the constant term at the end
380 ;; of each termlist.
[3064]381 (flet ((subtract-1 (p)
[3503]382 (poly-append-term p (make-instance 'monom :dimension nvars) -1)))
[3083]383 (setf plist (mapc #'subtract-1 plist)))
[3077]384 plist)
[52]385
386
[3107]387(defun standard-sum (plist
388 &aux
389 (plist (standard-extension plist))
390 (nvars (poly-dimension (car plist))))
[3087]391 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
392Firstly, new K variables, U1, U2, ..., UK, are inserted into each
393polynomial. Subsequently, P1, P2, ..., PK are destructively modified
394tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]395are added. Finally, 1 is subtracted. It should be noted that the term
396order is not modified, which is equivalent to using a lexicographic
397order on the first K variables."
[3107]398 (flet ((subtract-1 (p)
[3504]399 (poly-append-term p (make-instance 'monom :dimension nvars) -1)))
[3108]400 (subtract-1
401 (make-instance
402 'poly
[3115]403 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]404
[3122]405#|
406
[1477]407(defun saturation-extension-1 (ring f p)
[1497]408 "Calculate [F, U*P-1]. It destructively modifies F."
[1908]409 (declare (type ring ring))
[1477]410 (polysaturation-extension ring f (list p)))
[53]411
[3122]412
[53]413
414
[1189]415(defun spoly (ring-and-order f g
416 &aux
417 (ring (ro-ring ring-and-order)))
[55]418 "It yields the S-polynomial of polynomials F and G."
[1911]419 (declare (type ring-and-order ring-and-order) (type poly f g))
[55]420 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
[2913]421 (mf (monom-div lcm (poly-lm f)))
422 (mg (monom-div lcm (poly-lm g))))
[55]423 (declare (type monom mf mg))
424 (multiple-value-bind (c cf cg)
425 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
426 (declare (ignore c))
427 (poly-sub
[1189]428 ring-and-order
[55]429 (scalar-times-poly ring cg (monom-times-poly mf f))
430 (scalar-times-poly ring cf (monom-times-poly mg g))))))
[53]431
432
[55]433(defun poly-primitive-part (ring p)
434 "Divide polynomial P with integer coefficients by gcd of its
435coefficients and return the result."
[1912]436 (declare (type ring ring) (type poly p))
[55]437 (if (poly-zerop p)
438 (values p 1)
[2913]439 (let ((c (poly-content ring p)))
440 (values (make-poly-from-termlist
441 (mapcar
442 #'(lambda (x)
443 (make-term :monom (term-monom x)
444 :coeff (funcall (ring-div ring) (term-coeff x) c)))
445 (poly-termlist p))
446 (poly-sugar p))
447 c))))
[55]448
449(defun poly-content (ring p)
450 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
451to compute the greatest common divisor."
[1913]452 (declare (type ring ring) (type poly p))
[55]453 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
[1066]454
[2456]455|#
Note: See TracBrowser for help on using the repository browser.