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

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

* empty log message *

File size: 13.8 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 :ring :monom :order :term #| :infix |# )
24 (:export "POLY")
25 (:documentation "Implements polynomials"))
26
27(in-package :polynomial)
28
29(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
30
31(defclass poly ()
32 ((termlist :initarg :termlist :accessor poly-termlist))
33 (:default-initargs :termlist nil))
34
35(defmethod print-object ((self poly) stream)
36 (format stream "#<POLY TERMLIST=~A >" (poly-termlist self)))
37
38(defmethod insert-item ((self poly) (item term))
39 (push item (poly-termlist self))
40 self)
41
42(defmethod append-item ((self poly) (item term))
43 (setf (cdr (last (poly-termlist self))) (list item))
44 self)
45
46;; Leading term
47(defgeneric leading-term (object)
48 (:method ((self poly))
49 (car (poly-termlist self)))
50 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
51
52;; Second term
53(defgeneric second-leading-term (object)
54 (:method ((self poly))
55 (cadar (poly-termlist self)))
56 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
57
58;; Leading coefficient
59(defgeneric leading-coefficient (object)
60 (:method ((self poly))
61 (r-coeff (leading-term self)))
62 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
63
64;; Second coefficient
65(defgeneric second-leading-coefficient (object)
66 (:method ((self poly))
67 (r-coeff (second-leading-term self)))
68 (:documentation "The second leading coefficient of a polynomial. It signals error for a polynomial with at most one term."))
69
70;; Testing for a zero polynomial
71(defmethod r-zerop ((self poly))
72 (null (poly-termlist self)))
73
74;; The number of terms
75(defmethod r-length ((self poly))
76 (length (poly-termlist self)))
77
78(defmethod multiply-by ((self poly) (other monom))
79 (mapc #'(lambda (term) (multiply-by term other))
80 (poly-termlist self))
81 self)
82
83(defmethod multiply-by ((self poly) (other scalar))
84 (mapc #'(lambda (term) (multiply-by term other))
85 (poly-termlist self))
86 self)
87
88(defmethod add-to ((self poly) (other poly))
89 "Adds to polynomial SELF another polynomial OTHER.
90This operation destructively modifies both polynomials.
91The result is stored in SELF. This implementation does
92no consing, entirely reusing the sells of SELF and OTHER."
93 (macrolet ((lt (termlist) `(car ,termlist))
94 (lc (termlist) `(r-coeff (car ,termlist))))
95 (with-slots ((termlist1 termlist))
96 self
97 (with-slots ((termlist2 termlist))
98 other
99 (do ((p termlist1)
100 (q termlist2))
101 ((or (endp p) (endp q))
102 (setf termlist1 p))
103 (multiple-value-bind
104 (greater-p equal-p)
105 (lex> (lt q) (lt p))
106 (cond
107 (greater-p
108 ;; P' <- Q
109 ;; (CDR P') <- P
110 ;; Q' <- (CDR Q)
111 (rotatef p q (cdr q)))
112 (equal-p
113 (setf (lc p) (add-to (lc p) (lc q))
114 p (cdr p)
115 q (cdr q)))))))))
116
117 self)
118
119(defmethod subtract-from ((self poly) (other poly)))
120
121(defmethod unary-uminus ((self poly)))
122
123#|
124
125(defun poly-standard-extension (plist &aux (k (length plist)))
126 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
127 (declare (list plist) (fixnum k))
128 (labels ((incf-power (g i)
129 (dolist (x (poly-termlist g))
130 (incf (monom-elt (term-monom x) i)))
131 (incf (poly-sugar g))))
132 (setf plist (poly-list-add-variables plist k))
133 (dotimes (i k plist)
134 (incf-power (nth i plist) i))))
135
136(defun saturation-extension (ring f plist
137 &aux
138 (k (length plist))
139 (d (monom-dimension (poly-lm (car plist))))
140 f-x plist-x)
141 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
142 (declare (type ring ring))
143 (setf f-x (poly-list-add-variables f k)
144 plist-x (mapcar #'(lambda (x)
145 (setf (poly-termlist x)
146 (nconc (poly-termlist x)
147 (list (make-term :monom (make-monom :dimension d)
148 :coeff (funcall (ring-uminus ring)
149 (funcall (ring-unit ring)))))))
150 x)
151 (poly-standard-extension plist)))
152 (append f-x plist-x))
153
154
155(defun polysaturation-extension (ring f plist
156 &aux
157 (k (length plist))
158 (d (+ k (monom-dimension (poly-lm (car plist)))))
159 ;; Add k variables to f
160 (f (poly-list-add-variables f k))
161 ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
162 (plist (apply #'poly-append (poly-standard-extension plist))))
163 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
164 ;; Add -1 as the last term
165 (declare (type ring ring))
166 (setf (cdr (last (poly-termlist plist)))
167 (list (make-term :monom (make-monom :dimension d)
168 :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
169 (append f (list plist)))
170
171(defun saturation-extension-1 (ring f p)
172 "Calculate [F, U*P-1]. It destructively modifies F."
173 (declare (type ring ring))
174 (polysaturation-extension ring f (list p)))
175
176;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177;;
178;; Evaluation of polynomial (prefix) expressions
179;;
180;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181
182(defun coerce-coeff (ring expr vars)
183 "Coerce an element of the coefficient ring to a constant polynomial."
184 ;; Modular arithmetic handler by rat
185 (declare (type ring ring))
186 (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
187 :coeff (funcall (ring-parse ring) expr)))
188 0))
189
190(defun poly-eval (expr vars
191 &optional
192 (ring +ring-of-integers+)
193 (order #'lex>)
194 (list-marker :[)
195 &aux
196 (ring-and-order (make-ring-and-order :ring ring :order order)))
197 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
198variables VARS. Return the resulting polynomial or list of
199polynomials. Standard arithmetical operators in form EXPR are
200replaced with their analogues in the ring of polynomials, and the
201resulting expression is evaluated, resulting in a polynomial or a list
202of polynomials in internal form. A similar operation in another computer
203algebra system could be called 'expand' or so."
204 (declare (type ring ring))
205 (labels ((p-eval (arg) (poly-eval arg vars ring order))
206 (p-eval-scalar (arg) (poly-eval-scalar arg))
207 (p-eval-list (args) (mapcar #'p-eval args))
208 (p-add (x y) (poly-add ring-and-order x y)))
209 (cond
210 ((null expr) (error "Empty expression"))
211 ((eql expr 0) (make-poly-zero))
212 ((member expr vars :test #'equalp)
213 (let ((pos (position expr vars :test #'equalp)))
214 (make-poly-variable ring (length vars) pos)))
215 ((atom expr)
216 (coerce-coeff ring expr vars))
217 ((eq (car expr) list-marker)
218 (cons list-marker (p-eval-list (cdr expr))))
219 (t
220 (case (car expr)
221 (+ (reduce #'p-add (p-eval-list (cdr expr))))
222 (- (case (length expr)
223 (1 (make-poly-zero))
224 (2 (poly-uminus ring (p-eval (cadr expr))))
225 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
226 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
227 (reduce #'p-add (p-eval-list (cddr expr)))))))
228 (*
229 (if (endp (cddr expr)) ;unary
230 (p-eval (cdr expr))
231 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
232 (/
233 ;; A polynomial can be divided by a scalar
234 (cond
235 ((endp (cddr expr))
236 ;; A special case (/ ?), the inverse
237 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
238 (t
239 (let ((num (p-eval (cadr expr)))
240 (denom-inverse (apply (ring-div ring)
241 (cons (funcall (ring-unit ring))
242 (mapcar #'p-eval-scalar (cddr expr))))))
243 (scalar-times-poly ring denom-inverse num)))))
244 (expt
245 (cond
246 ((member (cadr expr) vars :test #'equalp)
247 ;;Special handling of (expt var pow)
248 (let ((pos (position (cadr expr) vars :test #'equalp)))
249 (make-poly-variable ring (length vars) pos (caddr expr))))
250 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
251 ;; Negative power means division in coefficient ring
252 ;; Non-integer power means non-polynomial coefficient
253 (coerce-coeff ring expr vars))
254 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
255 (otherwise
256 (coerce-coeff ring expr vars)))))))
257
258(defun poly-eval-scalar (expr
259 &optional
260 (ring +ring-of-integers+)
261 &aux
262 (order #'lex>))
263 "Evaluate a scalar expression EXPR in ring RING."
264 (declare (type ring ring))
265 (poly-lc (poly-eval expr nil ring order)))
266
267(defun spoly (ring-and-order f g
268 &aux
269 (ring (ro-ring ring-and-order)))
270 "It yields the S-polynomial of polynomials F and G."
271 (declare (type ring-and-order ring-and-order) (type poly f g))
272 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
273 (mf (monom-div lcm (poly-lm f)))
274 (mg (monom-div lcm (poly-lm g))))
275 (declare (type monom mf mg))
276 (multiple-value-bind (c cf cg)
277 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
278 (declare (ignore c))
279 (poly-sub
280 ring-and-order
281 (scalar-times-poly ring cg (monom-times-poly mf f))
282 (scalar-times-poly ring cf (monom-times-poly mg g))))))
283
284
285(defun poly-primitive-part (ring p)
286 "Divide polynomial P with integer coefficients by gcd of its
287coefficients and return the result."
288 (declare (type ring ring) (type poly p))
289 (if (poly-zerop p)
290 (values p 1)
291 (let ((c (poly-content ring p)))
292 (values (make-poly-from-termlist
293 (mapcar
294 #'(lambda (x)
295 (make-term :monom (term-monom x)
296 :coeff (funcall (ring-div ring) (term-coeff x) c)))
297 (poly-termlist p))
298 (poly-sugar p))
299 c))))
300
301(defun poly-content (ring p)
302 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
303to compute the greatest common divisor."
304 (declare (type ring ring) (type poly p))
305 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
306
307(defun read-infix-form (&key (stream t))
308 "Parser of infix expressions with integer/rational coefficients
309The parser will recognize two kinds of polynomial expressions:
310
311- polynomials in fully expanded forms with coefficients
312 written in front of symbolic expressions; constants can be optionally
313 enclosed in (); for example, the infix form
314 X^2-Y^2+(-4/3)*U^2*W^3-5
315 parses to
316 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
317
318- lists of polynomials; for example
319 [X-Y, X^2+3*Z]
320 parses to
321 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
322 where the first symbol [ marks a list of polynomials.
323
324-other infix expressions, for example
325 [(X-Y)*(X+Y)/Z,(X+1)^2]
326parses to:
327 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
328Currently this function is implemented using M. Kantrowitz's INFIX package."
329 (read-from-string
330 (concatenate 'string
331 "#I("
332 (with-output-to-string (s)
333 (loop
334 (multiple-value-bind (line eof)
335 (read-line stream t)
336 (format s "~A" line)
337 (when eof (return)))))
338 ")")))
339
340(defun read-poly (vars &key
341 (stream t)
342 (ring +ring-of-integers+)
343 (order #'lex>))
344 "Reads an expression in prefix form from a stream STREAM.
345The expression read from the strem should represent a polynomial or a
346list of polynomials in variables VARS, over the ring RING. The
347polynomial or list of polynomials is returned, with terms in each
348polynomial ordered according to monomial order ORDER."
349 (poly-eval (read-infix-form :stream stream) vars ring order))
350
351(defun string->poly (str vars
352 &optional
353 (ring +ring-of-integers+)
354 (order #'lex>))
355 "Converts a string STR to a polynomial in variables VARS."
356 (with-input-from-string (s str)
357 (read-poly vars :stream s :ring ring :order order)))
358
359(defun poly->alist (p)
360 "Convert a polynomial P to an association list. Thus, the format of the
361returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
362MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
363corresponding coefficient in the ring."
364 (cond
365 ((poly-p p)
366 (mapcar #'term->cons (poly-termlist p)))
367 ((and (consp p) (eq (car p) :[))
368 (cons :[ (mapcar #'poly->alist (cdr p))))))
369
370(defun string->alist (str vars
371 &optional
372 (ring +ring-of-integers+)
373 (order #'lex>))
374 "Convert a string STR representing a polynomial or polynomial list to
375an association list (... (MONOM . COEFF) ...)."
376 (poly->alist (string->poly str vars ring order)))
377
378(defun poly-equal-no-sugar-p (p q)
379 "Compare polynomials for equality, ignoring sugar."
380 (declare (type poly p q))
381 (equalp (poly-termlist p) (poly-termlist q)))
382
383(defun poly-set-equal-no-sugar-p (p q)
384 "Compare polynomial sets P and Q for equality, ignoring sugar."
385 (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
386
387(defun poly-list-equal-no-sugar-p (p q)
388 "Compare polynomial lists P and Q for equality, ignoring sugar."
389 (every #'poly-equal-no-sugar-p p q))
390|#
Note: See TracBrowser for help on using the repository browser.