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

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

* empty log message *

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