Class: Dydx::Integrand

Inherits:
Object
  • Object
show all
Defined in:
lib/dydx/integrand.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, var) ⇒ Integrand

Returns a new instance of Integrand.



5
6
7
8
# File 'lib/dydx/integrand.rb', line 5

def initialize(function, var)
  @function = function
  @var = var
end

Instance Attribute Details

#functionObject

Returns the value of attribute function.



3
4
5
# File 'lib/dydx/integrand.rb', line 3

def function
  @function
end

#varObject

Returns the value of attribute var.



3
4
5
# File 'lib/dydx/integrand.rb', line 3

def var
  @var
end

Instance Method Details

#[](a, b, n = 1000) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dydx/integrand.rb', line 10

def [](a, b, n = 1000)
  # HOT FIX: should implement Infinity class
  a = - 1000 if a == - Float::INFINITY
  b = 1000 if b == Float::INFINITY

  a, b = [a, b].map(&:to_f)
  fail ArgumentError, 'b should be greater than a' if a > b
  $temp_cal_f = function

  n = [n, (b - a) * 2].max
  n += 1 if n.to_i.odd?
  h = (b - a) / n
  x = ->(i) { a + h * i }

  odd_sum = (1..n - 1).to_a.select(&:odd?).inject(0) { |sum, i| sum += f(x.(i)) }
  even_sum = (1..n - 1).to_a.select(&:even?).inject(0) { |sum, i| sum += f(x.(i)) }
  round_8( (h / 3) * (f(a) + f(b) + 2 * even_sum + 4 * odd_sum) )
end

#f(vars) ⇒ Object



29
30
31
# File 'lib/dydx/integrand.rb', line 29

def f(vars)
  temp_cal_f(vars)
end

#round_8(num) ⇒ Object



33
34
35
36
# File 'lib/dydx/integrand.rb', line 33

def round_8(num)
  return num if num.abs == Float::INFINITY
  (num * 10 ** 8).round * 10.0 ** (-8)
end