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

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

* empty log message *

File size: 13.2 KB
RevLine 
[77]1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
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
[431]23(defpackage "POLYNOMIAL"
[1072]24 (:use :cl :ring :ring-and-order :monomial :order :term :termlist :infix)
[432]25 (:export "POLY"
26 "POLY-TERMLIST"
27 "POLY-SUGAR"
28 "POLY-LT"
[433]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"
[458]39 "POLY-LENGTH"
[433]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 "SPOLY"
62 "POLY-PRIMITIVE-PART"
63 "POLY-CONTENT"
[1085]64 "READ-INFIX-FORM"
[1093]65 "READ-POLY"
[1104]66 "STRING->POLY"
[432]67 ))
[143]68
[431]69(in-package :polynomial)
70
[52]71;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72;;
73;; Polynomials
74;;
75;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76
77(defstruct (poly
78 ;;
79 ;; BOA constructor, by default constructs zero polynomial
80 (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
81 (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
82 ;; Constructor of polynomials representing a variable
83 (:constructor make-variable (ring nvars pos &optional (power 1)
[53]84 &aux
85 (termlist (list
86 (make-term-variable ring nvars pos power)))
87 (sugar power)))
88 (:constructor poly-unit (ring dimension
89 &aux
90 (termlist (termlist-unit ring dimension))
91 (sugar 0))))
[52]92 (termlist nil :type list)
93 (sugar -1 :type fixnum))
94
95;; Leading term
96(defmacro poly-lt (p) `(car (poly-termlist ,p)))
97
98;; Second term
99(defmacro poly-second-lt (p) `(cadar (poly-termlist ,p)))
100
101;; Leading monomial
102(defun poly-lm (p) (term-monom (poly-lt p)))
103
104;; Second monomial
105(defun poly-second-lm (p) (term-monom (poly-second-lt p)))
106
107;; Leading coefficient
108(defun poly-lc (p) (term-coeff (poly-lt p)))
109
110;; Second coefficient
111(defun poly-second-lc (p) (term-coeff (poly-second-lt p)))
112
113;; Testing for a zero polynomial
114(defun poly-zerop (p) (null (poly-termlist p)))
115
116;; The number of terms
117(defun poly-length (p) (length (poly-termlist p)))
118
119(defun scalar-times-poly (ring c p)
[981]120 (declare (type ring ring) (poly p))
[52]121 (make-poly-from-termlist (scalar-times-termlist ring c (poly-termlist p)) (poly-sugar p)))
122
123;; The scalar product omitting the head term
124(defun scalar-times-poly-1 (ring c p)
[981]125 (declare (type ring ring) (poly p))
[52]126 (make-poly-from-termlist (scalar-times-termlist ring c (cdr (poly-termlist p))) (poly-sugar p)))
[53]127
[52]128(defun monom-times-poly (m p)
[993]129 (declare (poly p))
[980]130 (make-poly-from-termlist
131 (monom-times-termlist m (poly-termlist p))
132 (+ (poly-sugar p) (monom-sugar m))))
[52]133
134(defun term-times-poly (ring term p)
[982]135 (declare (type ring ring) (type term term) (type poly p))
[979]136 (make-poly-from-termlist
137 (term-times-termlist ring term (poly-termlist p))
138 (+ (poly-sugar p) (term-sugar term))))
[52]139
[978]140(defun poly-add (ring-and-order p q)
[980]141 (declare (type ring-and-order ring-and-order) (type poly p q))
[978]142 (make-poly-from-termlist
143 (termlist-add ring-and-order
144 (poly-termlist p)
145 (poly-termlist q))
146 (max (poly-sugar p) (poly-sugar q))))
[52]147
[980]148(defun poly-sub (ring-and-order p q)
149 (declare (type ring-and-order ring-and-order) (type poly p q))
150 (make-poly-from-termlist
[990]151 (termlist-sub ring-and-order (poly-termlist p) (poly-termlist q))
[980]152 (max (poly-sugar p) (poly-sugar q))))
[52]153
154(defun poly-uminus (ring p)
[983]155 (declare (type ring ring) (type poly p))
156 (make-poly-from-termlist
157 (termlist-uminus ring (poly-termlist p))
158 (poly-sugar p)))
[52]159
[984]160(defun poly-mul (ring-and-order p q)
161 (declare (type ring-and-order ring-and-order) (type poly p q))
162 (make-poly-from-termlist
[991]163 (termlist-mul ring-and-order (poly-termlist p) (poly-termlist q))
[984]164 (+ (poly-sugar p) (poly-sugar q))))
[52]165
[985]166(defun poly-expt (ring-and-order p n)
167 (declare (type ring-and-order ring-and-order) (type poly p))
[992]168 (make-poly-from-termlist (termlist-expt ring-and-order (poly-termlist p) n) (* n (poly-sugar p))))
[52]169
170(defun poly-append (&rest plist)
171 (make-poly-from-termlist (apply #'append (mapcar #'poly-termlist plist))
[53]172 (apply #'max (mapcar #'poly-sugar plist))))
[52]173
174(defun poly-nreverse (p)
[986]175 (declare (type poly p))
[52]176 (setf (poly-termlist p) (nreverse (poly-termlist p)))
177 p)
178
179(defun poly-contract (p &optional (k 1))
[986]180 (declare (type poly p))
[52]181 (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
[53]182 (poly-sugar p)))
[52]183
[973]184(defun poly-extend (p &optional (m (make-monom :dimension 1)))
[987]185 (declare (type poly p))
[52]186 (make-poly-from-termlist
187 (termlist-extend (poly-termlist p) m)
188 (+ (poly-sugar p) (monom-sugar m))))
189
190(defun poly-add-variables (p k)
[988]191 (declare (type poly p))
[52]192 (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
193 p)
194
195(defun poly-list-add-variables (plist k)
196 (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
197
198(defun poly-standard-extension (plist &aux (k (length plist)))
199 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
200 (declare (list plist) (fixnum k))
201 (labels ((incf-power (g i)
202 (dolist (x (poly-termlist g))
203 (incf (monom-elt (term-monom x) i)))
204 (incf (poly-sugar g))))
205 (setf plist (poly-list-add-variables plist k))
206 (dotimes (i k plist)
207 (incf-power (nth i plist) i))))
208
209(defun saturation-extension (ring f plist &aux (k (length plist)) (d (monom-dimension (poly-lm (car plist)))))
210 "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
211 (setf f (poly-list-add-variables f k)
212 plist (mapcar #'(lambda (x)
213 (setf (poly-termlist x) (nconc (poly-termlist x)
[974]214 (list (make-term (make-monom :dimension d)
[52]215 (funcall (ring-uminus ring) (funcall (ring-unit ring)))))))
216 x)
217 (poly-standard-extension plist)))
218 (append f plist))
219
220
221(defun polysaturation-extension (ring f plist &aux (k (length plist))
[820]222 (d (+ k (monom-dimension (poly-lm (car plist))))))
[52]223 "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
224 (setf f (poly-list-add-variables f k)
225 plist (apply #'poly-append (poly-standard-extension plist))
[974]226 (cdr (last (poly-termlist plist))) (list (make-term (make-monom :dimension d)
[52]227 (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
228 (append f (list plist)))
229
230(defun saturation-extension-1 (ring f p) (polysaturation-extension ring f (list p)))
[53]231
232;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233;;
234;; Evaluation of polynomial (prefix) expressions
235;;
236;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237
238(defun coerce-coeff (ring expr vars)
239 "Coerce an element of the coefficient ring to a constant polynomial."
240 ;; Modular arithmetic handler by rat
[975]241 (make-poly-from-termlist (list (make-term (make-monom :dimension (length vars))
[53]242 (funcall (ring-parse ring) expr)))
243 0))
244
[1046]245(defun poly-eval (expr vars
246 &optional
247 (ring *ring-of-integers*)
[1048]248 (order #'lex>)
[1046]249 (list-marker '[)
[1047]250 &aux
251 (ring-and-order (make-ring-and-order :ring ring :order order)))
[1050]252 (labels ((p-eval (arg) (poly-eval arg vars ring order))
[1112]253 (p-eval-scalar (arg) (poly-eval arg nil ring order))
[53]254 (p-eval-list (args) (mapcar #'p-eval args))
[989]255 (p-add (x y) (poly-add ring-and-order x y)))
[53]256 (cond
257 ((eql expr 0) (make-poly-zero))
258 ((member expr vars :test #'equalp)
259 (let ((pos (position expr vars :test #'equalp)))
260 (make-variable ring (length vars) pos)))
261 ((atom expr)
262 (coerce-coeff ring expr vars))
263 ((eq (car expr) list-marker)
264 (cons list-marker (p-eval-list (cdr expr))))
265 (t
266 (case (car expr)
267 (+ (reduce #'p-add (p-eval-list (cdr expr))))
268 (- (case (length expr)
269 (1 (make-poly-zero))
270 (2 (poly-uminus ring (p-eval (cadr expr))))
[989]271 (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
272 (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
[53]273 (reduce #'p-add (p-eval-list (cddr expr)))))))
274 (*
275 (if (endp (cddr expr)) ;unary
276 (p-eval (cdr expr))
[989]277 (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
[1106]278 (/
[1107]279 (print expr)
[1106]280 ;; A polynomial can be divided by a scalar
[1107]281 (cond ((endp (cddr expr))
[1108]282 ;; A special case (/ x)
[1107]283 (apply (ring-div ring) (cdr expr)))
[1113]284 ((endp (cdddr expr))
285 (let ((num (p-eval (cadr expr)))
286 (denom-inverse (p-eval-scalar (cons '/ (last expr)))))
287 (scalar-times-poly ring denom-inverse denom)))
[1110]288 (t
[1111]289 (let ((num (p-eval (cons '* (cdr (butlast expr)))))
[1113]290 (denom (p-eval-scalar (cons '/ (last expr)))))
291 (cons num denom)))))
[53]292 (expt
293 (cond
294 ((member (cadr expr) vars :test #'equalp)
295 ;;Special handling of (expt var pow)
296 (let ((pos (position (cadr expr) vars :test #'equalp)))
297 (make-variable ring (length vars) pos (caddr expr))))
298 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
299 ;; Negative power means division in coefficient ring
300 ;; Non-integer power means non-polynomial coefficient
301 (coerce-coeff ring expr vars))
[989]302 (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
[53]303 (otherwise
304 (coerce-coeff ring expr vars)))))))
305
[55]306(defun spoly (ring f g)
307 "It yields the S-polynomial of polynomials F and G."
308 (declare (type poly f g))
309 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
310 (mf (monom-div lcm (poly-lm f)))
311 (mg (monom-div lcm (poly-lm g))))
312 (declare (type monom mf mg))
313 (multiple-value-bind (c cf cg)
314 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
315 (declare (ignore c))
316 (poly-sub
317 ring
318 (scalar-times-poly ring cg (monom-times-poly mf f))
319 (scalar-times-poly ring cf (monom-times-poly mg g))))))
[53]320
321
[55]322(defun poly-primitive-part (ring p)
323 "Divide polynomial P with integer coefficients by gcd of its
324coefficients and return the result."
325 (declare (type poly p))
326 (if (poly-zerop p)
327 (values p 1)
328 (let ((c (poly-content ring p)))
329 (values (make-poly-from-termlist (mapcar
330 #'(lambda (x)
331 (make-term (term-monom x)
332 (funcall (ring-div ring) (term-coeff x) c)))
333 (poly-termlist p))
334 (poly-sugar p))
335 c))))
336
337(defun poly-content (ring p)
338 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
339to compute the greatest common divisor."
340 (declare (type poly p))
341 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
[1066]342
[1091]343(defun read-infix-form (&key (stream t))
[1066]344 "Parser of infix expressions with integer/rational coefficients
345The parser will recognize two kinds of polynomial expressions:
346
347- polynomials in fully expanded forms with coefficients
348 written in front of symbolic expressions; constants can be optionally
349 enclosed in (); for example, the infix form
350 X^2-Y^2+(-4/3)*U^2*W^3-5
351 parses to
352 (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
353
354- lists of polynomials; for example
355 [X-Y, X^2+3*Z]
356 parses to
357 (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
358 where the first symbol [ marks a list of polynomials.
359
360-other infix expressions, for example
361 [(X-Y)*(X+Y)/Z,(X+1)^2]
362parses to:
363 (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
364Currently this function is implemented using M. Kantrowitz's INFIX package."
365 (read-from-string
366 (concatenate 'string
367 "#I("
368 (with-output-to-string (s)
369 (loop
370 (multiple-value-bind (line eof)
371 (read-line stream t)
372 (format s "~A" line)
373 (when eof (return)))))
374 ")")))
375
[1092]376(defun read-poly (vars &key (stream t))
[1067]377 "Reads an expression in prefix form from a stream STREAM.
378If the expression represents a polynomial or a list of polynomials in variables VARS then
379the polynomial or list of polynomials is returned."
[1090]380 (poly-eval (read-infix-form :stream stream) vars))
[1092]381
[1102]382(defun string->poly (str vars)
[1105]383 "Converts a string STR to a polynomial in variables VARS"
[1097]384 (with-input-from-string (s str)
[1095]385 (read-poly vars :stream s)))
386
387
Note: See TracBrowser for help on using the repository browser.