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

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

* empty log message *

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