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

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

* empty log message *

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