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

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

* empty log message *

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