source: CGBLisp/src/order.lisp@ 1

Last change on this file since 1 was 1, checked in by Marek Rychlik, 15 years ago

First import of a version circa 1997.

File size: 5.1 KB
Line 
1#|
2 $Id: order.lisp,v 1.4 2009/01/23 10:39:41 marek Exp $
3 *--------------------------------------------------------------------------*
4 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
5 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
6 | |
7 | Everyone is permitted to copy, distribute and modify the code in this |
8 | directory, as long as this copyright note is preserved verbatim. |
9 *--------------------------------------------------------------------------*
10|#
11;; Order relations for vectors of numbers
12;; Below p, q is a multiindex: p ---> (n1 n2 ... nk), where ni are
13;; nonnegative integers. The package, though, will work with any
14;; kind of real numbers.
15;; These functions have a common interface
16;; Their arguments are:
17;; -two monomials P and Q to be compared
18;; -function KEY which is called before comparing the entries
19;; -START and END which restrict the index range for comparison
20;; Each of the functions returs two values
21;; -T or NIL depending on whether P>Q or not
22;; -the second value is T if the sequences are equal
23
24(defpackage "ORDER"
25 (:use "COMMON-LISP")
26 (:export
27 lex>
28 invlex>
29 grlex>
30 grevlex>
31 elimination-order
32 elimination-order-1
33 total-degree))
34
35(in-package "ORDER")
36
37#+debug(proclaim '(optimize (speed 0) (debug 3)))
38#-debug(proclaim '(optimize (speed 3) (debug 0)))
39
40;; pure lexicographic
41(defun lex> (p q &optional (start 0) (end (length p)))
42 "Return T if P>Q with respect to lexicographic order, otherwise NIL.
43The second returned value is T if P=Q, otherwise it is NIL."
44 (do ((i start (1+ i)))
45 ((>= i end) (values NIL T))
46 (cond
47 ((> (elt p i) (elt q i))
48 (return-from lex> (values t nil)))
49 ((< (elt p i) (elt q i))
50 (return-from lex> (values nil nil))))))
51
52;; total degree of a multiindex
53(defun total-degree (m &optional (start 0) (end (length m)))
54 "Return the todal degree of a monomoal M."
55 (reduce #'+ (subseq m start end)))
56
57;; total degree order , ties broken by lexicographic
58(defun grlex> (p q &optional (start 0) (end (length p)))
59 "Return T if P>Q with respect to graded lexicographic order, otherwise NIL.
60The second returned value is T if P=Q, otherwise it is NIL."
61 (let ((d1 (total-degree p start end))
62 (d2 (total-degree q start end)))
63 (cond
64 ((> d1 d2) (values t nil))
65 ((< d1 d2) (values nil nil))
66 (t
67 (lex> p q start end)))))
68
69;; reverse lexicographic
70(defun revlex> (p q &optional (start 0) (end (length p)))
71 "Return T if P>Q with respect to reverse lexicographic order, NIL
72otherwise. The second returned value is T if P=Q, otherwise it is
73NIL. This is not and admissible monomial order because some sets do
74not have a minimal element. This order is useful in constructing other
75orders."
76 (do ((i (1- end) (1- i)))
77 ((< i start) (values NIL T))
78 (cond
79 ((< (elt p i) (elt q i))
80 (return-from revlex> (values t nil)))
81 ((> (elt p i) (elt q i))
82 (return-from revlex> (values nil nil))))))
83
84;; total degree, ties broken by reverse lexicographic
85(defun grevlex> (p q &optional (start 0) (end (length p)))
86 "Return T if P>Q with respect to graded reverse lexicographic order,
87NIL otherwise. The second returned value is T if P=Q, otherwise it is NIL."
88 (let ((d1 (total-degree p start end))
89 (d2 (total-degree q start end)))
90 (cond
91 ((> d1 d2) (values t nil))
92 ((< d1 d2) (values nil nil))
93 (t (revlex> p q start end)))))
94
95(defun invlex> (p q &optional (start 0) (end (length p)))
96 "Return T if P>Q with respect to inverse lexicographic order, NIL otherwise
97The second returned value is T if P=Q, otherwise it is NIL."
98 (do ((i (1- end) (1- i)))
99 ((< i start) (values NIL T))
100 (cond
101 ((> (elt p i) (elt q i))
102 (return-from invlex> (values t nil)))
103 ((< (elt p i) (elt q i))
104 (return-from invlex> (values nil nil))))))
105
106
107;;----------------------------------------------------------------
108;; Order making functions
109;;----------------------------------------------------------------
110
111;; Make an order which compares the first K variables according to
112;; PRIMARY-ORDER and the remaining elements according to
113;; SECONDARY-ORDER
114(defun elimination-order (k &key (primary-order #'lex>)
115 (secondary-order #'lex>))
116 "Return a predicate which compares monomials according to the
117K-th elimination order. Two optional arguments are PRIMARY-ORDER
118and SECONDARY-ORDER and they should be term orders which are used
119on the first K and the remaining variables."
120 #'(lambda (p q &optional (start 0) (end (length p)))
121 (multiple-value-bind (primary equal)
122 (funcall primary-order p q start k)
123 (if equal
124 (funcall secondary-order p q k end)
125 (values primary nil)))))
126
127(defun elimination-order-1 (order)
128 "A special case of the ELIMINATION-ORDER when there is only
129one primary variable."
130 #'(lambda (p q
131 &optional (start 0)
132 (end (length p)))
133 (cond
134 ((> (elt p start) (elt q start)) (values t nil))
135 ((< (elt p start) (elt q start)) (values nil nil))
136 (t (funcall order p q (1+ start) end)))))
137
138
Note: See TracBrowser for help on using the repository browser.