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

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

* empty log message *

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