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

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

* empty log message *

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