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

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

* empty log message *

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