Module: MoreMath::CantorPairingFunction
- Defined in:
- lib/more_math/cantor_pairing_function.rb
Overview
Provides Cantor pairing function implementations for encoding tuples into natural numbers and decoding them back.
The Cantor pairing function is a mathematical construct that uniquely encodes pairs of natural numbers into a single natural number, and vice versa. This module implements both the forward pairing function and its inverse for arbitrary tuples of integers.
Class Method Summary collapse
-
.cantor_pairing(*xs) ⇒ Integer
Encodes multiple integers into a single natural number using the Cantor pairing function.
-
.cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
Computes the inverse of the Cantor pairing function for a given value.
-
.cantor_pairing_inv_f(z) ⇒ Integer
Computes the cantor pairing function f(z) = z * (z + 1) / 2.
-
.cantor_pairing_inv_q(z) ⇒ Integer
Computes the quotient used in the inverse Cantor pairing function.
Instance Method Summary collapse
-
#cantor_pairing(*xs) ⇒ Integer
private
Encodes multiple integers into a single natural number using the Cantor pairing function.
-
#cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
private
Computes the inverse of the Cantor pairing function for a given value.
Class Method Details
.cantor_pairing(*xs) ⇒ Integer
Encodes multiple integers into a single natural number using the Cantor pairing function.
This method implements the Cantor pairing function, which provides a unique encoding of tuples of natural numbers into a single natural number. For two integers, it uses the standard Cantor pairing formula. For more than two integers, it recursively applies the pairing function to reduce the tuple to a single value.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/more_math/cantor_pairing_function.rb', line 37 def cantor_pairing(*xs) if xs.size == 1 and xs.first.respond_to?(:to_ary) xs = xs.first.to_ary end case xs.size when 0, 1 raise ArgumentError, "at least two arguments are required" when 2 x, y, = *xs (x + y) * (x + y + 1) / 2 + y else cantor_pairing(cantor_pairing(*xs[0..1]), *xs[2..-1]) end end |
.cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
Computes the inverse of the Cantor pairing function for a given value
This method decodes a natural number back into its original tuple representation using the inverse Cantor pairing function. It supports decoding tuples of arbitrary length by recursively applying the inverse operation.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/more_math/cantor_pairing_function.rb', line 92 def cantor_pairing_inv(c, n = 2) raise ArgumentError, "n is required to be >= 2" unless n >= 2 result = [] begin q = CantorPairingFunction.cantor_pairing_inv_q(c) y = c - CantorPairingFunction.cantor_pairing_inv_f(q) x = q - y result.unshift y c = x n -= 1 end until n <= 1 result.unshift x end |
.cantor_pairing_inv_f(z) ⇒ Integer
Computes the cantor pairing function f(z) = z * (z + 1) / 2
This helper method calculates a partial result used in the inverse cantor pairing function to compute the triangular number sequence that helps in decoding paired values
60 61 62 |
# File 'lib/more_math/cantor_pairing_function.rb', line 60 def self.cantor_pairing_inv_f(z) z * (z + 1) / 2 end |
.cantor_pairing_inv_q(z) ⇒ Integer
Computes the quotient used in the inverse Cantor pairing function
This method calculates the largest value v such that the Cantor pairing function applied to v is less than or equal to the given value z. It’s a helper method that supports the inverse pairing algorithm by determining the appropriate triangular number boundary for decoding.
73 74 75 76 77 78 79 |
# File 'lib/more_math/cantor_pairing_function.rb', line 73 def self.cantor_pairing_inv_q(z) v = 0 while cantor_pairing_inv_f(v) <= z v += 1 end v - 1 end |
Instance Method Details
#cantor_pairing(*xs) ⇒ Integer (private)
Encodes multiple integers into a single natural number using the Cantor pairing function.
This method implements the Cantor pairing function, which provides a unique encoding of tuples of natural numbers into a single natural number. For two integers, it uses the standard Cantor pairing formula. For more than two integers, it recursively applies the pairing function to reduce the tuple to a single value.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/more_math/cantor_pairing_function.rb', line 37 def cantor_pairing(*xs) if xs.size == 1 and xs.first.respond_to?(:to_ary) xs = xs.first.to_ary end case xs.size when 0, 1 raise ArgumentError, "at least two arguments are required" when 2 x, y, = *xs (x + y) * (x + y + 1) / 2 + y else cantor_pairing(cantor_pairing(*xs[0..1]), *xs[2..-1]) end end |
#cantor_pairing_inv(c, n = 2) ⇒ Array<Integer> (private)
Computes the inverse of the Cantor pairing function for a given value
This method decodes a natural number back into its original tuple representation using the inverse Cantor pairing function. It supports decoding tuples of arbitrary length by recursively applying the inverse operation.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/more_math/cantor_pairing_function.rb', line 92 def cantor_pairing_inv(c, n = 2) raise ArgumentError, "n is required to be >= 2" unless n >= 2 result = [] begin q = CantorPairingFunction.cantor_pairing_inv_q(c) y = c - CantorPairingFunction.cantor_pairing_inv_f(q) x = q - y result.unshift y c = x n -= 1 end until n <= 1 result.unshift x end |