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

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

* empty log message *

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