Class: Statsample::Combination
- Inherits:
-
Object
- Object
- Statsample::Combination
- Defined in:
- lib/statsample/combination.rb
Overview
Combination class systematically generates all combinations of n elements, taken r at a time. With rbgsl, GSL::Combination is available for extra speed
Use:
comb=Statsample::Combination.new(3,5)
=> #<Statsample::Combination:0x7f6323804e08 @n=5, @d=#<Statsample::Combination::CombinationGsl:0x7f63237ff7f0 @n=5, @k=3, @c=GSL::Combination>, @k=3>
comb.each{|c| p c }
[0, 1, 2]
[0, 1, 3]
[0, 1, 4]
[0, 2, 3]
[0, 2, 4]
[0, 3, 4]
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]
Reference:
Defined Under Namespace
Classes: CombinationGsl, CombinationRuby
Instance Attribute Summary collapse
-
#d ⇒ Object
readonly
Returns the value of attribute d.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(k, n, only_ruby = false) ⇒ Combination
constructor
A new instance of Combination.
- #next_value ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(k, n, only_ruby = false) ⇒ Combination
Returns a new instance of Combination.
23 24 25 26 27 28 29 30 31 |
# File 'lib/statsample/combination.rb', line 23 def initialize(k,n,only_ruby=false) @k=k @n=n if Statsample.has_gsl? and !only_ruby @d=CombinationGsl.new(@k,@n) else @d=CombinationRuby.new(@k,@n) end end |
Instance Attribute Details
#d ⇒ Object (readonly)
Returns the value of attribute d.
22 23 24 |
# File 'lib/statsample/combination.rb', line 22 def d @d end |
Instance Method Details
#each ⇒ Object
32 33 34 35 36 37 |
# File 'lib/statsample/combination.rb', line 32 def each reset while a=next_value yield a end end |
#next_value ⇒ Object
41 42 43 |
# File 'lib/statsample/combination.rb', line 41 def next_value @d.next_value end |
#reset ⇒ Object
38 39 40 |
# File 'lib/statsample/combination.rb', line 38 def reset @d.reset end |