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

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

* empty log message *

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