Class: Plurimath::Math::Evaluation::Iteration

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/math/evaluation/iteration.rb

Overview

Evaluates bounded Sum/Prod iterations: parses the ‘i=1` lower bound, validates the integer bounds and step limit, then folds the body with the index temporarily bound.

Instance Method Summary collapse

Constructor Details

#initialize(evaluator, lower, upper, body) ⇒ Iteration

Returns a new instance of Iteration.



10
11
12
13
14
15
# File 'lib/plurimath/math/evaluation/iteration.rb', line 10

def initialize(evaluator, lower, upper, body)
  @evaluator = evaluator
  @lower = lower
  @upper = upper
  @body = body
end

Instance Method Details

#accumulate(initial, operation) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/plurimath/math/evaluation/iteration.rb', line 17

def accumulate(initial, operation)
  name, from = index_definition
  to = evaluator.evaluate_node(upper)
  validate_bounds(from, to)

  (from..to).reduce(initial) do |accumulator, index|
    accumulator.public_send(operation, body_value(name, index))
  end
end