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

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

* empty log message *

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