ruby-nuggets 0.9.6.pre1 → 0.9.6.pre2
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.
- checksums.yaml +4 -4
- data/lib/nuggets/array/flush.rb +5 -0
- data/lib/nuggets/array/flush_mixin.rb +47 -0
- data/lib/nuggets/array/mean_mixin.rb +21 -2
- data/lib/nuggets/env/user_home_mixin.rb +6 -0
- data/lib/nuggets/lsi.rb +149 -67
- data/lib/nuggets/midos.rb +1 -3
- data/lib/nuggets/ruby.rb +5 -9
- data/lib/nuggets/uri/redirect_mixin.rb +1 -1
- data/lib/nuggets/version.rb +1 -1
- data/spec/nuggets/array/flush_spec.rb +45 -0
- data/spec/nuggets/array/mean_spec.rb +39 -6
- data/spec/nuggets/array/variance_spec.rb +16 -0
- metadata +15 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 204c7a58b06959ce60a857c5e70c60ca6e278e09
|
4
|
+
data.tar.gz: ecbcbaaa1a3ed0df93694678a4f42811dd406385
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 070ece04d3311e4eb79cf033e89f404ce79ed19027fadd1d6fd97ae9eeb9b6df1d6aea7dbf545ddd592d5a4950fdde74763a4111bc0bae7496bcc63638f330de
|
7
|
+
data.tar.gz: 18bda153c1274c40e83fb65149fc2306ea9581239ee4d3b7b3ea80d972e8da4cdb5a251471eb2ea76d1bc065a1cb80637fc40787664b4f04defddd42e430fe4b
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2013 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU Affero General Public License as published by #
|
14
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
15
|
+
# option) any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
|
20
|
+
# for more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU Affero General Public License #
|
23
|
+
# along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
module Nuggets
|
29
|
+
class Array
|
30
|
+
module FlushMixin
|
31
|
+
|
32
|
+
# call-seq:
|
33
|
+
# array.flush => anArray
|
34
|
+
# array.flush { |array| ... } => anObject
|
35
|
+
#
|
36
|
+
# If a block is given, yields _array_ to the block and
|
37
|
+
# clears it afterwards; returns the return value of the
|
38
|
+
# block. Otherwise clears _array_ and returns its contents.
|
39
|
+
def flush
|
40
|
+
ret = block_given? ? yield(self) : dup
|
41
|
+
clear
|
42
|
+
ret
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
5
|
# language. #
|
6
6
|
# #
|
7
|
-
# Copyright (C) 2007-
|
7
|
+
# Copyright (C) 2007-2013 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
10
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -145,7 +145,12 @@ module Nuggets
|
|
145
145
|
# with the standard deviation, if available; all values are subject to
|
146
146
|
# +precision+.
|
147
147
|
#
|
148
|
-
#
|
148
|
+
# If _array_ is a flat array of numeric values, it is treated as a single
|
149
|
+
# "column".
|
150
|
+
#
|
151
|
+
# Returns +nil+ if _array_ is empty.
|
152
|
+
#
|
153
|
+
# Examples (with standard deviation):
|
149
154
|
#
|
150
155
|
# [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]].report_mean
|
151
156
|
# #=> ["9.4567 +/- 0.0450", "34.6800 +/- 0.0572"]
|
@@ -158,7 +163,21 @@ module Nuggets
|
|
158
163
|
#
|
159
164
|
# CSV.read('csv', headers: true, converters: :numeric).to_a.report_mean
|
160
165
|
# #=> ["a 9.4567 +/- 0.0450", "b 34.6800 +/- 0.0572"]
|
166
|
+
#
|
167
|
+
# [9.4, 9.46, 9.51].report_mean
|
168
|
+
# #=> ["9.4567 +/- 0.0450"]
|
169
|
+
#
|
170
|
+
# [34.75, 34.68, 34.61].report_mean
|
171
|
+
# #=> ["34.6800 +/- 0.0572"]
|
172
|
+
#
|
173
|
+
# [].report_mean
|
174
|
+
# #=> nil
|
161
175
|
def report_mean(method = nil, precision = 4)
|
176
|
+
return if empty?
|
177
|
+
|
178
|
+
return clone.replace(self.class.new.push(self).transpose).
|
179
|
+
report_mean(method, precision) unless first.is_a?(self.class)
|
180
|
+
|
162
181
|
met, sep = [method ||= :mean, 'mean'], ['', '_']
|
163
182
|
lab, std = first.first.is_a?(::String), respond_to?(:std)
|
164
183
|
|
@@ -34,6 +34,12 @@ module Nuggets
|
|
34
34
|
#
|
35
35
|
# Returns the user's home directory, or +default+ if it could not be found.
|
36
36
|
def user_home(default = ::File::ALT_SEPARATOR ? 'C:/' : '/')
|
37
|
+
begin
|
38
|
+
return ::Dir.home
|
39
|
+
rescue ::ArgumentError
|
40
|
+
# "couldn't find HOME environment -- expanding `~'"
|
41
|
+
end if ::Dir.respond_to?(:home)
|
42
|
+
|
37
43
|
%w[HOME HOMEDRIVE:HOMEPATH USERPROFILE APPDATA].each { |key|
|
38
44
|
home = values_at(*key.split(':')).join
|
39
45
|
return home.gsub(/\\/, '/') if home && !home.empty?
|
data/lib/nuggets/lsi.rb
CHANGED
@@ -25,32 +25,55 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
+
require 'forwardable'
|
28
29
|
require 'gsl'
|
29
30
|
|
30
31
|
module Nuggets
|
31
32
|
|
32
33
|
class LSI
|
33
34
|
|
34
|
-
include Enumerable
|
35
|
+
include ::Enumerable
|
35
36
|
|
36
|
-
|
37
|
+
extend ::Forwardable
|
38
|
+
|
39
|
+
DEFAULT_EPSILON = ::Float::EPSILON * 10
|
40
|
+
|
41
|
+
DEFAULT_PRECISION = 2
|
42
|
+
|
43
|
+
DEFAULT_TRANSFORM = :tfidf
|
44
|
+
|
45
|
+
DEFAULT_CUTOFF = 0.75
|
46
|
+
|
47
|
+
class << self
|
48
|
+
|
49
|
+
def build(items, options = {})
|
50
|
+
lsi = new(items)
|
51
|
+
lsi if lsi.build(options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def each_norm(items, options = {}, build_options = {}, &block)
|
55
|
+
lsi = new(items)
|
56
|
+
lsi.each_norm(nil, options, &block) if lsi.build(build_options)
|
57
|
+
end
|
37
58
|
|
38
|
-
def self.each_norm(items, options = {}, &block)
|
39
|
-
lsi = new(items)
|
40
|
-
lsi.each_norm(nil, options, &block) if lsi.build
|
41
59
|
end
|
42
60
|
|
43
61
|
def initialize(items = {})
|
44
62
|
reset
|
45
|
-
items.each { |k, v| self[k] = v }
|
63
|
+
items.each { |k, v| self[k] = v || k }
|
46
64
|
end
|
47
65
|
|
48
|
-
|
49
|
-
|
50
|
-
|
66
|
+
def_delegators :@hash, :[], :each, :include?, :key, :keys, :size
|
67
|
+
|
68
|
+
def_delegator :@hash, :values, :docs
|
69
|
+
def_delegator :@hash, :values_at, :docs_at
|
70
|
+
|
71
|
+
def_delegator :@list, :keys, :terms
|
72
|
+
|
73
|
+
alias_method :doc, :[]
|
51
74
|
|
52
75
|
def []=(key, value)
|
53
|
-
@hash[key] = Doc.new(key, value, @list)
|
76
|
+
@hash[key] = Doc.new(key, value, @list, @freq)
|
54
77
|
end
|
55
78
|
|
56
79
|
def add(key, value = key)
|
@@ -62,27 +85,13 @@ module Nuggets
|
|
62
85
|
add(value.object_id, value)
|
63
86
|
end
|
64
87
|
|
65
|
-
def size
|
66
|
-
@hash.size
|
67
|
-
end
|
68
|
-
|
69
|
-
def keys
|
70
|
-
@hash.keys
|
71
|
-
end
|
72
|
-
|
73
|
-
def docs
|
74
|
-
@hash.values
|
75
|
-
end
|
76
|
-
|
77
|
-
def each(&block)
|
78
|
-
@hash.each(&block)
|
79
|
-
end
|
80
|
-
|
81
88
|
# min:: minimum value to consider
|
82
89
|
# abs:: minimum absolute value to consider
|
83
90
|
# nul:: exclude null values (true or Float)
|
84
91
|
# new:: exclude original terms / only yield new ones
|
85
92
|
def each_norm(key = nil, options = {})
|
93
|
+
return enum_for(:each_norm, key, options) unless block_given?
|
94
|
+
|
86
95
|
min, abs, nul, new = options.values_at(:min, :abs, :nul, :new)
|
87
96
|
nul = DEFAULT_EPSILON if nul == true
|
88
97
|
|
@@ -90,14 +99,14 @@ module Nuggets
|
|
90
99
|
|
91
100
|
(key ? [self[key]] : docs).each { |doc|
|
92
101
|
if doc && norm = doc.norm
|
93
|
-
i =
|
102
|
+
i = -1
|
94
103
|
|
95
104
|
norm.each { |v|
|
105
|
+
i += 1
|
96
106
|
yield doc, list[i], v unless (min && v < min) ||
|
97
107
|
(abs && v.abs < abs) ||
|
98
108
|
(nul && v.abs < nul) ||
|
99
109
|
(new && doc.include?(i))
|
100
|
-
i += 1
|
101
110
|
}
|
102
111
|
end
|
103
112
|
}
|
@@ -105,87 +114,160 @@ module Nuggets
|
|
105
114
|
|
106
115
|
def related(key, num = 5)
|
107
116
|
if doc = self[key] and norm = doc.norm
|
108
|
-
|
109
|
-
|
110
|
-
|
117
|
+
temp = sort_by { |k, v| -norm * v.norm.col }
|
118
|
+
temp.map! { |k,| k }.delete(key)
|
119
|
+
temp[0, num]
|
111
120
|
end
|
112
121
|
end
|
113
122
|
|
114
|
-
def
|
115
|
-
|
123
|
+
def related_score(key, num = 5)
|
124
|
+
if doc = self[key] and norm = doc.norm
|
125
|
+
temp = map { |k, v| [k, norm * v.norm.col] }.sort_by { |_, i| -i }
|
126
|
+
temp.delete(temp.assoc(key))
|
127
|
+
temp[0, num]
|
128
|
+
end
|
116
129
|
end
|
117
130
|
|
118
|
-
def
|
119
|
-
|
131
|
+
def build(options = {})
|
132
|
+
build!(docs, @list, options.is_a?(::Hash) ?
|
133
|
+
options : { :cutoff => options }) if size > 1
|
120
134
|
end
|
121
135
|
|
122
|
-
|
136
|
+
def reset
|
137
|
+
@hash, @list, @freq, @invlist =
|
138
|
+
{}, ::Hash.new { |h, k| h[k] = h.size }, ::Hash.new(0), {}
|
139
|
+
end
|
123
140
|
|
124
|
-
def
|
125
|
-
|
126
|
-
reduce(u, v, cutoff(s, cutoff), docs)
|
127
|
-
size
|
141
|
+
def inspect
|
142
|
+
'%s@%d/%d' % [self.class, size, @list.size]
|
128
143
|
end
|
129
144
|
|
130
|
-
def
|
131
|
-
|
132
|
-
|
145
|
+
def to_a(norm = true)
|
146
|
+
(norm ? map { |_, doc| doc.norm.to_a } :
|
147
|
+
map { |_, doc| doc.vector.to_a }).transpose
|
133
148
|
end
|
134
149
|
|
135
|
-
|
136
|
-
|
137
|
-
|
150
|
+
private
|
151
|
+
|
152
|
+
def build!(docs, list, options)
|
153
|
+
Doc.transform = options.fetch(:transform, DEFAULT_TRANSFORM)
|
154
|
+
|
155
|
+
@invlist = list.invert
|
156
|
+
|
157
|
+
# TODO: GSL::ERROR::EUNIMPL: Ruby/GSL error code 24, svd of
|
158
|
+
# MxN matrix, M<N, is not implemented (file svd.c, line 61)
|
159
|
+
u, v, s = matrix(docs, list.size, size = docs.size).SV_decomp
|
138
160
|
|
139
|
-
|
140
|
-
|
161
|
+
r, i = reduce(s, options.fetch(:cutoff, DEFAULT_CUTOFF)), -1
|
162
|
+
(u * r * v.trans).each_col { |c| docs[i += 1].vector = c.row }
|
163
|
+
|
164
|
+
size
|
141
165
|
end
|
142
166
|
|
143
|
-
def
|
144
|
-
|
145
|
-
|
146
|
-
|
167
|
+
def matrix(d = docs, m = @list.size, n = d.size)
|
168
|
+
x = ::GSL::Matrix.alloc(m, n)
|
169
|
+
d.each_with_index { |i, j| x.set_col(j, i.transformed_vector(m, n)) }
|
170
|
+
x
|
147
171
|
end
|
148
172
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
173
|
+
# k == nil:: keep all
|
174
|
+
# k >= 1:: keep this many
|
175
|
+
# k < 1:: keep (at most) this proportion
|
176
|
+
def reduce(s, k, m = s.size)
|
177
|
+
if k && k < m
|
178
|
+
if k > 0
|
179
|
+
k = (m * k).floor if k < 1
|
180
|
+
s[k, m - k] = 0
|
181
|
+
else
|
182
|
+
s.set_zero
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
s.to_m_diagonal
|
153
187
|
end
|
154
188
|
|
155
189
|
class Doc
|
156
190
|
|
191
|
+
include ::Enumerable
|
192
|
+
|
193
|
+
extend ::Forwardable
|
194
|
+
|
157
195
|
TOKEN_RE = %r{\s+}
|
158
196
|
|
159
|
-
|
160
|
-
|
161
|
-
|
197
|
+
class << self
|
198
|
+
|
199
|
+
attr_reader :transform
|
200
|
+
|
201
|
+
def transform=(transform)
|
202
|
+
method = :transformed_vector
|
203
|
+
|
204
|
+
case transform
|
205
|
+
when ::Proc then define_method(method, &transform)
|
206
|
+
when ::UnboundMethod then define_method(method, transform)
|
207
|
+
else alias_method(method, "#{transform ||= :raw}_vector")
|
208
|
+
end
|
209
|
+
|
210
|
+
@transform = transform
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
def initialize(key, value, list, freq)
|
216
|
+
@key, @list, @freq, @total = key, list, freq, 1
|
217
|
+
|
218
|
+
@map = !value.is_a?(::Hash) ? build_hash(value, list) :
|
162
219
|
value.inject({}) { |h, (k, v)| h[list[k]] = v; h }
|
220
|
+
|
221
|
+
@map.each_key { |k| freq[k] += 1 }
|
222
|
+
|
223
|
+
self.vector = raw_vector
|
163
224
|
end
|
164
225
|
|
165
226
|
attr_reader :key, :vector, :norm
|
166
227
|
|
167
|
-
|
168
|
-
|
169
|
-
|
228
|
+
def_delegators :@map, :each, :include?
|
229
|
+
|
230
|
+
def_delegator :raw_vector, :sum, :size
|
231
|
+
|
232
|
+
def raw_vector(size = @list.size, *)
|
233
|
+
vec = ::GSL::Vector.calloc(size)
|
234
|
+
each { |k, v| vec[k] = v }
|
170
235
|
vec
|
171
236
|
end
|
172
237
|
|
238
|
+
# TODO: "first-order association transform" ???
|
239
|
+
def foat_vector(*args)
|
240
|
+
vec, q = raw_vector(*args), 0
|
241
|
+
return vec unless (s = vec.sum) > 1
|
242
|
+
|
243
|
+
vec.each { |v| q -= (w = v / s) * ::Math.log(w) if v > 0 }
|
244
|
+
vec.map { |v| ::Math.log(v + 1) / q }
|
245
|
+
end
|
246
|
+
|
247
|
+
def tfidf_vector(*args)
|
248
|
+
vec, f, i = raw_vector(*args), @freq, -1
|
249
|
+
s, d = vec.sum, @total = args.fetch(1, @total).to_f
|
250
|
+
vec.map { |v| i += 1; v > 0 ? ::Math.log(d / f[i]) * v / s : v }
|
251
|
+
end
|
252
|
+
|
253
|
+
self.transform = DEFAULT_TRANSFORM
|
254
|
+
|
173
255
|
def vector=(vec)
|
174
256
|
@vector, @norm = vec, vec.normalize
|
175
257
|
end
|
176
258
|
|
177
|
-
def
|
178
|
-
|
259
|
+
def inspect
|
260
|
+
'%s@%p/%d' % [self.class, key, size]
|
179
261
|
end
|
180
262
|
|
181
263
|
private
|
182
264
|
|
183
|
-
def build_hash(value, list, hash = Hash.new(0))
|
184
|
-
|
265
|
+
def build_hash(value, list, hash = ::Hash.new(0))
|
266
|
+
build_enum(value).each { |i| hash[list[i]] += 1 }
|
185
267
|
hash
|
186
268
|
end
|
187
269
|
|
188
|
-
def
|
270
|
+
def build_enum(value, re = TOKEN_RE)
|
189
271
|
value = value.read if value.respond_to?(:read)
|
190
272
|
value = value.split(re) if value.respond_to?(:split)
|
191
273
|
value
|
data/lib/nuggets/midos.rb
CHANGED
data/lib/nuggets/ruby.rb
CHANGED
@@ -259,15 +259,11 @@ module Nuggets
|
|
259
259
|
opt = "-#{key.to_s[0, 1]}"
|
260
260
|
|
261
261
|
if val.is_a?(::Array)
|
262
|
-
val.each { |wal|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
argv << opt << val.to_s
|
268
|
-
elsif val != false
|
269
|
-
argv << "#{opt}#{val unless val == true}"
|
270
|
-
end
|
262
|
+
val.each { |wal| argv << opt << wal.to_s }
|
263
|
+
elsif opt == '-e'
|
264
|
+
argv << opt << val.to_s
|
265
|
+
elsif val != false
|
266
|
+
argv << "#{opt}#{val unless val == true}"
|
271
267
|
end
|
272
268
|
} if args.last.is_a?(::Hash)
|
273
269
|
|
data/lib/nuggets/version.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'nuggets/array/flush'
|
2
|
+
|
3
|
+
describe Array, 'when extended by', Nuggets::Array::FlushMixin do
|
4
|
+
|
5
|
+
it { Array.ancestors.should include(Nuggets::Array::FlushMixin) }
|
6
|
+
|
7
|
+
example do
|
8
|
+
a = [1, 2, 3, 4]
|
9
|
+
a.flush.should == [1, 2, 3, 4]
|
10
|
+
a.should be_empty
|
11
|
+
end
|
12
|
+
|
13
|
+
example do
|
14
|
+
a = [1, 2, 3, 4]
|
15
|
+
a.flush.push(42).should == [1, 2, 3, 4, 42]
|
16
|
+
a.should be_empty
|
17
|
+
end
|
18
|
+
|
19
|
+
example do
|
20
|
+
a = [1, 2, 3, 4]
|
21
|
+
a.flush { |b| b.should == [1, 2, 3, 4]; 42 }.should == 42
|
22
|
+
a.should be_empty
|
23
|
+
end
|
24
|
+
|
25
|
+
example do
|
26
|
+
a = [1, 2, 3, 4]
|
27
|
+
a.flush { |b| b }.should be_empty
|
28
|
+
a.should be_empty
|
29
|
+
end
|
30
|
+
|
31
|
+
example do
|
32
|
+
a, c = [1, 2, 3, 4], nil
|
33
|
+
a.flush { |b| c = b; c.should == [1, 2, 3, 4] }
|
34
|
+
a.should be_empty
|
35
|
+
c.should be_empty
|
36
|
+
end
|
37
|
+
|
38
|
+
example do
|
39
|
+
a, c = [1, 2, 3, 4], nil
|
40
|
+
a.flush { |b| c = b.dup; c.should == [1, 2, 3, 4] }
|
41
|
+
a.should be_empty
|
42
|
+
c.should == [1, 2, 3, 4]
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'nuggets/array/mean'
|
2
|
+
require 'nuggets/array/standard_deviation'
|
2
3
|
|
3
4
|
describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
4
5
|
|
@@ -77,6 +78,14 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
|
77
78
|
[-3, -2, -1].arithmetic_mean.should == -[3, 2, 1].arithmetic_mean
|
78
79
|
end
|
79
80
|
|
81
|
+
example do
|
82
|
+
[1e16, 1, 3, -1e16].arithmetic_mean.should == 1.0
|
83
|
+
end
|
84
|
+
|
85
|
+
example do
|
86
|
+
pending { [1e30, 1, 3, -1e30].arithmetic_mean.should == 1.0 }
|
87
|
+
end
|
88
|
+
|
80
89
|
context do
|
81
90
|
|
82
91
|
before :each do
|
@@ -124,12 +133,17 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
|
124
133
|
|
125
134
|
before :each do
|
126
135
|
@ary = [[9.4, 34.75], [9.46, 34.68], [9.51, 34.61]]
|
136
|
+
|
137
|
+
@ary1 = @ary.map(&:first)
|
138
|
+
@ary2 = @ary.map(&:last)
|
127
139
|
end
|
128
140
|
|
129
141
|
describe 'w/o stddev' do
|
130
142
|
|
131
143
|
before :each do
|
132
|
-
|
144
|
+
[@ary, @ary1, @ary2].each { |ary|
|
145
|
+
class << ary; undef_method :std; end if ary.respond_to?(:std)
|
146
|
+
}
|
133
147
|
end
|
134
148
|
|
135
149
|
example do
|
@@ -144,15 +158,22 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
|
144
158
|
@ary.unshift(%w[a b]).report_mean(nil, 2).should == ['a 9.46', 'b 34.68']
|
145
159
|
end
|
146
160
|
|
147
|
-
|
161
|
+
example do
|
162
|
+
@ary1.report_mean.should == ['9.4567']
|
163
|
+
end
|
148
164
|
|
149
|
-
|
165
|
+
example do
|
166
|
+
@ary2.report_mean.should == ['34.6800']
|
167
|
+
end
|
150
168
|
|
151
|
-
|
152
|
-
|
153
|
-
@ary.extend(Nuggets::Array::StandardDeviationMixin)
|
169
|
+
example do
|
170
|
+
[].report_mean.should be_nil
|
154
171
|
end
|
155
172
|
|
173
|
+
end
|
174
|
+
|
175
|
+
describe 'w/ stddev' do
|
176
|
+
|
156
177
|
example do
|
157
178
|
@ary.report_mean.should == ['9.4567 +/- 0.0450', '34.6800 +/- 0.0572']
|
158
179
|
end
|
@@ -165,6 +186,18 @@ describe Array, 'when extended by', Nuggets::Array::MeanMixin do
|
|
165
186
|
@ary.unshift(%w[a b]).report_mean(nil, 2).should == ['a 9.46 +/- 0.04', 'b 34.68 +/- 0.06']
|
166
187
|
end
|
167
188
|
|
189
|
+
example do
|
190
|
+
@ary1.report_mean.should == ['9.4567 +/- 0.0450']
|
191
|
+
end
|
192
|
+
|
193
|
+
example do
|
194
|
+
@ary2.report_mean.should == ['34.6800 +/- 0.0572']
|
195
|
+
end
|
196
|
+
|
197
|
+
example do
|
198
|
+
[].report_mean.should be_nil
|
199
|
+
end
|
200
|
+
|
168
201
|
end
|
169
202
|
|
170
203
|
end
|
@@ -32,6 +32,22 @@ describe Array, 'when extended by', Nuggets::Array::VarianceMixin do
|
|
32
32
|
[1, -2, 1, 2, 3, -4, 0, 3, 1, 2, 1, 0, 24].variance.should equal_float(42.0946745562130218)
|
33
33
|
end
|
34
34
|
|
35
|
+
example do
|
36
|
+
[1, 2, 4, 5, 8].variance.should == 6.0
|
37
|
+
end
|
38
|
+
|
39
|
+
example do
|
40
|
+
[1, 2, 4, 5, 8].variance { |i| i + 1e7 }.should == 6.0
|
41
|
+
end
|
42
|
+
|
43
|
+
example do
|
44
|
+
pending { [1, 2, 4, 5, 8].variance { |i| i + 1e12 }.should == 6.0 }
|
45
|
+
end
|
46
|
+
|
47
|
+
example do
|
48
|
+
([1, 2, 4, 5, 8] * 100).variance.should == 6.0
|
49
|
+
end
|
50
|
+
|
35
51
|
example do
|
36
52
|
[].covariance.should == 0.0
|
37
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.6.
|
4
|
+
version: 0.9.6.pre2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Some extensions to the Ruby programming language.
|
14
14
|
email: jens.wille@gmail.com
|
@@ -31,6 +31,8 @@ files:
|
|
31
31
|
- lib/nuggets/array/correlation.rb
|
32
32
|
- lib/nuggets/array/correlation_mixin.rb
|
33
33
|
- lib/nuggets/array/flatten_once.rb
|
34
|
+
- lib/nuggets/array/flush.rb
|
35
|
+
- lib/nuggets/array/flush_mixin.rb
|
34
36
|
- lib/nuggets/array/format.rb
|
35
37
|
- lib/nuggets/array/histogram.rb
|
36
38
|
- lib/nuggets/array/histogram_mixin.rb
|
@@ -182,6 +184,7 @@ files:
|
|
182
184
|
- Rakefile
|
183
185
|
- spec/nuggets/array/boost_spec.rb
|
184
186
|
- spec/nuggets/array/correlation_spec.rb
|
187
|
+
- spec/nuggets/array/flush_spec.rb
|
185
188
|
- spec/nuggets/array/histogram_spec.rb
|
186
189
|
- spec/nuggets/array/limit_spec.rb
|
187
190
|
- spec/nuggets/array/mean_spec.rb
|
@@ -218,36 +221,36 @@ files:
|
|
218
221
|
- spec/nuggets/uri/content_type_spec.rb
|
219
222
|
- spec/nuggets/uri/exist_spec.rb
|
220
223
|
- spec/spec_helper.rb
|
221
|
-
- .rspec
|
224
|
+
- ".rspec"
|
222
225
|
homepage: http://github.com/blackwinter/ruby-nuggets
|
223
226
|
licenses:
|
224
227
|
- AGPL
|
225
228
|
metadata: {}
|
226
229
|
post_install_message:
|
227
230
|
rdoc_options:
|
228
|
-
- --charset
|
231
|
+
- "--charset"
|
229
232
|
- UTF-8
|
230
|
-
- --line-numbers
|
231
|
-
- --all
|
232
|
-
- --title
|
233
|
-
- ruby-nuggets Application documentation (v0.9.6.
|
234
|
-
- --main
|
233
|
+
- "--line-numbers"
|
234
|
+
- "--all"
|
235
|
+
- "--title"
|
236
|
+
- ruby-nuggets Application documentation (v0.9.6.pre2)
|
237
|
+
- "--main"
|
235
238
|
- README
|
236
239
|
require_paths:
|
237
240
|
- lib
|
238
241
|
required_ruby_version: !ruby/object:Gem::Requirement
|
239
242
|
requirements:
|
240
|
-
- -
|
243
|
+
- - ">="
|
241
244
|
- !ruby/object:Gem::Version
|
242
245
|
version: '0'
|
243
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
244
247
|
requirements:
|
245
|
-
- -
|
248
|
+
- - ">"
|
246
249
|
- !ruby/object:Gem::Version
|
247
250
|
version: 1.3.1
|
248
251
|
requirements: []
|
249
252
|
rubyforge_project:
|
250
|
-
rubygems_version: 2.
|
253
|
+
rubygems_version: 2.1.11
|
251
254
|
signing_key:
|
252
255
|
specification_version: 4
|
253
256
|
summary: Some extensions to the Ruby programming language.
|