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

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

* empty log message *

File size: 6.8 KB
Line 
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
22(defpackage "MONOMIAL"
23 (:use :cl)
24 (:export "MAKE-MONOM"
25 "MONOM-ELT"
26 "MONOM-DIMENSION"
27 "MONOM-TOTAL-DEGREE"
28 "MONOM-SUGAR"
29 "MONOM-DIV"
30 "MONOM-MUL"
31 "MONOM-DIVIDES-P"
32 "MONOM-DIVIDES-MONOM-LCM-P"
33 "MONOM-LCM-DIVIDES-MONOM-LCM-P"
34 "MONOM-DIVISIBLE-BY-P"
35 "MONOM-REL-PRIME-P"
36 "MONOM-EQUAL-P"
37 "MONOM-LCM"
38 "MONOM-GCD"
39 "MONOM-MAP"
40 "MONOM-APPEND"
41 "MONOM-CONTRACT"
42 "MONOM-EXPONENTS"))
43
44;;----------------------------------------------------------------
45;; This package implements BASIC OPERATIONS ON MONOMIALS
46;;----------------------------------------------------------------
47;; DATA STRUCTURES: Conceptually, monomials can be represented as lists:
48;;
49;; monom: (n1 n2 ... nk) where ni are non-negative integers
50;;
51;; However, lists may be implemented as other sequence types,
52;; so the flexibility to change the representation should be
53;; maintained in the code to use general operations on sequences
54;; whenever possible. The optimization for the actual representation
55;; should be left to declarations and the compiler.
56;;----------------------------------------------------------------
57;; EXAMPLES: Suppose that variables are x and y. Then
58;;
59;; Monom x*y^2 ---> #(1 2)
60;;
61;;----------------------------------------------------------------
62
63(deftype exponent ()
64 "Type of exponent in a monomial."
65 'fixnum)
66
67(deftype monom (&optional dim)
68 "Type of monomial."
69 `(simple-array exponent (,dim)))
70
71;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
72;;
73;; Construction of monomials
74;;
75;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76
77(defmacro make-monom (dim &key (initial-contents nil initial-contents-supplied-p)
78 (initial-element 0 initial-element-supplied-p))
79 "Make a monomial with DIM variables. Additional argument
80INITIAL-CONTENTS specifies the list of powers of the consecutive
81variables. The alternative additional argument INITIAL-ELEMENT
82specifies the common power for all variables."
83 ;;(declare (fixnum dim))
84 `(make-array ,dim
85 :element-type 'exponent
86 ,@(when initial-contents-supplied-p `(:initial-contents ,initial-contents))
87 ,@(when initial-element-supplied-p `(:initial-element ,initial-element))))
88
89
90;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91;;
92;; Operations on monomials
93;;
94;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95
96(defmacro monom-elt (m index)
97 "Return the power in the monomial M of variable number INDEX."
98 `(elt ,m ,index))
99
100(defun monom-dimension (m)
101 "Return the number of variables in the monomial M."
102 (length m))
103
104(defun monom-total-degree (m &optional (start 0) (end (length m)))
105 "Return the todal degree of a monomoal M. Optinally, a range
106of variables may be specified with arguments START and END."
107 (declare (type monom m) (fixnum start end))
108 (reduce #'+ m :start start :end end))
109
110(defun monom-sugar (m &aux (start 0) (end (length m)))
111 "Return the sugar of a monomial M. Optinally, a range
112of variables may be specified with arguments START and END."
113 (declare (type monom m) (fixnum start end))
114 (monom-total-degree m start end))
115
116(defun monom-div (m1 m2 &aux (result (copy-seq m1)))
117 "Divide monomial M1 by monomial M2."
118 (declare (type monom m1 m2 result))
119 (map-into result #'- m1 m2))
120
121(defun monom-mul (m1 m2 &aux (result (copy-seq m1)))
122 "Multiply monomial M1 by monomial M2."
123 (declare (type monom m1 m2 result))
124 (map-into result #'+ m1 m2))
125
126(defun monom-divides-p (m1 m2)
127 "Returns T if monomial M1 divides monomial M2, NIL otherwise."
128 (declare (type monom m1 m2))
129 (every #'<= m1 m2))
130
131(defun monom-divides-monom-lcm-p (m1 m2 m3)
132 "Returns T if monomial M1 divides MONOM-LCM(M2,M3), NIL otherwise."
133 (declare (type monom m1 m2 m3))
134 (every #'(lambda (x y z) (declare (type exponent x y z)) (<= x (max y z))) m1 m2 m3))
135
136(defun monom-lcm-divides-monom-lcm-p (m1 m2 m3 m4)
137 "Returns T if monomial MONOM-LCM(M1,M2) divides MONOM-LCM(M3,M4), NIL otherwise."
138 (declare (type monom m1 m2 m3 m4))
139 (every #'(lambda (x y z w) (declare (type exponent x y z w)) (<= (max x y) (max z w))) m1 m2 m3 m4))
140
141(defun monom-lcm-equal-monom-lcm-p (m1 m2 m3 m4)
142 "Returns T if monomial MONOM-LCM(M1,M2) equals MONOM-LCM(M3,M4), NIL otherwise."
143 (declare (type monom m1 m2 m3 m4))
144 (every #'(lambda (x y z w) (declare (type exponent x y z w)) (= (max x y) (max z w))) m1 m2 m3 m4))
145
146(defun monom-divisible-by-p (m1 m2)
147 "Returns T if monomial M1 is divisible by monomial M2, NIL otherwise."
148 (declare (type monom m1 m2))
149 (every #'>= m1 m2))
150
151(defun monom-rel-prime-p (m1 m2)
152 "Returns T if two monomials M1 and M2 are relatively prime (disjoint)."
153 (declare (type monom m1 m2))
154 (every #'(lambda (x y) (declare (type exponent x y)) (zerop (min x y))) m1 m2))
155
156(defun monom-equal-p (m1 m2)
157 "Returns T if two monomials M1 and M2 are equal."
158 (declare (type monom m1 m2))
159 (every #'= m1 m2))
160
161(defun monom-lcm (m1 m2 &aux (result (copy-seq m1)))
162 "Returns least common multiple of monomials M1 and M2."
163 (declare (type monom m1 m2))
164 (map-into result #'max m1 m2))
165
166(defun monom-gcd (m1 m2 &aux (result (copy-seq m1)))
167 "Returns greatest common divisor of monomials M1 and M2."
168 (declare (type monom m1 m2))
169 (map-into result #'min m1 m2))
170
171(defun monom-depends-p (m k)
172 "Return T if the monomial M depends on variable number K."
173 (declare (type monom m) (fixnum k))
174 (plusp (elt m k)))
175
176(defmacro monom-map (fun m &rest ml &aux (result `(copy-seq ,m)))
177 `(map-into ,result ,fun ,m ,@ml))
178
179(defmacro monom-append (m1 m2)
180 `(concatenate 'monom ,m1 ,m2))
181
182(defmacro monom-contract (k m)
183 `(subseq ,m ,k))
184
185(defun monom-exponents (m)
186 (declare (type monom m))
187 (coerce m 'list))
Note: See TracBrowser for help on using the repository browser.