1 | ;;; -*- Mode: Lisp -*-
|
---|
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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
23 | ;;
|
---|
24 | ;; Run tests using 5am unit testing framework
|
---|
25 | ;;
|
---|
26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
27 |
|
---|
28 | ;; We assume that QuickLisp package manager is installed.
|
---|
29 | ;; See :
|
---|
30 | ;; https://www.quicklisp.org/beta/
|
---|
31 | ;;
|
---|
32 |
|
---|
33 | ;; The following is unnecessary after running:
|
---|
34 | ;; * (ql:add-to-init-file)
|
---|
35 | ;; at lisp prompt:
|
---|
36 | ;;(load "~/quicklisp/setup")
|
---|
37 |
|
---|
38 | (ql:quickload :fiveam)
|
---|
39 |
|
---|
40 | (load "ngrobner.asd")
|
---|
41 | (asdf:load-system :ngrobner)
|
---|
42 |
|
---|
43 | (defpackage #:ngrobner-tests
|
---|
44 | (:use :cl :it.bese.fiveam
|
---|
45 | :ngrobner :priority-queue :monomial
|
---|
46 | :utils :order :ring :term :ring-and-order
|
---|
47 | :termlist :polynomial
|
---|
48 | :priority-queue
|
---|
49 | :division
|
---|
50 | :grobner-wrap
|
---|
51 | )
|
---|
52 | )
|
---|
53 |
|
---|
54 | (in-package :ngrobner-tests)
|
---|
55 |
|
---|
56 | (def-suite ngrobner-suite
|
---|
57 | :description "New Groebner Package Suite")
|
---|
58 |
|
---|
59 | (in-suite ngrobner-suite)
|
---|
60 |
|
---|
61 | #+nil
|
---|
62 | (test dummy-test
|
---|
63 | "Makelist"
|
---|
64 | (is (= (+ 2 2)) "2 plus 2 wasn't equal to 4 (using #'= to test equality)")
|
---|
65 | (is (= 0 (+ -1 1)))
|
---|
66 | (signals
|
---|
67 | (error "Trying to add 4 to FOO didn't signal an error")
|
---|
68 | (+ 'foo 4))
|
---|
69 | (is (= 0 (+ 1 1)) "this should have failed"))
|
---|
70 |
|
---|
71 | (test makelist-1
|
---|
72 | "makelist-1 test"
|
---|
73 | (is (equal (makelist-1 (* 2 i) i 0 10) '(0 2 4 6 8 10 12 14 16 18 20)))
|
---|
74 | (is (equal (makelist-1 (* 2 i) i 0 10 3) '(0 6 12 18))))
|
---|
75 |
|
---|
76 | (test makelist
|
---|
77 | "makelist"
|
---|
78 | (is (equal (makelist (+ (* i i) (* j j)) (i 1 4) (j 1 i)) '(2 5 8 10 13 18 17 20 25 32)))
|
---|
79 | (is (equal (makelist (list i j '---> (+ (* i i) (* j j))) (i 1 4) (j 1 i))
|
---|
80 | '((1 1 ---> 2) (2 1 ---> 5) (2 2 ---> 8) (3 1 ---> 10) (3 2 ---> 13)
|
---|
81 | (3 3 ---> 18) (4 1 ---> 17) (4 2 ---> 20) (4 3 ---> 25) (4 4 ---> 32)))))
|
---|
82 |
|
---|
83 | (test monom
|
---|
84 | "monom"
|
---|
85 | (is (every #'= (make-monom :dimension 3) '(0 0 0)) "Trivial monomial is a vector of 0's")
|
---|
86 | (is (every #'= (make-monom :initial-exponents '(1 2 3)) '(1 2 3)) "Monomial with powers 1,2,3")
|
---|
87 | (let ((p (make-monom :initial-exponents '(1 2 3))))
|
---|
88 | (is (every #'= (monom-map (lambda (x) x) p) '(1 2 3)))))
|
---|
89 |
|
---|
90 |
|
---|
91 | (test order
|
---|
92 | "order"
|
---|
93 | (let ((p (make-monom :initial-exponents '(1 3 2)))
|
---|
94 | (q (make-monom :initial-exponents '(1 2 3))))
|
---|
95 | (is-true (lex> p q))
|
---|
96 | (is-true (grlex> p q))
|
---|
97 | (is-true (revlex> p q))
|
---|
98 | (is-true (grevlex> p q))
|
---|
99 | (is-false (invlex> p q))))
|
---|
100 |
|
---|
101 | (test elim-order
|
---|
102 | "elimination order"
|
---|
103 | (let* ((p (make-monom :initial-exponents '(1 2 3)))
|
---|
104 | (q (make-monom :initial-exponents '(4 5 6)))
|
---|
105 | (elim-order-factory (make-elimination-order-factory))
|
---|
106 | (elim-order-1 (funcall elim-order-factory 1))
|
---|
107 | (elim-order-2 (funcall elim-order-factory 2)))
|
---|
108 | (is-false (funcall elim-order-1 p q))
|
---|
109 | (is-false (funcall elim-order-2 p q))))
|
---|
110 |
|
---|
111 | (test term
|
---|
112 | "term"
|
---|
113 | (let* ((m1 (make-monom :initial-exponents '(1 2 3)))
|
---|
114 | (m2 (make-monom :initial-exponents '(3 5 2)))
|
---|
115 | (m3 (monom-mul m1 m2))
|
---|
116 | (t1 (make-term m1 7))
|
---|
117 | (t2 (make-term m2 9))
|
---|
118 | (t3 (make-term m3 (* 7 9))))
|
---|
119 | (is (equalp (term-mul *ring-of-integers* t1 t2) t3))))
|
---|
120 |
|
---|
121 | (test termlist
|
---|
122 | "termlist"
|
---|
123 | (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
|
---|
124 | (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
|
---|
125 | (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
|
---|
126 | (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
|
---|
127 | (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
|
---|
128 | (p (list t2 t1))
|
---|
129 | (p-sq (list t22 t12 t11))
|
---|
130 | (ring-and-order (make-ring-and-order))
|
---|
131 | (q (termlist-expt ring-and-order p 2)))
|
---|
132 | (is-true (equalp q p-sq))))
|
---|
133 |
|
---|
134 | (test poly
|
---|
135 | "poly"
|
---|
136 | (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
|
---|
137 | (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
|
---|
138 | (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
|
---|
139 | (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
|
---|
140 | (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
|
---|
141 | (p (make-poly-from-termlist (list t2 t1)))
|
---|
142 | (p-sq (make-poly-from-termlist (list t22 t12 t11)))
|
---|
143 | (ring-and-order (make-ring-and-order))
|
---|
144 | (q (poly-expt ring-and-order p 2)))
|
---|
145 | (is-true (equalp q p-sq))))
|
---|
146 |
|
---|
147 |
|
---|
148 | (test coerce-to-infix
|
---|
149 | "Conversion to infix form"
|
---|
150 | (is (equal
|
---|
151 | (coerce-to-infix :term (make-term-variable *ring-of-integers* 5 3) '(x y z w u v))
|
---|
152 | '(* 1 (EXPT X 0) (EXPT Y 0) (EXPT Z 0) (EXPT W 1) (EXPT U 0)))))
|
---|
153 |
|
---|
154 | (test priority-queue
|
---|
155 | "Priority queue"
|
---|
156 | (let ((q (make-priority-queue)))
|
---|
157 | (priority-queue-insert q 7)
|
---|
158 | (priority-queue-insert q 8)
|
---|
159 | (is (= (priority-queue-size q) 3) "Note that there is always a dummy element in the queue.")
|
---|
160 | (is (equalp (priority-queue-heap q) #(0 7 8)))
|
---|
161 | (is (= (priority-queue-remove q) 7))
|
---|
162 | (is (= (priority-queue-remove q) 8))
|
---|
163 | (is-true (priority-queue-empty-p q))
|
---|
164 | (signals
|
---|
165 | (error "Empty queue.")
|
---|
166 | (priority-queue-remove q))))
|
---|
167 |
|
---|
168 | ;;
|
---|
169 | ;; Currently parser cannot be tested, as it relies on many maxima functions
|
---|
170 | ;; to parse a polynomial expression.
|
---|
171 | ;;
|
---|
172 | #|
|
---|
173 | (test parser
|
---|
174 | "Parser"
|
---|
175 | (let (($f '((MLIST SIMP) ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y)) ((MPLUS SIMP) $X $Y)))
|
---|
176 | ($v '((MLIST SIMP) $X $Y)))
|
---|
177 | (is-true (parse-poly-list $f $v))))
|
---|
178 | |#
|
---|
179 |
|
---|
180 | (test infix-print
|
---|
181 | "Infix printer"
|
---|
182 | (is (string= (infix-print '(+ x y) nil) "X+Y"))
|
---|
183 | (is (string= (infix-print '(expt x 3) nil) "X^3"))
|
---|
184 | (is (string= (infix-print '(+ 1 (expt x 3)) nil) "1+(X^3)"))
|
---|
185 | (is (string= (infix-print '(* x y) nil) "X*Y"))
|
---|
186 | (is (string= (infix-print '(* x (expt y 2)) nil) "X*(Y^2)")))
|
---|
187 |
|
---|
188 | (test infix
|
---|
189 | "Infix parser"
|
---|
190 | (is (equal '#I( x^2 + y^2 ) '(+ (expt x 2) (expt y 2))))
|
---|
191 | (is (equal '#I( [ x, y ] ) '(:[ X Y)))
|
---|
192 | (is (equal '#I( x + y) '(+ x y)))
|
---|
193 | (is (equal '#I( x^3 ) '(expt x 3)))
|
---|
194 | (is (equal '#I( 1 + x^3) '(+ 1 (expt x 3))))
|
---|
195 | (is (equal '#I( x * y^2 ) '(* x (expt y 2)))))
|
---|
196 |
|
---|
197 | (test poly-reader
|
---|
198 | "Polynomial reader"
|
---|
199 | (is (equalp (with-input-from-string (s "X^2-Y^2+(-4/3)*U^2*W^3-5")
|
---|
200 | (read-infix-form :stream s))
|
---|
201 | '(+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))))
|
---|
202 | (is (equalp (string->alist "X^2-Y^2+(-4/3)*U^2*W^3-5" '(x y u w))
|
---|
203 | '(((2 0 0 0) . 1)
|
---|
204 | ((0 2 0 0) . -1)
|
---|
205 | ((0 0 2 3) . -4/3)
|
---|
206 | ((0 0 0 0) . -5))))
|
---|
207 | (is (equalp (string->alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w))
|
---|
208 | '(:[
|
---|
209 | (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
|
---|
210 | (((0 1 0 0) . 1)))))
|
---|
211 | (let ((p (make-poly-from-termlist (list (make-term (make-monom :initial-exponents '(2 0)) 1)
|
---|
212 | (make-term (make-monom :initial-exponents '(0 2)) 2)))))
|
---|
213 | (is (equalp (with-input-from-string (s "x^2+2*y^2")
|
---|
214 | (read-poly '(x y) :stream s))
|
---|
215 | p))
|
---|
216 | (is (equalp (string->poly "x^2+2*y^2" '(x y)) p))))
|
---|
217 |
|
---|
218 | ;; Manual calculation supporting the test below.
|
---|
219 | ;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
|
---|
220 | ;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
|
---|
221 | ;; Next, X^2 - X*(X+Y) = -X*Y.
|
---|
222 | ;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
|
---|
223 | ;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
|
---|
224 | ;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
|
---|
225 | ;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
|
---|
226 | (test division
|
---|
227 | "Division in polynomial ring"
|
---|
228 | (let* ((f (string->poly "x^2" '(x y)))
|
---|
229 | (y-sq (string->poly "y^2" '(x y)))
|
---|
230 | (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
231 | (ring *ring-of-integers*)
|
---|
232 | (order #'lex>)
|
---|
233 | (ring-and-order (make-ring-and-order :ring ring :order order))
|
---|
234 | (quotients (cdr (string->poly "[x-y,0]" '(x y)))))
|
---|
235 | (is (equalp (multiple-value-list (normal-form ring-and-order f fl)) (list y-sq 1 2)))
|
---|
236 | (is (equalp (multiple-value-list (poly-pseudo-divide ring-and-order f fl))
|
---|
237 | (list quotients y-sq 1 2)))
|
---|
238 | (is-false (buchberger-criterion ring-and-order fl)))
|
---|
239 | (let* ((f (string->poly "x^2-4*y^2" '(x y)))
|
---|
240 | (g (string->poly "x+2*y" '(x y)))
|
---|
241 | (h (string->poly "x-2*y" '(x y)))
|
---|
242 | (ring *ring-of-integers*)
|
---|
243 | (order #'lex>)
|
---|
244 | (ring-and-order (make-ring-and-order :ring ring :order order)))
|
---|
245 | (is (equalp (poly-exact-divide ring-and-order f g) h))))
|
---|
246 |
|
---|
247 |
|
---|
248 | (test buchberger
|
---|
249 | "Buchberger algorithm"
|
---|
250 | (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
251 | (ring *ring-of-integers*)
|
---|
252 | (order #'lex>)
|
---|
253 | (ring-and-order (make-ring-and-order :ring ring :order order))
|
---|
254 | (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y)))))
|
---|
255 | (is-true (grobner-test ring-and-order gb fl))
|
---|
256 | (is (equalp (buchberger ring-and-order fl) gb))
|
---|
257 | (is (equalp (parallel-buchberger ring-and-order fl) gb))))
|
---|
258 |
|
---|
259 | (test gebauer-moeller
|
---|
260 | "Gebauer-Moeller algorithm"
|
---|
261 | (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
262 | (ring *ring-of-integers*)
|
---|
263 | (order #'lex>)
|
---|
264 | (ring-and-order (make-ring-and-order :ring ring :order order))
|
---|
265 | (gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
|
---|
266 | (is-true (grobner-test ring-and-order gb fl))
|
---|
267 | (is (equalp (gebauer-moeller ring-and-order fl) gb))))
|
---|
268 |
|
---|
269 | (test gb-postprocessing
|
---|
270 | "Grobner basis postprocessing"
|
---|
271 | (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
272 | (ring *ring-of-integers*)
|
---|
273 | (order #'lex>)
|
---|
274 | (ring-and-order (make-ring-and-order :ring ring :order order))
|
---|
275 | (gb (cdr (string->poly "[y,x-2*y]" '(x y))))
|
---|
276 | (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
|
---|
277 | (is-true (grobner-test ring-and-order gb fl))
|
---|
278 | (is (equalp (reduction ring-and-order gb) reduced-gb)))
|
---|
279 | (let* ((gb (cdr (string->poly "[x,y,x-2*y,x^2]" '(x y))))
|
---|
280 | (minimal-gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
|
---|
281 | (is (equalp (minimization gb) minimal-gb))))
|
---|
282 |
|
---|
283 | (test grobner-wrap
|
---|
284 | "Grobner interface to many algorithms"
|
---|
285 | (let* (($poly_grobner_algorithm :buchberger)
|
---|
286 | (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
287 | (ring *ring-of-integers*)
|
---|
288 | (order #'lex>)
|
---|
289 | (ring-and-order (make-ring-and-order :ring ring :order order))
|
---|
290 | (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y))))
|
---|
291 | (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
|
---|
292 | (is-true (grobner-test ring-and-order gb fl))
|
---|
293 | (is (equalp (grobner ring-and-order fl) gb))
|
---|
294 | (is (equalp (reduced-grobner ring-and-order fl) reduced-gb))))
|
---|
295 |
|
---|
296 |
|
---|
297 | (run! 'ngrobner-suite)
|
---|
298 | (format t "All tests done!~%")
|
---|
299 |
|
---|
300 |
|
---|