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/5am-tests.lisp@ 1475

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

* empty log message *

File size: 13.6 KB
Line 
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 summation
84 "summation"
85 (is (= (summation i (i 0 100)) 5050)))
86
87(test inner-product
88 "summation"
89 (is (= (inner-product '(1 2 3) '(4 5 6)) 32)))
90
91(test monom
92 "monom"
93 (is (every #'= (make-monom :dimension 3) '(0 0 0)) "Trivial monomial is a vector of 0's")
94 (is (every #'= (make-monom :initial-exponents '(1 2 3)) '(1 2 3)) "Monomial with powers 1,2,3")
95 (let ((p (make-monom :initial-exponents '(1 2 3))))
96 (is (every #'= (monom-map (lambda (x) x) p) '(1 2 3)))))
97
98
99(test order
100 "order"
101 (let ((p (make-monom :initial-exponents '(1 3 2)))
102 (q (make-monom :initial-exponents '(1 2 3))))
103 (is-true (lex> p q))
104 (is-true (grlex> p q))
105 (is-true (revlex> p q))
106 (is-true (grevlex> p q))
107 (is-false (invlex> p q))))
108
109(test elim-order
110 "elimination order"
111 (let* ((p (make-monom :initial-exponents '(1 2 3)))
112 (q (make-monom :initial-exponents '(4 5 6)))
113 (elim-order-factory (make-elimination-order-factory))
114 (elim-order-1 (funcall elim-order-factory 1))
115 (elim-order-2 (funcall elim-order-factory 2)))
116 (is-false (funcall elim-order-1 p q))
117 (is-false (funcall elim-order-2 p q))))
118
119(test term
120 "term"
121 (let* ((m1 (make-monom :initial-exponents '(1 2 3)))
122 (m2 (make-monom :initial-exponents '(3 5 2)))
123 (m3 (monom-mul m1 m2))
124 (t1 (make-term m1 7))
125 (t2 (make-term m2 9))
126 (t3 (make-term m3 (* 7 9))))
127 (is (equalp (term-mul *ring-of-integers* t1 t2) t3))))
128
129(test termlist
130 "termlist"
131 (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
132 (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
133 (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
134 (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
135 (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
136 (p (list t2 t1))
137 (p-sq (list t22 t12 t11))
138 (ring-and-order (make-ring-and-order))
139 (q (termlist-expt ring-and-order p 2)))
140 (is-true (equalp q p-sq))))
141
142(test poly
143 "poly"
144 (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
145 (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
146 (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
147 (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
148 (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
149 (p (make-poly-from-termlist (list t2 t1)))
150 (p-sq (make-poly-from-termlist (list t22 t12 t11)))
151 (ring-and-order (make-ring-and-order))
152 (q (poly-expt ring-and-order p 2)))
153 (is-true (equalp q p-sq))))
154
155
156(test coerce-to-infix
157 "Conversion to infix form"
158 (is (equal
159 (coerce-to-infix :term (make-term-variable *ring-of-integers* 5 3) '(x y z w u v))
160 '(* 1 (EXPT X 0) (EXPT Y 0) (EXPT Z 0) (EXPT W 1) (EXPT U 0)))))
161
162(test priority-queue
163 "Priority queue"
164 (let ((q (make-priority-queue)))
165 (priority-queue-insert q 7)
166 (priority-queue-insert q 8)
167 (is (= (priority-queue-size q) 3) "Note that there is always a dummy element in the queue.")
168 (is (equalp (priority-queue-heap q) #(0 7 8)))
169 (is (= (priority-queue-remove q) 7))
170 (is (= (priority-queue-remove q) 8))
171 (is-true (priority-queue-empty-p q))
172 (signals
173 (error "Empty queue.")
174 (priority-queue-remove q))))
175
176;;
177;; Currently parser cannot be tested, as it relies on many maxima functions
178;; to parse a polynomial expression.
179;;
180#|
181(test parser
182 "Parser"
183 (let (($f '((MLIST SIMP) ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y)) ((MPLUS SIMP) $X $Y)))
184 ($v '((MLIST SIMP) $X $Y)))
185 (is-true (parse-poly-list $f $v))))
186|#
187
188(test infix-print
189 "Infix printer"
190 (is (string= (infix-print '(+ x y) nil) "X+Y"))
191 (is (string= (infix-print '(expt x 3) nil) "X^3"))
192 (is (string= (infix-print '(+ 1 (expt x 3)) nil) "1+(X^3)"))
193 (is (string= (infix-print '(* x y) nil) "X*Y"))
194 (is (string= (infix-print '(* x (expt y 2)) nil) "X*(Y^2)")))
195
196(test infix
197 "Infix parser"
198 (is (equal '#I( x^2 + y^2 ) '(+ (expt x 2) (expt y 2))))
199 (is (equal '#I( [ x, y ] ) '(:[ X Y)))
200 (is (equal '#I( x + y) '(+ x y)))
201 (is (equal '#I( x^3 ) '(expt x 3)))
202 (is (equal '#I( 1 + x^3) '(+ 1 (expt x 3))))
203 (is (equal '#I( x * y^2 ) '(* x (expt y 2)))))
204
205(test poly-reader
206 "Polynomial reader"
207 (is (equalp (with-input-from-string (s "X^2-Y^2+(-4/3)*U^2*W^3-5")
208 (read-infix-form :stream s))
209 '(+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))))
210 (is (equalp (string->alist "X^2-Y^2+(-4/3)*U^2*W^3-5" '(x y u w))
211 '(((2 0 0 0) . 1)
212 ((0 2 0 0) . -1)
213 ((0 0 2 3) . -4/3)
214 ((0 0 0 0) . -5))))
215 (is (equalp (string->alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w))
216 '(:[
217 (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
218 (((0 1 0 0) . 1)))))
219 (let ((p (make-poly-from-termlist (list (make-term (make-monom :initial-exponents '(2 0)) 1)
220 (make-term (make-monom :initial-exponents '(0 2)) 2)))))
221 (is (equalp (with-input-from-string (s "x^2+2*y^2")
222 (read-poly '(x y) :stream s))
223 p))
224 (is (equalp (string->poly "x^2+2*y^2" '(x y)) p))))
225
226;; Manual calculation supporting the test below.
227;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
228;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
229;; Next, X^2 - X*(X+Y) = -X*Y.
230;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
231;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
232;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
233;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
234(test division
235 "Division in polynomial ring"
236 (let* ((f (string->poly "x^2" '(x y)))
237 (y-sq (string->poly "y^2" '(x y)))
238 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
239 (ring *ring-of-integers*)
240 (order #'lex>)
241 (ring-and-order (make-ring-and-order :ring ring :order order))
242 (quotients (cdr (string->poly "[x-y,0]" '(x y)))))
243 (is (equalp (multiple-value-list (normal-form ring-and-order f fl)) (list y-sq 1 2)))
244 (is (equalp (multiple-value-list (poly-pseudo-divide ring-and-order f fl))
245 (list quotients y-sq 1 2)))
246 (is-false (buchberger-criterion ring-and-order fl)))
247 (let* ((f (string->poly "x^2-4*y^2" '(x y)))
248 (g (string->poly "x+2*y" '(x y)))
249 (h (string->poly "x-2*y" '(x y)))
250 (ring *ring-of-integers*)
251 (order #'lex>)
252 (ring-and-order (make-ring-and-order :ring ring :order order)))
253 (is (poly-equal-no-sugar-p (poly-exact-divide ring-and-order f g) h))))
254
255
256(test buchberger
257 "Buchberger algorithm"
258 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
259 (ring *ring-of-integers*)
260 (order #'lex>)
261 (ring-and-order (make-ring-and-order :ring ring :order order))
262 (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y)))))
263 (is-true (grobner-test ring-and-order gb fl))
264 (is (every #'poly-equal-no-sugar-p (buchberger ring-and-order fl) gb))
265 (is (every #'poly-equal-no-sugar-p (parallel-buchberger ring-and-order fl) gb))))
266
267(test gebauer-moeller
268 "Gebauer-Moeller algorithm"
269 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
270 (ring *ring-of-integers*)
271 (order #'lex>)
272 (ring-and-order (make-ring-and-order :ring ring :order order))
273 (gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
274 (is-true (grobner-test ring-and-order gb fl))
275 (is (every #'poly-equal-no-sugar-p (gebauer-moeller ring-and-order fl) gb))))
276
277(test gb-postprocessing
278 "Grobner basis postprocessing"
279 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
280 (ring *ring-of-integers*)
281 (order #'lex>)
282 (ring-and-order (make-ring-and-order :ring ring :order order))
283 (gb (cdr (string->poly "[y,x-2*y]" '(x y))))
284 (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
285 (is-true (grobner-test ring-and-order gb fl))
286 (is (every #'poly-equal-no-sugar-p (reduction ring-and-order gb) reduced-gb)))
287 (let* ((gb (cdr (string->poly "[x,y,x-2*y,x^2]" '(x y))))
288 (minimal-gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
289 (is (equalp (minimization gb) minimal-gb))))
290
291(test grobner-wrap
292 "Grobner interface to many algorithms"
293 (let* (($poly_grobner_algorithm :buchberger)
294 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
295 (ring *ring-of-integers*)
296 (order #'lex>)
297 (ring-and-order (make-ring-and-order :ring ring :order order))
298 (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y))))
299 (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
300 (is-true (grobner-test ring-and-order gb fl))
301 (is (every #'poly-equal-no-sugar-p (grobner ring-and-order fl) gb))
302 (is (every #'poly-equal-no-sugar-p (reduced-grobner ring-and-order fl) reduced-gb))))
303
304
305(test elimination-ideal
306 "Elimination ideal"
307 (let* (($poly_grobner_algorithm :buchberger)
308 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
309 (ring *ring-of-integers*)
310 (order #'lex>)
311 (ring-and-order (make-ring-and-order :ring ring :order order))
312 (elim-1-fl (cdr (string->poly "[y]" '(x y)))))
313 (is (every #'poly-equal-no-sugar-p (elimination-ideal ring-and-order fl 1) elim-1-fl))
314 (is (every #'poly-equal-no-sugar-p (elimination-ideal ring-and-order fl 2) nil))))
315
316(test colon-ideal
317 "Colon ideal"
318 (let* (($poly_grobner_algorithm :buchberger)
319 (I (cdr (string->poly "[x^2*y,x*y^2]" '(x y))))
320 (J (cdr (string->poly "[x,y]" '(x y))))
321 (ring *ring-of-integers*)
322 (order #'lex>)
323 (ring-and-order (make-ring-and-order :ring ring :order order))
324 (I-colon-J (cdr (string->poly "[x*y]" '(x y)))))
325 (is (every #'poly-equal-no-sugar-p (colon-ideal ring-and-order I J) I-colon-J))))
326
327(test poly-lcm
328 "Polynomial LCM"
329 (let* (($poly_grobner_algorithm :buchberger)
330 (f (string->poly "x^2-y^2" '(x y)))
331 (g (string->poly "(x+y)^2" '(x y)))
332 (ring *ring-of-integers*)
333 (order #'lex>)
334 (ring-and-order (make-ring-and-order :ring ring :order order))
335 (lcm-f-and-g (string->poly "(x+y)^2*(x-y)" '(x y))))
336 (is (poly-equal-no-sugar-p (poly-lcm ring-and-order f g) lcm-f-and-g))))
337
338(test grobner-member
339 "Ideal membership"
340 (let* (($poly_grobner_algorithm :buchberger)
341 (f (string->poly "y" '(x y)))
342 (fl (cdr (string->poly "[x-y,x+y,y]" '(x y))))
343 (ring *ring-of-integers*)
344 (order #'lex>)
345 (ring-and-order (make-ring-and-order :ring ring :order order)))
346 (is-true (buchberger-criterion ring-and-order fl))
347 (is-true (grobner-member ring-and-order f fl))))
348
349(test grobner-equal
350 "Equality of ideal generated by Groebner bases"
351 (let* (($poly_grobner_algorithm :buchberger)
352 (fl (cdr (string->poly "[x,x-y,y]" '(x y))))
353 (gl (cdr (string->poly "[x-y,x+2*y,y]" '(x y))))
354 (ring *ring-of-integers*)
355 (order #'lex>)
356 (ring-and-order (make-ring-and-order :ring ring :order order)))
357 (is-true (buchberger-criterion ring-and-order fl))
358 (is-true (buchberger-criterion ring-and-order gl))
359 (is-true (grobner-equal ring-and-order fl gl))))
360
361(test ideal-saturation-1
362 "Calculate F : p^inf"
363 (let* (($poly_grobner_algorithm :buchberger)
364 (F (cdr (string->poly "[x^3,x^2*y]" '(x y))))
365 (p (string->poly "x^2" '(x y)))
366 (ring *ring-of-integers*)
367 (order #'lex>)
368 (ring-and-order (make-ring-and-order :ring ring :order order)))
369 (is-true (print (ideal-saturation-1 ring-and-order F p)))))
370
371
372(run! 'ngrobner-suite)
373(format t "All tests done!~%")
374
375
Note: See TracBrowser for help on using the repository browser.