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

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