more_math 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +5 -0
- data/{README → README.rdoc} +1 -1
- data/Rakefile +30 -82
- data/VERSION +1 -1
- data/lib/more_math.rb +16 -6
- data/lib/more_math/{cantor_pairing_funtion.rb → cantor_pairing_function.rb} +0 -0
- data/lib/more_math/functions.rb +39 -0
- data/lib/more_math/numberify_string_function.rb +66 -0
- data/lib/more_math/sequence.rb +70 -48
- data/lib/more_math/string_numeral.rb +153 -0
- data/lib/more_math/version.rb +1 -1
- data/more_math.gemspec +35 -0
- data/tests/test_numberify_string_function.rb +60 -0
- metadata +74 -31
- data/install.rb +0 -19
- data/make_doc.rb +0 -5
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/{README → README.rdoc}
RENAMED
data/Rakefile
CHANGED
@@ -1,84 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
task :doc do
|
32
|
-
ruby 'make_doc.rb'
|
33
|
-
end
|
34
|
-
|
35
|
-
if defined? Gem
|
36
|
-
spec = Gem::Specification.new do |s|
|
37
|
-
s.name = PKG_NAME
|
38
|
-
s.version = PKG_VERSION
|
39
|
-
s.summary = "Library that provides more mathematics."
|
40
|
-
s.description = "Library that provides more mathematical functions/algorithms than standard Ruby."
|
41
|
-
s.add_dependency('dslkit', '~> 0.2')
|
42
|
-
|
43
|
-
s.files = PKG_FILES
|
44
|
-
|
45
|
-
s.require_path = 'lib'
|
46
|
-
|
47
|
-
s.has_rdoc = true
|
48
|
-
s.rdoc_options <<
|
49
|
-
'--title' << 'MoreMath -- More Math in Ruby' << '--main' << 'README'
|
50
|
-
s.extra_rdoc_files << 'README'
|
51
|
-
s.test_files = Dir['tests/*.rb']
|
52
|
-
|
53
|
-
s.author = "Florian Frank"
|
54
|
-
s.email = "flori@ping.de"
|
55
|
-
s.homepage = "http://flori.github.com/#{PKG_NAME}"
|
56
|
-
s.rubyforge_project = PKG_NAME
|
57
|
-
end
|
58
|
-
|
59
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
60
|
-
pkg.need_tar = true
|
61
|
-
pkg.package_files += PKG_FILES
|
1
|
+
# vim: set filetype=ruby et sw=2 ts=2:
|
2
|
+
|
3
|
+
require 'gem_hadar'
|
4
|
+
|
5
|
+
GemHadar do
|
6
|
+
name 'more_math'
|
7
|
+
author 'Florian Frank'
|
8
|
+
email 'flori@ping.de'
|
9
|
+
homepage "http://flori.github.com/#{name}"
|
10
|
+
summary 'Library that provides more mathematics.'
|
11
|
+
description 'Library that provides more mathematical functions/algorithms than standard Ruby.'
|
12
|
+
test_dir 'tests'
|
13
|
+
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock'
|
14
|
+
readme 'README.rdoc'
|
15
|
+
title "#{name.camelize} -- More Math in Ruby"
|
16
|
+
|
17
|
+
dependency 'spruz', '~>0.2'
|
18
|
+
|
19
|
+
install_library do
|
20
|
+
libdir = CONFIG["sitelibdir"]
|
21
|
+
file = 'lib/more_math.rb'
|
22
|
+
install(file, libdir, :mode => 0644)
|
23
|
+
mkdir_p subdir = File.join(libdir, 'more_math')
|
24
|
+
for f in Dir['lib/more_math/*.rb']
|
25
|
+
install(f, subdir)
|
26
|
+
end
|
27
|
+
mkdir_p subdir = File.join(libdir, 'more_math', 'constants')
|
28
|
+
for f in Dir['lib/more_math/constants/*.rb']
|
29
|
+
install(f, subdir)
|
30
|
+
end
|
62
31
|
end
|
63
32
|
end
|
64
|
-
|
65
|
-
desc m = "Writing version information for #{PKG_VERSION}"
|
66
|
-
task :version do
|
67
|
-
puts m
|
68
|
-
File.open(File.join('lib', PKG_NAME, 'version.rb'), 'w') do |v|
|
69
|
-
v.puts <<EOT
|
70
|
-
module MoreMath
|
71
|
-
# MoreMath version
|
72
|
-
VERSION = '#{PKG_VERSION}'
|
73
|
-
VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
|
74
|
-
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
|
75
|
-
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
|
76
|
-
VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
|
77
|
-
end
|
78
|
-
EOT
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
task :default => [ :version, :test ]
|
83
|
-
|
84
|
-
task :release => [ :clobber, :version, :package ]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/more_math.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
1
|
module MoreMath
|
2
|
-
Infinity
|
3
|
-
|
4
|
-
Dir.chdir(File.join(File.dirname(__FILE__), 'more_math')) do
|
5
|
-
Dir['**/*.rb'].each do |filename|
|
6
|
-
require File.join('more_math', filename.gsub(/\.rb\Z/, ''))
|
7
|
-
end
|
2
|
+
unless defined?(::MoreMath::Infinity) == 'constant'
|
3
|
+
Infinity = 1.0 / 0 # Refers to floating point infinity.
|
8
4
|
end
|
5
|
+
|
6
|
+
require 'more_math/cantor_pairing_function'
|
7
|
+
require 'more_math/constants/functions_constants'
|
8
|
+
require 'more_math/continued_fraction'
|
9
|
+
require 'more_math/distributions'
|
10
|
+
require 'more_math/exceptions'
|
11
|
+
require 'more_math/functions'
|
12
|
+
require 'more_math/histogram'
|
13
|
+
require 'more_math/linear_regression'
|
14
|
+
require 'more_math/newton_bisection'
|
15
|
+
require 'more_math/numberify_string_function'
|
16
|
+
require 'more_math/sequence'
|
17
|
+
require 'more_math/string_numeral'
|
18
|
+
require 'more_math/version'
|
9
19
|
end
|
File without changes
|
data/lib/more_math/functions.rb
CHANGED
@@ -136,6 +136,34 @@ module MoreMath
|
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
|
+
def log_ceil(n, b = 2)
|
140
|
+
raise ArgumentError, "n is required to be > 0" unless n > 0
|
141
|
+
raise ArgumentError, "b is required to be > 1" unless b > 1
|
142
|
+
e, result = 1, 0
|
143
|
+
until e >= n
|
144
|
+
e *= b
|
145
|
+
result += 1
|
146
|
+
end
|
147
|
+
result
|
148
|
+
end
|
149
|
+
|
150
|
+
def log_floor(n, b = 2)
|
151
|
+
raise ArgumentError, "n is required to be > 0" unless n > 0
|
152
|
+
raise ArgumentError, "b is required to be > 1" unless b > 1
|
153
|
+
e, result = 1, 0
|
154
|
+
until e * b > n
|
155
|
+
e *= b
|
156
|
+
result += 1
|
157
|
+
end
|
158
|
+
result
|
159
|
+
end
|
160
|
+
|
161
|
+
# Returns the base +b+ logarithm of the number +x+. +b+ defaults to base
|
162
|
+
# 2, binary logarithm.
|
163
|
+
def logb(x, b = 2)
|
164
|
+
Math.log(x) / Math.log(b)
|
165
|
+
end
|
166
|
+
|
139
167
|
# Returns Cantor's tuple function for the tuple +*xs+ (the size must be at
|
140
168
|
# least 2).
|
141
169
|
def cantor_pairing(*xs)
|
@@ -147,5 +175,16 @@ module MoreMath
|
|
147
175
|
def cantor_pairing_inv(c, n = 2)
|
148
176
|
CantorPairingFunction.cantor_pairing_inv(c, n)
|
149
177
|
end
|
178
|
+
|
179
|
+
# Computes a Gödel number from +string+ in the +alphabet+ and returns it.
|
180
|
+
def numberify_string(string, alphabet = 'a'..'z')
|
181
|
+
NumberifyStringFunction.numberify_string(string, alphabet)
|
182
|
+
end
|
183
|
+
|
184
|
+
# Computes the string in the +alphabet+ from a Gödel number +number+ and
|
185
|
+
# returns it. This is the inverse function of numberify_string.
|
186
|
+
def stringify_number(number, alphabet = 'a'..'z')
|
187
|
+
NumberifyStringFunction.stringify_number(number, alphabet)
|
188
|
+
end
|
150
189
|
end
|
151
190
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'more_math'
|
2
|
+
require 'spruz/memoize'
|
3
|
+
|
4
|
+
module MoreMath
|
5
|
+
module NumberifyStringFunction
|
6
|
+
Functions = MoreMath::Functions
|
7
|
+
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def numberify_string(string, alphabet = 'a'..'z')
|
11
|
+
alphabet = NumberifyStringFunction.convert_alphabet alphabet
|
12
|
+
s, k = string.size, alphabet.size
|
13
|
+
result = 0
|
14
|
+
for i in 0...s
|
15
|
+
c = string[i, 1]
|
16
|
+
a = (alphabet.index(c) || raise(ArgumentError, "#{c.inspect} not in alphabet")) + 1
|
17
|
+
j = s - i - 1
|
18
|
+
result += a * k ** j
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def stringify_number(number, alphabet = 'a'..'z')
|
24
|
+
case
|
25
|
+
when number < 0
|
26
|
+
raise ArgumentError, "number is required to be >= 0"
|
27
|
+
when number == 0
|
28
|
+
return ''
|
29
|
+
end
|
30
|
+
alphabet = NumberifyStringFunction.convert_alphabet alphabet
|
31
|
+
s = NumberifyStringFunction.compute_size(number, alphabet.size)
|
32
|
+
k, m = alphabet.size, number
|
33
|
+
result = ' ' * s
|
34
|
+
q = m
|
35
|
+
s.downto(1) do |i|
|
36
|
+
r = q / k
|
37
|
+
q = r * k < q ? r : r - 1
|
38
|
+
result[i - 1] = alphabet[m - q * k - 1]
|
39
|
+
m = q
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
class << self
|
45
|
+
def compute_size(n, b)
|
46
|
+
i = 0
|
47
|
+
while n > 0
|
48
|
+
i += 1
|
49
|
+
n -= b ** i
|
50
|
+
end
|
51
|
+
i
|
52
|
+
end
|
53
|
+
memoize_function :compute_size
|
54
|
+
|
55
|
+
def convert_alphabet(alphabet)
|
56
|
+
if alphabet.respond_to?(:to_ary)
|
57
|
+
alphabet.to_ary
|
58
|
+
elsif alphabet.respond_to?(:to_str)
|
59
|
+
alphabet.to_str.split(//)
|
60
|
+
else
|
61
|
+
alphabet.to_a
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/more_math/sequence.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'more_math'
|
2
|
+
require 'spruz/memoize'
|
2
3
|
|
3
4
|
module MoreMath
|
4
5
|
# This class is used to contain elements and compute various statistical
|
5
6
|
# values for them.
|
6
7
|
class Sequence
|
7
8
|
def initialize(elements)
|
8
|
-
@elements = elements
|
9
|
-
@elements.freeze
|
9
|
+
@elements = elements.dup.freeze
|
10
10
|
end
|
11
11
|
|
12
12
|
# Returns the array of elements.
|
@@ -28,105 +28,126 @@ module MoreMath
|
|
28
28
|
@elements.size
|
29
29
|
end
|
30
30
|
|
31
|
+
# Reset all memoized values of this sequence.
|
32
|
+
def reset
|
33
|
+
self.class.memoize_cache_clear
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
31
37
|
# Returns the variance of the elements.
|
32
38
|
def variance
|
33
|
-
|
39
|
+
sum_of_squares / size
|
34
40
|
end
|
41
|
+
memoize_method :variance
|
35
42
|
|
36
43
|
# Returns the sample_variance of the elements.
|
37
44
|
def sample_variance
|
38
|
-
|
45
|
+
size > 1 ? sum_of_squares / (size - 1.0) : 0.0
|
39
46
|
end
|
47
|
+
memoize_method :sample_variance
|
40
48
|
|
41
49
|
# Returns the sum of squares (the sum of the squared deviations) of the
|
42
50
|
# elements.
|
43
51
|
def sum_of_squares
|
44
|
-
@
|
52
|
+
@elements.inject(0.0) { |s, t| s + (t - arithmetic_mean) ** 2 }
|
45
53
|
end
|
54
|
+
memoize_method :sum_of_squares
|
46
55
|
|
47
56
|
# Returns the standard deviation of the elements.
|
48
57
|
def standard_deviation
|
49
|
-
|
58
|
+
Math.sqrt(variance)
|
50
59
|
end
|
60
|
+
memoize_method :standard_deviation
|
51
61
|
|
52
62
|
# Returns the standard deviation of the elements in percentage of the
|
53
63
|
# arithmetic mean.
|
54
64
|
def standard_deviation_percentage
|
55
|
-
|
65
|
+
100.0 * standard_deviation / arithmetic_mean
|
56
66
|
end
|
67
|
+
memoize_method :standard_deviation_percentage
|
57
68
|
|
58
69
|
# Returns the sample standard deviation of the elements.
|
59
70
|
def sample_standard_deviation
|
60
|
-
|
71
|
+
Math.sqrt(sample_variance)
|
61
72
|
end
|
73
|
+
memoize_method :sample_standard_deviation
|
62
74
|
|
63
75
|
# Returns the sample standard deviation of the elements in percentage
|
64
76
|
# of the arithmetic mean.
|
65
77
|
def sample_standard_deviation_percentage
|
66
|
-
|
78
|
+
100.0 * sample_standard_deviation / arithmetic_mean
|
67
79
|
end
|
80
|
+
memoize_method :sample_standard_deviation_percentage
|
68
81
|
|
69
82
|
# Returns the sum of all elements.
|
70
83
|
def sum
|
71
|
-
@
|
84
|
+
@elements.inject(0.0) { |s, t| s + t }
|
72
85
|
end
|
86
|
+
memoize_method :sum
|
73
87
|
|
74
88
|
# Returns the arithmetic mean of the elements.
|
75
89
|
def arithmetic_mean
|
76
|
-
|
90
|
+
sum / size
|
77
91
|
end
|
92
|
+
memoize_method :arithmetic_mean
|
78
93
|
|
79
94
|
alias mean arithmetic_mean
|
80
95
|
|
81
96
|
# Returns the harmonic mean of the elements. If any of the elements
|
82
97
|
# is less than or equal to 0.0, this method returns NaN.
|
83
98
|
def harmonic_mean
|
84
|
-
@
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
sum ? size / sum : 0 / 0.0
|
93
|
-
)
|
99
|
+
sum = @elements.inject(0.0) { |s, t|
|
100
|
+
if t > 0
|
101
|
+
s + 1.0 / t
|
102
|
+
else
|
103
|
+
break nil
|
104
|
+
end
|
105
|
+
}
|
106
|
+
sum ? size / sum : 0 / 0.0
|
94
107
|
end
|
108
|
+
memoize_method :harmonic_mean
|
95
109
|
|
96
110
|
# Returns the geometric mean of the elements. If any of the
|
97
111
|
# elements is less than 0.0, this method returns NaN.
|
98
112
|
def geometric_mean
|
99
|
-
@
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
break :null
|
106
|
-
else
|
107
|
-
break nil
|
108
|
-
end
|
109
|
-
}
|
110
|
-
case sum
|
111
|
-
when :null
|
112
|
-
0.0
|
113
|
-
when Float
|
114
|
-
Math.exp(sum / size)
|
113
|
+
sum = @elements.inject(0.0) { |s, t|
|
114
|
+
case
|
115
|
+
when t > 0
|
116
|
+
s + Math.log(t)
|
117
|
+
when t == 0
|
118
|
+
break :null
|
115
119
|
else
|
116
|
-
|
120
|
+
break nil
|
117
121
|
end
|
118
|
-
|
122
|
+
}
|
123
|
+
case sum
|
124
|
+
when :null
|
125
|
+
0.0
|
126
|
+
when Float
|
127
|
+
Math.exp(sum / size)
|
128
|
+
else
|
129
|
+
0 / 0.0
|
130
|
+
end
|
119
131
|
end
|
132
|
+
memoize_method :geometric_mean
|
120
133
|
|
121
134
|
# Returns the minimum of the elements.
|
122
135
|
def min
|
123
|
-
@
|
136
|
+
@elements.min
|
124
137
|
end
|
138
|
+
memoize_method :min
|
125
139
|
|
126
140
|
# Returns the maximum of the elements.
|
127
141
|
def max
|
128
|
-
@
|
142
|
+
@elements.max
|
143
|
+
end
|
144
|
+
memoize_method :max
|
145
|
+
|
146
|
+
# Return a sorted array of the elements.
|
147
|
+
def sorted
|
148
|
+
@elements.sort
|
129
149
|
end
|
150
|
+
memoize_method :sorted
|
130
151
|
|
131
152
|
# Returns the +p+-percentile of the elements.
|
132
153
|
# There are many methods to compute the percentile, this method uses the
|
@@ -136,17 +157,17 @@ module MoreMath
|
|
136
157
|
(0...100).include?(p) or
|
137
158
|
raise ArgumentError, "p = #{p}, but has to be in (0...100)"
|
138
159
|
p /= 100.0
|
139
|
-
|
140
|
-
r = p * (
|
160
|
+
sorted_elements = sorted
|
161
|
+
r = p * (sorted_elements.size + 1)
|
141
162
|
r_i = r.to_i
|
142
163
|
r_f = r - r_i
|
143
164
|
if r_i >= 1
|
144
|
-
result =
|
145
|
-
if r_i <
|
146
|
-
result += r_f * (
|
165
|
+
result = sorted_elements[r_i - 1]
|
166
|
+
if r_i < sorted_elements.size
|
167
|
+
result += r_f * (sorted_elements[r_i] - sorted_elements[r_i - 1])
|
147
168
|
end
|
148
169
|
else
|
149
|
-
result =
|
170
|
+
result = sorted_elements[0]
|
150
171
|
end
|
151
172
|
result
|
152
173
|
end
|
@@ -325,8 +346,9 @@ module MoreMath
|
|
325
346
|
# Returns the LinearRegression object for the equation a * x + b which
|
326
347
|
# represents the line computed by the linear regression algorithm.
|
327
348
|
def linear_regression
|
328
|
-
|
349
|
+
LinearRegression.new @elements
|
329
350
|
end
|
351
|
+
memoize_method :linear_regression
|
330
352
|
|
331
353
|
# Returns a Histogram instance with +bins+ as the number of bins for this
|
332
354
|
# analysis' elements.
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'more_math'
|
2
|
+
|
3
|
+
module MoreMath
|
4
|
+
class StringNumeral
|
5
|
+
include ::MoreMath::NumberifyStringFunction
|
6
|
+
|
7
|
+
def self.from(object, alphabet = 'a'..'z')
|
8
|
+
if object.respond_to?(:to_str)
|
9
|
+
StringNumeral.from_string(object.to_str, alphabet)
|
10
|
+
elsif object.respond_to?(:to_int)
|
11
|
+
StringNumeral.from_number(object.to_int, alphabet)
|
12
|
+
else
|
13
|
+
StringNumeral.from_string(object.to_s, alphabet)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_string(string, alphabet)
|
18
|
+
new string, nil, alphabet
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_number(number, alphabet)
|
22
|
+
new nil, number, alphabet
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(string, number, alphabet)
|
26
|
+
@alphabet = NumberifyStringFunction.convert_alphabet(alphabet).freeze
|
27
|
+
if string
|
28
|
+
@string = string.to_s
|
29
|
+
string.each_char.each do |c|
|
30
|
+
@alphabet.include?(c) or raise ArgumentError,
|
31
|
+
"illegal character #{c.inspect} in #{@string.inspect} for alphabet #{@alphabet.inspect}"
|
32
|
+
end
|
33
|
+
elsif number
|
34
|
+
@number = number.to_i
|
35
|
+
end
|
36
|
+
end
|
37
|
+
private_class_method :new
|
38
|
+
|
39
|
+
def number
|
40
|
+
@number ||= numberify_string(@string, @alphabet)
|
41
|
+
end
|
42
|
+
alias to_i number
|
43
|
+
|
44
|
+
def string
|
45
|
+
@string ||= stringify_number(@number, @alphabet).freeze
|
46
|
+
end
|
47
|
+
|
48
|
+
alias to_s string
|
49
|
+
|
50
|
+
def inspect
|
51
|
+
"#<#{self.class}: #{string.inspect} #{number.inspect}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :alphabet
|
55
|
+
|
56
|
+
def coerce(other)
|
57
|
+
[ naturalize(other), number ]
|
58
|
+
end
|
59
|
+
|
60
|
+
def *(other)
|
61
|
+
self.class.from_number(number * naturalize(other), @alphabet)
|
62
|
+
end
|
63
|
+
|
64
|
+
def +(other)
|
65
|
+
self.class.from_number(number + naturalize(other), @alphabet)
|
66
|
+
end
|
67
|
+
|
68
|
+
def -(other)
|
69
|
+
self.class.from_number(naturalize(number - other), @alphabet)
|
70
|
+
end
|
71
|
+
|
72
|
+
def /(other)
|
73
|
+
self.class.from_number((number / naturalize(other)), @alphabet)
|
74
|
+
end
|
75
|
+
|
76
|
+
def %(other)
|
77
|
+
self.class.from_number((number % naturalize(other)), @alphabet)
|
78
|
+
end
|
79
|
+
|
80
|
+
def **(other)
|
81
|
+
self.class.from_number(number ** naturalize(other), @alphabet)
|
82
|
+
end
|
83
|
+
|
84
|
+
def <<(other)
|
85
|
+
self.class.from_number(number << naturalize(other), @alphabet)
|
86
|
+
end
|
87
|
+
|
88
|
+
def >>(other)
|
89
|
+
self.class.from_number(number >> naturalize(other), @alphabet)
|
90
|
+
end
|
91
|
+
|
92
|
+
def ^(other)
|
93
|
+
self.class.from_number(number ^ naturalize(other), @alphabet)
|
94
|
+
end
|
95
|
+
|
96
|
+
def &(other)
|
97
|
+
self.class.from_number(number & naturalize(other), @alphabet)
|
98
|
+
end
|
99
|
+
|
100
|
+
def |(other)
|
101
|
+
self.class.from_number(number | naturalize(other), @alphabet)
|
102
|
+
end
|
103
|
+
|
104
|
+
def [](other)
|
105
|
+
self.class.from_number(number[other.to_i], @alphabet)
|
106
|
+
end
|
107
|
+
|
108
|
+
def succ
|
109
|
+
self.class.from_number(number + 1, @alphabet)
|
110
|
+
end
|
111
|
+
|
112
|
+
def succ!
|
113
|
+
@number += 1
|
114
|
+
@string = nil
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
def pred
|
119
|
+
self.class.from_number(naturalize(number - 1), @alphabet)
|
120
|
+
end
|
121
|
+
|
122
|
+
def pred!
|
123
|
+
@number = naturalize(@number - 1)
|
124
|
+
@string = nil
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
128
|
+
def to_string_numeral
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def naturalize(number)
|
135
|
+
number = number.to_i
|
136
|
+
number < 0 ? 0 : number
|
137
|
+
end
|
138
|
+
|
139
|
+
module Functions
|
140
|
+
def StringNumeral(other, alphabet = 'a'..'z')
|
141
|
+
::MoreMath::StringNumeral.from(other, alphabet)
|
142
|
+
end
|
143
|
+
|
144
|
+
def to_sn(alphabet = 'a'..'z')
|
145
|
+
StringNumeral === self ? self : StringNumeral.from(self, alphabet)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
class ::Object
|
151
|
+
include StringNumeral::Functions
|
152
|
+
end
|
153
|
+
end
|
data/lib/more_math/version.rb
CHANGED
data/more_math.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{more_math}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Florian Frank"]
|
9
|
+
s.date = %q{2011-07-17}
|
10
|
+
s.description = %q{Library that provides more mathematical functions/algorithms than standard Ruby.}
|
11
|
+
s.email = %q{flori@ping.de}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/more_math/cantor_pairing_function.rb", "lib/more_math/version.rb", "lib/more_math/functions.rb", "lib/more_math/sequence.rb", "lib/more_math/linear_regression.rb", "lib/more_math/string_numeral.rb", "lib/more_math/histogram.rb", "lib/more_math/constants/functions_constants.rb", "lib/more_math/numberify_string_function.rb", "lib/more_math/distributions.rb", "lib/more_math/exceptions.rb", "lib/more_math/newton_bisection.rb", "lib/more_math/continued_fraction.rb", "lib/more_math.rb"]
|
13
|
+
s.files = [".gitignore", ".travis.yml", "CHANGES", "Gemfile", "LICENSE", "README.rdoc", "Rakefile", "VERSION", "lib/more_math.rb", "lib/more_math/cantor_pairing_function.rb", "lib/more_math/constants/functions_constants.rb", "lib/more_math/continued_fraction.rb", "lib/more_math/distributions.rb", "lib/more_math/exceptions.rb", "lib/more_math/functions.rb", "lib/more_math/histogram.rb", "lib/more_math/linear_regression.rb", "lib/more_math/newton_bisection.rb", "lib/more_math/numberify_string_function.rb", "lib/more_math/sequence.rb", "lib/more_math/string_numeral.rb", "lib/more_math/version.rb", "more_math.gemspec", "tests/test_analysis.rb", "tests/test_cantor_pairing_function.rb", "tests/test_continued_fraction.rb", "tests/test_distribution.rb", "tests/test_functions.rb", "tests/test_histogram.rb", "tests/test_newton_bisection.rb", "tests/test_numberify_string_function.rb"]
|
14
|
+
s.homepage = %q{http://flori.github.com/more_math}
|
15
|
+
s.rdoc_options = ["--title", "MoreMath -- More Math in Ruby", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubygems_version = %q{1.6.2}
|
18
|
+
s.summary = %q{Library that provides more mathematics.}
|
19
|
+
s.test_files = ["tests/test_cantor_pairing_function.rb", "tests/test_continued_fraction.rb", "tests/test_newton_bisection.rb", "tests/test_analysis.rb", "tests/test_functions.rb", "tests/test_distribution.rb", "tests/test_histogram.rb", "tests/test_numberify_string_function.rb"]
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
s.add_development_dependency(%q<gem_hadar>, ["~> 0.0.5"])
|
26
|
+
s.add_runtime_dependency(%q<spruz>, ["~> 0.2"])
|
27
|
+
else
|
28
|
+
s.add_dependency(%q<gem_hadar>, ["~> 0.0.5"])
|
29
|
+
s.add_dependency(%q<spruz>, ["~> 0.2"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<gem_hadar>, ["~> 0.0.5"])
|
33
|
+
s.add_dependency(%q<spruz>, ["~> 0.2"])
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'more_math'
|
5
|
+
|
6
|
+
class TestNumberifyStringFunction < Test::Unit::TestCase
|
7
|
+
include MoreMath::Functions
|
8
|
+
|
9
|
+
def test_log_ceil
|
10
|
+
assert_raise(ArgumentError) { log_ceil(-1) }
|
11
|
+
assert_raise(ArgumentError) { log_ceil(0) }
|
12
|
+
for x in 1..256
|
13
|
+
assert_equal logb(x).ceil, log_ceil(x), "log_ceil(#{x}) failed"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_log_floor
|
18
|
+
assert_raise(ArgumentError) { log_floor(-1) }
|
19
|
+
assert_raise(ArgumentError) { log_floor(0) }
|
20
|
+
for x in 1..256
|
21
|
+
assert_equal logb(x).floor, log_floor(x), "log_floor(#{x}) failed"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_numberify_string_function
|
26
|
+
assert_equal 0, numberify_string('', 'ab')
|
27
|
+
assert_equal 1, numberify_string('a', 'ab')
|
28
|
+
assert_equal 2, numberify_string('b', 'ab')
|
29
|
+
assert_equal 3, numberify_string('aa', 'ab')
|
30
|
+
assert_equal 4, numberify_string('ab', 'ab')
|
31
|
+
assert_equal 5, numberify_string('ba', 'ab')
|
32
|
+
assert_equal 6, numberify_string('bb', 'ab')
|
33
|
+
|
34
|
+
assert_equal 0, numberify_string('', 'abc')
|
35
|
+
assert_equal 1, numberify_string('a', 'abc')
|
36
|
+
assert_equal 2, numberify_string('b', 'abc')
|
37
|
+
assert_equal 3, numberify_string('c', 'abc')
|
38
|
+
assert_equal 4, numberify_string('aa', 'abc')
|
39
|
+
assert_equal 5, numberify_string('ab', 'abc')
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_numberify_string_inv_function
|
43
|
+
assert_raise(ArgumentError) { stringify_number(-1, 'ab') }
|
44
|
+
assert_equal '', stringify_number(0, 'ab')
|
45
|
+
assert_equal 'a', stringify_number(1, 'ab')
|
46
|
+
assert_equal 'b', stringify_number(2, 'ab')
|
47
|
+
assert_equal 'aa', stringify_number(3, 'ab')
|
48
|
+
assert_equal 'ab', stringify_number(4, 'ab')
|
49
|
+
assert_equal 'ba', stringify_number(5, 'ab')
|
50
|
+
assert_equal 'bb', stringify_number(6, 'ab')
|
51
|
+
|
52
|
+
assert_raise(ArgumentError) { stringify_number(-1, 'abc') }
|
53
|
+
assert_equal '', stringify_number(0, 'abc')
|
54
|
+
assert_equal 'a', stringify_number(1, 'abc')
|
55
|
+
assert_equal 'b', stringify_number(2, 'abc')
|
56
|
+
assert_equal 'c', stringify_number(3, 'abc')
|
57
|
+
assert_equal 'aa', stringify_number(4, 'abc')
|
58
|
+
assert_equal 'ab', stringify_number(5, 'abc')
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: more_math
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Florian Frank
|
@@ -14,22 +15,40 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-07-17 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
22
|
+
name: gem_hadar
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ~>
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 21
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
- 5
|
34
|
+
version: 0.0.5
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: spruz
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 15
|
27
46
|
segments:
|
28
47
|
- 0
|
29
48
|
- 2
|
30
49
|
version: "0.2"
|
31
50
|
type: :runtime
|
32
|
-
version_requirements: *
|
51
|
+
version_requirements: *id002
|
33
52
|
description: Library that provides more mathematical functions/algorithms than standard Ruby.
|
34
53
|
email: flori@ping.de
|
35
54
|
executables: []
|
@@ -37,34 +56,53 @@ executables: []
|
|
37
56
|
extensions: []
|
38
57
|
|
39
58
|
extra_rdoc_files:
|
40
|
-
- README
|
41
|
-
|
42
|
-
- CHANGES
|
43
|
-
- VERSION
|
44
|
-
- README
|
45
|
-
- make_doc.rb
|
46
|
-
- Rakefile
|
47
|
-
- lib/more_math/cantor_pairing_funtion.rb
|
48
|
-
- lib/more_math/functions.rb
|
49
|
-
- lib/more_math/distributions.rb
|
50
|
-
- lib/more_math/newton_bisection.rb
|
51
|
-
- lib/more_math/constants/functions_constants.rb
|
59
|
+
- README.rdoc
|
60
|
+
- lib/more_math/cantor_pairing_function.rb
|
52
61
|
- lib/more_math/version.rb
|
62
|
+
- lib/more_math/functions.rb
|
53
63
|
- lib/more_math/sequence.rb
|
54
64
|
- lib/more_math/linear_regression.rb
|
65
|
+
- lib/more_math/string_numeral.rb
|
55
66
|
- lib/more_math/histogram.rb
|
56
|
-
- lib/more_math/
|
67
|
+
- lib/more_math/constants/functions_constants.rb
|
68
|
+
- lib/more_math/numberify_string_function.rb
|
69
|
+
- lib/more_math/distributions.rb
|
57
70
|
- lib/more_math/exceptions.rb
|
71
|
+
- lib/more_math/newton_bisection.rb
|
72
|
+
- lib/more_math/continued_fraction.rb
|
58
73
|
- lib/more_math.rb
|
74
|
+
files:
|
75
|
+
- .gitignore
|
76
|
+
- .travis.yml
|
77
|
+
- CHANGES
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE
|
80
|
+
- README.rdoc
|
81
|
+
- Rakefile
|
82
|
+
- VERSION
|
83
|
+
- lib/more_math.rb
|
84
|
+
- lib/more_math/cantor_pairing_function.rb
|
85
|
+
- lib/more_math/constants/functions_constants.rb
|
86
|
+
- lib/more_math/continued_fraction.rb
|
87
|
+
- lib/more_math/distributions.rb
|
88
|
+
- lib/more_math/exceptions.rb
|
89
|
+
- lib/more_math/functions.rb
|
90
|
+
- lib/more_math/histogram.rb
|
91
|
+
- lib/more_math/linear_regression.rb
|
92
|
+
- lib/more_math/newton_bisection.rb
|
93
|
+
- lib/more_math/numberify_string_function.rb
|
94
|
+
- lib/more_math/sequence.rb
|
95
|
+
- lib/more_math/string_numeral.rb
|
96
|
+
- lib/more_math/version.rb
|
97
|
+
- more_math.gemspec
|
98
|
+
- tests/test_analysis.rb
|
99
|
+
- tests/test_cantor_pairing_function.rb
|
59
100
|
- tests/test_continued_fraction.rb
|
101
|
+
- tests/test_distribution.rb
|
60
102
|
- tests/test_functions.rb
|
61
|
-
- tests/test_analysis.rb
|
62
103
|
- tests/test_histogram.rb
|
63
|
-
- tests/test_distribution.rb
|
64
|
-
- tests/test_cantor_pairing_function.rb
|
65
104
|
- tests/test_newton_bisection.rb
|
66
|
-
-
|
67
|
-
- LICENSE
|
105
|
+
- tests/test_numberify_string_function.rb
|
68
106
|
has_rdoc: true
|
69
107
|
homepage: http://flori.github.com/more_math
|
70
108
|
licenses: []
|
@@ -74,35 +112,40 @@ rdoc_options:
|
|
74
112
|
- --title
|
75
113
|
- MoreMath -- More Math in Ruby
|
76
114
|
- --main
|
77
|
-
- README
|
115
|
+
- README.rdoc
|
78
116
|
require_paths:
|
79
117
|
- lib
|
80
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
81
120
|
requirements:
|
82
121
|
- - ">="
|
83
122
|
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
84
124
|
segments:
|
85
125
|
- 0
|
86
126
|
version: "0"
|
87
127
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
88
129
|
requirements:
|
89
130
|
- - ">="
|
90
131
|
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
91
133
|
segments:
|
92
134
|
- 0
|
93
135
|
version: "0"
|
94
136
|
requirements: []
|
95
137
|
|
96
|
-
rubyforge_project:
|
97
|
-
rubygems_version: 1.
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 1.6.2
|
98
140
|
signing_key:
|
99
141
|
specification_version: 3
|
100
142
|
summary: Library that provides more mathematics.
|
101
143
|
test_files:
|
144
|
+
- tests/test_cantor_pairing_function.rb
|
102
145
|
- tests/test_continued_fraction.rb
|
103
|
-
- tests/
|
146
|
+
- tests/test_newton_bisection.rb
|
104
147
|
- tests/test_analysis.rb
|
105
|
-
- tests/
|
148
|
+
- tests/test_functions.rb
|
106
149
|
- tests/test_distribution.rb
|
107
|
-
- tests/
|
108
|
-
- tests/
|
150
|
+
- tests/test_histogram.rb
|
151
|
+
- tests/test_numberify_string_function.rb
|
data/install.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'rbconfig'
|
4
|
-
require 'fileutils'
|
5
|
-
include FileUtils::Verbose
|
6
|
-
|
7
|
-
include Config
|
8
|
-
|
9
|
-
file = 'lib/more_math.rb'
|
10
|
-
libdir = CONFIG["sitelibdir"]
|
11
|
-
install(file, libdir, :mode => 0755)
|
12
|
-
mkdir_p subdir = File.join(libdir, 'more_math')
|
13
|
-
for f in Dir['lib/more_math/*.rb']
|
14
|
-
install(f, subdir)
|
15
|
-
end
|
16
|
-
mkdir_p subdir = File.join(libdir, 'more_math', 'constants')
|
17
|
-
for f in Dir['lib/more_math/constants/*.rb']
|
18
|
-
install(f, subdir)
|
19
|
-
end
|