Module: MoreMath::NumberifyStringFunction
- Includes:
- Functions
- Included in:
- StringNumeral
- Defined in:
- lib/more_math/numberify_string_function.rb
Overview
Provides functions for converting between strings and numbers using a base-N numeral system.
This module implements Gödel numbering, where strings are encoded into unique natural numbers and decoded back. It’s particularly useful for applications requiring ordered enumeration of strings or mathematical operations on textual data.
The encoding follows a positional numeral system where each character position represents a power of the alphabet size.
Class Method Summary collapse
-
.compute_size(n, b) ⇒ Integer
private
Calculates the minimum number of digits needed to represent a number in base N.
-
.convert_alphabet(alphabet) ⇒ Array<String>
private
Converts various alphabet representations into a consistent Array format.
-
.numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
Converts a string into a unique natural number using the specified alphabet.
-
.stringify_number(number, alphabet = 'a'..'z') ⇒ String
Converts a natural number back into its corresponding string representation.
Instance Method Summary collapse
-
#compute_size(n, b) ⇒ Integer
private
private
Calculates the minimum number of digits needed to represent a number in base N.
-
#convert_alphabet(alphabet) ⇒ Array<String>
private
private
Converts various alphabet representations into a consistent Array format.
-
#numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
private
Converts a string into a unique natural number using the specified alphabet.
-
#stringify_number(number, alphabet = 'a'..'z') ⇒ String
private
Converts a natural number back into its corresponding string representation.
Methods included from Functions
#beta, beta, #beta_regularized, beta_regularized, #cantor_pairing, cantor_pairing, #cantor_pairing_inv, cantor_pairing_inv, #erf, #erfc, #gamma, gamma, #gammaP_regularized, gammaP_regularized, #gammaQ_regularized, gammaQ_regularized, #log_beta, log_beta, #log_ceil, log_ceil, #log_floor, log_floor, #log_gamma, #logb, logb
Class Method Details
.compute_size(n, b) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Calculates the minimum number of digits needed to represent a number in base N.
This helper method is used internally to determine how many characters are needed when converting a number back to its string representation.
110 111 112 113 114 115 116 117 |
# File 'lib/more_math/numberify_string_function.rb', line 110 def compute_size(n, b) i = 0 while n > 0 i += 1 n -= b ** i end i end |
.convert_alphabet(alphabet) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts various alphabet representations into a consistent Array format.
This method handles different input types for the alphabet:
-
Range: converts to array of characters
-
String: splits into individual characters
-
Array: returns as-is
129 130 131 132 133 134 135 136 137 |
# File 'lib/more_math/numberify_string_function.rb', line 129 def convert_alphabet(alphabet) if alphabet.respond_to?(:to_ary) alphabet.to_ary elsif alphabet.respond_to?(:to_str) alphabet.to_str.split(//) else alphabet.to_a end end |
.numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
Converts a string into a unique natural number using the specified alphabet.
This method implements a base-N numeral system where N is the size of the alphabet. Each character in the string contributes to the final number based on its position and value within the alphabet.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/more_math/numberify_string_function.rb', line 48 def numberify_string(string, alphabet = 'a'..'z') alphabet = NumberifyStringFunction.convert_alphabet(alphabet) s, k = string.size, alphabet.size result = 0 for i in 0...s c = string[i, 1] a = (alphabet.index(c) || raise(ArgumentError, "#{c.inspect} not in alphabet")) + 1 j = s - i - 1 result += a * k ** j end result end |
.stringify_number(number, alphabet = 'a'..'z') ⇒ String
Converts a natural number back into its corresponding string representation.
This is the inverse operation of numberify_string. It reconstructs the original string by reversing the positional numeral system encoding.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/more_math/numberify_string_function.rb', line 79 def stringify_number(number, alphabet = 'a'..'z') case when number < 0 raise ArgumentError, "number is required to be >= 0" when number == 0 return '' end alphabet = NumberifyStringFunction.convert_alphabet(alphabet) s = NumberifyStringFunction.compute_size(number, alphabet.size) k, m = alphabet.size, number result = ' ' * s q = m s.downto(1) do |i| r = q / k q = r * k < q ? r : r - 1 result[i - 1] = alphabet[m - q * k - 1] m = q end result end |
Instance Method Details
#compute_size(n, b) ⇒ Integer (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Calculates the minimum number of digits needed to represent a number in base N.
This helper method is used internally to determine how many characters are needed when converting a number back to its string representation.
110 111 112 113 114 115 116 117 |
# File 'lib/more_math/numberify_string_function.rb', line 110 def compute_size(n, b) i = 0 while n > 0 i += 1 n -= b ** i end i end |
#convert_alphabet(alphabet) ⇒ Array<String> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts various alphabet representations into a consistent Array format.
This method handles different input types for the alphabet:
-
Range: converts to array of characters
-
String: splits into individual characters
-
Array: returns as-is
129 130 131 132 133 134 135 136 137 |
# File 'lib/more_math/numberify_string_function.rb', line 129 def convert_alphabet(alphabet) if alphabet.respond_to?(:to_ary) alphabet.to_ary elsif alphabet.respond_to?(:to_str) alphabet.to_str.split(//) else alphabet.to_a end end |
#numberify_string(string, alphabet = 'a'..'z') ⇒ Integer (private)
Converts a string into a unique natural number using the specified alphabet.
This method implements a base-N numeral system where N is the size of the alphabet. Each character in the string contributes to the final number based on its position and value within the alphabet.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/more_math/numberify_string_function.rb', line 48 def numberify_string(string, alphabet = 'a'..'z') alphabet = NumberifyStringFunction.convert_alphabet(alphabet) s, k = string.size, alphabet.size result = 0 for i in 0...s c = string[i, 1] a = (alphabet.index(c) || raise(ArgumentError, "#{c.inspect} not in alphabet")) + 1 j = s - i - 1 result += a * k ** j end result end |
#stringify_number(number, alphabet = 'a'..'z') ⇒ String (private)
Converts a natural number back into its corresponding string representation.
This is the inverse operation of numberify_string. It reconstructs the original string by reversing the positional numeral system encoding.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/more_math/numberify_string_function.rb', line 79 def stringify_number(number, alphabet = 'a'..'z') case when number < 0 raise ArgumentError, "number is required to be >= 0" when number == 0 return '' end alphabet = NumberifyStringFunction.convert_alphabet(alphabet) s = NumberifyStringFunction.compute_size(number, alphabet.size) k, m = alphabet.size, number result = ' ' * s q = m s.downto(1) do |i| r = q / k q = r * k < q ? r : r - 1 result[i - 1] = alphabet[m - q * k - 1] m = q end result end |