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

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

* empty log message *

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