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

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