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

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

* empty log message *

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