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

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

* empty log message *

File size: 15.6 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 (declare (type poly p))
192 (setf (poly-termlist p) (nreverse (poly-termlist p)))
193 p)
194
195(defun poly-reverse (p)
196 (declare (type poly p))
197 (make-poly-from-termlist (reverse (poly-termlist p))
198 (poly-sugar p)))
199
200
201(defun poly-contract (p &optional (k 1))
202 (declare (type poly p))
203 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
204 (poly-sugar p)))
205
206(defun poly-extend (p &optional (m (make-monom :dimension 1)))
207 (declare (type poly p))
208 (make-poly-from-termlist
209 (termlist-extend (poly-termlist p) m)
210 (+ (poly-sugar p) (monom-sugar m))))
211
212(defun poly-add-variables (p k)
213 (declare (type poly p))
214 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
215 p)
216
217(defun poly-list-add-variables (plist k)
218 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
219
220(defun poly-standard-extension (plist &aux (k (length plist)))
221 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
222 (declare (list plist) (fixnum k))
223 (labels ((incf-power (g i)
224 (dolist (x (poly-termlist g))
225 (incf (monom-elt (term-monom x) i)))
226 (incf (poly-sugar g))))
227 (setf plist (poly-list-add-variables plist k))
228 (dotimes (i k plist)
229 (incf-power (nth i plist) i))))
230
231(defun saturation-extension (ring f plist &aux (k (length plist)) (d (monom-dimension (poly-lm (car plist)))))
232 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
233 (setf f (poly-list-add-variables f k)
234 plist (mapcar #'(lambda (x)
235 (setf (poly-termlist x) (nconc (poly-termlist x)
236 (list (make-term (make-monom :dimension d)
237 (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
238 x)
239 (poly-standard-extension plist)))
240 (append f plist))
241
242
243(defun polysaturation-extension (ring f plist &aux (k (length plist))
244 (d (+ k (monom-dimension (poly-lm (car plist))))))
245 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
246 (setf f (poly-list-add-variables f k)
247 plist (apply #'poly-append (poly-standard-extension plist))
248 (cdr (last (poly-termlist plist))) (list (make-term (make-monom :dimension d)
249 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
250 (append f (list plist)))
251
252(defun saturation-extension-1 (ring f p) (polysaturation-extension ring f (list p)))
253
254;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
255;;
256;; Evaluation of polynomial (prefix) expressions
257;;
258;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
259
260(defun coerce-coeff (ring expr vars)
261 "Coerce an element of the coefficient ring to a constant polynomial."
262 ;; Modular arithmetic handler by rat
263 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
264 (funcall (ring-parse ring) expr)))
265 0))
266
267(defun poly-eval (expr vars
268 &optional
269 (ring *ring-of-integers*)
270 (order #'lex>)
271 (list-marker :[)
272 &aux
273 (ring-and-order (make-ring-and-order :ring ring :order order)))
274 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
275variables VARS. Return the resulting polynomial or list of
276polynomials. Standard arithmetical operators in form EXPR are
277replaced with their analogues in the ring of polynomials, and the
278resulting expression is evaluated, resulting in a polynomial or a list
279of polynomials in internal form. A similar operation in another computer
280algebra system could be called 'expand' or so."
281 (labels ((p-eval (arg) (poly-eval arg vars ring order))
282 (p-eval-scalar (arg) (poly-eval-scalar arg))
283 (p-eval-list (args) (mapcar #'p-eval args))
284 (p-add (x y) (poly-add ring-and-order x y)))
285 (cond
286 ((null expr) (error "Empty expression"))
287 ((eql expr 0) (make-poly-zero))
288 ((member expr vars :test #'equalp)
289 (let ((pos (position expr vars :test #'equalp)))
290 (make-variable ring (length vars) pos)))
291 ((atom expr)
292 (coerce-coeff ring expr vars))
293 ((eq (car expr) list-marker)
294 (cons list-marker (p-eval-list (cdr expr))))
295 (t
296 (case (car expr)
297 (+ (reduce #'p-add (p-eval-list (cdr expr))))
298 (- (case (length expr)
299 (1 (make-poly-zero))
300 (2 (poly-uminus ring (p-eval (cadr expr))))
301 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
302 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
303 (reduce #'p-add (p-eval-list (cddr expr)))))))
304 (*
305 (if (endp (cddr expr)) ;unary
306 (p-eval (cdr expr))
307 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
308 (/
309 ;; A polynomial can be divided by a scalar
310 (cond
311 ((endp (cddr expr))
312 ;; A special case (/ ?), the inverse
313 (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
314 (t
315 (let ((num (p-eval (cadr expr)))
316 (denom-inverse (apply (ring-div ring)
317 (cons (funcall (ring-unit ring))
318 (mapcar #'p-eval-scalar (cddr expr))))))
319 (scalar-times-poly ring denom-inverse num)))))
320 (expt
321 (cond
322 ((member (cadr expr) vars :test #'equalp)
323 ;;Special handling of (expt var pow)
324 (let ((pos (position (cadr expr) vars :test #'equalp)))
325 (make-variable ring (length vars) pos (caddr expr))))
326 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
327 ;; Negative power means division in coefficient ring
328 ;; Non-integer power means non-polynomial coefficient
329 (coerce-coeff ring expr vars))
330 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
331 (otherwise
332 (coerce-coeff ring expr vars)))))))
333
334(defun poly-eval-scalar (expr
335 &optional
336 (ring *ring-of-integers*)
337 &aux
338 (order #'lex>))
339 "Evaluate a scalar expression EXPR in ring RING."
340 (poly-lc (poly-eval expr nil ring order)))
341
342(defun spoly (ring-and-order f g
343 &aux
344 (ring (ro-ring ring-and-order)))
345 "It yields the S-polynomial of polynomials F and G."
346 (declare (type poly f g))
347 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
348 (mf (monom-div lcm (poly-lm f)))
349 (mg (monom-div lcm (poly-lm g))))
350 (declare (type monom mf mg))
351 (multiple-value-bind (c cf cg)
352 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
353 (declare (ignore c))
354 (poly-sub
355 ring-and-order
356 (scalar-times-poly ring cg (monom-times-poly mf f))
357 (scalar-times-poly ring cf (monom-times-poly mg g))))))
358
359
360(defun poly-primitive-part (ring p)
361 "Divide polynomial P with integer coefficients by gcd of its
362coefficients and return the result."
363 (declare (type poly p))
364 (if (poly-zerop p)
365 (values p 1)
366 (let ((c (poly-content ring p)))
367 (values (make-poly-from-termlist
368 (mapcar
369 #'(lambda (x)
370 (make-term (term-monom x)
371 (funcall (ring-div ring) (term-coeff x) c)))
372 (poly-termlist p))
373 (poly-sugar p))
374 c))))
375
376(defun poly-content (ring p)
377 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
378to compute the greatest common divisor."
379 (declare (type poly p))
380 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
381
382(defun read-infix-form (&key (stream t))
383 "Parser of infix expressions with integer/rational coefficients
384The parser will recognize two kinds of polynomial expressions:
385
386- polynomials in fully expanded forms with coefficients
387 written in front of symbolic expressions; constants can be optionally
388 enclosed in (); for example, the infix form
389 X^2-Y^2+(-4/3)*U^2*W^3-5
390 parses to
391 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
392
393- lists of polynomials; for example
394 [X-Y, X^2+3*Z]
395 parses to
396 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
397 where the first symbol [ marks a list of polynomials.
398
399-other infix expressions, for example
400 [(X-Y)*(X+Y)/Z,(X+1)^2]
401parses to:
402 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
403Currently this function is implemented using M. Kantrowitz's INFIX package."
404 (read-from-string
405 (concatenate 'string
406 "#I("
407 (with-output-to-string (s)
408 (loop
409 (multiple-value-bind (line eof)
410 (read-line stream t)
411 (format s "~A" line)
412 (when eof (return)))))
413 ")")))
414
415(defun read-poly (vars &key
416 (stream t)
417 (ring *ring-of-integers*)
418 (order #'lex>))
419 "Reads an expression in prefix form from a stream STREAM.
420The expression read from the strem should represent a polynomial or a
421list of polynomials in variables VARS, over the ring RING. The
422polynomial or list of polynomials is returned, with terms in each
423polynomial ordered according to monomial order ORDER."
424 (poly-eval (read-infix-form :stream stream) vars ring order))
425
426(defun string->poly (str vars
427 &optional
428 (ring *ring-of-integers*)
429 (order #'lex>))
430 "Converts a string STR to a polynomial in variables VARS."
431 (with-input-from-string (s str)
432 (read-poly vars :stream s :ring ring :order order)))
433
434(defun poly->alist (p)
435 "Convert a polynomial P to an association list. Thus, the format of the
436returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
437MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
438corresponding coefficient in the ring."
439 (cond
440 ((poly-p p)
441 (mapcar #'term->cons (poly-termlist p)))
442 ((and (consp p) (eq (car p) :[))
443 (cons :[ (mapcar #'poly->alist (cdr p))))))
444
445(defun string->alist (str vars
446 &optional
447 (ring *ring-of-integers*)
448 (order #'lex>))
449 "Convert a string STR representing a polynomial or polynomial list to
450an association list (... (MONOM . COEFF) ...)."
451 (poly->alist (string->poly str vars ring order)))
Note: See TracBrowser for help on using the repository browser.