lib/more_math/permutation.rb
# Returns the cycles representation of this Permutation instance.
# The return value of this method can be used to create a
# new Permutation instance with the Permutation.from_cycles method.
#
# <b>Example:</b>
#  perm = Permutation.new(7, 23)
#  # => #<Permutation:0x58541c @last=5039, @rank=23, @size=7>
#  perm.cycles
#  # => [[3, 6], [4, 5]]
Returns the cycles representation of this Permutation instance.
The return value of this method can be used to create a
new Permutation instance with the Permutation.from_cycles method.


Returns the cycles representation of this Permutation instance . The return value of this method can be used to create a new Permutation instance with the Permutation . from_cycles method .
def cycles
      perm = value
      result = [[]]
      seen = {}
      current = nil
      loop do
        current or current = perm.find { |x| !seen[x] }
        break unless current
        if seen[current]
          current = nil
          result << []
        else
          seen[current] = true
          result[-1] << current
          current = perm[current]
        end
      end
      result.pop
      result.select { |c| c.size > 1 }.map do |c|
        min_index = c.index(c.min)
        c[min_index..-1] + c[0...min_index]
      end
    end
def cycles perm = value result = [ [ ] ] seen = { } current = nil loop do current or current = perm . find { | x | ! seen [ x ] } break unless current if seen [ current ] current = nil result << [ ] else seen [ current ] = true result [ - 1 ] << current current = perm [ current ] end end result . pop result . select { | c | c . size > 1 } . map do | c | min_index = c . index ( c . min ) c [ min_index .. - 1 ] + c [ 0 ... min_index ] end end
Returns the cycles representation of this Permutation instance.
Returns the cycles representation of this Permutation instance .