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