;;; -*- Mode: Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik ;;; ;;; This program is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;; ;;; This program is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with this program; if not, write to the Free Software ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Run tests using 5am unit testing framework ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; We assume that QuickLisp package manager is installed. ;; See : ;; https://www.quicklisp.org/beta/ ;; ;; The following is unnecessary after running: ;; * (ql:add-to-init-file) ;; at lisp prompt: ;;(load "~/quicklisp/setup") (ql:quickload :fiveam) (load "ngrobner.asd") (asdf:load-system :ngrobner) (defpackage #:ngrobner-tests (:use :cl :it.bese.fiveam :ngrobner :priority-queue :monom :utils :order :ring :term :ring-and-order :termlist :polynomial :priority-queue :division :grobner-wrap ) ) (in-package :ngrobner-tests) (def-suite ngrobner-suite :description "New Groebner Package Suite") (in-suite ngrobner-suite) #+nil (test dummy-test "Makelist" (is (= (+ 2 2)) "2 plus 2 wasn't equal to 4 (using #'= to test equality)") (is (= 0 (+ -1 1))) (signals (error "Trying to add 4 to FOO didn't signal an error") (+ 'foo 4)) (is (= 0 (+ 1 1)) "this should have failed")) (test makelist-1 "makelist-1 test" (is (equal (makelist-1 (* 2 i) i 0 10) '(0 2 4 6 8 10 12 14 16 18 20))) (is (equal (makelist-1 (* 2 i) i 0 10 3) '(0 6 12 18)))) (test makelist "makelist" (is (equal (makelist (+ (* i i) (* j j)) (i 1 4) (j 1 i)) '(2 5 8 10 13 18 17 20 25 32))) (is (equal (makelist (list i j '---> (+ (* i i) (* j j))) (i 1 4) (j 1 i)) '((1 1 ---> 2) (2 1 ---> 5) (2 2 ---> 8) (3 1 ---> 10) (3 2 ---> 13) (3 3 ---> 18) (4 1 ---> 17) (4 2 ---> 20) (4 3 ---> 25) (4 4 ---> 32))))) (test summation "summation" (is (= (summation i (i 0 100)) 5050))) (test inner-product "summation" (is (= (inner-product '(1 2 3) '(4 5 6)) 32))) (test monom "monom" (is (every #'= (make-monom :dimension 3) '(0 0 0)) "Trivial monomial is a vector of 0's") (is (every #'= (make-monom :initial-exponents '(1 2 3)) '(1 2 3)) "Monomial with powers 1,2,3") (let ((p (make-monom :initial-exponents '(1 2 3)))) (is (every #'= (monom-map (lambda (x) x) p) '(1 2 3))))) (test order "order" (let ((p (make-monom :initial-exponents '(1 3 2))) (q (make-monom :initial-exponents '(1 2 3)))) (is-true (lex> p q)) (is-true (grlex> p q)) (is-true (revlex> p q)) (is-true (grevlex> p q)) (is-false (invlex> p q)))) (test elim-order "elimination order" (let* ((p (make-monom :initial-exponents '(1 2 3))) (q (make-monom :initial-exponents '(4 5 6))) (elim-order-factory (make-elimination-order-factory)) (elim-order-1 (funcall elim-order-factory 1)) (elim-order-2 (funcall elim-order-factory 2))) (is-false (funcall elim-order-1 p q)) (is-false (funcall elim-order-2 p q)))) (test term "term" (let* ((m1 (make-monom :initial-exponents '(1 2 3))) (m2 (make-monom :initial-exponents '(3 5 2))) (m3 (monom-mul m1 m2)) (t1 (make-term :monom m1 :coeff 7)) (t2 (make-term :monom m2 :coeff 9)) (t3 (make-term :monom m3 :coeff (* 7 9)))) (is (equalp (term-mul +ring-of-integers+ t1 t2) t3)))) (test termlist "termlist" (let* ((t1 (make-term :monom (make-monom :initial-exponents '(1 2 3)) :coeff 7)) (t2 (make-term :monom (make-monom :initial-exponents '(3 5 2)) :coeff 9)) (t11 (make-term :monom (make-monom :initial-exponents '(2 4 6)) :coeff 49)) (t12 (make-term :monom (make-monom :initial-exponents '(4 7 5)) :coeff 126)) (t22 (make-term :monom (make-monom :initial-exponents '(6 10 4)) :coeff 81)) (p (list t2 t1)) (p-sq (list t22 t12 t11)) (ring-and-order (make-ring-and-order)) (q (termlist-expt ring-and-order p 2))) (is-true (equalp q p-sq)))) (test poly "poly" (let* ((t1 (make-term :monom (make-monom :initial-exponents '(1 2 3)) :coeff 7)) (t2 (make-term :monom (make-monom :initial-exponents '(3 5 2)) :coeff 9)) (t11 (make-term :monom (make-monom :initial-exponents '(2 4 6)) :coeff 49)) (t12 (make-term :monom (make-monom :initial-exponents '(4 7 5)) :coeff 126)) (t22 (make-term :monom (make-monom :initial-exponents '(6 10 4)) :coeff 81)) (p (make-poly-from-termlist (list t2 t1))) (p-sq (make-poly-from-termlist (list t22 t12 t11))) (ring-and-order (make-ring-and-order)) (q (poly-expt ring-and-order p 2))) (is-true (equalp q p-sq)))) (test coerce-to-infix "Conversion to infix form" (is (equal (coerce-to-infix :term (make-term-variable +ring-of-integers+ 5 3) '(x y z w u v)) '(* 1 (EXPT X 0) (EXPT Y 0) (EXPT Z 0) (EXPT W 1) (EXPT U 0))))) (test priority-queue "Priority queue" (let ((q (make-priority-queue))) (priority-queue-insert q 7) (priority-queue-insert q 8) (is (= (priority-queue-size q) 3) "Note that there is always a dummy element in the queue.") (is (equalp (priority-queue-heap q) #(0 7 8))) (is (= (priority-queue-remove q) 7)) (is (= (priority-queue-remove q) 8)) (is-true (priority-queue-empty-p q)) (signals (error "Empty queue.") (priority-queue-remove q)))) ;; ;; Currently parser cannot be tested, as it relies on many maxima functions ;; to parse a polynomial expression. ;; #| (test parser "Parser" (let (($f '((MLIST SIMP) ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y)) ((MPLUS SIMP) $X $Y))) ($v '((MLIST SIMP) $X $Y))) (is-true (parse-poly-list $f $v)))) |# (test infix-print "Infix printer" (is (string= (infix-print '(+ x y) nil) "X+Y")) (is (string= (infix-print '(expt x 3) nil) "X^3")) (is (string= (infix-print '(+ 1 (expt x 3)) nil) "1+(X^3)")) (is (string= (infix-print '(* x y) nil) "X*Y")) (is (string= (infix-print '(* x (expt y 2)) nil) "X*(Y^2)"))) (test infix "Infix parser" (is (equal '#I( x^2 + y^2 ) '(+ (expt x 2) (expt y 2)))) (is (equal '#I( [ x, y ] ) '(:[ X Y))) (is (equal '#I( x + y) '(+ x y))) (is (equal '#I( x^3 ) '(expt x 3))) (is (equal '#I( 1 + x^3) '(+ 1 (expt x 3)))) (is (equal '#I( x * y^2 ) '(* x (expt y 2))))) (test poly-reader "Polynomial reader" (is (equalp (with-input-from-string (s "X^2-Y^2+(-4/3)*U^2*W^3-5") (read-infix-form :stream s)) '(+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5)))) (is (equalp (string->alist "X^2-Y^2+(-4/3)*U^2*W^3-5" '(x y u w)) '(((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5)))) (is (equalp (string->alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w)) '(:[ (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5)) (((0 1 0 0) . 1))))) (let ((p (make-poly-from-termlist (list (make-term :monom (make-monom :initial-exponents '(2 0)) :coeff 1) (make-term :monom (make-monom :initial-exponents '(0 2)) :coeff 2))))) (is (equalp (with-input-from-string (s "x^2+2*y^2") (read-poly '(x y) :stream s)) p)) (is (equalp (string->poly "x^2+2*y^2" '(x y)) p)))) ;; Manual calculation supporting the test below. ;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order. ;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X. ;; Next, X^2 - X*(X+Y) = -X*Y. ;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y. ;; Next, -X*Y-(-Y)*(X+Y) = Y^2. ;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division ;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2 (test division "Division in polynomial ring" (let* ((f (string->poly "x^2" '(x y))) (y-sq (string->poly "y^2" '(x y))) (fl (cdr (string->poly "[x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (quotients (cdr (string->poly "[x-y,0]" '(x y))))) (is (equalp (multiple-value-list (normal-form ring-and-order f fl)) (list y-sq 1 2))) (is (equalp (multiple-value-list (poly-pseudo-divide ring-and-order f fl)) (list quotients y-sq 1 2))) (is-false (buchberger-criterion ring-and-order fl))) (let* ((f (string->poly "x^2-4*y^2" '(x y))) (g (string->poly "x+2*y" '(x y))) (h (string->poly "x-2*y" '(x y))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order))) (is (poly-equal-no-sugar-p (poly-exact-divide ring-and-order f g) h)))) (test buchberger "Buchberger algorithm" (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y))))) (is-true (grobner-test ring-and-order gb fl)) (is (every #'poly-equal-no-sugar-p (buchberger ring-and-order fl) gb)) (is (every #'poly-equal-no-sugar-p (parallel-buchberger ring-and-order fl) gb)))) (test gebauer-moeller "Gebauer-Moeller algorithm" (let* ((fl (cdr (string->poly "[x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (gb (cdr (string->poly "[y,x-2*y]" '(x y))))) (is-true (grobner-test ring-and-order gb fl)) (is (every #'poly-equal-no-sugar-p (gebauer-moeller ring-and-order fl) gb)))) (test reduction "Reduction algorithm" (let* ((fl (cdr (string->poly "[x^2,x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (gb (cdr (string->poly "[y,x-2*y]" '(x y)))) (reduced-gb (cdr (string->poly "[y,x]" '(x y))))) (is-true (grobner-test ring-and-order gb fl)) (is (poly-set-equal-no-sugar-p (reduction ring-and-order gb) reduced-gb)))) (test minimization "Minimization algorithm" (let* ((gb (cdr (string->poly "[x,y,x-2*y,x^2]" '(x y)))) (minimal-gb (cdr (string->poly "[y,x-2*y]" '(x y))))) (is (equalp (minimization gb) minimal-gb)))) (test grobner-wrap "Grobner interface to many algorithms" (let* (($poly_grobner_algorithm :buchberger) (fl (cdr (string->poly "[x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (gb (cdr (string->poly "[x+y,x-2*y,y]" '(x y)))) (reduced-gb (cdr (string->poly "[y,x]" '(x y))))) (is-true (grobner-test ring-and-order gb fl)) (is (poly-set-equal-no-sugar-p (grobner ring-and-order fl) gb)) (is (poly-set-equal-no-sugar-p (reduced-grobner ring-and-order fl) reduced-gb)))) (test elimination-ideal "Elimination ideal" (let* (($poly_grobner_algorithm :buchberger) (fl (cdr (string->poly "[x+y,x-2*y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (elim-1-fl (cdr (string->poly "[y]" '(x y))))) (is (poly-set-equal-no-sugar-p (elimination-ideal ring-and-order fl 1) elim-1-fl)) (is (null (elimination-ideal ring-and-order fl 2))))) (test colon-ideal "Colon ideal" (let* (($poly_grobner_algorithm :buchberger) (I (cdr (string->poly "[x^2*y,x*y^2]" '(x y)))) (J (cdr (string->poly "[x,y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (I-colon-J (cdr (string->poly "[x*y]" '(x y))))) (is (poly-set-equal-no-sugar-p (colon-ideal ring-and-order I J) I-colon-J)))) (test poly-lcm "Polynomial LCM" (let* (($poly_grobner_algorithm :buchberger) (f (string->poly "x^2-y^2" '(x y))) (g (string->poly "(x+y)^2" '(x y))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (lcm-f-and-g (string->poly "(x+y)^2*(x-y)" '(x y)))) (is (poly-equal-no-sugar-p (poly-lcm ring-and-order f g) lcm-f-and-g)))) (test grobner-member "Ideal membership" (let* (($poly_grobner_algorithm :buchberger) (f (string->poly "y" '(x y))) (fl (cdr (string->poly "[x-y,x+y,y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order))) (is-true (buchberger-criterion ring-and-order fl)) (is-true (grobner-member ring-and-order f fl)))) (test grobner-equal "Equality of ideal generated by Groebner bases" (let* (($poly_grobner_algorithm :buchberger) (fl (cdr (string->poly "[x,x-y,y]" '(x y)))) (gl (cdr (string->poly "[x-y,x+2*y,y]" '(x y)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order))) (is-true (buchberger-criterion ring-and-order fl)) (is-true (buchberger-criterion ring-and-order gl)) (is-true (grobner-equal ring-and-order fl gl)))) ;; Calculates [F, U*P-1] (test saturation-extension-1 "Saturation extension with 1 polynomial" (let* ((F-str "[x^3,x^2*y]") (F (cdr (string->poly F-str '(x y)))) (P (string->poly "x^2" '(x y))) (ring +ring-of-integers+) (F-sat (append (cdr (string->poly F-str '(u x y))) (cdr (string->poly "[u*x^2-1]" '(u x y)))))) (is (poly-set-equal-no-sugar-p (saturation-extension-1 ring F p) F-sat)))) ;; Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F. (test polysaturation-extension "Polysaturation extension" (let* ((F-str "[x^3,x^2*y]") (F (cdr (string->poly F-str '(x y)))) (P (cdr (string->poly "[x^2,x*y]" '(x y)))) (ring +ring-of-integers+) (F-sat (append (cdr (string->poly F-str '(u1 u2 x y))) (cdr (string->poly "[u1*(x^2) + u2*(x*y)-1]" '(u1 u2 x y)))))) (is (poly-set-equal-no-sugar-p (polysaturation-extension ring F P) F-sat)))) ;; Calculate F : p^inf (test ideal-saturation-1 "Ideal saturation with 1 polynomial" (let* (($poly_grobner_algorithm :buchberger) (F (cdr (string->poly "[x^3*(y+z^2),x^2*(y-z^2)]" '(x y z)))) (p (string->poly "x" '(x y z))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (G (cdr (string->poly "[y,z^2]" '(x y z))))) (is (poly-set-equal-no-sugar-p (ideal-saturation-1 ring-and-order F p) G)))) ;; Calculate F : p1^inf : p2^inf : ... : ps^inf (test ideal-polysaturation-1 "Ideal polysaturation one-by-one with 2 polynomials" (let* (($poly_grobner_algorithm :buchberger) (F (cdr (string->poly "[x^3*z*y,x*z*y^2]" '(x y z)))) (P (cdr (string->poly "[x,z]" '(x y z)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (G (cdr (string->poly "[y]" '(x y z))))) (is (poly-set-equal-no-sugar-p (ideal-polysaturation-1 ring-and-order F p) G)))) ;; Calculate F : P^inf (test ideal-saturation "Ideal saturation" (let* (($poly_grobner_algorithm :buchberger) (F (cdr (string->poly "[x^3*(y+z^2),x^2*(y-z^2)]" '(x y z)))) (P (cdr (string->poly "[x]" '(x y z)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (G (cdr (string->poly "[y,z^2]" '(x y z))))) (is (poly-set-equal-no-sugar-p (ideal-saturation ring-and-order F P) G)))) ;; Calculate F : P1^inf : P2^inf : ... : Ps^inf where Pi are ideals (test ideal-polysaturation "Ideal polysaturation" (let* (($poly_grobner_algorithm :buchberger) (F (cdr (string->poly "[x^3*z*y,x*z*y^2]" '(x y z)))) (P1 (cdr (string->poly "[x]" '(x y z)))) (P2 (cdr (string->poly "[z]" '(x y z)))) (ring +ring-of-integers+) (order #'lex>) (ring-and-order (make-ring-and-order :ring ring :order order)) (G (cdr (string->poly "[y]" '(x y z))))) (is (poly-set-equal-no-sugar-p (ideal-polysaturation ring-and-order F (list P1 P2)) G)))) (run! 'ngrobner-suite) (format t "All tests done!~%")