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

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

* empty log message *

File size: 15.6 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 :after ((old monom) (new poly) &key)
92 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
93 (reinitialize-instance new
94 :dimension (monom-dimension old)
95 :termlist (list (change-class old 'term))))
96
97(defmethod r-equalp ((self poly) (other poly))
98 "POLY instances are R-EQUALP if they have the same
99order and if all terms are R-EQUALP."
100 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
101 (eq (poly-term-order self) (poly-term-order other))))
102
103(defmethod insert-item ((self poly) (item term))
104 (cond ((null (poly-dimension self))
105 (setf (poly-dimension self) (monom-dimension item)))
106 (t (assert (= (poly-dimension self) (monom-dimension item)))))
107 (push item (poly-termlist self))
108 self)
109
110(defmethod append-item ((self poly) (item term))
111 (cond ((null (poly-dimension self))
112 (setf (poly-dimension self) (monom-dimension item)))
113 (t (assert (= (poly-dimension self) (monom-dimension item)))))
114 (setf (cdr (last (poly-termlist self))) (list item))
115 self)
116
117;; Leading term
118(defgeneric leading-term (object)
119 (:method ((self poly))
120 (car (poly-termlist self)))
121 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
122
123;; Second term
124(defgeneric second-leading-term (object)
125 (:method ((self poly))
126 (cadar (poly-termlist self)))
127 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
128
129;; Leading coefficient
130(defgeneric leading-coefficient (object)
131 (:method ((self poly))
132 (scalar-coeff (leading-term self)))
133 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
134
135;; Second coefficient
136(defgeneric second-leading-coefficient (object)
137 (:method ((self poly))
138 (scalar-coeff (second-leading-term self)))
139 (:documentation "The second leading coefficient of a polynomial. It
140 signals error for a polynomial with at most one term."))
141
142;; Testing for a zero polynomial
143(defmethod r-zerop ((self poly))
144 (null (poly-termlist self)))
145
146;; The number of terms
147(defmethod r-length ((self poly))
148 (length (poly-termlist self)))
149
150(defmethod multiply-by ((self poly) (other monom))
151 (mapc #'(lambda (term) (multiply-by term other))
152 (poly-termlist self))
153 self)
154
155(defmethod multiply-by ((self poly) (other term))
156 (mapc #'(lambda (term) (multiply-by term other))
157 (poly-termlist self))
158 self)
159
160(defmethod multiply-by ((self poly) (other scalar))
161 (mapc #'(lambda (term) (multiply-by term other))
162 (poly-termlist self))
163 self)
164
165
166(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
167 "Return an expression which will efficiently adds/subtracts two
168polynomials, P and Q. The addition/subtraction of coefficients is
169performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
170is supplied, it is used to negate the coefficients of Q which do not
171have a corresponding coefficient in P. The code implements an
172efficient algorithm to add two polynomials represented as sorted lists
173of terms. The code destroys both arguments, reusing the terms to build
174the result."
175 `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
176 (do ((p ,p)
177 (q ,q)
178 r)
179 ((or (endp p) (endp q))
180 ;; NOTE: R contains the result in reverse order. Can it
181 ;; be more efficient to produce the terms in correct order?
182 (unless (endp q)
183 ;; Upon subtraction, we must change the sign of
184 ;; all coefficients in q
185 ,@(when uminus-fn
186 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
187 (setf r (nreconc r q)))
188 r)
189 (multiple-value-bind
190 (greater-p equal-p)
191 (funcall ,order-fn (car p) (car q))
192 (cond
193 (greater-p
194 (rotatef (cdr p) r p)
195 )
196 (equal-p
197 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
198 (cond
199 ((r-zerop s)
200 (setf p (cdr p))
201 )
202 (t
203 (setf (lc p) s)
204 (rotatef (cdr p) r p))))
205 (setf q (cdr q))
206 )
207 (t
208 ;;Negate the term of Q if UMINUS provided, signallig
209 ;;that we are doing subtraction
210 ,(when uminus-fn
211 `(setf (lc q) (funcall ,uminus-fn (lc q))))
212 (rotatef (cdr q) r q)))))))
213
214
215(defmacro def-add/subtract-method (add/subtract-method-name
216 uminus-method-name
217 &optional
218 (doc-string nil doc-string-supplied-p))
219 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
220 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
221 ,@(when doc-string-supplied-p `(,doc-string))
222 ;; Ensure orders are compatible
223 (change-term-order other self)
224 (setf (poly-termlist self) (fast-add/subtract
225 (poly-termlist self) (poly-termlist other)
226 (poly-term-order self)
227 #',add/subtract-method-name
228 ,(when uminus-method-name `(function ,uminus-method-name))))
229 self))
230
231(eval-when (:compile-toplevel :load-toplevel :execute)
232
233 (def-add/subtract-method add-to nil
234 "Adds to polynomial SELF another polynomial OTHER.
235This operation destructively modifies both polynomials.
236The result is stored in SELF. This implementation does
237no consing, entirely reusing the sells of SELF and OTHER.")
238
239 (def-add/subtract-method subtract-from unary-minus
240 "Subtracts from polynomial SELF another polynomial OTHER.
241This operation destructively modifies both polynomials.
242The result is stored in SELF. This implementation does
243no consing, entirely reusing the sells of SELF and OTHER.")
244 )
245
246(defmethod unary-minus ((self poly))
247 "Destructively modifies the coefficients of the polynomial SELF,
248by changing their sign."
249 (mapc #'unary-minus (poly-termlist self))
250 self)
251
252(defun add-termlists (p q order-fn)
253 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
254 (fast-add/subtract p q order-fn #'add-to nil))
255
256(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
257 &optional (reverse-arg-order-P nil))
258 "Multiplies term TERM by a list of term, TERMLIST.
259Takes into accound divisors of zero in the ring, by
260deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
261is T, change the order of arguments; this may be important
262if we extend the package to non-commutative rings."
263 `(mapcan #'(lambda (other-term)
264 (let ((prod (r*
265 ,@(cond
266 (reverse-arg-order-p
267 `(other-term ,term))
268 (t
269 `(,term other-term))))))
270 (cond
271 ((r-zerop prod) nil)
272 (t (list prod)))))
273 ,termlist))
274
275(defun multiply-termlists (p q order-fn)
276 "A version of polynomial multiplication, operating
277directly on termlists."
278 (cond
279 ((or (endp p) (endp q))
280 ;;p or q is 0 (represented by NIL)
281 nil)
282 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
283 ((endp (cdr p))
284 (multiply-term-by-termlist-dropping-zeros (car p) q))
285 ((endp (cdr q))
286 (multiply-term-by-termlist-dropping-zeros (car q) p t))
287 (t
288 (cons (r* (car p) (car q))
289 (add-termlists
290 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
291 (multiply-termlists (cdr p) q order-fn)
292 order-fn)))))
293
294(defmethod multiply-by ((self poly) (other poly))
295 (change-term-order other self)
296 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
297 (poly-termlist other)
298 (poly-term-order self)))
299 self)
300
301(defmethod r* ((poly1 poly) (poly2 poly))
302 "Non-destructively multiply POLY1 by POLY2."
303 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
304
305(defmethod left-tensor-product-by ((self poly) (other term))
306 (setf (poly-termlist self)
307 (mapcan #'(lambda (term)
308 (let ((prod (left-tensor-product-by term other)))
309 (cond
310 ((r-zerop prod) nil)
311 (t (list prod)))))
312 (poly-termlist self)))
313 self)
314
315(defmethod right-tensor-product-by ((self poly) (other term))
316 (setf (poly-termlist self)
317 (mapcan #'(lambda (term)
318 (let ((prod (right-tensor-product-by term other)))
319 (cond
320 ((r-zerop prod) nil)
321 (t (list prod)))))
322 (poly-termlist self)))
323 self)
324
325(defmethod left-tensor-product-by ((self poly) (other monom))
326 (setf (poly-termlist self)
327 (mapcan #'(lambda (term)
328 (let ((prod (left-tensor-product-by term other)))
329 (cond
330 ((r-zerop prod) nil)
331 (t (list prod)))))
332 (poly-termlist self)))
333 (incf (poly-dimension self) (monom-dimension other))
334 self)
335
336(defmethod right-tensor-product-by ((self poly) (other monom))
337 (setf (poly-termlist self)
338 (mapcan #'(lambda (term)
339 (let ((prod (right-tensor-product-by term other)))
340 (cond
341 ((r-zerop prod) nil)
342 (t (list prod)))))
343 (poly-termlist self)))
344 (incf (poly-dimension self) (monom-dimension other))
345 self)
346
347
348(defun standard-extension (plist &aux (k (length plist)) (i 0))
349 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
350is a list of polynomials. Destructively modifies PLIST elements."
351 (mapc #'(lambda (poly)
352 (left-tensor-product-by
353 poly
354 (prog1
355 (make-monom-variable k i)
356 (incf i))))
357 plist))
358
359(defun standard-extension-1 (plist
360 &aux
361 (plist (standard-extension plist))
362 (nvars (poly-dimension (car plist))))
363 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
364Firstly, new K variables U1, U2, ..., UK, are inserted into each
365polynomial. Subsequently, P1, P2, ..., PK are destructively modified
366tantamount to replacing PI with UI*PI-1. It assumes that all
367polynomials have the same dimension, and only the first polynomial
368is examined to determine this dimension."
369 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
370 ;; 1 from each polynomial; since UI*PI has no constant term,
371 ;; we just need to append the constant term at the end
372 ;; of each termlist.
373 (flet ((subtract-1 (p)
374 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
375 (setf plist (mapc #'subtract-1 plist)))
376 plist)
377
378
379(defun standard-sum (plist
380 &aux
381 (plist (standard-extension plist))
382 (nvars (poly-dimension (car plist))))
383 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
384Firstly, new K variables, U1, U2, ..., UK, are inserted into each
385polynomial. Subsequently, P1, P2, ..., PK are destructively modified
386tantamount to replacing PI with UI*PI, and the resulting polynomials
387are added. Finally, 1 is subtracted. It should be noted that the term
388order is not modified, which is equivalent to using a lexicographic
389order on the first K variables."
390 (flet ((subtract-1 (p)
391 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
392 (subtract-1
393 (make-instance
394 'poly
395 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
396
397#|
398
399(defun saturation-extension-1 (ring f p)
400 "Calculate [F, U*P-1]. It destructively modifies F."
401 (declare (type ring ring))
402 (polysaturation-extension ring f (list p)))
403
404
405
406
407(defun spoly (ring-and-order f g
408 &aux
409 (ring (ro-ring ring-and-order)))
410 "It yields the S-polynomial of polynomials F and G."
411 (declare (type ring-and-order ring-and-order) (type poly f g))
412 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
413 (mf (monom-div lcm (poly-lm f)))
414 (mg (monom-div lcm (poly-lm g))))
415 (declare (type monom mf mg))
416 (multiple-value-bind (c cf cg)
417 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
418 (declare (ignore c))
419 (poly-sub
420 ring-and-order
421 (scalar-times-poly ring cg (monom-times-poly mf f))
422 (scalar-times-poly ring cf (monom-times-poly mg g))))))
423
424
425(defun poly-primitive-part (ring p)
426 "Divide polynomial P with integer coefficients by gcd of its
427coefficients and return the result."
428 (declare (type ring ring) (type poly p))
429 (if (poly-zerop p)
430 (values p 1)
431 (let ((c (poly-content ring p)))
432 (values (make-poly-from-termlist
433 (mapcar
434 #'(lambda (x)
435 (make-term :monom (term-monom x)
436 :coeff (funcall (ring-div ring) (term-coeff x) c)))
437 (poly-termlist p))
438 (poly-sugar p))
439 c))))
440
441(defun poly-content (ring p)
442 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
443to compute the greatest common divisor."
444 (declare (type ring ring) (type poly p))
445 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
446
447|#
Note: See TracBrowser for help on using the repository browser.