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

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

* empty log message *

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