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

Last change on this file since 1426 was 1268, checked in by Marek Rychlik, 10 years ago

* empty log message *

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