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

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

* empty log message *

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