another dataframe example.
[CommonLispStat.git] / src / unittests / unittests-arrays.lisp
blobb8c9604dee8752d8cf5bd985d82edf85ad1ebfd0
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 :lisp-stat-unittests)
13 ;;; TEST for Arrays and Linear Algebra.
15 (deftestsuite lisp-stat-ut-array (lisp-stat-ut) ())
17 (addtest (lisp-stat-ut-array) cholesky-decomposition-1
18 (ensure-same
19 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
20 (list #2A((1.7888543819998317 0.0 0.0)
21 (1.6770509831248424 0.11180339887498929 0.0)
22 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
23 5.000000000000003)
24 :test 'almost=lists))
26 (addtest (lisp-stat-ut-array) cholesky-decomposition-1
27 (ensure-same
28 (chol-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
29 (list #2A((1.7888543819998317 0.0 0.0)
30 (1.6770509831248424 0.11180339887498929 0.0)
31 (2.23606797749979 2.23606797749979 3.332000937312528e-8))
32 5.000000000000003)
33 :test 'almost=lists))
36 (addtest (lisp-stat-ut-array) cholesky-decomposition-2
37 (ensure-same
38 (matlisp:chol)))
43 (addtest (lisp-stat-ut-array) lu-decomposition
44 (ensure-same
45 (lu-decomp #2A((2 3 4)
46 (1 2 4)
47 (2 4 5)))
48 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
49 #(0 2 2)
50 -1.0
51 NIL)))
53 (addtest (lisp-stat-ut-array) lu-decomposition-2
54 (ensure-same
55 (lu-decomp (make-real-matrix #2A((2 3 4)
56 (1 2 4)
57 (2 4 5))))
58 (list #2A((2.0 3.0 4.0) (1.0 1.0 1.0) (0.5 0.5 1.5))
59 #(0 2 2)
60 -1.0
61 NIL)))
65 (addtest (lisp-stat-ut-array) rcondest
66 ;; (ensure-same
67 (ensure-error ;; it barfs, FIXME!!
68 (rcondest #2A((2 3 4) (1 2 4) (2 4 5)))
69 6.8157451e7
70 :test 'almost=))
72 (addtest (lisp-stat-ut-array) lu-solve
73 (ensure-same
74 (lu-solve
75 (lu-decomp
76 #2A((2 3 4) (1 2 4) (2 4 5)))
77 #(2 3 4))
78 #(-2.333333333333333 1.3333333333333335 0.6666666666666666)))
80 (addtest (lisp-stat-ut-array) inverse
81 (ensure-same
82 (inverse #2A((2 3 4) (1 2 4) (2 4 5)))
83 #2A((2.0 -0.33333333333333326 -1.3333333333333335)
84 (-1.0 -0.6666666666666666 1.3333333333333333)
85 (0.0 0.6666666666666666 -0.3333333333333333))))
87 (addtest (lisp-stat-ut-array) sv-decomp
88 (ensure-same
89 (sv-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
90 (list #2A((-0.5536537653489974 0.34181191712789266 -0.7593629708013371)
91 (-0.4653437312661058 -0.8832095891230851 -0.05827549615722014)
92 (-0.6905959164998124 0.3211003503429828 0.6480523475178517))
93 #(9.699290438141343 0.8971681569301373 0.3447525123483081)
94 #2A((-0.30454218417339873 0.49334669582252344 -0.8147779426198863)
95 (-0.5520024849987308 0.6057035911404464 0.5730762743603965)
96 (-0.7762392122368734 -0.6242853493399995 -0.08786630745236332))
98 :test 'almost=lists))
100 (addtest (lisp-stat-ut-array) qr-decomp
101 (ensure-same
102 (qr-decomp #2A((2 3 4) (1 2 4) (2 4 5)))
103 (list #2A((-0.6666666666666665 0.7453559924999298 5.551115123125783e-17)
104 (-0.3333333333333333 -0.2981423969999719 -0.894427190999916)
105 (-0.6666666666666666 -0.5962847939999439 0.44721359549995787))
106 #2A((-3.0 -5.333333333333334 -7.333333333333332)
107 (0.0 -0.7453559924999292 -1.1925695879998877)
108 (0.0 0.0 -1.3416407864998738)))
109 :test 'almost=lists))
111 (addtest (lisp-stat-ut-array) eigen
112 (ensure-same
113 (eigen #2A((2 3 4) (1 2 4) (2 4 5)))
114 (list #(10.656854249492381 -0.6568542494923802 -0.9999999999999996)
115 (list #(0.4999999999999998 0.4999999999999997 0.7071067811865475)
116 #(-0.49999999999999856 -0.5000000000000011 0.7071067811865474)
117 #(0.7071067811865483 -0.7071067811865466 -1.2560739669470215e-15))
118 NIL)))
120 (addtest (lisp-stat-ut-array) spline
121 (ensure-same
122 (spline #(1.0 1.2 1.3 1.8 2.1 2.5)
123 #(1.2 2.0 2.1 2.0 1.1 2.8)
124 :xvals 6)
125 (list (list 1.0 1.3 1.6 1.9 2.2 2.5)
126 (list 1.2 2.1 2.2750696543866313 1.6465231041904045 1.2186576148879609 2.8))
127 :test 'almost=lists))
129 (addtest (lisp-stat-ut-array) kernel-smooth
130 (ensure-same
131 ;; using KERNEL-SMOOTH-FRONT, not KERNEL-SMOOTH-CPORT
132 (kernel-smooth
133 #(1.0 1.2 1.3 1.8 2.1 2.5)
134 #(1.2 2.0 2.1 2.0 1.1 2.8)
135 :xvals 5)
136 (list (list 1.0 1.375 1.75 2.125 2.5)
137 (list 1.6603277642110226 1.9471748095239771 1.7938127405752287
138 1.5871511322219498 2.518194783156392))
139 :test 'almost=lists))
141 (addtest (lisp-stat-ut-array) kernel-dens
142 (ensure-same
143 (kernel-dens
144 #(1.0 1.2 2.5 2.1 1.8 1.2)
145 :xvals 5)
146 (list (list 1.0 1.375 1.75 2.125 2.5)
147 (list 0.7224150453621405 0.5820045548233707 0.38216411702854214
148 0.4829822708587095 0.3485939156929503))))
151 (addtest (lisp-stat-ut-array) fft
152 (ensure-same
153 (fft #(1.0 1.2 2.5 2.1 1.8))
154 (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)))
155 :test 'almost=lists))
158 (addtest (lisp-stat-ut-array) lowess
159 (ensure-same
160 (lowess #(1.0 1.2 2.5 2.1 1.8 1.2)
161 #(1.2 2.0 2.1 2.0 1.1 2.8))
162 #(1.0 1.2 1.2 1.8 2.1 2.5)
163 :test 'almost=lists)) ;; result isn't a list!
165 ;; (run-tests :suite 'lisp-stat-ut-array)