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

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

* empty log message *

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