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

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

* empty log message *

File size: 12.5 KB
Line 
1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
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
22
23(defpackage "POLYNOMIAL"
24 (:use :cl :ring :ring-and-order :monomial :order :term :termlist :infix)
25 (:export "POLY"
26 "POLY-TERMLIST"
27 "POLY-SUGAR"
28 "POLY-LT"
29 "MAKE-POLY-FROM-TERMLIST"
30 "MAKE-POLY-ZERO"
31 "MAKE-VARIABLE"
32 "POLY-UNIT"
33 "POLY-LM"
34 "POLY-SECOND-LM"
35 "POLY-SECOND-LT"
36 "POLY-LC"
37 "POLY-SECOND-LC"
38 "POLY-ZEROP"
39 "POLY-LENGTH"
40 "SCALAR-TIMES-POLY"
41 "SCALAR-TIMES-POLY-1"
42 "MONOM-TIMES-POLY"
43 "TERM-TIMES-POLY"
44 "POLY-ADD"
45 "POLY-SUB"
46 "POLY-UMINUS"
47 "POLY-MUL"
48 "POLY-EXPT"
49 "POLY-APPEND"
50 "POLY-NREVERSE"
51 "POLY-CONTRACT"
52 "POLY-EXTEND"
53 "POLY-ADD-VARIABLES"
54 "POLY-LIST-ADD-VARIABLES"
55 "POLY-STANDARD-EXTENSION"
56 "SATURATION-EXTENSION"
57 "POLYSATURATION-EXTENSION"
58 "SATURATION-EXTENSION-1"
59 "COERCE-COEFF"
60 "POLY-EVAL"
61 "SPOLY"
62 "POLY-PRIMITIVE-PART"
63 "POLY-CONTENT"
64 "READ-INFIX-FORM"
65 "POLY-READER"
66 ))
67
68(in-package :polynomial)
69
70;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71;;
72;; Polynomials
73;;
74;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75
76(defstruct (poly
77 ;;
78 ;; BOA constructor, by default constructs zero polynomial
79 (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
80 (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
81 ;; Constructor of polynomials representing a variable
82 (:constructor make-variable (ring nvars pos &optional (power 1)
83 &aux
84 (termlist (list
85 (make-term-variable ring nvars pos power)))
86 (sugar power)))
87 (:constructor poly-unit (ring dimension
88 &aux
89 (termlist (termlist-unit ring dimension))
90 (sugar 0))))
91 (termlist nil :type list)
92 (sugar -1 :type fixnum))
93
94;; Leading term
95(defmacro poly-lt (p) `(car (poly-termlist ,p)))
96
97;; Second term
98(defmacro poly-second-lt (p) `(cadar (poly-termlist ,p)))
99
100;; Leading monomial
101(defun poly-lm (p) (term-monom (poly-lt p)))
102
103;; Second monomial
104(defun poly-second-lm (p) (term-monom (poly-second-lt p)))
105
106;; Leading coefficient
107(defun poly-lc (p) (term-coeff (poly-lt p)))
108
109;; Second coefficient
110(defun poly-second-lc (p) (term-coeff (poly-second-lt p)))
111
112;; Testing for a zero polynomial
113(defun poly-zerop (p) (null (poly-termlist p)))
114
115;; The number of terms
116(defun poly-length (p) (length (poly-termlist p)))
117
118(defun scalar-times-poly (ring c p)
119 (declare (type ring ring) (poly p))
120 (make-poly-from-termlist (scalar-times-termlist ring c (poly-termlist p)) (poly-sugar p)))
121
122;; The scalar product omitting the head term
123(defun scalar-times-poly-1 (ring c p)
124 (declare (type ring ring) (poly p))
125 (make-poly-from-termlist (scalar-times-termlist ring c (cdr (poly-termlist p))) (poly-sugar p)))
126
127(defun monom-times-poly (m p)
128 (declare (poly p))
129 (make-poly-from-termlist
130 (monom-times-termlist m (poly-termlist p))
131 (+ (poly-sugar p) (monom-sugar m))))
132
133(defun term-times-poly (ring term p)
134 (declare (type ring ring) (type term term) (type poly p))
135 (make-poly-from-termlist
136 (term-times-termlist ring term (poly-termlist p))
137 (+ (poly-sugar p) (term-sugar term))))
138
139(defun poly-add (ring-and-order p q)
140 (declare (type ring-and-order ring-and-order) (type poly p q))
141 (make-poly-from-termlist
142 (termlist-add ring-and-order
143 (poly-termlist p)
144 (poly-termlist q))
145 (max (poly-sugar p) (poly-sugar q))))
146
147(defun poly-sub (ring-and-order p q)
148 (declare (type ring-and-order ring-and-order) (type poly p q))
149 (make-poly-from-termlist
150 (termlist-sub ring-and-order (poly-termlist p) (poly-termlist q))
151 (max (poly-sugar p) (poly-sugar q))))
152
153(defun poly-uminus (ring p)
154 (declare (type ring ring) (type poly p))
155 (make-poly-from-termlist
156 (termlist-uminus ring (poly-termlist p))
157 (poly-sugar p)))
158
159(defun poly-mul (ring-and-order p q)
160 (declare (type ring-and-order ring-and-order) (type poly p q))
161 (make-poly-from-termlist
162 (termlist-mul ring-and-order (poly-termlist p) (poly-termlist q))
163 (+ (poly-sugar p) (poly-sugar q))))
164
165(defun poly-expt (ring-and-order p n)
166 (declare (type ring-and-order ring-and-order) (type poly p))
167 (make-poly-from-termlist (termlist-expt ring-and-order (poly-termlist p) n) (* n (poly-sugar p))))
168
169(defun poly-append (&rest plist)
170 (make-poly-from-termlist (apply #'append (mapcar #'poly-termlist plist))
171 (apply #'max (mapcar #'poly-sugar plist))))
172
173(defun poly-nreverse (p)
174 (declare (type poly p))
175 (setf (poly-termlist p) (nreverse (poly-termlist p)))
176 p)
177
178(defun poly-contract (p &optional (k 1))
179 (declare (type poly p))
180 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
181 (poly-sugar p)))
182
183(defun poly-extend (p &optional (m (make-monom :dimension 1)))
184 (declare (type poly p))
185 (make-poly-from-termlist
186 (termlist-extend (poly-termlist p) m)
187 (+ (poly-sugar p) (monom-sugar m))))
188
189(defun poly-add-variables (p k)
190 (declare (type poly p))
191 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
192 p)
193
194(defun poly-list-add-variables (plist k)
195 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
196
197(defun poly-standard-extension (plist &aux (k (length plist)))
198 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
199 (declare (list plist) (fixnum k))
200 (labels ((incf-power (g i)
201 (dolist (x (poly-termlist g))
202 (incf (monom-elt (term-monom x) i)))
203 (incf (poly-sugar g))))
204 (setf plist (poly-list-add-variables plist k))
205 (dotimes (i k plist)
206 (incf-power (nth i plist) i))))
207
208(defun saturation-extension (ring f plist &aux (k (length plist)) (d (monom-dimension (poly-lm (car plist)))))
209 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
210 (setf f (poly-list-add-variables f k)
211 plist (mapcar #'(lambda (x)
212 (setf (poly-termlist x) (nconc (poly-termlist x)
213 (list (make-term (make-monom :dimension d)
214 (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
215 x)
216 (poly-standard-extension plist)))
217 (append f plist))
218
219
220(defun polysaturation-extension (ring f plist &aux (k (length plist))
221 (d (+ k (monom-dimension (poly-lm (car plist))))))
222 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
223 (setf f (poly-list-add-variables f k)
224 plist (apply #'poly-append (poly-standard-extension plist))
225 (cdr (last (poly-termlist plist))) (list (make-term (make-monom :dimension d)
226 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
227 (append f (list plist)))
228
229(defun saturation-extension-1 (ring f p) (polysaturation-extension ring f (list p)))
230
231;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232;;
233;; Evaluation of polynomial (prefix) expressions
234;;
235;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
236
237(defun coerce-coeff (ring expr vars)
238 "Coerce an element of the coefficient ring to a constant polynomial."
239 ;; Modular arithmetic handler by rat
240 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
241 (funcall (ring-parse ring) expr)))
242 0))
243
244(defun poly-eval (expr vars
245 &optional
246 (ring *ring-of-integers*)
247 (order #'lex>)
248 (list-marker '[)
249 &aux
250 (ring-and-order (make-ring-and-order :ring ring :order order)))
251 (labels ((p-eval (arg) (poly-eval arg vars ring order))
252 (p-eval-list (args) (mapcar #'p-eval args))
253 (p-add (x y) (poly-add ring-and-order x y)))
254 (cond
255 ((eql expr 0) (make-poly-zero))
256 ((member expr vars :test #'equalp)
257 (let ((pos (position expr vars :test #'equalp)))
258 (make-variable ring (length vars) pos)))
259 ((atom expr)
260 (coerce-coeff ring expr vars))
261 ((eq (car expr) list-marker)
262 (cons list-marker (p-eval-list (cdr expr))))
263 (t
264 (case (car expr)
265 (+ (reduce #'p-add (p-eval-list (cdr expr))))
266 (- (case (length expr)
267 (1 (make-poly-zero))
268 (2 (poly-uminus ring (p-eval (cadr expr))))
269 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
270 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
271 (reduce #'p-add (p-eval-list (cddr expr)))))))
272 (*
273 (if (endp (cddr expr)) ;unary
274 (p-eval (cdr expr))
275 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
276 (expt
277 (cond
278 ((member (cadr expr) vars :test #'equalp)
279 ;;Special handling of (expt var pow)
280 (let ((pos (position (cadr expr) vars :test #'equalp)))
281 (make-variable ring (length vars) pos (caddr expr))))
282 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
283 ;; Negative power means division in coefficient ring
284 ;; Non-integer power means non-polynomial coefficient
285 (coerce-coeff ring expr vars))
286 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
287 (otherwise
288 (coerce-coeff ring expr vars)))))))
289
290(defun spoly (ring f g)
291 "It yields the S-polynomial of polynomials F and G."
292 (declare (type poly f g))
293 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
294 (mf (monom-div lcm (poly-lm f)))
295 (mg (monom-div lcm (poly-lm g))))
296 (declare (type monom mf mg))
297 (multiple-value-bind (c cf cg)
298 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
299 (declare (ignore c))
300 (poly-sub
301 ring
302 (scalar-times-poly ring cg (monom-times-poly mf f))
303 (scalar-times-poly ring cf (monom-times-poly mg g))))))
304
305
306(defun poly-primitive-part (ring p)
307 "Divide polynomial P with integer coefficients by gcd of its
308coefficients and return the result."
309 (declare (type poly p))
310 (if (poly-zerop p)
311 (values p 1)
312 (let ((c (poly-content ring p)))
313 (values (make-poly-from-termlist (mapcar
314 #'(lambda (x)
315 (make-term (term-monom x)
316 (funcall (ring-div ring) (term-coeff x) c)))
317 (poly-termlist p))
318 (poly-sugar p))
319 c))))
320
321(defun poly-content (ring p)
322 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
323to compute the greatest common divisor."
324 (declare (type poly p))
325 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
326
327(defun read-infix-form (&optional stream)
328 "Parser of infix expressions with integer/rational coefficients
329The parser will recognize two kinds of polynomial expressions:
330
331- polynomials in fully expanded forms with coefficients
332 written in front of symbolic expressions; constants can be optionally
333 enclosed in (); for example, the infix form
334 X^2-Y^2+(-4/3)*U^2*W^3-5
335 parses to
336 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
337
338- lists of polynomials; for example
339 [X-Y, X^2+3*Z]
340 parses to
341 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
342 where the first symbol [ marks a list of polynomials.
343
344-other infix expressions, for example
345 [(X-Y)*(X+Y)/Z,(X+1)^2]
346parses to:
347 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
348Currently this function is implemented using M. Kantrowitz's INFIX package."
349 (read-from-string
350 (concatenate 'string
351 "#I("
352 (with-output-to-string (s)
353 (loop
354 (multiple-value-bind (line eof)
355 (read-line stream t)
356 (format s "~A" line)
357 (when eof (return)))))
358 ")")))
359
360(defun poly-reader (vars &key (stream t))
361 "Reads an expression in prefix form from a stream STREAM.
362If the expression represents a polynomial or a list of polynomials in variables VARS then
363the polynomial or list of polynomials is returned."
364 (poly-eval (read-infix-form stream) vars))
Note: See TracBrowser for help on using the repository browser.