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

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

* empty log message *

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