File: RootFinding.hs

package info (click to toggle)
haskell-math-functions 0.3.4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,132 kB
  • sloc: haskell: 2,675; python: 121; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,678 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- |
module Tests.RootFinding ( tests ) where

import Data.Default.Class
import Test.Tasty
import Test.Tasty.HUnit

import Numeric.RootFinding
import Tests.Helpers


tests :: TestTree
tests = testGroup "Root finding"
  [ testGroup "Ridders"
    [ testAssertion "sin x - 0.525 [exact]"     $ testRiddersSin0_525 (AbsTol 0)
    , testAssertion "sin x - 0.525 [abs 1e-12]" $ testRiddersSin0_525 (AbsTol 1e-12)
    , testAssertion "sin x - 0.525 [abs 1e-6]"  $ testRiddersSin0_525 (AbsTol 1e-6)
    , testAssertion "sin x - 0.525 [rel 1e-12]" $ testRiddersSin0_525 (RelTol 1e-12)
    , testAssertion "sin x - 0.525 [rel 1e-6]"  $ testRiddersSin0_525 (RelTol 1e-6)
    ]
  , testGroup "Newton-Raphson"
    [ testAssertion "sin x - 0.525 [rel 1e-12]" $ testNewtonSin0_525 (RelTol 1e-12)
    , testAssertion "sin x - 0.525 [rel 1e-6]"  $ testNewtonSin0_525 (RelTol 1e-6)
    , testAssertion "sin x - 0.525 [abs 1e-12]" $ testNewtonSin0_525 (AbsTol 1e-12)
    , testAssertion "sin x - 0.525 [abs 1e-6]"  $ testNewtonSin0_525 (AbsTol 1e-6)
    , testAssertion "1/x - 0.5     [0]"         $
        let Root r = newtonRaphson def{newtonTol=RelTol 0} (1,1000,1000)
                       (\x -> (1/x - 0.5, -1/(x*x)))
        in  r == 2
    ]
  ]
  where
    -- Exact root for equation: sin x - 0.525 = 0
    exactRoot = 0.5527151130967832
    --
    testRiddersSin0_525 tol
      = withinTolerance tol r exactRoot
      where
        Root r = ridders def{riddersTol = tol} (0, pi/2) (\x -> sin x - 0.525)
    --
    testNewtonSin0_525 tol
      = withinTolerance tol r exactRoot
      where
        Root r = newtonRaphson def{newtonTol=tol} (0, pi/4, pi/2) (\x -> (sin x - 0.525, cos x))