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

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

* empty log message *

File size: 9.6 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[81]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
[1610]22(defpackage "MONOM"
[2025]23 (:use :cl :ring)
[422]24 (:export "MONOM"
[423]25 "EXPONENT"
[2548]26 "MONOM="
[2524]27 "MAKE-MONOM-VARIABLE")
28 (:documentation
29 "This package implements basic operations on monomials.
30DATA STRUCTURES: Conceptually, monomials can be represented as lists:
[81]31
[2524]32 monom: (n1 n2 ... nk) where ni are non-negative integers
33
34However, lists may be implemented as other sequence types, so the
35flexibility to change the representation should be maintained in the
36code to use general operations on sequences whenever possible. The
37optimization for the actual representation should be left to
38declarations and the compiler.
39
40EXAMPLES: Suppose that variables are x and y. Then
41
42 Monom x*y^2 ---> (1 2) "))
43
[1610]44(in-package :monom)
[48]45
[1925]46(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[1923]47
[48]48(deftype exponent ()
49 "Type of exponent in a monomial."
50 'fixnum)
51
[2022]52(defclass monom ()
[2361]53 ((dimension :initarg :dimension :accessor r-dimension)
54 (exponents :initarg :exponents :accessor r-exponents))
[2268]55 (:default-initargs :dimension nil :exponents nil :exponent nil))
[880]56
[2245]57(defmethod print-object ((self monom) stream)
58 (format stream "#<MONOM DIMENSION=~A EXPONENTS=~A>"
[2362]59 (r-dimension self)
[2366]60 (r-exponents self)))
[2027]61
[2390]62(defmethod shared-initialize :after ((self monom) slot-names
63 &key
64 dimension
65 exponents
66 exponent
67 &allow-other-keys
68 )
[2354]69 (if (eq slot-names t) (setf slot-names '(dimension exponents)))
70 (dolist (slot-name slot-names)
[2357]71 (case slot-name
[2354]72 (dimension
[2355]73 (cond (dimension
74 (setf (slot-value self 'dimension) dimension))
[2354]75 (exponents
76 (setf (slot-value self 'dimension) (length exponents)))
77 (t
78 (error "DIMENSION or EXPONENTS must not be NIL"))))
79 (exponents
80 (cond
81 ;; when exponents are supplied
82 (exponents
[2356]83 (let ((dim (length exponents)))
[2405]84 (when (and dimension (/= dimension dim))
85 (error "EXPONENTS must have length DIMENSION"))
[2356]86 (setf (slot-value self 'dimension) dim
87 (slot-value self 'exponents) (make-array dim :initial-contents exponents))))
[2354]88 ;; when all exponents are to be identical
[2356]89 (t
90 (let ((dim (slot-value self 'dimension)))
91 (setf (slot-value self 'exponents)
92 (make-array (list dim) :initial-element (or exponent 0)
93 :element-type 'exponent)))))))))
[717]94
[2547]95(defgeneric monom= (object1 object2)
96 (:method ((object1 monom) (object2 monom))
97 (equal (r-exponents object1) (r-exponents object2))))
98
[2398]99(defmethod r-coeff ((m monom))
100 "A MONOM can be treated as a special case of TERM,
101where the coefficient is 1."
102 1)
[2397]103
[2143]104(defmethod r-elt ((m monom) index)
[48]105 "Return the power in the monomial M of variable number INDEX."
[2023]106 (with-slots (exponents)
107 m
[2154]108 (elt exponents index)))
[48]109
[2160]110(defmethod (setf r-elt) (new-value (m monom) index)
[2023]111 "Return the power in the monomial M of variable number INDEX."
112 (with-slots (exponents)
113 m
[2154]114 (setf (elt exponents index) new-value)))
[2023]115
[2149]116(defmethod r-total-degree ((m monom) &optional (start 0) (end (r-dimension m)))
[48]117 "Return the todal degree of a monomoal M. Optinally, a range
118of variables may be specified with arguments START and END."
[2023]119 (declare (type fixnum start end))
120 (with-slots (exponents)
121 m
[2154]122 (reduce #'+ exponents :start start :end end)))
[48]123
[2064]124
[2149]125(defmethod r-sugar ((m monom) &aux (start 0) (end (r-dimension m)))
[48]126 "Return the sugar of a monomial M. Optinally, a range
127of variables may be specified with arguments START and END."
[2032]128 (declare (type fixnum start end))
[2155]129 (r-total-degree m start end))
[48]130
[2414]131(defmethod r* ((m1 monom) (m2 monom))
[2072]132 "Multiply monomial M1 by monomial M2."
[2195]133 (with-slots ((exponents1 exponents) dimension)
[2038]134 m1
[2170]135 (with-slots ((exponents2 exponents))
[2038]136 m2
[2167]137 (let* ((exponents (copy-seq exponents1)))
[2154]138 (map-into exponents #'+ exponents1 exponents2)
[2414]139 (make-instance 'monom :dimension dimension :exponents exponents)))))
[2038]140
[2478]141(defmethod multiply-by ((self monom) (other monom))
[2479]142 (with-slots ((exponents1 exponents))
[2478]143 self
144 (with-slots ((exponents2 exponents))
145 other
[2480]146 (map-into exponents1 #'+ exponents1 exponents2)))
147 self)
[2069]148
[2144]149(defmethod r/ ((m1 monom) (m2 monom))
[1896]150 "Divide monomial M1 by monomial M2."
[2313]151 (with-slots ((exponents1 exponents) (dimension1 dimension))
[2034]152 m1
[2037]153 (with-slots ((exponents2 exponents))
[2034]154 m2
155 (let* ((exponents (copy-seq exponents1))
[2314]156 (dimension dimension1))
[2154]157 (map-into exponents #'- exponents1 exponents2)
[2195]158 (make-instance 'monom :dimension dimension :exponents exponents)))))
[48]159
[2144]160(defmethod r-divides-p ((m1 monom) (m2 monom))
[48]161 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
[2039]162 (with-slots ((exponents1 exponents))
163 m1
164 (with-slots ((exponents2 exponents))
165 m2
166 (every #'<= exponents1 exponents2))))
[48]167
[2075]168
[2144]169(defmethod r-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom))
[2055]170 "Returns T if monomial M1 divides LCM(M2,M3), NIL otherwise."
[875]171 (every #'(lambda (x y z) (<= x (max y z)))
[869]172 m1 m2 m3))
[48]173
[2049]174
[2144]175(defmethod r-lcm-divides-lcm-p ((m1 monom) (m2 monom) (m3 monom) (m4 monom))
[48]176 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
[1890]177 (declare (type monom m1 m2 m3 m4))
[869]178 (every #'(lambda (x y z w) (<= (max x y) (max z w)))
179 m1 m2 m3 m4))
180
[2144]181(defmethod r-lcm-equal-lcm-p (m1 m2 m3 m4)
[2075]182 "Returns T if monomial LCM(M1,M2) equals LCM(M3,M4), NIL otherwise."
[2171]183 (with-slots ((exponents1 exponents))
[2076]184 m1
[2171]185 (with-slots ((exponents2 exponents))
[2076]186 m2
[2171]187 (with-slots ((exponents3 exponents))
[2076]188 m3
[2171]189 (with-slots ((exponents4 exponents))
[2076]190 m4
[2077]191 (every
192 #'(lambda (x y z w) (= (max x y) (max z w)))
193 exponents1 exponents2 exponents3 exponents4))))))
[48]194
[2144]195(defmethod r-divisible-by-p ((m1 monom) (m2 monom))
[48]196 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
[2171]197 (with-slots ((exponents1 exponents))
[2144]198 m1
[2171]199 (with-slots ((exponents2 exponents))
[2144]200 m2
201 (every #'>= exponents1 exponents2))))
[2078]202
[2146]203(defmethod r-rel-prime-p ((m1 monom) (m2 monom))
[48]204 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
[2171]205 (with-slots ((exponents1 exponents))
[2078]206 m1
[2171]207 (with-slots ((exponents2 exponents))
[2078]208 m2
[2154]209 (every #'(lambda (x y) (zerop (min x y))) exponents1 exponents2))))
[48]210
[2076]211
[2163]212(defmethod r-equalp ((m1 monom) (m2 monom))
[48]213 "Returns T if two monomials M1 and M2 are equal."
[2171]214 (with-slots ((exponents1 exponents))
[2079]215 m1
[2171]216 (with-slots ((exponents2 exponents))
[2079]217 m2
218 (every #'= exponents1 exponents2))))
[48]219
[2146]220(defmethod r-lcm ((m1 monom) (m2 monom))
[48]221 "Returns least common multiple of monomials M1 and M2."
[2319]222 (with-slots ((exponents1 exponents) (dimension1 dimension))
[2082]223 m1
[2171]224 (with-slots ((exponents2 exponents))
[2082]225 m2
226 (let* ((exponents (copy-seq exponents1))
[2319]227 (dimension dimension1))
[2082]228 (map-into exponents #'max exponents1 exponents2)
[2200]229 (make-instance 'monom :dimension dimension :exponents exponents)))))
[48]230
[2080]231
[2146]232(defmethod r-gcd ((m1 monom) (m2 monom))
[48]233 "Returns greatest common divisor of monomials M1 and M2."
[2320]234 (with-slots ((exponents1 exponents) (dimension1 dimension))
[2082]235 m1
[2171]236 (with-slots ((exponents2 exponents))
[2082]237 m2
238 (let* ((exponents (copy-seq exponents1))
[2320]239 (dimension dimension1))
[2082]240 (map-into exponents #'min exponents1 exponents2)
[2197]241 (make-instance 'monom :dimension dimension :exponents exponents)))))
[48]242
[2146]243(defmethod r-depends-p ((m monom) k)
[48]244 "Return T if the monomial M depends on variable number K."
[2083]245 (declare (type fixnum k))
246 (with-slots (exponents)
247 m
[2154]248 (plusp (elt exponents k))))
[48]249
[2321]250(defmethod r-tensor-product ((m1 monom) (m2 monom))
251 (with-slots ((exponents1 exponents) (dimension1 dimension))
[2087]252 m1
[2321]253 (with-slots ((exponents2 exponents) (dimension2 dimension))
[2087]254 m2
[2147]255 (make-instance 'monom
[2321]256 :dimension (+ dimension1 dimension2)
[2147]257 :exponents (concatenate 'vector exponents1 exponents2)))))
[48]258
[2148]259(defmethod r-contract ((m monom) k)
[1638]260 "Drop the first K variables in monomial M."
[2085]261 (declare (fixnum k))
[2196]262 (with-slots (dimension exponents)
[2085]263 m
[2197]264 (setf dimension (- dimension k)
[2085]265 exponents (subseq exponents k))))
[886]266
267(defun make-monom-variable (nvars pos &optional (power 1)
[2218]268 &aux (m (make-instance 'monom :dimension nvars)))
[886]269 "Construct a monomial in the polynomial ring
270RING[X[0],X[1],X[2],...X[NVARS-1]] over the (unspecified) ring RING
271which represents a single variable. It assumes number of variables
272NVARS and the variable is at position POS. Optionally, the variable
273may appear raised to power POWER. "
[1924]274 (declare (type fixnum nvars pos power) (type monom m))
[2089]275 (with-slots (exponents)
276 m
[2154]277 (setf (elt exponents pos) power)
[2089]278 m))
[1151]279
[2150]280(defmethod r->list ((m monom))
[1152]281 "A human-readable representation of a monomial M as a list of exponents."
[2364]282 (coerce (r-exponents m) 'list))
Note: See TracBrowser for help on using the repository browser.