improved packaging in anticipatin for correcting my split of packages.
[CommonLispStat.git] / src / unittests / unittests-arrays.lisp
blob07592787b7df7f071f712dc17e28b707756c4e9e
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 ;; objects
23 slot-value call-method call-next-method
25 ;; lsmath
26 expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
27 asin acos atan sinh cosh tanh asinh acosh atanh float random
28 truncate floor ceiling round minusp zerop plusp evenp oddp
29 < <= = /= >= >
30 min max logand logior logxor lognot ffloor fceiling
31 ftruncate fround signum cis
33 ;; complex
34 conjugate realpart imagpart phase
36 ;; matlisp
37 fft transpose)
38 (:shadowing-import-from :matlisp
39 real ;; common-lisp
41 (:export run-lisp-stat-tests run-lisp-stat-test scoreboard))
44 (in-package :lisp-stat-unittests-arrays)
46 ;;; TEST for Arrays and Linear Algebra.
48 (deftestsuite lisp-stat-ut-array (lisp-stat-ut) ())
50 (addtest (lisp-stat-ut-array) cholesky-decomposition-1
51 (ensure-same
52 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
53 (list #2A((1.7888543819998317 0.0 0.0)
54 (1.6770509831248424 0.11180339887498929 0.0)
55 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
56 5.000000000000003)
57 :test 'almost=lists))
60 (addtest (lisp-stat-ut-array) cholesky-decomposition-2
61 (ensure-same
62 (matlisp:chol)))
67 (addtest (lisp-stat-ut-array) lu-decomposition
68 (ensure-same
69 (lu-decomp #2A((2 3 4)
70 (1 2 4)
71 (2 4 5)))
72 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
73 #(0 2 2)
74 -1.0
75 NIL)))
77 (addtest (lisp-stat-ut-array) lu-decomposition-2
78 (ensure-same
79 (lu-decomp (make-real-matrix #2A((2 3 4)
80 (1 2 4)
81 (2 4 5))))
82 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
83 #(0 2 2)
84 -1.0
85 NIL)))
89 (addtest (lisp-stat-ut-array) rcondest
90 ;; (ensure-same
91 (ensure-error ;; it barfs, FIXME!!
92 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
93 6.8157451e7
94 :test 'almost=))
96 (addtest (lisp-stat-ut-array) lu-solve
97 (ensure-same
98 (lu-solve
99 (lu-decomp
100 #2A((2 3 4) (1 2 4) (2 4 5)))
101 #(2 3 4))
102 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
104 (addtest (lisp-stat-ut-array) inverse
105 (ensure-same
106 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
107 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
108 (-1.0 -0.6666666666666666 1.3333333333333333)
109 (0.0 0.6666666666666666 -0.3333333333333333))))
111 (addtest (lisp-stat-ut-array) sv-decomp
112 (ensure-same
113 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
114 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
115 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
116 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
117 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
118 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
119 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
120 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
122 :test 'almost=lists))
124 (addtest (lisp-stat-ut-array) qr-decomp
125 (ensure-same
126 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
127 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
128 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
129 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
130 #2A((-3.0 -5.333333333333334 -7.333333333333332)
131 (0.0 -0.7453559924999292 -1.1925695879998877)
132 (0.0 0.0 -1.3416407864998738)))
133 :test 'almost=lists))
135 (addtest (lisp-stat-ut-array) eigen
136 (ensure-same
137 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
138 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
139 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
140 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
141 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
142 NIL)))
144 (addtest (lisp-stat-ut-array) spline
145 (ensure-same
146 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
147 #(1.2 2.0 2.1 2.0 1.1 2.8)
148 :xvals 6)
149 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
150 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
151 :test 'almost=lists))
153 (addtest (lisp-stat-ut-array) kernel-smooth
154 (ensure-same
155 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
156 (kernel-smooth
157 #(1.0 1.2 1.3 1.8 2.1 2.5)
158 #(1.2 2.0 2.1 2.0 1.1 2.8)
159 :xvals 5)
160 (list (list 1.0 1.375 1.75 2.125 2.5)
161 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
162 1.5871511322219498 2.518194783156392))
163 :test 'almost=lists))
165 (addtest (lisp-stat-ut-array) kernel-dens
166 (ensure-same
167 (kernel-dens
168 #(1.0 1.2 2.5 2.1 1.8 1.2)
169 :xvals 5)
170 (list (list 1.0 1.375 1.75 2.125 2.5)
171 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
172 0.4829822708587095 0.3485939156929503))))
175 (addtest (lisp-stat-ut-array) fft
176 (ensure-same
177 (fft #(1.0 1.2 2.5 2.1 1.8))
178 (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)))
179 :test 'almost=lists))
182 (addtest (lisp-stat-ut-array) lowess
183 (ensure-same
184 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
185 #(1.2 2.0 2.1 2.0 1.1 2.8))
186 #(1.0 1.2 1.2 1.8 2.1 2.5)
187 :test 'almost=lists)) ;; result isn't a list!
189 ;; (run-tests :suite 'lisp-stat-ut-array)