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

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