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

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

* empty log message *

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