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
Line 
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;;----------------------------------------------------------------
10;;; -*- Mode: Lisp -*-
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
31(defpackage "POLYNOMIAL"
32 (:use :cl :utils :monom :copy)
33 (:export "POLY"
34 "POLY-DIMENSION"
35 "POLY-TERMLIST"
36 "POLY-TERM-ORDER"
37 "POLY-INSERT-TERM"
38 "LEADING-TERM"
39 "LEADING-COEFFICIENT"
40 "ADD-TO"
41 "SUBTRACT-FROM"
42 "CHANGE-TERM-ORDER"
43 "STANDARD-EXTENSION"
44 "STANDARD-EXTENSION-1"
45 "STANDARD-SUM"
46 "SATURATION-EXTENSION"
47 "ALIST->POLY")
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."))
51
52(in-package :polynomial)
53
54(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
55
56(defclass poly ()
57 ((dimension :initform nil
58 :initarg :dimension
59 :accessor poly-dimension
60 :documentation "Shared dimension of all terms, the number of variables")
61 (termlist :initform nil :initarg :termlist :accessor poly-termlist
62 :documentation "List of terms.")
63 (order :initform #'lex> :initarg :order :accessor poly-term-order
64 :documentation "Monomial/term order."))
65 (:default-initargs :dimension nil :termlist nil :order #'lex>)
66 (:documentation "A polynomial with a list of terms TERMLIST, ordered
67according to term order ORDER, which defaults to LEX>."))
68
69(defmethod print-object ((self poly) stream)
70 (print-unreadable-object (self stream :type t :identity t)
71 (with-accessors ((dimension poly-dimension)
72 (termlist poly-termlist)
73 (order poly-term-order))
74 self
75 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
76 dimension termlist order))))
77
78(defgeneric change-term-order (self other)
79 (:documentation "Change term order of SELF to the term order of OTHER.")
80 (:method ((self poly) (other poly))
81 (unless (eq (poly-term-order self) (poly-term-order other))
82 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
83 (poly-term-order self) (poly-term-order other)))
84 self))
85
86(defgeneric poly-insert-term (self term)
87 (:documentation "Insert a term TERM into SELF before all other
88 terms. Order is not enforced.")
89 (:method ((self poly) (term term))
90 (cond ((null (poly-dimension self))
91 (setf (poly-dimension self) (monom-dimension term)))
92 (t (assert (= (poly-dimension self) (monom-dimension term)))))
93 (push term (poly-termlist self))
94 self))
95
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))
99 (cond ((null (poly-dimension self))
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))
103 self))
104
105(defun alist->poly (alist &aux (poly (make-instance 'poly)))
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."
116 (dolist (x alist poly)
117 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
118
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)
123 :termlist (list (cons old 1))))
124
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))))
130
131(defgeneric leading-term (object)
132 (:method ((self poly))
133 (car (poly-termlist self)))
134 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
135
136(defgeneric second-leading-term (object)
137 (:method ((self poly))
138 (cadar (poly-termlist self)))
139 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
140
141(defgeneric leading-coefficient (object)
142 (:method ((self poly))
143 (term-coeff (leading-term self)))
144 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
145
146
147(defgeneric second-leading-coefficient (object)
148 (:method ((self poly))
149 (term-coeff (poly-second-leading-term self)))
150 (:documentation "The second leading coefficient of a polynomial. It
151 signals error for a polynomial with at most one term."))
152
153(defmethod universal-zerop ((self poly))
154 "Return T iff SELF is a zero polynomial."
155 (null (poly-termlist self)))
156
157(defgeneric poly-length (self)
158 (:documentation "Return the number of terms.")
159 (:method ((self poly))
160 (length (poly-termlist self))))
161
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)
167
168(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
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
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."
177 `(macrolet ((lc (x) `(term-coeff (car ,x))))
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?
184 (unless (endp q)
185 ;; Upon subtraction, we must change the sign of
186 ;; all coefficients in q
187 ,@(when uminus-fn
188 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
189 (setf r (nreconc r q)))
190 r)
191 (multiple-value-bind
192 (greater-p equal-p)
193 (funcall ,order-fn (car p) (car q))
194 (cond
195 (greater-p
196 (rotatef (cdr p) r p)
197 )
198 (equal-p
199 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
200 (cond
201 ((universal-zerop s)
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
210 ;;Negate the term of Q if UMINUS provided, signallig
211 ;;that we are doing subtraction
212 ,(when uminus-fn
213 `(setf (lc q) (funcall ,uminus-fn (lc q))))
214 (rotatef (cdr q) r q)))))))
215
216
217(defmacro def-add/subtract-method (add/subtract-method-name
218 uminus-method-name
219 &optional
220 (doc-string nil doc-string-supplied-p))
221 "This macro avoids code duplication for two similar operations: POLY-ADD-TO and POLY-SUBTRACT-FROM."
222 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
223 ,@(when doc-string-supplied-p `(,doc-string))
224 ;; Ensure orders are compatible
225 (change-term-order other self)
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))))
231 self))
232
233(eval-when (:compile-toplevel :load-toplevel :execute)
234
235 (def-add/subtract-method add-to nil
236 "Adds to polynomial SELF another polynomial OTHER.
237This operation destructively modifies both polynomials.
238The result is stored in SELF. This implementation does
239no consing, entirely reusing the sells of SELF and OTHER.")
240
241 (def-add/subtract-method subtract-from unary-minus
242 "Subtracts from polynomial SELF another polynomial OTHER.
243This operation destructively modifies both polynomials.
244The result is stored in SELF. This implementation does
245no consing, entirely reusing the sells of SELF and OTHER.")
246 )
247
248(defmethod unary-minus ((self poly))
249 "Destructively modifies the coefficients of the polynomial SELF,
250by changing their sign."
251 (mapc #'unary-minus (poly-termlist self))
252 self)
253
254(defun add-termlists (p q order-fn)
255 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
256 (fast-add/subtract p q order-fn #'add-to nil))
257
258(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
259 &optional (reverse-arg-order-P nil))
260 "Multiplies term TERM by a list of term, TERMLIST.
261Takes into accound divisors of zero in the ring, by
262deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
263is T, change the order of arguments; this may be important
264if we extend the package to non-commutative rings."
265 `(mapcan #'(lambda (other-term)
266 (let ((prod (multiply
267 ,@(cond
268 (reverse-arg-order-p
269 `(other-term ,term))
270 (t
271 `(,term other-term))))))
272 (cond
273 ((universal-zerop prod) nil)
274 (t (list prod)))))
275 ,termlist))
276
277(defun multiply-termlists (p q order-fn)
278 "A version of polynomial multiplication, operating
279directly on termlists."
280 (cond
281 ((or (endp p) (endp q))
282 ;;p or q is 0 (represented by NIL)
283 nil)
284 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
285 ((endp (cdr p))
286 (multiply-term-by-termlist-dropping-zeros (car p) q))
287 ((endp (cdr q))
288 (multiply-term-by-termlist-dropping-zeros (car q) p t))
289 (t
290 (cons (multiply (car p) (car q))
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)))))
295
296(defmethod multiply-by ((self poly) (other poly))
297 (change-term-order other self)
298 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
299 (poly-termlist other)
300 (poly-term-order self)))
301 self)
302
303(defun add (object1 object2)
304 "Non-destructively add POLY1 by POLY2."
305 (add-to (copy-instance object1) (copy-instance object2)))
306
307(defun subtract (minuend &rest subtrahends)
308 "Non-destructively subtract MINUEND and SUBTRAHENDS."
309 (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))
310
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
316 ((universal-zerop prod) nil)
317 (t (list prod)))))
318 (poly-termlist self)))
319 (incf (poly-dimension self) (monom-dimension other))
320 self)
321
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
327 ((universal-zerop prod) nil)
328 (t (list prod)))))
329 (poly-termlist self)))
330 (incf (poly-dimension self) (monom-dimension other))
331 self)
332
333
334(defun standard-extension (plist &aux (k (length plist)) (i 0))
335 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
336is a list of polynomials. Destructively modifies PLIST elements."
337 (mapc #'(lambda (poly)
338 (left-tensor-product-by
339 poly
340 (prog1
341 (make-monom-variable k i)
342 (incf i))))
343 plist))
344
345(defun standard-extension-1 (plist
346 &aux
347 (plist (standard-extension plist))
348 (nvars (poly-dimension (car plist))))
349 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
350Firstly, new K variables U1, U2, ..., UK, are inserted into each
351polynomial. Subsequently, P1, P2, ..., PK are destructively modified
352tantamount to replacing PI with UI*PI-1. It assumes that all
353polynomials have the same dimension, and only the first polynomial
354is examined to determine this dimension."
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.
359 (flet ((subtract-1 (p)
360 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
361 (setf plist (mapc #'subtract-1 plist)))
362 plist)
363
364
365(defun standard-sum (plist
366 &aux
367 (plist (standard-extension plist))
368 (nvars (poly-dimension (car plist))))
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
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."
376 (flet ((subtract-1 (p)
377 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
378 (subtract-1
379 (make-instance
380 'poly
381 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
382
383#|
384
385(defun saturation-extension-1 (ring f p)
386 "Calculate [F, U*P-1]. It destructively modifies F."
387 (declare (type ring ring))
388 (polysaturation-extension ring f (list p)))
389
390
391
392
393(defun spoly (ring-and-order f g
394 &aux
395 (ring (ro-ring ring-and-order)))
396 "It yields the S-polynomial of polynomials F and G."
397 (declare (type ring-and-order ring-and-order) (type poly f g))
398 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
399 (mf (monom-div lcm (poly-lm f)))
400 (mg (monom-div lcm (poly-lm g))))
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
406 ring-and-order
407 (scalar-times-poly ring cg (monom-times-poly mf f))
408 (scalar-times-poly ring cf (monom-times-poly mg g))))))
409
410
411(defun poly-primitive-part (ring p)
412 "Divide polynomial P with integer coefficients by gcd of its
413coefficients and return the result."
414 (declare (type ring ring) (type poly p))
415 (if (poly-zerop p)
416 (values p 1)
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))))
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."
430 (declare (type ring ring) (type poly p))
431 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
432
433|#
Note: See TracBrowser for help on using the repository browser.