must export in order to test.
[CommonLispStat.git] / unittests-arrays.lisp
blob126798840042294929246ddabfadcd3631dc018c
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;; This is semi-external to lispstat core packages. The dependency
7 ;;; should be that lispstat packages are dependencies for the unit
8 ;;; tests. However, where they will end up is still to be
9 ;;; determined.
11 (in-package :cl-user)
13 (defpackage :lisp-stat-unittests-arrays
14 (:use :common-lisp :lift
15 ;; use feature tests to check for matrix packages, i.e.
16 #+matlisp :matlisp
17 #+clem :clem
18 ;; and likewise, handle the tests appropriately as well.
19 :lisp-stat ;; basic tools
20 :lisp-stat-unittests) ;; support
21 (:shadowing-import-from :lisp-stat
22 slot-value call-method call-next-method ;; objects
23 expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan ;; lsmath
24 asin acos atan sinh cosh tanh asinh acosh atanh float random
25 truncate floor ceiling round minusp zerop plusp evenp oddp
26 < <= = /= >= > ;; complex
27 conjugate realpart imagpart phase
28 min max logand logior logxor lognot ffloor fceiling
29 ftruncate fround signum cis)
30 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard))
32 (in-package :lisp-stat-unittests-arrays)
34 ;;; TESTS
36 (defun run-lisp-stat-tests ()
37 (run-tests :suite 'lisp-stat))
39 (defun run-lisp-stat-test (&rest x)
40 (run-test x))
42 (deftestsuite lisp-stat-array (lisp-stat) ())
46 (addtest (lisp-stat-array) cholesky-decomposition-1
47 (ensure-same
48 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
49 (list #2A((1.7888543819998317 0.0 0.0)
50 (1.6770509831248424 0.11180339887498929 0.0)
51 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
52 5.000000000000003)
53 :test 'almost=lists))
56 (addtest (lisp-stat-array) cholesky-decomposition-2
57 (ensure-same
58 (matlisp:chol)))
63 (addtest (lisp-stat-array) lu-decomposition
64 (ensure-same
65 (lu-decomp #2A((2 3 4)
66 (1 2 4)
67 (2 4 5)))
68 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
69 #(0 2 2)
70 -1.0
71 NIL)))
73 (addtest (lisp-stat-array) lu-decomposition-2
74 (ensure-same
75 (lu-decomp (make-real-matrix #2A((2 3 4)
76 (1 2 4)
77 (2 4 5))))
78 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
79 #(0 2 2)
80 -1.0
81 NIL)))
85 (addtest (lisp-stat-array) rcondest
86 ;; (ensure-same
87 (ensure-error ;; it barfs, FIXME!!
88 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
89 6.8157451e7
90 :test 'almost=))
92 (addtest (lisp-stat-array) lu-solve
93 (ensure-same
94 (lu-solve
95 (lu-decomp
96 #2A((2 3 4) (1 2 4) (2 4 5)))
97 #(2 3 4))
98 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
100 (addtest (lisp-stat-array) inverse
101 (ensure-same
102 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
103 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
104 (-1.0 -0.6666666666666666 1.3333333333333333)
105 (0.0 0.6666666666666666 -0.3333333333333333))))
107 (addtest (lisp-stat-array) sv-decomp
108 (ensure-same
109 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
110 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
111 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
112 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
113 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
114 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
115 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
116 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
118 :test 'almost=lists))
120 (addtest (lisp-stat-array) qr-decomp
121 (ensure-same
122 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
123 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
124 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
125 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
126 #2A((-3.0 -5.333333333333334 -7.333333333333332)
127 (0.0 -0.7453559924999292 -1.1925695879998877)
128 (0.0 0.0 -1.3416407864998738)))
129 :test 'almost=lists))
131 (addtest (lisp-stat-array) eigen
132 (ensure-same
133 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
134 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
135 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
136 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
137 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
138 NIL)))
140 (addtest (lisp-stat-array) spline
141 (ensure-same
142 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
143 #(1.2 2.0 2.1 2.0 1.1 2.8)
144 :xvals 6)
145 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
146 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
147 :test 'almost=lists))
149 (addtest (lisp-stat-array) kernel-smooth
150 (ensure-same
151 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
152 (kernel-smooth
153 #(1.0 1.2 1.3 1.8 2.1 2.5)
154 #(1.2 2.0 2.1 2.0 1.1 2.8)
155 :xvals 5)
156 (list (list 1.0 1.375 1.75 2.125 2.5)
157 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
158 1.5871511322219498 2.518194783156392))
159 :test 'almost=lists))
161 (addtest (lisp-stat-array) kernel-dens
162 (ensure-same
163 (kernel-dens
164 #(1.0 1.2 2.5 2.1 1.8 1.2)
165 :xvals 5)
166 (list (list 1.0 1.375 1.75 2.125 2.5)
167 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
168 0.4829822708587095 0.3485939156929503))))
171 (addtest (lisp-stat-array) fft
172 (ensure-same
173 (fft #(1.0 1.2 2.5 2.1 1.8))
174 (list #(#C(1.0 0.0) #C(1.2 0.0) #C(2.5 0.0) #C(2.1 0.0) #C(1.8 0.0)))
175 :test 'almost=lists))
178 (addtest (lisp-stat-array) lowess
179 (ensure-same
180 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
181 #(1.2 2.0 2.1 2.0 1.1 2.8))
182 #(1.0 1.2 1.2 1.8 2.1 2.5)
183 :test 'almost=lists)) ;; result isn't a list!