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

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

* empty log message *

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