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

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