Method: MoreMath::Permutation#signum
- Defined in:
- lib/more_math/permutation.rb
#signum ⇒ Object Also known as: sgn
Returns the signum of this Permutation instance. It’s -1 if this permutation is odd and 1 if it’s an even permutation.
A permutation is odd if it can be represented by an odd number of transpositions (cycles of length 2), or even if it can be represented of an even number of transpositions.
249 250 251 252 253 254 255 |
# File 'lib/more_math/permutation.rb', line 249 def signum s = 1 cycles.each do |c| c.size % 2 == 0 and s *= -1 end s end |