Module: Num4EquLib

Extended by:
FFI::Library
Defined in:
lib/num4equ.rb

Overview

数値計算による方程式の解法ライブラリ

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bisection(a, b, func) ⇒ double

二分法による解法

Parameters:

  • a (double)

    aの値

  • b (double)

    bの値

  • func (callback)

    aに対する値を計算

Returns:

  • (double)

    xの値

Raises:

  • RangeError



47
48
49
50
51
52
53
54
55
56
# File 'lib/num4equ.rb', line 47

def bisectionMethod(a, b, func)
    ok_ptr = FFI::MemoryPointer.new :int 
    x = bisectionMethodFFI(a, b, func, ok_ptr)
    ok = ok_ptr.read_int
    ok_ptr.free()
    if ok < 0 then
        raise RangeError.new("a:" + a.to_s + " " + "b:" + b.to_s)
    end
    return x
end

.newtonMethod(a, func) ⇒ double

ニュートン法による解法

Parameters:

  • a (double)

    aの値

  • func (callback)

    aに対する値を計算

Returns:

  • (double)

    xの値

Raises:

  • RangeError



30
31
32
33
34
35
36
37
38
39
# File 'lib/num4equ.rb', line 30

def newtonMethod(a, func)
    ok_ptr = FFI::MemoryPointer.new :int 
    x = newtonMethodFFI(a, func, ok_ptr)
    ok = ok_ptr.read_int
    ok_ptr.free()
    if ok < 0 then
        raise RangeError.new("a:" + a.to_s)
    end
    return x
end

.secantMethod(a, b, func) ⇒ double

割線法による解法

Parameters:

  • a (double)

    aの値

  • b (double)

    bの値

  • func (callback)

    aに対する値を計算

Returns:

  • (double)

    xの値

Raises:

  • RangeError



64
65
66
67
68
69
70
71
72
73
# File 'lib/num4equ.rb', line 64

def secantMethod(a, b, func)
    ok_ptr = FFI::MemoryPointer.new :int 
    x = secantMethodFFI(a, b, func, ok_ptr)
    ok = ok_ptr.read_int
    ok_ptr.free()
    if ok < 0 then
        raise RangeError.new("a:" + a.to_s + " " + "b:" + b.to_s)
    end
    return x
end

Instance Method Details

#func(a) {|a| ... } ⇒ double

Returns xの値.

Yields:

  • (a)

    関数

Yield Parameters:

  • aの値 (double)

Returns:

  • (double)

    xの値



15
# File 'lib/num4equ.rb', line 15

callback   :f, [:double], :double