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 "MONOMIAL"
|
---|
42 | (:use :cl)
|
---|
43 | (:export "MONOM"
|
---|
44 | "EXPONENT"
|
---|
45 | "MAKE-MONOM"
|
---|
46 | "MAKE-MONOM-VARIABLE"
|
---|
47 | "MONOM-ELT"
|
---|
48 | "MONOM-DIMENSION"
|
---|
49 | "MONOM-TOTAL-DEGREE"
|
---|
50 | "MONOM-SUGAR"
|
---|
51 | "MONOM-DIV"
|
---|
52 | "MONOM-MUL"
|
---|
53 | "MONOM-DIVIDES-P"
|
---|
54 | "MONOM-DIVIDES-MONOM-LCM-P"
|
---|
55 | "MONOM-LCM-DIVIDES-MONOM-LCM-P"
|
---|
56 | "MONOM-LCM-EQUAL-MONOM-LCM-P"
|
---|
57 | "MONOM-DIVISIBLE-BY-P"
|
---|
58 | "MONOM-REL-PRIME-P"
|
---|
59 | "MONOM-EQUAL-P"
|
---|
60 | "MONOM-LCM"
|
---|
61 | "MONOM-GCD"
|
---|
62 | "MONOM-DEPENDS-P"
|
---|
63 | "MONOM-MAP"
|
---|
64 | "MONOM-APPEND"
|
---|
65 | "MONOM-CONTRACT"
|
---|
66 | "MONOM->LIST"))
|
---|
67 |
|
---|
68 | (in-package :monomial)
|
---|
69 |
|
---|
70 | (deftype exponent ()
|
---|
71 | "Type of exponent in a monomial."
|
---|
72 | 'fixnum)
|
---|
73 |
|
---|
74 | (deftype monom (&optional dim)
|
---|
75 | "Type of monomial."
|
---|
76 | `(simple-array exponent (,dim)))
|
---|
77 |
|
---|
78 | ;; If a monomial is redefined as structure with slot EXPONENTS, the function
|
---|
79 | ;; below can be the BOA constructor.
|
---|
80 | (defun make-monom (&key
|
---|
81 | (dimension nil dimension-suppied-p)
|
---|
82 | (initial-exponents nil initial-exponents-supplied-p)
|
---|
83 | (initial-exponent nil initial-exponent-supplied-p)
|
---|
84 | &aux
|
---|
85 | (dim (cond (dimension-suppied-p dimension)
|
---|
86 | (initial-exponents-supplied-p (length initial-exponents))
|
---|
87 | (t (error "You must provide DIMENSION nor INITIAL-EXPONENTS"))))
|
---|
88 | (monom (cond
|
---|
89 | ;; when exponents are supplied
|
---|
90 | (initial-exponents-supplied-p
|
---|
91 | (make-array (list dim) :initial-contents initial-exponents
|
---|
92 | :element-type 'exponent))
|
---|
93 | ;; when all exponents are to be identical
|
---|
94 | (initial-exponent-supplied-p
|
---|
95 | (make-array (list dim) :initial-element initial-exponent
|
---|
96 | :element-type 'exponent))
|
---|
97 | ;; otherwise, all exponents are zero
|
---|
98 | (t
|
---|
99 | (make-array (list dim) :element-type 'exponent :initial-element 0)))))
|
---|
100 | "A constructor (factory) of monomials. If DIMENSION is given, a sequence of
|
---|
101 | DIMENSION elements of type EXPONENT is constructed, where individual
|
---|
102 | elements are the value of INITIAL-EXPONENT, which defaults to 0.
|
---|
103 | Alternatively, all elements may be specified as a list
|
---|
104 | INITIAL-EXPONENTS."
|
---|
105 | monom)
|
---|
106 |
|
---|
107 |
|
---|
108 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
109 | ;;
|
---|
110 | ;; Operations on monomials
|
---|
111 | ;;
|
---|
112 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
113 |
|
---|
114 | (defun monom-dimension (m)
|
---|
115 | (length m))
|
---|
116 |
|
---|
117 | (defmacro monom-elt (m index)
|
---|
118 | "Return the power in the monomial M of variable number INDEX."
|
---|
119 | `(elt ,m ,index))
|
---|
120 |
|
---|
121 | (defun monom-total-degree (m &optional (start 0) (end (monom-dimension m)))
|
---|
122 | "Return the todal degree of a monomoal M. Optinally, a range
|
---|
123 | of variables may be specified with arguments START and END."
|
---|
124 | (reduce #'+ m :start start :end end))
|
---|
125 |
|
---|
126 | (defun monom-sugar (m &aux (start 0) (end (monom-dimension m)))
|
---|
127 | "Return the sugar of a monomial M. Optinally, a range
|
---|
128 | of variables may be specified with arguments START and END."
|
---|
129 | (monom-total-degree m start end))
|
---|
130 |
|
---|
131 | (defun monom-div (m1 m2 &aux (result (copy-seq m1)))
|
---|
132 | "Divide monomial M1 by monomial M2."
|
---|
133 | (map-into result #'- m1 m2))
|
---|
134 |
|
---|
135 | (defun monom-mul (m1 m2 &aux (result (copy-seq m1)))
|
---|
136 | "Multiply monomial M1 by monomial M2."
|
---|
137 | (map-into result #'+ m1 m2))
|
---|
138 |
|
---|
139 | (defun monom-divides-p (m1 m2)
|
---|
140 | "Returns T if monomial M1 divides monomial M2, NIL otherwise."
|
---|
141 | (every #'<= m1 m2))
|
---|
142 |
|
---|
143 | (defun monom-divides-monom-lcm-p (m1 m2 m3)
|
---|
144 | "Returns T if monomial M1 divides MONOM-LCM(M2,M3), NIL otherwise."
|
---|
145 | (every #'(lambda (x y z) (<= x (max y z)))
|
---|
146 | m1 m2 m3))
|
---|
147 |
|
---|
148 | (defun monom-lcm-divides-monom-lcm-p (m1 m2 m3 m4)
|
---|
149 | "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
|
---|
150 | (every #'(lambda (x y z w) (<= (max x y) (max z w)))
|
---|
151 | m1 m2 m3 m4))
|
---|
152 |
|
---|
153 |
|
---|
154 | (defun monom-lcm-equal-monom-lcm-p (m1 m2 m3 m4)
|
---|
155 | "Returns T if monomial MONOM-LCM(M1,M2) equals MONOM-LCM(M3,M4), NIL otherwise."
|
---|
156 | (every #'(lambda (x y z w) (= (max x y) (max z w)))
|
---|
157 | m1 m2 m3 m4))
|
---|
158 |
|
---|
159 |
|
---|
160 | (defun monom-divisible-by-p (m1 m2)
|
---|
161 | "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
|
---|
162 | (every #'>= m1 m2))
|
---|
163 |
|
---|
164 | (defun monom-rel-prime-p (m1 m2)
|
---|
165 | "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
|
---|
166 | (every #'(lambda (x y) (zerop (min x y))) m1 m2))
|
---|
167 |
|
---|
168 | (defun monom-equal-p (m1 m2)
|
---|
169 | "Returns T if two monomials M1 and M2 are equal."
|
---|
170 | (every #'= m1 m2))
|
---|
171 |
|
---|
172 | (defun monom-lcm (m1 m2 &aux (result (copy-seq m1)))
|
---|
173 | "Returns least common multiple of monomials M1 and M2."
|
---|
174 | (map-into result #'max m1 m2))
|
---|
175 |
|
---|
176 | (defun monom-gcd (m1 m2 &aux (result (copy-seq m1)))
|
---|
177 | "Returns greatest common divisor of monomials M1 and M2."
|
---|
178 | (map-into result #'min m1 m2))
|
---|
179 |
|
---|
180 | (defun monom-depends-p (m k)
|
---|
181 | "Return T if the monomial M depends on variable number K."
|
---|
182 | (plusp (monom-elt m k)))
|
---|
183 |
|
---|
184 | (defmacro monom-map (fun m &rest ml &aux (result `(copy-seq ,m)))
|
---|
185 | `(map-into ,result ,fun ,m ,@ml))
|
---|
186 |
|
---|
187 | (defmacro monom-append (m1 m2)
|
---|
188 | `(concatenate 'monom ,m1 ,m2))
|
---|
189 |
|
---|
190 | (defmacro monom-contract (k m)
|
---|
191 | `(setf ,m (subseq ,m ,k)))
|
---|
192 |
|
---|
193 | (defun make-monom-variable (nvars pos &optional (power 1)
|
---|
194 | &aux (m (make-monom :dimension nvars)))
|
---|
195 | "Construct a monomial in the polynomial ring
|
---|
196 | RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
|
---|
197 | which represents a single variable. It assumes number of variables
|
---|
198 | NVARS and the variable is at position POS. Optionally, the variable
|
---|
199 | may appear raised to power POWER. "
|
---|
200 | (setf (monom-elt m pos) power)
|
---|
201 | m)
|
---|
202 |
|
---|
203 | (defun monom->list (m)
|
---|
204 | "A human-readable representation of a monomial M as a list of exponents."
|
---|
205 | (coerce m 'list))
|
---|
206 |
|
---|
207 | (defclass term (monom)
|
---|
208 | ()
|
---|
209 | (:documentation "Implements a term, i.e. a product of a scalar
|
---|
210 | and powers of some variables, such as 5*X^2*Y^3."))
|
---|
211 |
|
---|
212 | (defmethod print-object ((self term) stream)
|
---|
213 | (print-unreadable-object (self stream :type t :identity t)
|
---|
214 | (with-accessors ((exponents monom-exponents)
|
---|
215 | (coeff scalar-coeff))
|
---|
216 | self
|
---|
217 | (format stream "EXPONENTS=~A COEFF=~A"
|
---|
218 | exponents coeff))))
|
---|
219 |
|
---|
220 |
|
---|
221 | (defmethod r-equalp ((term1 term) (term2 term))
|
---|
222 | (when (r-equalp (scalar-coeff term1) (scalar-coeff term2))
|
---|
223 | (call-next-method)))
|
---|
224 |
|
---|
225 | (defmethod update-instance-for-different-class :after ((old monom) (new scalar) &key)
|
---|
226 | (setf (scalar-coeff new) 1))
|
---|
227 |
|
---|
228 | (defmethod multiply-by :before ((self term) (other term))
|
---|
229 | "Destructively multiply terms SELF and OTHER and store the result into SELF.
|
---|
230 | It returns SELF."
|
---|
231 | (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other))))
|
---|
232 |
|
---|
233 | (defmethod left-tensor-product-by ((self term) (other term))
|
---|
234 | (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other)))
|
---|
235 | (call-next-method))
|
---|
236 |
|
---|
237 | (defmethod right-tensor-product-by ((self term) (other term))
|
---|
238 | (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other)))
|
---|
239 | (call-next-method))
|
---|
240 |
|
---|
241 | (defmethod left-tensor-product-by ((self term) (other monom))
|
---|
242 | (call-next-method))
|
---|
243 |
|
---|
244 | (defmethod right-tensor-product-by ((self term) (other monom))
|
---|
245 | (call-next-method))
|
---|
246 |
|
---|
247 | (defmethod divide-by ((self term) (other term))
|
---|
248 | "Destructively divide term SELF by OTHER and store the result into SELF.
|
---|
249 | It returns SELF."
|
---|
250 | (setf (scalar-coeff self) (divide-by (scalar-coeff self) (scalar-coeff other)))
|
---|
251 | (call-next-method))
|
---|
252 |
|
---|
253 | (defmethod unary-minus ((self term))
|
---|
254 | (setf (scalar-coeff self) (unary-minus (scalar-coeff self)))
|
---|
255 | self)
|
---|
256 |
|
---|
257 | (defmethod r* ((term1 term) (term2 term))
|
---|
258 | "Non-destructively multiply TERM1 by TERM2."
|
---|
259 | (multiply-by (copy-instance term1) (copy-instance term2)))
|
---|
260 |
|
---|
261 | (defmethod r* ((term1 number) (term2 monom))
|
---|
262 | "Non-destructively multiply TERM1 by TERM2."
|
---|
263 | (r* term1 (change-class (copy-instance term2) 'term)))
|
---|
264 |
|
---|
265 | (defmethod r* ((term1 number) (term2 term))
|
---|
266 | "Non-destructively multiply TERM1 by TERM2."
|
---|
267 | (setf (scalar-coeff term2)
|
---|
268 | (r* term1 (scalar-coeff term2)))
|
---|
269 | term2)
|
---|
270 |
|
---|
271 | (defmethod r-zerop ((self term))
|
---|
272 | (r-zerop (scalar-coeff self)))
|
---|