File: qrng_test.rb

package info (click to toggle)
ruby-gsl 2.1.0.3%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,604 kB
  • sloc: ansic: 62,050; ruby: 15,845; sh: 19; makefile: 10
file content (94 lines) | stat: -rw-r--r-- 1,782 bytes parent folder | download | duplicates (5)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'test_helper'

class QRngTest < GSL::TestCase

  def _test_sobol(v, d, g = nil)
    status = 0

    if g
      g.init
      reinitialized = true
    else
      g = GSL::QRng.alloc(GSL::QRng::SOBOL, d)
    end

    3.times { g.get(v) }

    status += (v[0] != 0.25 || v[1] != 0.75) ? 1 : 0
    g.get(v)

    status += (v[0] != 0.375 || v[1] != 0.375) ? 1 : 0
    assert status.zero?, "Sobol d=#{d}#{' (reinitialized)' if reinitialized}"

    g
  end

  def test_sobol
    v = GSL::Vector.alloc(3)

    _test_sobol(v, 2)
    _test_sobol(v, 3, _test_sobol(v, 3))
  end

  def _test_nied2(v, d, g = nil)
    status = 0

    if g
      g.init
      reinitialized = true
    else
      g = GSL::QRng.alloc(GSL::QRng::NIEDERREITER_2, d)
    end

    3.times { g.get(v) }

    status += (v[0] != 0.75 || v[1] != 0.25) ? 1 : 0
    g.get(v)

    status += (v[0] != 0.25 || v[1] != 0.75) ? 1 : 0
    3.times { g.get(v) }

    status += (v[0] != 0.625 || v[1] != 0.125) ? 1 : 0
    assert status.zero?, "Niederreiter d=#{d}#{' (reinitialized)' if reinitialized}"

    g
  end

  def test_nied2
    v = GSL::Vector.alloc(3)

    _test_nied2(v, 2)
    _test_nied2(v, 3, _test_nied2(v, 3))
  end

  def _test_hdsobol(v, d, g = nil)
    status = 0

    if g
      g.init
      reinitialized = true
    else
      g = GSL::QRng.alloc(GSL::QRng::HDSOBOL, d)
    end

    3.times { g.get(v) }

    status += (v[0] != 0.25 || v[1] != 0.75) ? 1 : 0
    g.get(v)

    status += (v[0] != 0.375 || v[1] != 0.375) ? 1 : 0
    assert status.zero?, "HDSobol d=#{d}#{' (reinitialized)' if reinitialized}"

    g
  end

  def test_hdsobol
    return unless GSL::QRng.const_defined?(:HDSOBOL)

    v = GSL::Vector.alloc(3)

    _test_hdsobol(v, 2)
    _test_hdsobol(v, 3, _test_hdsobol(v, 3))
  end

end