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

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

* empty log message *

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