Module: Mathpack::Integration

Defined in:
lib/mathpack/integration.rb

Constant Summary collapse

STEP =
0.0078125
INTEGRATION_LIMIT =
1e2

Class Method Summary collapse

Class Method Details

.basic_integrate(a, b, &f) ⇒ Object



26
27
28
29
30
31
# File 'lib/mathpack/integration.rb', line 26

def self.basic_integrate(a, b, &f)
  result = 5.0/9.0 * f.call((a + b)/2.0 + (b - a)/2.0 * (-(3.0/5.0)**0.5))
  result += 8.0/9.0 * f.call((a + b)/2.0)
  result += 5.0/9.0 * f.call((a + b)/2.0 + (b - a)/2.0 * (3.0/5.0)**0.5)
  result * (b - a)/2.0
end

.integrate(params = {}, &f) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mathpack/integration.rb', line 6

def self.integrate(params = {}, &f)
  if params[:from] == -Float::INFINITY && params[:to] == Float::INFINITY
    result = solve_oi(-INTEGRATION_LIMIT, INTEGRATION_LIMIT, &f)
    g = ->(x) { f.call(-x) }
    result += solve_ni_1(INTEGRATION_LIMIT, &g)
    result += solve_ni_1(INTEGRATION_LIMIT, &f)
  elsif params[:to] == Float::INFINITY || params[:from] == -Float::INFINITY
    if params[:from] == -Float::INFINITY
      params[:from] = -params[:to]
      g = ->(x) { f.call(-x) }
    else
      g = f
    end
    result = solve_ni_1(params[:from], &g)
  else
    result = solve_oi(params[:from], params[:to], &f)
  end
  result
end

.solve_ni_1(from, &f) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/mathpack/integration.rb', line 42

def self.solve_ni_1(from, &f)
  to = [from, INTEGRATION_LIMIT].max
  result = solve_oi(from, to, &f)
  coefficients_array = [0.4589646740, 0.4170008308, 0.1133733821, 0.0103991974, 0.0002610172, 0.0000008985]
  nodes_array = [0.2228466042, 1.8889321017, 2.9927363261, 5.7751435691, 9.8374674184, 15.9828739806]
  nodes_array.each_index do |i|
    result += coefficients_array[i] * Math.exp(nodes_array[i]) * f.call(to + nodes_array[i])
  end
  result
end

.solve_oi(from, to, &f) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/mathpack/integration.rb', line 33

def self.solve_oi(from, to, &f)
  result = 0.0
  nodes = Mathpack::Approximation.generate_nodes(from: from, to: to, step: STEP)
  for i in 0...nodes.length - 1  do
    result += basic_integrate(nodes[i], nodes[i + 1], &f)
  end
  result
end