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

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