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

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

* empty log message *

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