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

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

* empty log message *

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