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

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

* empty log message *

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