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

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

* empty log message *

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