File: cheb_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 (80 lines) | stat: -rw-r--r-- 2,318 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
require 'test_helper'

class ChebTest < GSL::TestCase

  def test_cheb
    tol, pi, order = 100.0 * GSL::DBL_EPSILON, GSL::M_PI, 40

    cs = GSL::Cheb.alloc(order)

    cs.init(GSL::Function.alloc { |x| 1.0 }, -1.0, 1.0)
    order.times { |i|
      assert_abs cs.c[i], i == 0 ? 2.0 : 0.0, tol, 'c[%d] for T_0(x)' % i
    }

    cs.init(GSL::Function.alloc { |x| x }, -1.0, 1.0)
    order.times { |i|
      assert_abs cs.c[i], i == 1 ? 1.0 : 0.0, tol, 'c[%d] for T_1(x)' % i
    }

    cs.init(GSL::Function.alloc { |x| 2.0 * x * x - 1.0 }, -1.0, 1.0)
    order.times { |i|
      assert_abs cs.c[i], i == 2 ? 1.0 : 0.0, tol, 'c[%d] for T_2(x)' % i
    }

    cs.init(GSL::Function.alloc { |x| Math.sin(x) }, -pi, pi)
    assert_abs cs.c[0], 0.0, tol, 'c[0] for F_sin(x)'
    assert_abs cs.c[1], 5.69230686359506e-01, tol, 'c[1] for F_sin(x)'
    assert_abs cs.c[2], 0.0, tol, 'c[2] for F_sin(x)'
    assert_abs cs.c[3], -6.66916672405979e-01, tol, 'c[3] for F_sin(x)'
    assert_abs cs.c[4], 0.0, tol, 'c[4] for F_sin(x)'
    assert_abs cs.c[5], 1.04282368734237e-01, tol, 'c[5] for F_sin(x)'

    x = -pi
    while x < pi
      assert_abs cs.eval(x), Math.sin(x), tol, 'GSL::Cheb#eval, sin(%.3g)' % x
      x += pi / 100.0
    end

    x = -pi
    while x < pi
      r, e = cs.eval_err(x)

      assert_abs r, Math.sin(x), tol, 'GSL::Cheb#eval_err, sin(%.3g)' % x
      assert_factor((r - Math.sin(x)).abs + GSL::DBL_EPSILON, e, 10.0,
        'GSL::Cheb#eval_err, error sin(%.3g)' % x)

      x += pi / 100.0
    end

    x = -pi
    while x < pi
      assert_abs cs.eval_n(25, x), Math.sin(x), tol, 'GSL::Cheb#eval_n, sin(%.3g)' % x
      x += pi / 100.0
    end

    x = -pi
    while x < pi
      r, e = cs.eval_n_err(25, x)

      assert_abs r, Math.sin(x), tol, 'GSL::Cheb#eval_n_err, sin(%.3g)' % x
      assert_factor((r - Math.sin(x)).abs + GSL::DBL_EPSILON, e, 10.0,
        'GSL::Cheb#eval_n_err, error sin(%.3g)' % x)

      x += pi / 100.0
    end

    csd, x = cs.calc_deriv, -pi
    while x < pi
      assert_abs csd.eval(x), Math.cos(x), 1600 * tol, 'GSL::Cheb#eval, deriv sin(%.3g)' % x
      x += pi / 100.0
    end

    csi, x = cs.calc_integ, -pi
    while x < pi
      assert_abs csi.eval(x), -(1 + Math.cos(x)), tol, 'GSL::Cheb#eval, integ sin(%.3g)' % x
      x += pi / 100.0
    end
  end

end