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

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

* empty log message *

File size: 10.9 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[302]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
[309]22;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23;;
[355]24;; Run tests using 5am unit testing framework
[309]25;;
26;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
[342]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
[286]38(ql:quickload :fiveam)
[301]39
[292]40(load "ngrobner.asd")
[291]41(asdf:load-system :ngrobner)
[1326]42
[367]43(defpackage #:ngrobner-tests
[604]44 (:use :cl :it.bese.fiveam
45 :ngrobner :priority-queue :monomial
[957]46 :utils :order :ring :term :ring-and-order
[1069]47 :termlist :polynomial
[1024]48 :priority-queue
[1175]49 :division
[1364]50 :grobner-wrap
[604]51 )
52 )
[286]53
[1251]54(in-package :ngrobner-tests)
[287]55
[367]56(def-suite ngrobner-suite
[368]57 :description "New Groebner Package Suite")
[281]58
[367]59(in-suite ngrobner-suite)
[287]60
[312]61#+nil
[289]62(test dummy-test
[281]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"))
[289]70
[293]71(test makelist-1
[303]72 "makelist-1 test"
[597]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))))
[294]75
[303]76(test makelist
[314]77 "makelist"
[598]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))
[303]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)))))
[290]82
[1414]83(test summation
84 "summation"
85 (is (= (summation i (i 0 100)) 5050)))
86
[314]87(test monom
88 "monom"
[885]89 (is (every #'= (make-monom :dimension 3) '(0 0 0)) "Trivial monomial is a vector of 0's")
90 (is (every #'= (make-monom :initial-exponents '(1 2 3)) '(1 2 3)) "Monomial with powers 1,2,3")
[867]91 (let ((p (make-monom :initial-exponents '(1 2 3))))
[885]92 (is (every #'= (monom-map (lambda (x) x) p) '(1 2 3)))))
[867]93
[303]94
[347]95(test order
96 "order"
[948]97 (let ((p (make-monom :initial-exponents '(1 3 2)))
98 (q (make-monom :initial-exponents '(1 2 3))))
[600]99 (is-true (lex> p q))
100 (is-true (grlex> p q))
101 (is-true (revlex> p q))
102 (is-true (grevlex> p q))
[948]103 (is-false (invlex> p q))))
104
105(test elim-order
106 "elimination order"
107 (let* ((p (make-monom :initial-exponents '(1 2 3)))
108 (q (make-monom :initial-exponents '(4 5 6)))
109 (elim-order-factory (make-elimination-order-factory))
110 (elim-order-1 (funcall elim-order-factory 1))
111 (elim-order-2 (funcall elim-order-factory 2)))
[949]112 (is-false (funcall elim-order-1 p q))
113 (is-false (funcall elim-order-2 p q))))
[347]114
[381]115(test term
116 "term"
[855]117 (let* ((m1 (make-monom :initial-exponents '(1 2 3)))
118 (m2 (make-monom :initial-exponents '(3 5 2)))
[602]119 (m3 (monom-mul m1 m2))
120 (t1 (make-term m1 7))
121 (t2 (make-term m2 9))
122 (t3 (make-term m3 (* 7 9))))
123 (is (equalp (term-mul *ring-of-integers* t1 t2) t3))))
[381]124
[950]125(test termlist
126 "termlist"
[968]127 (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
[966]128 (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
[967]129 (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
[963]130 (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
131 (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
132 (p (list t2 t1))
133 (p-sq (list t22 t12 t11))
[956]134 (ring-and-order (make-ring-and-order))
135 (q (termlist-expt ring-and-order p 2)))
[969]136 (is-true (equalp q p-sq))))
[950]137
[970]138(test poly
139 "poly"
140 (let* ((t1 (make-term (make-monom :initial-exponents '(1 2 3)) 7))
141 (t2 (make-term (make-monom :initial-exponents '(3 5 2)) 9))
142 (t11 (make-term (make-monom :initial-exponents '(2 4 6)) 49))
143 (t12 (make-term (make-monom :initial-exponents '(4 7 5)) 126))
144 (t22 (make-term (make-monom :initial-exponents '(6 10 4)) 81))
145 (p (make-poly-from-termlist (list t2 t1)))
146 (p-sq (make-poly-from-termlist (list t22 t12 t11)))
147 (ring-and-order (make-ring-and-order))
[1027]148 (q (poly-expt ring-and-order p 2)))
[972]149 (is-true (equalp q p-sq))))
[950]150
151
[381]152(test coerce-to-infix
[582]153 "Conversion to infix form"
154 (is (equal
[605]155 (coerce-to-infix :term (make-term-variable *ring-of-integers* 5 3) '(x y z w u v))
[582]156 '(* 1 (EXPT X 0) (EXPT Y 0) (EXPT Z 0) (EXPT W 1) (EXPT U 0)))))
[381]157
[584]158(test priority-queue
159 "Priority queue"
[607]160 (let ((q (make-priority-queue)))
161 (priority-queue-insert q 7)
162 (priority-queue-insert q 8)
163 (is (= (priority-queue-size q) 3) "Note that there is always a dummy element in the queue.")
164 (is (equalp (priority-queue-heap q) #(0 7 8)))
165 (is (= (priority-queue-remove q) 7))
166 (is (= (priority-queue-remove q) 8))
[610]167 (is-true (priority-queue-empty-p q))
[613]168 (signals
169 (error "Empty queue.")
170 (priority-queue-remove q))))
[584]171
[634]172;;
173;; Currently parser cannot be tested, as it relies on many maxima functions
174;; to parse a polynomial expression.
175;;
[614]176#|
177(test parser
178 "Parser"
179 (let (($f '((MLIST SIMP) ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y)) ((MPLUS SIMP) $X $Y)))
[620]180 ($v '((MLIST SIMP) $X $Y)))
181 (is-true (parse-poly-list $f $v))))
[633]182|#
[614]183
[691]184(test infix-print
[694]185 "Infix printer"
[691]186 (is (string= (infix-print '(+ x y) nil) "X+Y"))
187 (is (string= (infix-print '(expt x 3) nil) "X^3"))
[693]188 (is (string= (infix-print '(+ 1 (expt x 3)) nil) "1+(X^3)"))
189 (is (string= (infix-print '(* x y) nil) "X*Y"))
190 (is (string= (infix-print '(* x (expt y 2)) nil) "X*(Y^2)")))
[832]191
192(test infix
193 "Infix parser"
[693]194 (is (equal '#I( x^2 + y^2 ) '(+ (expt x 2) (expt y 2))))
[831]195 (is (equal '#I( [ x, y ] ) '(:[ X Y)))
196 (is (equal '#I( x + y) '(+ x y)))
[864]197 (is (equal '#I( x^3 ) '(expt x 3)))
198 (is (equal '#I( 1 + x^3) '(+ 1 (expt x 3))))
199 (is (equal '#I( x * y^2 ) '(* x (expt y 2)))))
[691]200
[1070]201(test poly-reader
202 "Polynomial reader"
[1084]203 (is (equalp (with-input-from-string (s "X^2-Y^2+(-4/3)*U^2*W^3-5")
[1089]204 (read-infix-form :stream s))
[1083]205 '(+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))))
[1167]206 (is (equalp (string->alist "X^2-Y^2+(-4/3)*U^2*W^3-5" '(x y u w))
207 '(((2 0 0 0) . 1)
208 ((0 2 0 0) . -1)
209 ((0 0 2 3) . -4/3)
210 ((0 0 0 0) . -5))))
[1173]211 (is (equalp (string->alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w))
212 '(:[
213 (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5))
214 (((0 1 0 0) . 1)))))
[1099]215 (let ((p (make-poly-from-termlist (list (make-term (make-monom :initial-exponents '(2 0)) 1)
216 (make-term (make-monom :initial-exponents '(0 2)) 2)))))
[1101]217 (is (equalp (with-input-from-string (s "x^2+2*y^2")
218 (read-poly '(x y) :stream s))
219 p))
[1103]220 (is (equalp (string->poly "x^2+2*y^2" '(x y)) p))))
[1023]221
[1223]222;; Manual calculation supporting the test below.
223;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
224;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
225;; Next, X^2 - X*(X+Y) = -X*Y.
[1224]226;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
[1281]227;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
228;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
229;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
[1174]230(test division
231 "Division in polynomial ring"
[1183]232 (let* ((f (string->poly "x^2" '(x y)))
[1186]233 (y-sq (string->poly "y^2" '(x y)))
[1183]234 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
235 (ring *ring-of-integers*)
236 (order #'lex>)
[1194]237 (ring-and-order (make-ring-and-order :ring ring :order order))
238 (quotients (cdr (string->poly "[x-y,0]" '(x y)))))
[1250]239 (is (equalp (multiple-value-list (normal-form ring-and-order f fl)) (list y-sq 1 2)))
[1196]240 (is (equalp (multiple-value-list (poly-pseudo-divide ring-and-order f fl))
[1202]241 (list quotients y-sq 1 2)))
[1283]242 (is-false (buchberger-criterion ring-and-order fl)))
[1287]243 (let* ((f (string->poly "x^2-4*y^2" '(x y)))
244 (g (string->poly "x+2*y" '(x y)))
245 (h (string->poly "x-2*y" '(x y)))
[1286]246 (ring *ring-of-integers*)
247 (order #'lex>)
248 (ring-and-order (make-ring-and-order :ring ring :order order)))
[1285]249 (is (equalp (poly-exact-divide ring-and-order f g) h))))
[1174]250
[1303]251
252(test buchberger
253 "Buchberger algorithm"
254 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
255 (ring *ring-of-integers*)
256 (order #'lex>)
257 (ring-and-order (make-ring-and-order :ring ring :order order))
[1306]258 (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y)))))
[1324]259 (is-true (grobner-test ring-and-order gb fl))
[1311]260 (is (equalp (buchberger ring-and-order fl) gb))
261 (is (equalp (parallel-buchberger ring-and-order fl) gb))))
[1303]262
[1318]263(test gebauer-moeller
264 "Gebauer-Moeller algorithm"
265 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
266 (ring *ring-of-integers*)
267 (order #'lex>)
268 (ring-and-order (make-ring-and-order :ring ring :order order))
[1322]269 (gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
[1324]270 (is-true (grobner-test ring-and-order gb fl))
[1318]271 (is (equalp (gebauer-moeller ring-and-order fl) gb))))
[1303]272
[1336]273(test gb-postprocessing
[1338]274 "Grobner basis postprocessing"
[1336]275 (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
276 (ring *ring-of-integers*)
277 (order #'lex>)
278 (ring-and-order (make-ring-and-order :ring ring :order order))
[1337]279 (gb (cdr (string->poly "[y,x-2*y]" '(x y))))
[1339]280 (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
[1336]281 (is-true (grobner-test ring-and-order gb fl))
[1340]282 (is (equalp (reduction ring-and-order gb) reduced-gb)))
283 (let* ((gb (cdr (string->poly "[x,y,x-2*y,x^2]" '(x y))))
[1350]284 (minimal-gb (cdr (string->poly "[y,x-2*y]" '(x y)))))
[1340]285 (is (equalp (minimization gb) minimal-gb))))
[1303]286
[1365]287(test grobner-wrap
[1366]288 "Grobner interface to many algorithms"
[1368]289 (let* (($poly_grobner_algorithm :buchberger)
[1367]290 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
[1365]291 (ring *ring-of-integers*)
292 (order #'lex>)
293 (ring-and-order (make-ring-and-order :ring ring :order order))
[1371]294 (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y))))
[1372]295 (reduced-gb (cdr (string->poly "[y,x]" '(x y)))))
[1365]296 (is-true (grobner-test ring-and-order gb fl))
[1370]297 (is (equalp (grobner ring-and-order fl) gb))
298 (is (equalp (reduced-grobner ring-and-order fl) reduced-gb))))
[1336]299
[1365]300
[367]301(run! 'ngrobner-suite)
[346]302(format t "All tests done!~%")
[345]303
304
Note: See TracBrowser for help on using the repository browser.