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

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

* empty log message *

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