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

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

* empty log message *

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