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/pol.lisp@ 1977

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

* empty log message *

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