Module: Combinatorial

Extended by:
Cubic, Diagonal
Includes:
Cubic, Diagonal
Defined in:
lib/algebra/combinatorial.rb

Overview

Combinatorial Calculation Library

by Shin-ichiro Hara

Version 2.0 (2001.09.14)

Defined Under Namespace

Modules: Cubic, Diagonal

Class Method Summary collapse

Methods included from Diagonal

co_diagonal, diagonal_cone

Methods included from Cubic

power_cubic

Class Method Details

.comb(n, m = n) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/algebra/combinatorial.rb', line 32

def comb(n, m = n)
  if m == 0
    yield([])
  else
    comb(n, m - 1) do |x|
      (((x[0] || -1) + 1)...n).each do |i|
        yield([i] + x)
      end
    end
  end
end

.perm(n, m = n) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/algebra/combinatorial.rb', line 20

def perm(n, m = n)
  if m == 0
    yield([])
  else
    perm(n - 1, m - 1) do |x|
      (0...n).each do |i|
        yield([i] + x.collect{|j| j < i ? j : j + 1})
      end
    end
  end
end

.perm0(n, stack = []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/algebra/combinatorial.rb', line 62

def perm0(n, stack = [])
  (0...n).each do |x|
    unless stack.include? x
	stack.push x
	if stack.size < n
 perm0(n, stack) do |y|; yield y; end
	else
 yield stack.self
	end
	stack.pop
    end
  end
end

.power(n, m = n) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/algebra/combinatorial.rb', line 8

def power(n, m = n)
  if m == 0
    yield([])
  else
    power(n, m - 1) do |x|
      (0...n).each do |i|
        yield([i] + x)
      end
    end
  end
end

.power0(n, m = n) ⇒ Object



56
57
58
59
60
# File 'lib/algebra/combinatorial.rb', line 56

def power0(n, m = n)
  0.upto n**m-1 do |i|
    yield( (0...m).collect{ (i, = i.divmod n).last } )
  end
end

.rep_comb(n, m) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/algebra/combinatorial.rb', line 44

def rep_comb(n, m)
   if m == 0
     yield([])
   else
     rep_comb(n, m - 1) do |x|
       ((x.empty? ? 0 : x.last)...n).each do |i|
        yield(x+[i])
       end
     end
   end
end