diff options
author | Alberto Ruiz <aruiz@um.es> | 2015-07-22 19:34:00 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2015-07-22 19:34:00 +0200 |
commit | f59617b712cb610edaf630b186d00b33bd37a375 (patch) | |
tree | 82ae9187ee3d0a61954c7c2dea87106411e37f5f /packages/tests | |
parent | 233e419a2a48eb53e05370e5aa898608716ca114 (diff) | |
parent | 998f5710d43e860987f43217ca57a5721e3eabb0 (diff) |
Merge pull request #140 from peddie/unittests
Unit tests for interpolation and simulated annealing modules
Diffstat (limited to 'packages/tests')
-rw-r--r-- | packages/tests/src/Numeric/GSL/Tests.hs | 58 | ||||
-rw-r--r-- | packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs | 5 |
2 files changed, 61 insertions, 2 deletions
diff --git a/packages/tests/src/Numeric/GSL/Tests.hs b/packages/tests/src/Numeric/GSL/Tests.hs index e5d205d..025427b 100644 --- a/packages/tests/src/Numeric/GSL/Tests.hs +++ b/packages/tests/src/Numeric/GSL/Tests.hs | |||
@@ -21,8 +21,9 @@ import Test.HUnit (runTestTT, failures, Test(..), errors) | |||
21 | 21 | ||
22 | import Numeric.LinearAlgebra.HMatrix | 22 | import Numeric.LinearAlgebra.HMatrix |
23 | import Numeric.GSL | 23 | import Numeric.GSL |
24 | import Numeric.GSL.SimulatedAnnealing | ||
24 | import Numeric.LinearAlgebra.Tests (qCheck, utest) | 25 | import Numeric.LinearAlgebra.Tests (qCheck, utest) |
25 | import Numeric.LinearAlgebra.Tests.Properties ((|~|), (~~)) | 26 | import Numeric.LinearAlgebra.Tests.Properties ((|~|), (~~), (~=)) |
26 | 27 | ||
27 | --------------------------------------------------------------------- | 28 | --------------------------------------------------------------------- |
28 | 29 | ||
@@ -66,6 +67,59 @@ rootFindingTest = TestList [ utest "root Hybrids" (fst sol1 ~~ [1,1]) | |||
66 | jacobian a b [x,_y] = [ [-a , 0] | 67 | jacobian a b [x,_y] = [ [-a , 0] |
67 | , [-2*b*x, b] ] | 68 | , [-2*b*x, b] ] |
68 | 69 | ||
70 | -------------------------------------------------------------------- | ||
71 | |||
72 | interpolationTest = TestList [ | ||
73 | utest "interpolation evaluateV" (esol ~= ev) | ||
74 | , utest "interpolation evaluate" (esol ~= eval) | ||
75 | , utest "interpolation evaluateDerivativeV" (desol ~= dev) | ||
76 | , utest "interpolation evaluateDerivative" (desol ~= de) | ||
77 | , utest "interpolation evaluateDerivative2V" (d2esol ~= d2ev) | ||
78 | , utest "interpolation evaluateDerivative2" (d2esol ~= d2e) | ||
79 | , utest "interpolation evaluateIntegralV" (intesol ~= intev) | ||
80 | , utest "interpolation evaluateIntegral" (intesol ~= inte) | ||
81 | ] | ||
82 | where | ||
83 | xtest = 2.2 | ||
84 | applyVec f = f Akima xs ys xtest | ||
85 | applyList f = f Akima (zip xs' ys') xtest | ||
86 | |||
87 | esol = xtest**2 | ||
88 | ev = applyVec evaluateV | ||
89 | eval = applyList evaluate | ||
90 | |||
91 | desol = 2*xtest | ||
92 | dev = applyVec evaluateDerivativeV | ||
93 | de = applyList evaluateDerivative | ||
94 | |||
95 | d2esol = 2 | ||
96 | d2ev = applyVec evaluateDerivative2V | ||
97 | d2e = applyList evaluateDerivative2 | ||
98 | |||
99 | intesol = 1/3 * xtest**3 | ||
100 | intev = evaluateIntegralV Akima xs ys 0 xtest | ||
101 | inte = evaluateIntegral Akima (zip xs' ys') (0, xtest) | ||
102 | |||
103 | xs' = [-1..10] | ||
104 | ys' = map (**2) xs' | ||
105 | xs = vector xs' | ||
106 | ys = vector ys' | ||
107 | |||
108 | --------------------------------------------------------------------- | ||
109 | |||
110 | simanTest = TestList [ | ||
111 | -- We use a slightly more relaxed tolerance here because the | ||
112 | -- simulated annealer is randomized | ||
113 | utest "simulated annealing manual example" $ abs (result - 1.3631300) < 1e-6 | ||
114 | ] | ||
115 | where | ||
116 | -- This is the example from the GSL manual. | ||
117 | result = simanSolve 0 1 exampleParams 15.5 exampleE exampleM exampleS Nothing | ||
118 | exampleParams = SimulatedAnnealingParams 200 10000 1.0 1.0 0.008 1.003 2.0e-6 | ||
119 | exampleE x = exp (-(x - 1)**2) * sin (8 * x) | ||
120 | exampleM x y = abs $ x - y | ||
121 | exampleS rands stepSize current = (rands ! 0) * 2 * stepSize - stepSize + current | ||
122 | |||
69 | --------------------------------------------------------------------- | 123 | --------------------------------------------------------------------- |
70 | 124 | ||
71 | minimizationTest = TestList | 125 | minimizationTest = TestList |
@@ -123,6 +177,8 @@ runTests n = do | |||
123 | , odeTest | 177 | , odeTest |
124 | , rootFindingTest | 178 | , rootFindingTest |
125 | , minimizationTest | 179 | , minimizationTest |
180 | , interpolationTest | ||
181 | , simanTest | ||
126 | , utest "deriv" derivTest | 182 | , utest "deriv" derivTest |
127 | , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5**3) < 1E-8) | 183 | , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5**3) < 1E-8) |
128 | , utest "polySolve" (polySolveProp [1,2,3,4]) | 184 | , utest "polySolve" (polySolveProp [1,2,3,4]) |
diff --git a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs index 720b7bd..046644f 100644 --- a/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs +++ b/packages/tests/src/Numeric/LinearAlgebra/Tests/Properties.hs | |||
@@ -14,7 +14,7 @@ Testing properties. | |||
14 | -} | 14 | -} |
15 | 15 | ||
16 | module Numeric.LinearAlgebra.Tests.Properties ( | 16 | module Numeric.LinearAlgebra.Tests.Properties ( |
17 | dist, (|~|), (~~), (~:), Aprox((:~)), | 17 | dist, (|~|), (~~), (~:), Aprox((:~)), (~=), |
18 | zeros, ones, | 18 | zeros, ones, |
19 | square, | 19 | square, |
20 | unitary, | 20 | unitary, |
@@ -45,6 +45,9 @@ module Numeric.LinearAlgebra.Tests.Properties ( | |||
45 | import Numeric.LinearAlgebra.HMatrix hiding (Testable,unitary) | 45 | import Numeric.LinearAlgebra.HMatrix hiding (Testable,unitary) |
46 | import Test.QuickCheck | 46 | import Test.QuickCheck |
47 | 47 | ||
48 | (~=) :: Double -> Double -> Bool | ||
49 | a ~= b = abs (a - b) < 1e-10 | ||
50 | |||
48 | trivial :: Testable a => Bool -> a -> Property | 51 | trivial :: Testable a => Bool -> a -> Property |
49 | trivial = (`classify` "trivial") | 52 | trivial = (`classify` "trivial") |
50 | 53 | ||