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-monom.lisp@ 4167

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

* empty log message *

File size: 8.9 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;; Unless NGROBNER system loaded by ASDF,
41;; load the dependencies directly
42#-ngrobner
43(progn
44 (require :copy "copy")
45 (require :monom "monom"))
46
47(defpackage #:5am-monom
48 (:use :cl :it.bese.fiveam :monom :copy)
49 (:documentation "Monom and derived classes tests"))
50
51(in-package :5am-monom)
52
53(def-suite monom-suite
54 :description "Monom package suite")
55
56(in-suite monom-suite)
57
58(def-fixture monom-context ()
59 ;; Use SYMBOL-MACROLET not let to avoid 'unused variable' complaints
60 (symbol-macrolet
61 ((m (make-instance 'monom :exponents '(1 2 3)))
62 (n (make-instance 'monom :exponents '(4 5 6)))
63 (m*n (make-instance 'monom :exponents '(5 7 9)))
64 (n/m (make-instance 'monom :exponents '(3 3 3)))
65 (m-tensor-n (make-instance 'monom :exponents '(1 2 3 4 5 6))))
66 (&body)))
67
68(test monom-basics
69 "Monom basics"
70 (with-fixture monom-context ()
71 (is (= (monom-dimension m) 3))
72 (is (= (monom-elt m 2) 3))
73 (is (= (total-degree m) 6))
74 (is (= (sugar m) 6))
75 (is (equalp (->list (make-instance 'monom :dimension 3)) '(0 0 0)) "Trivial monomial is a vector of 0's")
76 (is (universal-equalp (multiply m n) m*n))
77 (is (universal-equalp (divide-by n m) n/m))
78 (is (universal-equalp (right-tensor-product-by m n) m-tensor-n))
79 (signals
80 (error "EXPONENTS must have length DIMENSION")
81 (make-instance 'monom :dimension 3 :exponents '(1 2 3 4 5 6)))
82 (is-true (divides-p m n))
83 (is-false (divides-p n m))
84 (is (universal-equalp (universal-gcd m n) m))
85 (is (universal-equalp (universal-lcm m n) n))
86 (is-true (depends-p m 0))
87 (signals
88 (error "Index out of bounds")
89 (depends-p m 3))
90 )
91 (with-fixture monom-context ()
92 (is (universal-equalp (multiply-by m n) m*n)))
93 (with-fixture monom-context ()
94 (is (universal-equalp (divide-by n m) n/m)))
95 (with-fixture monom-context ()
96 (is (equal (->list m) '(1 2 3)))))
97
98;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
99;;
100;; Order generics (LEX>, GRLEX>,...) tests
101;;
102;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103
104(def-fixture order-context ()
105 (symbol-macrolet
106 ((p (make-instance 'monom :exponents '(1 3 2)))
107 (q (make-instance 'monom :exponents '(1 2 3))))
108 (&body)))
109
110(test order
111 "order"
112 (with-fixture order-context ()
113 (is-true (lex> p q))
114 (is-true (grlex> p q))
115 (is-true (revlex> p q))
116 (is-true (grevlex> p q))
117 (is-false (invlex> p q))))
118
119(def-fixture elim-order-context ()
120 (let* ((p (make-instance 'monom :exponents '(1 2 3)))
121 (q (make-instance 'monom :exponents '(4 5 6)))
122 (elim-order-factory (make-elimination-order-factory))
123 (elim-order-1 (funcall elim-order-factory 1))
124 (elim-order-2 (funcall elim-order-factory 2)))
125 (&body)))
126
127
128(test elim-order
129 "Elimination order"
130 (with-fixture elim-order-context ()
131 (is-false (funcall elim-order-1 p q))
132 (is-false (funcall elim-order-2 p q))))
133
134;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135;;
136;; TERM class tests
137;;
138;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
139
140(def-fixture term-context ()
141 (symbol-macrolet
142 ((z (make-instance 'term :dimension 3 :coeff 5))
143 (m (make-instance 'term :dimension 3 :exponents '(1 2 3) :coeff 6))
144 (n (make-instance 'term :dimension 3 :exponents '(4 5 6) :coeff 12))
145 (m*n (make-instance 'term :dimension 3 :exponents '(5 7 9) :coeff 72))
146 (n/m (make-instance 'term :dimension 3 :exponents '(3 3 3) :coeff 2))
147 (m-tensor-n (make-instance 'term :exponents '(1 2 3 4 5 6) :coeff 72))
148 (m-uminus (make-instance 'term :dimension 3 :exponents '(1 2 3) :coeff -6)))
149 (&body)))
150
151(test term-basics
152 "Term basics"
153 (with-fixture term-context ()
154 (is (= (monom-dimension m) 3))
155 (is (= (monom-elt m 2) 3))
156 (is (= (total-degree m) 6))
157 (is (= (sugar m) 6))
158 (is (equalp (->list z) '((0 0 0) . 5)) "Trivial term is a vector of 0's")
159 (is (universal-equalp (multiply m n) m*n))
160 (is (universal-equalp (divide n m) n/m))
161 (is (universal-equalp (right-tensor-product-by m n) m-tensor-n))
162 (signals
163 (error "EXPONENTS must have length DIMENSION")
164 (make-instance 'term :dimension 3 :exponents '(1 2 3 4 5 6) :coeff 77))
165 (is-true (divides-p m n))
166 (is-false (divides-p n m))
167 (is (universal-equalp (universal-gcd m n) m))
168 (is (universal-equalp (universal-lcm m n) n))
169 (is-true (depends-p m 0))
170 (signals
171 (error "Index out of bounds")
172 (depends-p m 3))
173 )
174 (with-fixture term-context ()
175 (is (universal-equalp (multiply-by m n) m*n)))
176 (with-fixture term-context ()
177 (is (universal-equalp (divide-by n m) n/m)))
178 (with-fixture term-context ()
179 (is (universal-equalp (unary-minus m) m-uminus))))
180
181
182(def-fixture monom/term-conversion-context ()
183 (symbol-macrolet
184 ((term (make-instance 'term :exponents '(1 2 3) :coeff 4))
185 (monom (make-instance 'monom :exponents '(1 2 3)))
186 (promoted-monom (make-instance 'term :exponents '(1 2 3) :coeff 1)))
187 (&body)))
188
189(test monom/term-conversion
190 "Monom/term conversion"
191 (with-fixture monom/term-conversion-context ()
192 (is (universal-equalp (change-class term 'monom) monom)))
193 (with-fixture monom/term-conversion-context ()
194 (is (universal-equalp (change-class monom 'term) promoted-monom))))
195
196(test monom/term-copy
197 "Monom/term copy"
198 (with-fixture monom/term-conversion-context ()
199 (is (universal-equalp (copy-instance monom) monom))
200 (is (universal-equalp (copy-instance term) term))))
201
202(test term-tensor-product
203 "Term tensor product"
204 (let ((term1 (make-instance 'term :exponents '(1 2 3) :coeff 4))
205 (term2 (make-instance 'term :exponents '(4 5) :coeff 3))
206 (term1-left-tensor-by-term2 (make-instance 'term :exponents '(4 5 1 2 3) :coeff 12)))
207 (is (universal-equalp (left-tensor-product-by term1 term2) term1-left-tensor-by-term2))))
208
209(test term-contract
210 "Term contract"
211 (let ((term (make-instance 'term :exponents '(1 2 3) :coeff 4))
212 (term-contracted (make-instance 'term :exponents '(2 3) :coeff 4)))
213 (is (universal-equalp (left-contract term 1) term-contracted))))
214
215(test sexp-conversion
216 "Sexp conversion"
217 (is (equal (->sexp (make-instance 'term :exponents '(0 0 0 0) :coeff -5) '(x y u v)) -5))
218 (signals (error "Variables NIL and exponents #(0 0 0 0) must have the same length.")
219 (->sexp (make-instance 'monom :exponents '(0 0 0 0))))
220 (is (equal (->sexp (make-instance 'monom :exponents '(0 0 0 0)) '(x y u w)) 1))
221 (is (equal (->sexp (make-instance 'monom :exponents '(0 0 1 0)) '(x y u w)) 'u))
222 (is (equalp (->sexp (make-instance 'monom :exponents '(1 2 1 1)) '(x y u w))
223 '(* X (EXPT Y 2) U W)))
224 (is (equal (->sexp (make-instance 'term :exponents '(0 1 0 0) :coeff -5) '(x y u v))
225 '(* -5 Y)))
226 (is (equal (->sexp (make-instance 'term :exponents '(1 1 0 0) :coeff -5) '(x y u v))
227 '(* -5 X Y)))
228 (is (equal (->sexp (make-instance 'term :exponents '(1 0 0 0) :coeff -5) '(x y u v))
229 '(* -5 X)))
230 (is (equal (->sexp (make-instance 'term :exponents '(11 0 0 0) :coeff -5) '(x y u v))
231 '(* -5 (EXPT X 11))))
232 (is (equal (->sexp (make-instance 'term :exponents '(11 0 0 0) :coeff 5) '(x y u v))
233 '(* 5 (EXPT X 11))))
234 (is (equal (->sexp (make-instance 'term :exponents '(11 0 4 0) :coeff 5) '(x y u v))
235 '(* 5 (EXPT X 11) (EXPT U 4))))
236)
237
238(run! 'monom-suite)
239(format t "All tests done!~%")
240
241
Note: See TracBrowser for help on using the repository browser.