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-division.lisp@ 4440

Last change on this file since 4440 was 4438, checked in by Marek Rychlik, 8 years ago

* empty log message *

File size: 8.5 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(defpackage #:5am-division
41 (:use :cl :it.bese.fiveam :copy :monom :polynomial :infix :symbolic-polynomial :division :ring))
42
43(in-package :5am-division)
44
45(def-suite division-suite
46 :description "Division algorithm suite")
47
48(in-suite division-suite)
49
50;; Manual calculation supporting the test below.
51;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
52;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
53;; Next, X^2 - X*(X+Y) = -X*Y.
54;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
55;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
56;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
57;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
58
59(def-fixture division-context ()
60 (let* ((f (string->poly "x^2" '(x y)))
61 (y-sq (string->poly "y^2" '(x y)))
62 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
63 (quotients (cdr (string->poly "[x-y,0]" '(x y))))
64 (one (make-instance 'rational-field :value 1)))
65 (&body)))
66
67(test normal-form
68 "Normal form"
69 (with-fixture division-context ()
70 (is (universal-equalp (multiple-value-list (normal-form f fl)) (list y-sq one 2)))
71 (is (universal-equalp (multiple-value-list (poly-pseudo-divide f fl)) (list quotients y-sq one 2)))
72 (is-false (buchberger-criterion fl))
73 )
74 )
75
76;; Maxima
77;;poly_normal_form(3*x^3*y+2*z, [x^2*y+x,x-y^2-z^3],[x,y,z]);
78;;Result: (-3*z^6)-6*y^2*z^3+2*z-3*y^4
79(test normal-form-simple
80 (let ((vars '(x y z)))
81 (is (universal-equalp (normal-form (string->poly "3*x^3*y+2*z" vars)
82 (cdr (string->poly "[x^2*y+x,x-y^2-z^3]" vars)))
83 (string->poly "(-3*z^6)-6*y^2*z^3+2*z-3*y^4" vars)))))
84
85(test normal-form-easy
86 "Easy normal form tests"
87 (is (universal-zerop (normal-form (string->poly "0" '(x y)) (cdr (string->poly "[x,y]" '(x y))))))
88 ;; Maxima equivalent: poly_normal_form(3*x^2*y-x*y-1,[x-y,x+y],[x,y]);
89 (is (universal-equalp (normal-form (string->poly "3*x^2*y-x*y-1" '(x y)) (cdr (string->poly "[x-y,x+y]" '(x y))))
90 (string->poly "3*y^3-y^2-1" '(x y))))
91 ;; Maxima equivalent: poly_normal_form(3*x^2*y*z-x*y^3-1,[x^2-2*y,x+y*z],[x,y,z]);
92 (is (universal-equalp (normal-form (string->poly "3*x^2*y*z-x*y^3-1" '(x y z))
93 (cdr (string->poly "[x^2-2*y,x+y*z]" '(x y z))))
94 (string->poly "y^4*z+6*y^2*z-1" '(x y z)))))
95
96(def-fixture exact-division-context ()
97 (let* ((f (string->poly "x^2-4*y^2" '(x y)))
98 (g (string->poly "x+2*y" '(x y)))
99 (h (string->poly "x-2*y" '(x y))))
100 (&body)))
101
102(test exact-division
103 "Exact division in polynomial ring"
104 (with-fixture exact-division-context ()
105 (is (universal-equalp (poly-exact-divide f g) h))
106 (is (universal-zerop (subtract-from (poly-exact-divide f g) h)))))
107
108
109;; Check if a set of generators satisfies the Buchberger criterion
110;; The example is the Enneper surface ideal. Run this in Maxima to
111;; obtain the Grobner basis:
112;; poly_grobner([x-3*u-3*u*v^2+u^3,y-3*v-3*u^2*v+v^3,z-3*u^2+3*v^2],[u,v,x,y,z]);
113(def-fixture buchberger-criterion-context ()
114 (let ((fl (cdr (string->poly "[x-3*u-3*u*v^2+u^3,y-3*v-3*u^2*v+v^3,z-3*u^2+3*v^2]" '(u v x y z))))
115 (gb (cdr (string->poly "[x-3*u*v^2+u^3-3*u,y+v^3-3*u^2*v-3*v,z+3*v^2-3*u^2,
116 (-u*z)-3*x+6*u*v^2+9*u,(-v*z)+y-2*v^3-3*v,z^2+6*v^2*z-9*z-9*v*y+9*u*x,
117 4*u*v*z-3*u*y+3*v*x,2*u*z^2+6*x*z-18*u*z-9*u*v*y+9*v^2*x,
118 (-8*u*z^3)-24*x*z^2+72*u*z^2-36*v^2*x*z+27*u*y^2-27*v*x*y,
119 z^3+18*v^2*z^2-18*z^2-54*v*y*z+54*v^2*z+81*z+27*y^2-27*x^2,
120 (-4*z^4)+48*z^3-108*v*y*z^2+108*z^2+135*y^2*z+324*v*y*z+108*x^2*z
121 -1296*v^2*z-1944*z-243*v^2*y^2-648*y^2+243*v^2*x^2+648*x^2,
122 8*v*z^3-9*y*z^2+72*v*z^2+54*v^2*y*z-27*y*z-27*v*y^2+27*v*x^2,
123 (-8*v*z^4)+12*y*z^3-96*v*z^3-216*v*z^2-135*v*y^2*z+324*y*z-27*v*x^2*z
124 +81*y^3+81*v*y^2-81*x^2*y-81*v*x^2,
125 (-64*v*z^6)+120*y*z^5-1152*v*z^5+288*y*z^4-5184*v*z^4-648*v*y^2*z^3
126 -216*y*z^3+6912*v*z^3+81*y^3*z^2-9720*v*y^2*z^2
127 -1539*x^2*y*z^2+31104*y*z^2+62208*v*z^2+8505*y^3*z
128 +46656*v*y^2*z-8505*x^2*y*z-93312*y*z+729*v*y^4-23328*y^3
129 -1458*v*x^2*y^2-23328*v*y^2+23328*x^2*y+729*v*x^4
130 +23328*v*x^2,
131 8*z^6-72*z^5+648*v*y*z^4-648*z^4-945*y^2*z^3+5184*v*y*z^3-189*x^2*z^3
132 +5832*z^3+972*y^2*z^2+17496*v*y*z^2-2430*x^2*z^2+8748*v*y^3*z
133 -19683*y^2*z+2187*x^2*z-5103*y^4-4374*v*y^3+5832*x^2*y^2
134 +4374*v*x^2*y-729*x^4,
135 8*z^7-48*z^6+648*v*y*z^5-864*z^5-945*y^2*z^4+5832*v*y*z^4-189*x^2*z^4
136 +3888*z^4+81*y^2*z^3+17496*v*y*z^3-2997*x^2*z^3+17496*z^3
137 +8748*v*y^3*z^2-16767*y^2*z^2+17496*v*y*z^2-5103*x^2*z^2
138 -5103*y^4*z+5832*x^2*y^2*z-6561*y^2*z-729*x^4*z+6561*x^2*z
139 -2187*y^4+4374*x^2*y^2-2187*x^4,
140 64*z^9-10368*z^7+1296*y^2*z^6-1296*x^2*z^6-34992*y^2*z^5-34992*x^2*z^5
141 +419904*z^5+174960*y^2*z^4-174960*x^2*z^4-10935*y^4*z^3
142 -56862*x^2*y^2*z^3+314928*y^2*z^3-10935*x^4*z^3+314928*x^2*z^3
143 +118098*y^4*z^2-118098*x^4*z^2+59049*y^4*z-118098*x^2*y^2*z
144 +59049*x^4*z+19683*y^6-59049*x^2*y^4+59049*x^4*y^2-19683*x^6]" '(u v x y z)))))
145 (&body)))
146
147
148(test buchberger-containment
149 "Check ideal containment"
150 (with-fixture buchberger-criterion-context
151 ()
152 (let ((fl-copy (mapcar #'copy-instance fl))
153 (gb-copy (mapcar #'copy-instance gb)))
154 (loop
155 for i from 0 below (length fl)
156 do
157 (is (universal-zerop (normal-form (elt fl i) gb))
158 "Failed with I=~S~%" I))
159 ;; GB should not change in the process
160 (is (universal-equalp gb gb-copy))
161 ;; FL should not change either
162 (is (universal-equalp fl fl-copy)))))
163
164(test buchberger-criterion-with-normal-form
165 "Buchberger criterion using normal form"
166 (with-fixture buchberger-criterion-context
167 ()
168 (let ((fl-copy (mapcar #'copy-instance fl))
169 (gb-copy (mapcar #'copy-instance gb)))
170 (loop
171 for i from 0 below (length gb)
172 do
173 (loop
174 for j from (1+ i) below (length gb)
175 do
176 (is (universal-zerop (normal-form (s-polynomial (elt gb i) (elt gb j)) gb))
177 "Failed with I=~S, J=~S~%" I J)))
178 (is (universal-equalp fl fl-copy))
179 (is (universal-equalp gb gb-copy)))))
180
181(test buchberger-criterion-with-pseudo-division
182 "Buchberger criterion using pseudo-division"
183 (with-fixture buchberger-criterion-context ()
184 (let ((fl-copy (mapcar #'copy-instance fl))
185 (gb-copy (mapcar #'copy-instance gb)))
186 (loop
187 for i from 0 below (length gb)
188 do
189 (loop
190 for j from (1+ i) below (length gb)
191 do
192 (is (universal-zerop (second (multiple-value-list (poly-pseudo-divide (s-polynomial (elt gb i) (elt gb j)) gb))))
193 "Failed with I=~S, J=~S~%" I J)))
194 (is (universal-equalp fl fl-copy))
195 (is (universal-equalp gb gb-copy)))))
196
197(test buchberger-criterion-with-grobner-test
198 "Buchberger criterion using grobner-test"
199 (with-fixture buchberger-criterion-context ()
200 (is-true (grobner-test gb fl))
201 )
202 )
203
204
205(run! 'division-suite)
206(format t "All tests done!~%")
207
208
Note: See TracBrowser for help on using the repository browser.