Sha256: b74bda1ce7aa1a6378a7c7fedc61ea64d79bedfa3012e84397bfbb83b5164713

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

require 'test_helper'

class DerivTest < GSL::TestCase

  def setup
    @f = [GSL::Function.alloc { |x| Math.exp(x) }]
    @df = [GSL::Function.alloc { |x| Math.exp(x) }]

    @f << GSL::Function.alloc { |x|
      x >= 0.0 ? x * Math.sqrt(x) : 0.0
    }
    @df << GSL::Function.alloc { |x|
      x >= 0.0 ? 1.5 * Math.sqrt(x) : 0.0
    }

    @f << GSL::Function.alloc { |x|
      x != 0.0 ? Math.sin(1.0 / x) : 0.0
    }
    @df << GSL::Function.alloc { |x|
      x != 0.0 ? -Math.cos(1.0 / x) / (x * x) : 0.0
    }

    @f << GSL::Function.alloc { |x| Math.exp(-x * x) }
    @df << GSL::Function.alloc { |x| -2.0 * x * Math.exp(-x * x) }

    @f << GSL::Function.alloc { |x| x * x }
    @df << GSL::Function.alloc { |x| 2.0 * x }

    @f << GSL::Function.alloc { |x| 1.0 / x }
    @df << GSL::Function.alloc { |x| -1.0 / (x * x) }
  end

  {
    'exp(x)'    => 1.0,
    'x^(3/2)'   => 0.1,
    'sin(1/x)'  => 0.45,
    'exp(-x^2)' => 0.5,
    'x^2'       => 0.0,
    '1/x'       => 10.0
  }.each_with_index { |(f, x), i|
    %w[central forward backward].each { |deriv|
      define_method("test_#{deriv}_#{i}") {
        _test_deriv(deriv, @f[i], @df[i], x, "#{f}, x=#{x}, #{deriv} deriv")
      }
    }
  }

  def _test_deriv(deriv, f, df, x, desc)
    expected, h = df.eval(x), 1e-4

    result, abserr = f.send("deriv_#{deriv}", x, h)
    assert_abs result, expected, GSL.MIN(h, expected.abs) + GSL::DBL_EPSILON, desc

    if abserr < (diff = (result - expected).abs)
      assert_factor abserr, (result - expected).abs, 2, desc + ' error estimate'
    else
      zero = result == expected || expected.zero?
      assert_abs abserr, zero ? 0.0 : diff, zero ? 1e-6 : 1e6 * diff, desc + ' abserr'
    end
  end

end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
gsl-2.1.0.3 test/gsl/deriv_test.rb
gsl-2.1.0.2 test/gsl/deriv_test.rb
gsl-2.1.0.1 test/gsl/deriv_test.rb
gsl-2.1.0 test/gsl/deriv_test.rb
gsl-1.16.0.6 test/gsl/deriv_test.rb
rb-gsl-1.16.0.5 test/gsl/deriv_test.rb
rb-gsl-1.16.0.4 test/gsl/deriv_test.rb
rb-gsl-1.16.0.3 test/gsl/deriv_test.rb
rb-gsl-1.16.0.3.rc1 test/gsl/deriv_test.rb
rb-gsl-1.16.0.2 test/gsl/deriv_test.rb
rb-gsl-1.16.0.1 test/gsl/deriv_test.rb
rb-gsl-1.16.0 test/gsl/deriv_test.rb
rb-gsl-1.15.3.2 test/gsl/deriv_test.rb