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/monom.lisp@ 2355

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

* empty log message *

File size: 10.2 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;; This package implements BASIC OPERATIONS ON MONOMIALS
24;;----------------------------------------------------------------
25;; DATA STRUCTURES: Conceptually, monomials can be represented as lists:
26;;
27;; monom: (n1 n2 ... nk) where ni are non-negative integers
28;;
29;; However, lists may be implemented as other sequence types,
30;; so the flexibility to change the representation should be
31;; maintained in the code to use general operations on sequences
32;; whenever possible. The optimization for the actual representation
33;; should be left to declarations and the compiler.
34;;----------------------------------------------------------------
35;; EXAMPLES: Suppose that variables are x and y. Then
36;;
37;; Monom x*y^2 ---> (1 2)
38;;
39;;----------------------------------------------------------------
40
41(defpackage "MONOM"
42 (:use :cl :ring)
43 (:export "MONOM"
44 "EXPONENT"
45 "MONOM-DIMENSION"
46 "MONOM-EXPONENTS"
47 "MAKE-MONOM-VARIABLE"))
48
49(in-package :monom)
50
51(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
52
53(deftype exponent ()
54 "Type of exponent in a monomial."
55 'fixnum)
56
57(defclass monom ()
58 ((dimension :initarg :dimension :accessor monom-dimension)
59 (exponents :initarg :exponents :accessor monom-exponents))
60 (:default-initargs :dimension nil :exponents nil :exponent nil))
61
62(defmethod print-object ((self monom) stream)
63 (format stream "#<MONOM DIMENSION=~A EXPONENTS=~A>"
64 (slot-value self 'dimension)
65 (slot-value self 'exponents)))
66
67#|
68;; Debug calls to initialize-instance
69(defmethod initialize-instance :around ((self monom)
70 &rest
71 args
72 &key
73 &allow-other-keys)
74 (format t "MONOM::INITIALIZE-INSTANCE called with:~&ARGS: ~W.~%" args)
75 (call-next-method)
76 )
77|#
78
79(defmethod shared-initialize :after ((self monom) slot-names
80 ;;&rest initargs
81 &key
82 dimension
83 exponents
84 exponent
85 &allow-other-keys
86 )
87 ;;(format t "MONOM::SHARED-INITIALIZE called with:~&SLOT-NAMES: ~W~&INITARGS: ~W.~%" slot-names initargs)
88 (if (eq slot-names t) (setf slot-names '(dimension exponents)))
89 (dolist (slot-name slot-names)
90 (ecase slot-name
91 (dimension
92 (cond (dimension
93 (setf (slot-value self 'dimension) dimension))
94 (exponents
95 (setf (slot-value self 'dimension) (length exponents)))
96 (t
97 (error "DIMENSION or EXPONENTS must not be NIL"))))
98 (exponents
99 (cond
100 ;; when exponents are supplied
101 (exponents
102 (setf (slot-value self 'exponents)
103 (make-array (slot-value self 'dimension) :initial-contents exponents)))
104 ;; when all exponents are to be identical
105 (exponent
106 (setf (slot-value self 'exponents)
107 (make-array (list new-dimension) :initial-element exponent
108 :element-type 'exponent)))
109 ;; otherwise, all exponents are zero
110 (t
111 (setf (slot-value self 'exponents)
112 (make-array (list new-dimension) :element-type 'exponent :initial-element 0))))))))
113
114
115
116
117;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
118;;
119;; Operations on monomials
120;;
121;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122
123(defmethod r-dimension ((m monom))
124 (monom-dimension m))
125
126(defmethod r-elt ((m monom) index)
127 "Return the power in the monomial M of variable number INDEX."
128 (with-slots (exponents)
129 m
130 (elt exponents index)))
131
132(defmethod (setf r-elt) (new-value (m monom) index)
133 "Return the power in the monomial M of variable number INDEX."
134 (with-slots (exponents)
135 m
136 (setf (elt exponents index) new-value)))
137
138(defmethod r-total-degree ((m monom) &optional (start 0) (end (r-dimension m)))
139 "Return the todal degree of a monomoal M. Optinally, a range
140of variables may be specified with arguments START and END."
141 (declare (type fixnum start end))
142 (with-slots (exponents)
143 m
144 (reduce #'+ exponents :start start :end end)))
145
146
147(defmethod r-sugar ((m monom) &aux (start 0) (end (r-dimension m)))
148 "Return the sugar of a monomial M. Optinally, a range
149of variables may be specified with arguments START and END."
150 (declare (type fixnum start end))
151 (r-total-degree m start end))
152
153(defmethod r* ((m1 monom) (m2 monom))
154 "Multiply monomial M1 by monomial M2."
155 (format t "MONOM::R* called with:~& M1: ~A~& M2: ~A~%" m1 m2)
156 (with-slots ((exponents1 exponents) dimension)
157 m1
158 (with-slots ((exponents2 exponents))
159 m2
160 (let* ((exponents (copy-seq exponents1)))
161 (map-into exponents #'+ exponents1 exponents2)
162 (make-instance 'monom :dimension dimension :exponents exponents)))))
163
164
165
166(defmethod r/ ((m1 monom) (m2 monom))
167 "Divide monomial M1 by monomial M2."
168 (with-slots ((exponents1 exponents) (dimension1 dimension))
169 m1
170 (with-slots ((exponents2 exponents))
171 m2
172 (let* ((exponents (copy-seq exponents1))
173 (dimension dimension1))
174 (map-into exponents #'- exponents1 exponents2)
175 (make-instance 'monom :dimension dimension :exponents exponents)))))
176
177(defmethod r-divides-p ((m1 monom) (m2 monom))
178 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
179 (with-slots ((exponents1 exponents))
180 m1
181 (with-slots ((exponents2 exponents))
182 m2
183 (every #'<= exponents1 exponents2))))
184
185
186(defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
187 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
188 (every #'(lambda (x y z) (<= x (max y z)))
189 m1 m2 m3))
190
191
192(defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
193 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
194 (declare (type monom m1 m2 m3 m4))
195 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
196 m1 m2 m3 m4))
197
198(defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
199 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
200 (with-slots ((exponents1 exponents))
201 m1
202 (with-slots ((exponents2 exponents))
203 m2
204 (with-slots ((exponents3 exponents))
205 m3
206 (with-slots ((exponents4 exponents))
207 m4
208 (every
209 #'(lambda (x y z w) (= (max x y) (max z w)))
210 exponents1 exponents2 exponents3 exponents4))))))
211
212(defmethod r-divisible-by-p ((m1 monom) (m2 monom))
213 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
214 (with-slots ((exponents1 exponents))
215 m1
216 (with-slots ((exponents2 exponents))
217 m2
218 (every #'>= exponents1 exponents2))))
219
220(defmethod r-rel-prime-p ((m1 monom) (m2 monom))
221 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
222 (with-slots ((exponents1 exponents))
223 m1
224 (with-slots ((exponents2 exponents))
225 m2
226 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
227
228
229(defmethod r-equalp ((m1 monom) (m2 monom))
230 "Returns T if two monomials M1 and M2 are equal."
231 (with-slots ((exponents1 exponents))
232 m1
233 (with-slots ((exponents2 exponents))
234 m2
235 (every #'= exponents1 exponents2))))
236
237(defmethod r-lcm ((m1 monom) (m2 monom))
238 "Returns least common multiple of monomials M1 and M2."
239 (with-slots ((exponents1 exponents) (dimension1 dimension))
240 m1
241 (with-slots ((exponents2 exponents))
242 m2
243 (let* ((exponents (copy-seq exponents1))
244 (dimension dimension1))
245 (map-into exponents #'max exponents1 exponents2)
246 (make-instance 'monom :dimension dimension :exponents exponents)))))
247
248
249(defmethod r-gcd ((m1 monom) (m2 monom))
250 "Returns greatest common divisor of monomials M1 and M2."
251 (with-slots ((exponents1 exponents) (dimension1 dimension))
252 m1
253 (with-slots ((exponents2 exponents))
254 m2
255 (let* ((exponents (copy-seq exponents1))
256 (dimension dimension1))
257 (map-into exponents #'min exponents1 exponents2)
258 (make-instance 'monom :dimension dimension :exponents exponents)))))
259
260(defmethod r-depends-p ((m monom) k)
261 "Return T if the monomial M depends on variable number K."
262 (declare (type fixnum k))
263 (with-slots (exponents)
264 m
265 (plusp (elt exponents k))))
266
267(defmethod r-tensor-product ((m1 monom) (m2 monom))
268 (with-slots ((exponents1 exponents) (dimension1 dimension))
269 m1
270 (with-slots ((exponents2 exponents) (dimension2 dimension))
271 m2
272 (make-instance 'monom
273 :dimension (+ dimension1 dimension2)
274 :exponents (concatenate 'vector exponents1 exponents2)))))
275
276(defmethod r-contract ((m monom) k)
277 "Drop the first K variables in monomial M."
278 (declare (fixnum k))
279 (with-slots (dimension exponents)
280 m
281 (setf dimension (- dimension k)
282 exponents (subseq exponents k))))
283
284(defun make-monom-variable (nvars pos &optional (power 1)
285 &aux (m (make-instance 'monom :dimension nvars)))
286 "Construct a monomial in the polynomial ring
287RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
288which represents a single variable. It assumes number of variables
289NVARS and the variable is at position POS. Optionally, the variable
290may appear raised to power POWER. "
291 (declare (type fixnum nvars pos power) (type monom m))
292 (with-slots (exponents)
293 m
294 (setf (elt exponents pos) power)
295 m))
296
297(defmethod r->list ((m monom))
298 "A human-readable representation of a monomial M as a list of exponents."
299 (coerce (monom-exponents m) 'list))
Note: See TracBrowser for help on using the repository browser.