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/termlist.lisp@ 653

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

* empty log message *

File size: 7.1 KB
Line 
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(defpackage "TERMLIST"
23 (:use :cl :ring :monomial :order :term)
24 (:export "TERMLIST-SUGAR"
25 "TERMLIST-CONTRACT"
26 "TERMLIST-EXTEND"
27 "TERMLIST-ADD-VARIABLES"
28 "TERMLIST-LT"
29 "TERMLIST-LM"
30 "TERMLIST-LC"
31 "SCALAR-MUL"
32 "SCALAR-TIMES-TERMLIST"
33 "TERM-MUL-LST"
34 "TERMLIST-TIMES-TERM"
35 "TERM-TIMES-TERMLIST"
36 "MONOM-TIMES-TERM"
37 "MONOM-TIMES-TERMLIST"
38 "TERMLIST-UMINUS"
39 "TERMLIST-ADD"
40 "TERMLIST-SUB"
41 "TERMLIST-MUL"
42 "TERMLIST-UNIT"
43 "TERMLIST-EXPT"))
44
45(in-package :termlist)
46
47(defstruct (ring-and-order :include ring)
48 (order #'lex> :type function))
49
50(defun termlist-sugar (p &aux (sugar -1))
51 (declare (fixnum sugar))
52 (dolist (term p sugar)
53 (setf sugar (max sugar (term-sugar term)))))
54
55(defun termlist-contract (p &optional (k 1))
56 "Eliminate first K variables from a polynomial P."
57 (mapcar #'(lambda (term) (make-term (monom-contract k (term-monom term))
58 (term-coeff term)))
59 p))
60
61(defun termlist-extend (p &optional (m (make-monom 1 :initial-element 0)))
62 "Extend every monomial in a polynomial P by inserting at the
63beginning of every monomial the list of powers M."
64 (mapcar #'(lambda (term) (make-term (monom-append m (term-monom term))
65 (term-coeff term)))
66 p))
67
68(defun termlist-add-variables (p n)
69 "Add N variables to a polynomial P by inserting zero powers
70at the beginning of each monomial."
71 (declare (fixnum n))
72 (mapcar #'(lambda (term)
73 (make-term (monom-append (make-monom n :initial-element 0)
74 (term-monom term))
75 (term-coeff term)))
76 p))
77
78
79;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80;;
81;; Low-level polynomial arithmetic done on
82;; lists of terms
83;;
84;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
85
86(defmacro termlist-lt (p) `(car ,p))
87(defun termlist-lm (p) (term-monom (termlist-lt p)))
88(defun termlist-lc (p) (term-coeff (termlist-lt p)))
89
90(define-modify-macro scalar-mul (c) coeff-mul)
91
92(defun scalar-times-termlist (ring c p)
93 "Multiply scalar C by a polynomial P. This function works
94even if there are divisors of 0."
95 (mapcan
96 #'(lambda (term)
97 (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
98 (unless (funcall (ring-zerop ring) c1)
99 (list (make-term (term-monom term) c1)))))
100 p))
101
102
103(defun term-mul-lst (ring term1 term2)
104 "A special version of term multiplication. Returns (LIST TERM) where
105TERM is the product of the terms TERM1 TERM2, or NIL when the product
106is 0. This definition takes care of divisors of 0 in the coefficient
107ring."
108 (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
109 (unless (funcall (ring-zerop ring) c)
110 (list (make-term (monom-mul (term-monom term1) (term-monom term2)) c)))))
111
112(defun term-times-termlist (ring term f)
113 (declare (type ring ring))
114 (mapcan #'(lambda (term-f) (term-mul-lst ring term term-f)) f))
115
116(defun termlist-times-term (ring f term)
117 (mapcan #'(lambda (term-f) (term-mul-lst ring term-f term)) f))
118
119(defun monom-times-term (m term)
120 (make-term (monom-mul m (term-monom term)) (term-coeff term)))
121
122(defun monom-times-termlist (m f)
123 (cond
124 ((null f) nil)
125 (t
126 (mapcar #'(lambda (x) (monom-times-term m x)) f))))
127
128(defun termlist-uminus (ring f)
129 (mapcar #'(lambda (x)
130 (make-term (term-monom x) (funcall (ring-uminus ring) (term-coeff x))))
131 f))
132
133(defun termlist-add (ring p q)
134 (declare (type list p q))
135 (do (r)
136 ((cond
137 ((endp p)
138 (setf r (revappend r q)) t)
139 ((endp q)
140 (setf r (revappend r p)) t)
141 (t
142 (multiple-value-bind
143 (lm-greater lm-equal)
144 (monomial-order (termlist-lm p) (termlist-lm q))
145 (cond
146 (lm-equal
147 (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
148 (unless (funcall (ring-zerop ring) s) ;check for cancellation
149 (setf r (cons (make-term (termlist-lm p) s) r)))
150 (setf p (cdr p) q (cdr q))))
151 (lm-greater
152 (setf r (cons (car p) r)
153 p (cdr p)))
154 (t (setf r (cons (car q) r)
155 q (cdr q)))))
156 nil))
157 r)))
158
159(defun termlist-sub (ring p q)
160 (declare (type list p q))
161 (do (r)
162 ((cond
163 ((endp p)
164 (setf r (revappend r (termlist-uminus ring q)))
165 t)
166 ((endp q)
167 (setf r (revappend r p))
168 t)
169 (t
170 (multiple-value-bind
171 (mgreater mequal)
172 (monomial-order (termlist-lm p) (termlist-lm q))
173 (cond
174 (mequal
175 (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
176 (unless (funcall (ring-zerop ring) s) ;check for cancellation
177 (setf r (cons (make-term (termlist-lm p) s) r)))
178 (setf p (cdr p) q (cdr q))))
179 (mgreater
180 (setf r (cons (car p) r)
181 p (cdr p)))
182 (t (setf r (cons (make-term (termlist-lm q) (funcall (ring-uminus ring) (termlist-lc q))) r)
183 q (cdr q)))))
184 nil))
185 r)))
186
187;; Multiplication of polynomials
188;; Non-destructive version
189(defun termlist-mul (ring p q)
190 (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
191 ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
192 ((endp (cdr p))
193 (term-times-termlist ring (car p) q))
194 ((endp (cdr q))
195 (termlist-times-term ring p (car q)))
196 (t
197 (let ((head (term-mul-lst ring (termlist-lt p) (termlist-lt q)))
198 (tail (termlist-add ring (term-times-termlist ring (car p) (cdr q))
199 (termlist-mul ring (cdr p) q))))
200 (cond ((null head) tail)
201 ((null tail) head)
202 (t (nconc head tail)))))))
203
204(defun termlist-unit (ring dimension)
205 (declare (fixnum dimension))
206 (list (make-term (make-monom dimension :initial-element 0)
207 (funcall (ring-unit ring)))))
208
209(defun termlist-expt (ring poly n &aux (dim (monom-dimension (termlist-lm poly))))
210 (declare (type fixnum n dim))
211 (cond
212 ((minusp n) (error "termlist-expt: Negative exponent."))
213 ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
214 (t
215 (do ((k 1 (ash k 1))
216 (q poly (termlist-mul ring q q)) ;keep squaring
217 (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring p q) p)))
218 ((> k n) p)
219 (declare (fixnum k))))))
Note: See TracBrowser for help on using the repository browser.