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

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

* empty log message *

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