def get_sequence_nonlinear(count, start = 0, stop = 1, curvature: 1)
step = 1 / (count - 1).to_f
linear_steps = 0.step(by: step, to: 1)
sequence = if curvature.positive?
linear_steps.map { |num| 1 - Math.exp(num * -curvature.abs) }
else
linear_steps.map { |num| Math.exp(num * curvature.abs) - 1 }
end
max = sequence.max
sequence.map { |num| (num / max) * (stop - start) + start }
end
puts get_sequence_nonlinear(14, 1, 10, curvature: -5)
1.0
1.0286368107863673
1.070705694586487
1.132506954419181
1.2232960453552535
1.356669686205176
1.5526021257764628
1.840436518177533
2.2632794002963466
2.8844563953977844
3.796995959289106
5.137561469978989
7.106918152759751
10.0
Created
September 19, 2021 11:46
-
-
Save ChildrenofkoRn/cc39ec97aa2744a274532b921557fc00 to your computer and use it in GitHub Desktop.
nonlinear sequence generator between numbers on Ruby
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can make generator a little faster using numo/narray