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/monomial.lisp@ 422

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

* empty log message *

File size: 6.9 KB
RevLine 
[81]1;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
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
[418]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
[394]41(defpackage "MONOMIAL"
[395]42 (:use :cl)
[422]43 (:export "MONOM"
44 "MAKE-MONOM"
[396]45 "MONOM-ELT"
46 "MONOM-DIMENSION"
47 "MONOM-TOTAL-DEGREE"
48 "MONOM-SUGAR"
49 "MONOM-DIV"
50 "MONOM-MUL"
51 "MONOM-DIVIDES-P"
[395]52 "MONOM-DIVIDES-MONOM-LCM-P"
53 "MONOM-LCM-DIVIDES-MONOM-LCM-P"
54 "MONOM-DIVISIBLE-BY-P"
55 "MONOM-REL-PRIME-P"
56 "MONOM-EQUAL-P"
57 "MONOM-LCM"
58 "MONOM-GCD"
59 "MONOM-MAP"
60 "MONOM-APPEND"
61 "MONOM-CONTRACT"
62 "MONOM-EXPONENTS"))
[81]63
[419]64(in-package :monomial)
[48]65
66(deftype exponent ()
67 "Type of exponent in a monomial."
68 'fixnum)
69
70(deftype monom (&optional dim)
71 "Type of monomial."
72 `(simple-array exponent (,dim)))
73
74;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75;;
76;; Construction of monomials
77;;
78;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
79
80(defmacro make-monom (dim &key (initial-contents nil initial-contents-supplied-p)
81 (initial-element 0 initial-element-supplied-p))
82 "Make a monomial with DIM variables. Additional argument
83INITIAL-CONTENTS specifies the list of powers of the consecutive
84variables. The alternative additional argument INITIAL-ELEMENT
85specifies the common power for all variables."
86 ;;(declare (fixnum dim))
87 `(make-array ,dim
88 :element-type 'exponent
89 ,@(when initial-contents-supplied-p `(:initial-contents ,initial-contents))
90 ,@(when initial-element-supplied-p `(:initial-element ,initial-element))))
91
[392]92
[48]93;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94;;
95;; Operations on monomials
96;;
97;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98
99(defmacro monom-elt (m index)
100 "Return the power in the monomial M of variable number INDEX."
101 `(elt ,m ,index))
102
103(defun monom-dimension (m)
104 "Return the number of variables in the monomial M."
105 (length m))
106
107(defun monom-total-degree (m &optional (start 0) (end (length m)))
108 "Return the todal degree of a monomoal M. Optinally, a range
109of variables may be specified with arguments START and END."
110 (declare (type monom m) (fixnum start end))
111 (reduce #'+ m :start start :end end))
112
113(defun monom-sugar (m &aux (start 0) (end (length m)))
114 "Return the sugar of a monomial M. Optinally, a range
115of variables may be specified with arguments START and END."
116 (declare (type monom m) (fixnum start end))
117 (monom-total-degree m start end))
118
119(defun monom-div (m1 m2 &aux (result (copy-seq m1)))
120 "Divide monomial M1 by monomial M2."
121 (declare (type monom m1 m2 result))
122 (map-into result #'- m1 m2))
123
124(defun monom-mul (m1 m2 &aux (result (copy-seq m1)))
125 "Multiply monomial M1 by monomial M2."
126 (declare (type monom m1 m2 result))
127 (map-into result #'+ m1 m2))
128
129(defun monom-divides-p (m1 m2)
130 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
131 (declare (type monom m1 m2))
132 (every #'<= m1 m2))
133
134(defun monom-divides-monom-lcm-p (m1 m2 m3)
135 "Returns T if monomial M1 divides MONOM-LCM(M2,M3), NIL otherwise."
136 (declare (type monom m1 m2 m3))
137 (every #'(lambda (x y z) (declare (type exponent x y z)) (<= x (max y z))) m1 m2 m3))
138
139(defun monom-lcm-divides-monom-lcm-p (m1 m2 m3 m4)
140 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
141 (declare (type monom m1 m2 m3 m4))
142 (every #'(lambda (x y z w) (declare (type exponent x y z w)) (<= (max x y) (max z w))) m1 m2 m3 m4))
143
144(defun monom-lcm-equal-monom-lcm-p (m1 m2 m3 m4)
145 "Returns T if monomial MONOM-LCM(M1,M2) equals MONOM-LCM(M3,M4), NIL otherwise."
146 (declare (type monom m1 m2 m3 m4))
147 (every #'(lambda (x y z w) (declare (type exponent x y z w)) (= (max x y) (max z w))) m1 m2 m3 m4))
148
149(defun monom-divisible-by-p (m1 m2)
150 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
151 (declare (type monom m1 m2))
152 (every #'>= m1 m2))
153
154(defun monom-rel-prime-p (m1 m2)
155 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
156 (declare (type monom m1 m2))
157 (every #'(lambda (x y) (declare (type exponent x y)) (zerop (min x y))) m1 m2))
158
159(defun monom-equal-p (m1 m2)
160 "Returns T if two monomials M1 and M2 are equal."
161 (declare (type monom m1 m2))
162 (every #'= m1 m2))
163
164(defun monom-lcm (m1 m2 &aux (result (copy-seq m1)))
165 "Returns least common multiple of monomials M1 and M2."
166 (declare (type monom m1 m2))
167 (map-into result #'max m1 m2))
168
169(defun monom-gcd (m1 m2 &aux (result (copy-seq m1)))
170 "Returns greatest common divisor of monomials M1 and M2."
171 (declare (type monom m1 m2))
172 (map-into result #'min m1 m2))
173
174(defun monom-depends-p (m k)
175 "Return T if the monomial M depends on variable number K."
176 (declare (type monom m) (fixnum k))
177 (plusp (elt m k)))
178
179(defmacro monom-map (fun m &rest ml &aux (result `(copy-seq ,m)))
180 `(map-into ,result ,fun ,m ,@ml))
181
182(defmacro monom-append (m1 m2)
183 `(concatenate 'monom ,m1 ,m2))
184
185(defmacro monom-contract (k m)
186 `(subseq ,m ,k))
187
188(defun monom-exponents (m)
189 (declare (type monom m))
190 (coerce m 'list))
Note: See TracBrowser for help on using the repository browser.