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

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

* empty log message *

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