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

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

* empty log message *

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