Module: LogisticMap

Defined in:
lib/logisticmap.rb

Class Method Summary collapse

Class Method Details

.logistic_map(x0, r, rounding) ⇒ Object

basic Logistic map



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logisticmap.rb', line 5

def self.logistic_map(x0, r, rounding)
  case x0
    when BigDecimal
      Enumerator.new do |yielder|
        number = BigDecimal("#{x0}")
        o = BigDecimal("#{r}")
        loop do
          #precalc = number.mult(multiplier, rounding)
          yielder.yield(number.round(rounding))
          number = o.mult(number * (1 - number), rounding)
        end
      end
  else
    Enumerator.new do |yielder|
      number = x0
      o = r
      loop do
        #precalc = number*multiplier
        yielder.yield(number)
        number = o * number * (1 - number)
      end
    end
  end
end

.logistic_points(x0_start, x0_end, x0_step, r, nth_iteration) ⇒ Object

should be refactored into an Enumerator later, now it returns the whole array

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logisticmap.rb', line 31

def self.logistic_points(x0_start, x0_end, x0_step, r, nth_iteration)
  raise(ArgumentError, "Stepping value cannot be negative!") if x0_step < 0
  raise(ArgumentError, "Iteration counter cannot be zero or negative!") if nth_iteration <= 0

  x0_current = x0_start
  result_array = []
  while x0_current < x0_end do
    lm = logistic_map(x0_current, r, 16)
    (nth_iteration-1).times {lm.next} # skipping first values
    result_array << lm.next.round(15) # return the Nth iteration
    x0_current += x0_step
  end
  result_array
end