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.

Examples:

Basic usage

# Convert string to number
MoreMath::NumberifyStringFunction.numberify_string("abc") # => 731

# Convert number back to string
MoreMath::NumberifyStringFunction.stringify_number(731) # => "abc"

With custom alphabet

alphabet = ['a', 'b', 'c']
MoreMath::NumberifyStringFunction.numberify_string("abc", alphabet) # => 18
MoreMath::NumberifyStringFunction.stringify_number(18, alphabet) # => "abc"

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • n (Integer)

    The number to calculate size for

  • b (Integer)

    The base of the numeral system

Returns:

  • (Integer)

    The minimum number of digits required



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

Parameters:

  • alphabet (Object)

    The alphabet in various formats (Range, String, or Array)

Returns:

  • (Array<String>)

    Standardized array representation of the alphabet



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.

Examples:

Basic usage

MoreMath::NumberifyStringFunction.numberify_string("hello") # => 123456789

With custom alphabet

alphabet = ['a', 'b', 'c']
MoreMath::NumberifyStringFunction.numberify_string("abc", alphabet) # => 18

Parameters:

  • string (String)

    The input string to convert to a number

  • alphabet (Array<String>, Range<String>) (defaults to: 'a'..'z')

    The alphabet to use for conversion. Defaults to ‘a’..‘z’ (lowercase English letters)

Returns:

  • (Integer)

    A unique natural number representing the input string

Raises:

  • (ArgumentError)

    If any character in the string is not found in 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.

Examples:

Basic usage

MoreMath::NumberifyStringFunction.stringify_number(731) # => "abc"

With custom alphabet

alphabet = ['a', 'b', 'c']
MoreMath::NumberifyStringFunction.stringify_number(18, alphabet) # => "abc"

Parameters:

  • number (Integer)

    The natural number to convert back to a string

  • alphabet (Array<String>, Range<String>) (defaults to: 'a'..'z')

    The alphabet to use for conversion. Defaults to ‘a’..‘z’ (lowercase English letters)

Returns:

  • (String)

    The original string representation of the number

Raises:

  • (ArgumentError)

    If the number is negative



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.

Parameters:

  • n (Integer)

    The number to calculate size for

  • b (Integer)

    The base of the numeral system

Returns:

  • (Integer)

    The minimum number of digits required



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

Parameters:

  • alphabet (Object)

    The alphabet in various formats (Range, String, or Array)

Returns:

  • (Array<String>)

    Standardized array representation of the alphabet



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.

Examples:

Basic usage

MoreMath::NumberifyStringFunction.numberify_string("hello") # => 123456789

With custom alphabet

alphabet = ['a', 'b', 'c']
MoreMath::NumberifyStringFunction.numberify_string("abc", alphabet) # => 18

Parameters:

  • string (String)

    The input string to convert to a number

  • alphabet (Array<String>, Range<String>) (defaults to: 'a'..'z')

    The alphabet to use for conversion. Defaults to ‘a’..‘z’ (lowercase English letters)

Returns:

  • (Integer)

    A unique natural number representing the input string

Raises:

  • (ArgumentError)

    If any character in the string is not found in 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.

Examples:

Basic usage

MoreMath::NumberifyStringFunction.stringify_number(731) # => "abc"

With custom alphabet

alphabet = ['a', 'b', 'c']
MoreMath::NumberifyStringFunction.stringify_number(18, alphabet) # => "abc"

Parameters:

  • number (Integer)

    The natural number to convert back to a string

  • alphabet (Array<String>, Range<String>) (defaults to: 'a'..'z')

    The alphabet to use for conversion. Defaults to ‘a’..‘z’ (lowercase English letters)

Returns:

  • (String)

    The original string representation of the number

Raises:

  • (ArgumentError)

    If the number is negative



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