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

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

* empty log message *

File size: 15.5 KB
Line 
1;;; -*- Mode: Lisp -*-
2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;
4;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
5;;;
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 2 of the License, or
9;;; (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19;;;
20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
22(defpackage "POLYNOMIAL"
23 (:use :cl :utils :ring :monom :order :term)
24 (:export "POLY"
25 "POLY-DIMENSION"
26 "POLY-TERMLIST"
27 "POLY-TERM-ORDER"
28 "CHANGE-TERM-ORDER"
29 "STANDARD-EXTENSION"
30 "STANDARD-EXTENSION-1"
31 "STANDARD-SUM"
32 "SATURATION-EXTENSION"
33 "ALIST->POLY")
34 (:documentation "Implements polynomials."))
35
36(in-package :polynomial)
37
38(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
39
40(defclass poly ()
41 ((dimension :initform nil
42 :initarg :dimension
43 :accessor poly-dimension
44 :documentation "Shared dimension of all terms, the number of variables")
45 (termlist :initform nil :initarg :termlist :accessor poly-termlist
46 :documentation "List of terms.")
47 (order :initform #'lex> :initarg :order :accessor poly-term-order
48 :documentation "Monomial/term order."))
49 (:default-initargs :dimension nil :termlist nil :order #'lex>)
50 (:documentation "A polynomial with a list of terms TERMLIST, ordered
51according to term order ORDER, which defaults to LEX>."))
52
53(defmethod print-object ((self poly) stream)
54 (print-unreadable-object (self stream :type t :identity t)
55 (with-accessors ((dimension poly-dimension)
56 (termlist poly-termlist)
57 (order poly-term-order))
58 self
59 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
60 dimension termlist order))))
61
62(defgeneric change-term-order (self other)
63 (:documentation "Change term order of SELF to the term order of OTHER.")
64 (:method ((self poly) (other poly))
65 (unless (eq (poly-term-order self) (poly-term-order other))
66 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
67 (poly-term-order self) (poly-term-order other)))
68 self))
69
70(defun alist->poly (alist &aux (poly (make-instance 'poly)))
71 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
72It can be used to enter simple polynomials by hand, e.g the polynomial
73in two variables, X and Y, given in standard notation as:
74
75 3*X^2*Y^3+2*Y+7
76
77can be entered as
78(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
79
80NOTE: The primary use is for low-level debugging of the package."
81 (dolist (x alist poly)
82 (insert-item poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
83
84
85(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
86 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
87 (reinitialize-instance new
88 :dimension (monom-dimension old)
89 :termlist (list old)))
90
91(defmethod update-instance-for-different-class ((old monom) (new poly) &key)
92 (change-class (change-class old 'term) 'poly)
93 (call-next-method))
94
95(defmethod r-equalp ((self poly) (other poly))
96 "POLY instances are R-EQUALP if they have the same
97order and if all terms are R-EQUALP."
98 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
99 (eq (poly-term-order self) (poly-term-order other))))
100
101(defmethod insert-item ((self poly) (item term))
102 (cond ((null (poly-dimension self))
103 (setf (poly-dimension self) (monom-dimension item)))
104 (t (assert (= (poly-dimension self) (monom-dimension item)))))
105 (push item (poly-termlist self))
106 self)
107
108(defmethod append-item ((self poly) (item term))
109 (cond ((null (poly-dimension self))
110 (setf (poly-dimension self) (monom-dimension item)))
111 (t (assert (= (poly-dimension self) (monom-dimension item)))))
112 (setf (cdr (last (poly-termlist self))) (list item))
113 self)
114
115;; Leading term
116(defgeneric leading-term (object)
117 (:method ((self poly))
118 (car (poly-termlist self)))
119 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
120
121;; Second term
122(defgeneric second-leading-term (object)
123 (:method ((self poly))
124 (cadar (poly-termlist self)))
125 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
126
127;; Leading coefficient
128(defgeneric leading-coefficient (object)
129 (:method ((self poly))
130 (scalar-coeff (leading-term self)))
131 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
132
133;; Second coefficient
134(defgeneric second-leading-coefficient (object)
135 (:method ((self poly))
136 (scalar-coeff (second-leading-term self)))
137 (:documentation "The second leading coefficient of a polynomial. It
138 signals error for a polynomial with at most one term."))
139
140;; Testing for a zero polynomial
141(defmethod r-zerop ((self poly))
142 (null (poly-termlist self)))
143
144;; The number of terms
145(defmethod r-length ((self poly))
146 (length (poly-termlist self)))
147
148(defmethod multiply-by ((self poly) (other monom))
149 (mapc #'(lambda (term) (multiply-by term other))
150 (poly-termlist self))
151 self)
152
153(defmethod multiply-by ((self poly) (other term))
154 (mapc #'(lambda (term) (multiply-by term other))
155 (poly-termlist self))
156 self)
157
158(defmethod multiply-by ((self poly) (other scalar))
159 (mapc #'(lambda (term) (multiply-by term other))
160 (poly-termlist self))
161 self)
162
163
164(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
165 "Return an expression which will efficiently adds/subtracts two
166polynomials, P and Q. The addition/subtraction of coefficients is
167performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
168is supplied, it is used to negate the coefficients of Q which do not
169have a corresponding coefficient in P. The code implements an
170efficient algorithm to add two polynomials represented as sorted lists
171of terms. The code destroys both arguments, reusing the terms to build
172the result."
173 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
174 (do ((p ,p)
175 (q ,q)
176 r)
177 ((or (endp p) (endp q))
178 ;; NOTE: R contains the result in reverse order. Can it
179 ;; be more efficient to produce the terms in correct order?
180 (unless (endp q)
181 ;; Upon subtraction, we must change the sign of
182 ;; all coefficients in q
183 ,@(when uminus-fn
184 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
185 (setf r (nreconc r q)))
186 r)
187 (multiple-value-bind
188 (greater-p equal-p)
189 (funcall ,order-fn (car p) (car q))
190 (cond
191 (greater-p
192 (rotatef (cdr p) r p)
193 )
194 (equal-p
195 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
196 (cond
197 ((r-zerop s)
198 (setf p (cdr p))
199 )
200 (t
201 (setf (lc p) s)
202 (rotatef (cdr p) r p))))
203 (setf q (cdr q))
204 )
205 (t
206 ;;Negate the term of Q if UMINUS provided, signallig
207 ;;that we are doing subtraction
208 ,(when uminus-fn
209 `(setf (lc q) (funcall ,uminus-fn (lc q))))
210 (rotatef (cdr q) r q)))))))
211
212
213(defmacro def-add/subtract-method (add/subtract-method-name
214 uminus-method-name
215 &optional
216 (doc-string nil doc-string-supplied-p))
217 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
218 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
219 ,@(when doc-string-supplied-p `(,doc-string))
220 ;; Ensure orders are compatible
221 (change-term-order other self)
222 (setf (poly-termlist self) (fast-add/subtract
223 (poly-termlist self) (poly-termlist other)
224 (poly-term-order self)
225 #',add/subtract-method-name
226 ,(when uminus-method-name `(function ,uminus-method-name))))
227 self))
228
229(eval-when (:compile-toplevel :load-toplevel :execute)
230
231 (def-add/subtract-method add-to nil
232 "Adds to polynomial SELF another polynomial OTHER.
233This operation destructively modifies both polynomials.
234The result is stored in SELF. This implementation does
235no consing, entirely reusing the sells of SELF and OTHER.")
236
237 (def-add/subtract-method subtract-from unary-minus
238 "Subtracts from polynomial SELF another polynomial OTHER.
239This operation destructively modifies both polynomials.
240The result is stored in SELF. This implementation does
241no consing, entirely reusing the sells of SELF and OTHER.")
242 )
243
244(defmethod unary-minus ((self poly))
245 "Destructively modifies the coefficients of the polynomial SELF,
246by changing their sign."
247 (mapc #'unary-minus (poly-termlist self))
248 self)
249
250(defun add-termlists (p q order-fn)
251 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
252 (fast-add/subtract p q order-fn #'add-to nil))
253
254(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
255 &optional (reverse-arg-order-P nil))
256 "Multiplies term TERM by a list of term, TERMLIST.
257Takes into accound divisors of zero in the ring, by
258deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
259is T, change the order of arguments; this may be important
260if we extend the package to non-commutative rings."
261 `(mapcan #'(lambda (other-term)
262 (let ((prod (r*
263 ,@(cond
264 (reverse-arg-order-p
265 `(other-term ,term))
266 (t
267 `(,term other-term))))))
268 (cond
269 ((r-zerop prod) nil)
270 (t (list prod)))))
271 ,termlist))
272
273(defun multiply-termlists (p q order-fn)
274 "A version of polynomial multiplication, operating
275directly on termlists."
276 (cond
277 ((or (endp p) (endp q))
278 ;;p or q is 0 (represented by NIL)
279 nil)
280 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
281 ((endp (cdr p))
282 (multiply-term-by-termlist-dropping-zeros (car p) q))
283 ((endp (cdr q))
284 (multiply-term-by-termlist-dropping-zeros (car q) p t))
285 (t
286 (cons (r* (car p) (car q))
287 (add-termlists
288 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
289 (multiply-termlists (cdr p) q order-fn)
290 order-fn)))))
291
292(defmethod multiply-by ((self poly) (other poly))
293 (change-term-order other self)
294 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
295 (poly-termlist other)
296 (poly-term-order self)))
297 self)
298
299(defmethod r* ((poly1 poly) (poly2 poly))
300 "Non-destructively multiply POLY1 by POLY2."
301 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
302
303(defmethod left-tensor-product-by ((self poly) (other term))
304 (setf (poly-termlist self)
305 (mapcan #'(lambda (term)
306 (let ((prod (left-tensor-product-by term other)))
307 (cond
308 ((r-zerop prod) nil)
309 (t (list prod)))))
310 (poly-termlist self)))
311 self)
312
313(defmethod right-tensor-product-by ((self poly) (other term))
314 (setf (poly-termlist self)
315 (mapcan #'(lambda (term)
316 (let ((prod (right-tensor-product-by term other)))
317 (cond
318 ((r-zerop prod) nil)
319 (t (list prod)))))
320 (poly-termlist self)))
321 self)
322
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
328 ((r-zerop prod) nil)
329 (t (list prod)))))
330 (poly-termlist self)))
331 (incf (poly-dimension self) (monom-dimension other))
332 self)
333
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
339 ((r-zerop prod) nil)
340 (t (list prod)))))
341 (poly-termlist self)))
342 (incf (poly-dimension self) (monom-dimension other))
343 self)
344
345
346(defun standard-extension (plist &aux (k (length plist)) (i 0))
347 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
348is a list of polynomials. Destructively modifies PLIST elements."
349 (mapc #'(lambda (poly)
350 (left-tensor-product-by
351 poly
352 (prog1
353 (make-monom-variable k i)
354 (incf i))))
355 plist))
356
357(defun standard-extension-1 (plist
358 &aux
359 (plist (standard-extension plist))
360 (nvars (poly-dimension (car plist))))
361 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
362Firstly, new K variables U1, U2, ..., UK, are inserted into each
363polynomial. Subsequently, P1, P2, ..., PK are destructively modified
364tantamount to replacing PI with UI*PI-1. It assumes that all
365polynomials have the same dimension, and only the first polynomial
366is examined to determine this dimension."
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.
371 (flet ((subtract-1 (p)
372 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
373 (setf plist (mapc #'subtract-1 plist)))
374 plist)
375
376
377(defun standard-sum (plist
378 &aux
379 (plist (standard-extension plist))
380 (nvars (poly-dimension (car plist))))
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
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."
388 (flet ((subtract-1 (p)
389 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
390 (subtract-1
391 (make-instance
392 'poly
393 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
394
395#|
396
397(defun saturation-extension-1 (ring f p)
398 "Calculate [F, U*P-1]. It destructively modifies F."
399 (declare (type ring ring))
400 (polysaturation-extension ring f (list p)))
401
402
403
404
405(defun spoly (ring-and-order f g
406 &aux
407 (ring (ro-ring ring-and-order)))
408 "It yields the S-polynomial of polynomials F and G."
409 (declare (type ring-and-order ring-and-order) (type poly f g))
410 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
411 (mf (monom-div lcm (poly-lm f)))
412 (mg (monom-div lcm (poly-lm g))))
413 (declare (type monom mf mg))
414 (multiple-value-bind (c cf cg)
415 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
416 (declare (ignore c))
417 (poly-sub
418 ring-and-order
419 (scalar-times-poly ring cg (monom-times-poly mf f))
420 (scalar-times-poly ring cf (monom-times-poly mg g))))))
421
422
423(defun poly-primitive-part (ring p)
424 "Divide polynomial P with integer coefficients by gcd of its
425coefficients and return the result."
426 (declare (type ring ring) (type poly p))
427 (if (poly-zerop p)
428 (values p 1)
429 (let ((c (poly-content ring p)))
430 (values (make-poly-from-termlist
431 (mapcar
432 #'(lambda (x)
433 (make-term :monom (term-monom x)
434 :coeff (funcall (ring-div ring) (term-coeff x) c)))
435 (poly-termlist p))
436 (poly-sugar p))
437 c))))
438
439(defun poly-content (ring p)
440 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
441to compute the greatest common divisor."
442 (declare (type ring ring) (type poly p))
443 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
444
445|#
Note: See TracBrowser for help on using the repository browser.