ruby-nuggets 0.8.9 → 0.9.0
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 +7 -0
- data/README +3 -3
- data/Rakefile +1 -1
- data/lib/nuggets/all.rb +1 -1
- data/lib/nuggets/all_mixins.rb +1 -1
- data/lib/nuggets/array/boost.rb +5 -0
- data/lib/nuggets/array/boost_mixin.rb +78 -0
- data/lib/nuggets/array/combination.rb +1 -1
- data/lib/nuggets/array/correlation_mixin.rb +2 -2
- data/lib/nuggets/array/flatten_once.rb +1 -1
- data/lib/nuggets/array/format.rb +1 -1
- data/lib/nuggets/array/histogram_mixin.rb +1 -1
- data/lib/nuggets/array/in_order.rb +1 -1
- data/lib/nuggets/array/limit_mixin.rb +1 -1
- data/lib/nuggets/array/mean_mixin.rb +1 -1
- data/lib/nuggets/array/median_mixin.rb +1 -1
- data/lib/nuggets/array/mode_mixin.rb +1 -1
- data/lib/nuggets/array/monotone.rb +1 -1
- data/lib/nuggets/array/only.rb +1 -1
- data/lib/nuggets/array/rand.rb +1 -1
- data/lib/nuggets/array/regression_mixin.rb +100 -20
- data/lib/nuggets/array/runiq_mixin.rb +1 -1
- data/lib/nuggets/array/shuffle.rb +1 -1
- data/lib/nuggets/array/standard_deviation_mixin.rb +1 -1
- data/lib/nuggets/array/to_hash.rb +1 -1
- data/lib/nuggets/array/variance_mixin.rb +2 -2
- data/lib/nuggets/enumerable/agrep.rb +1 -1
- data/lib/nuggets/enumerable/all_any_extended.rb +1 -1
- data/lib/nuggets/enumerable/minmax.rb +1 -1
- data/lib/nuggets/env/set_mixin.rb +1 -1
- data/lib/nuggets/env/user_encoding_mixin.rb +1 -1
- data/lib/nuggets/env/user_home_mixin.rb +1 -1
- data/lib/nuggets/file/ext_mixin.rb +1 -1
- data/lib/nuggets/file/replace_mixin.rb +1 -1
- data/lib/nuggets/file/sub_mixin.rb +1 -1
- data/lib/nuggets/file/which_mixin.rb +1 -1
- data/lib/nuggets/hash/at.rb +1 -1
- data/lib/nuggets/hash/deep_merge_mixin.rb +1 -1
- data/lib/nuggets/hash/in_order.rb +1 -1
- data/lib/nuggets/hash/insert.rb +1 -1
- data/lib/nuggets/hash/nest_mixin.rb +1 -1
- data/lib/nuggets/hash/only.rb +1 -1
- data/lib/nuggets/hash/seen.rb +5 -0
- data/lib/nuggets/hash/seen_mixin.rb +59 -0
- data/lib/nuggets/hash/unroll_mixin.rb +1 -1
- data/lib/nuggets/integer/factorial.rb +1 -1
- data/lib/nuggets/integer/length_mixin.rb +1 -1
- data/lib/nuggets/integer/map_mixin.rb +1 -1
- data/lib/nuggets/integer/to_binary_s.rb +1 -1
- data/lib/nuggets/io/agrep.rb +1 -1
- data/lib/nuggets/io/interact_mixin.rb +1 -1
- data/lib/nuggets/io/modes.rb +1 -1
- data/lib/nuggets/io/null_mixin.rb +1 -1
- data/lib/nuggets/io/redirect_mixin.rb +1 -1
- data/lib/nuggets/net/success.rb +1 -1
- data/lib/nuggets/numeric/duration.rb +1 -1
- data/lib/nuggets/numeric/limit.rb +1 -1
- data/lib/nuggets/numeric/signum.rb +1 -1
- data/lib/nuggets/numeric/to_multiple.rb +1 -1
- data/lib/nuggets/object/blank_mixin.rb +1 -1
- data/lib/nuggets/object/boolean_mixin.rb +1 -1
- data/lib/nuggets/object/msend_mixin.rb +1 -1
- data/lib/nuggets/object/silence_mixin.rb +1 -1
- data/lib/nuggets/object/singleton_class_mixin.rb +1 -1
- data/lib/nuggets/proc/bind_mixin.rb +1 -1
- data/lib/nuggets/range/quantile_mixin.rb +1 -1
- data/lib/nuggets/statistics.rb +1 -0
- data/lib/nuggets/statistics_mixins.rb +1 -0
- data/lib/nuggets/string/camelscore_mixin.rb +1 -1
- data/lib/nuggets/string/capitalize_first.rb +1 -1
- data/lib/nuggets/string/case.rb +1 -1
- data/lib/nuggets/string/evaluate_mixin.rb +1 -1
- data/lib/nuggets/string/msub.rb +1 -1
- data/lib/nuggets/string/nsub.rb +1 -1
- data/lib/nuggets/string/sub_with_md.rb +1 -1
- data/lib/nuggets/string/wc_mixin.rb +1 -1
- data/lib/nuggets/string/word_wrap.rb +1 -1
- data/lib/nuggets/string/xor_mixin.rb +1 -1
- data/lib/nuggets/tempfile/open.rb +1 -1
- data/lib/nuggets/uri/content_type_mixin.rb +1 -1
- data/lib/nuggets/uri/exist_mixin.rb +1 -1
- data/lib/nuggets/uri/redirect_mixin.rb +1 -1
- data/lib/nuggets/util/ansicolor2css.rb +1 -1
- data/lib/nuggets/util/cli.rb +1 -1
- data/lib/nuggets/util/content_type.rb +1 -1
- data/lib/nuggets/util/dotted_decimal.rb +1 -1
- data/lib/nuggets/util/i18n.rb +1 -1
- data/lib/nuggets/util/lazy_attr.rb +1 -1
- data/lib/nuggets/util/log_parser/apache.rb +1 -1
- data/lib/nuggets/util/log_parser/rails.rb +1 -1
- data/lib/nuggets/util/log_parser.rb +1 -1
- data/lib/nuggets/util/mysql.rb +209 -0
- data/lib/nuggets/util/pluggable.rb +1 -1
- data/lib/nuggets/util/ruby.rb +1 -1
- data/lib/nuggets/version.rb +2 -2
- data/lib/nuggets.rb +1 -1
- data/spec/nuggets/array/boost_spec.rb +52 -0
- data/spec/nuggets/array/regression_spec.rb +16 -0
- data/spec/nuggets/hash/seen_spec.rb +38 -0
- data/spec/spec_helper.rb +3 -0
- metadata +155 -150
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c05c33666a38a6baa993e4efe6dbafc67e657837
|
4
|
+
data.tar.gz: 857e2ccfae07f7907292c943fcf74fea25d90a8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd5a08ccad8c5e1a72f45bf133d4bc57bc028a95d3a272119dbff59a5097a08a345e4cb3989409dfe0b91425a505d909056d30b23382d2fb5962593e90730ff8
|
7
|
+
data.tar.gz: 388c24087544ad117694982d5c420e97c60ae5a03ea1e39c880eeab88a6750145fee73df98c4d86d3a6caeaf108b97fbbdec64fbc1e126bd5a4251985f1038cf
|
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
== VERSION
|
4
4
|
|
5
|
-
This documentation refers to ruby-nuggets version 0.
|
5
|
+
This documentation refers to ruby-nuggets version 0.9.0
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
@@ -42,12 +42,12 @@ RubyGem:: http://rubygems.org/gems/ruby-nuggets
|
|
42
42
|
|
43
43
|
== AUTHORS
|
44
44
|
|
45
|
-
* Jens Wille <mailto:jens.wille@
|
45
|
+
* Jens Wille <mailto:jens.wille@gmail.com>
|
46
46
|
|
47
47
|
|
48
48
|
== LICENSE AND COPYRIGHT
|
49
49
|
|
50
|
-
Copyright (C) 2007-
|
50
|
+
Copyright (C) 2007-2013 Jens Wille
|
51
51
|
|
52
52
|
ruby-nuggets is free software: you can redistribute it and/or modify it under
|
53
53
|
the terms of the GNU Affero General Public License as published by the Free
|
data/Rakefile
CHANGED
data/lib/nuggets/all.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
data/lib/nuggets/all_mixins.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -0,0 +1,78 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2012 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
|
+
require 'nuggets/array/mean_mixin'
|
29
|
+
|
30
|
+
module Nuggets
|
31
|
+
class Array
|
32
|
+
module BoostMixin
|
33
|
+
|
34
|
+
def self.included(base)
|
35
|
+
base.send :include, Nuggets::Array::MeanMixin
|
36
|
+
end
|
37
|
+
|
38
|
+
# call-seq:
|
39
|
+
# array.boost_factor(other_array) => aFloat
|
40
|
+
# array.boost_factor(other_array) { |array| ... } => aFloat
|
41
|
+
#
|
42
|
+
# Calculates the "boost factor" *from* the series of values in _array_ *to*
|
43
|
+
# the series of values in +other_array+ by means of their arithmetic mean
|
44
|
+
# or the value returned from the block given.
|
45
|
+
#
|
46
|
+
# Example:
|
47
|
+
#
|
48
|
+
# # series of runtime measurements for old version
|
49
|
+
# a = [1.5, 1.6, 1.4]
|
50
|
+
#
|
51
|
+
# # series of runtime measurements for new version
|
52
|
+
# b = [0.7, 0.8, 0.8]
|
53
|
+
#
|
54
|
+
# # what speedup did we get? => almost 50%
|
55
|
+
# a.boost_factor(b) #=> -0.48888888888888893
|
56
|
+
def boost_factor(other, &block)
|
57
|
+
block ||= :mean.to_proc
|
58
|
+
block[other] / block[self] - 1
|
59
|
+
end
|
60
|
+
|
61
|
+
alias_method :boof, :boost_factor
|
62
|
+
|
63
|
+
# call-seq:
|
64
|
+
# array.boost(factor) => anArray
|
65
|
+
#
|
66
|
+
# Maps each value in _array_ to its "boosted" value according to +factor+.
|
67
|
+
# (Cf. #boost_factor)
|
68
|
+
#
|
69
|
+
# Example:
|
70
|
+
#
|
71
|
+
# a.boost(a.boost_factor(b)).mean == b.mean
|
72
|
+
def boost(factor)
|
73
|
+
map { |x| x * (1 + factor) }
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -47,7 +47,7 @@ module Nuggets
|
|
47
47
|
return 0.0 if empty?
|
48
48
|
|
49
49
|
target = first.respond_to?(:to_ary) ? self :
|
50
|
-
|
50
|
+
self.class.new(size) { |i| [i + 1, at(i)] }
|
51
51
|
|
52
52
|
c = target.cov
|
53
53
|
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
data/lib/nuggets/array/format.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2012 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
data/lib/nuggets/array/only.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
data/lib/nuggets/array/rand.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -4,10 +4,10 @@
|
|
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-2012 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -25,16 +25,10 @@
|
|
25
25
|
###############################################################################
|
26
26
|
#++
|
27
27
|
|
28
|
-
require 'nuggets/array/variance_mixin'
|
29
|
-
|
30
28
|
module Nuggets
|
31
29
|
class Array
|
32
30
|
module RegressionMixin
|
33
31
|
|
34
|
-
def self.included(base)
|
35
|
-
base.send :include, Nuggets::Array::VarianceMixin
|
36
|
-
end
|
37
|
-
|
38
32
|
# call-seq:
|
39
33
|
# array.linear_least_squares => anArray
|
40
34
|
#
|
@@ -45,26 +39,112 @@ module Nuggets
|
|
45
39
|
def linear_least_squares
|
46
40
|
return [] if empty?
|
47
41
|
|
48
|
-
|
49
|
-
|
42
|
+
sx, sy, sq, sp, xys = 0.0, 0.0, 0.0, 0.0, first.respond_to?(:to_ary) ?
|
43
|
+
self : self.class.new(size) { |i| [i + 1, at(i)] }
|
50
44
|
|
51
|
-
|
45
|
+
xys.each { |x, y| sx += x; sy += y; sq += x ** 2; sp += x * y }
|
52
46
|
|
53
|
-
|
54
|
-
sx += x
|
55
|
-
sy += y
|
56
|
-
}
|
57
|
-
|
58
|
-
v = target.var { |x, _| x }
|
59
|
-
|
60
|
-
b = v.zero? ? 0.0 : target.cov / v
|
47
|
+
b = (v = sq * size - sx ** 2) == 0 ? 0 : (sp * size - sx * sy) / v
|
61
48
|
a = (sy - b * sx) / size
|
62
49
|
|
63
|
-
|
50
|
+
xys.map { |x, _| [x, a + b * x] }
|
64
51
|
end
|
65
52
|
|
66
53
|
alias_method :llsq, :linear_least_squares
|
67
54
|
|
55
|
+
# call-seq:
|
56
|
+
# array.linear_least_squares_incremental => anIncrementalLinearRegression
|
57
|
+
#
|
58
|
+
# Returns an instance of IncrementalLinearRegression for _array_; _array_
|
59
|
+
# being a list of values (in contrast to #linear_least_squares, which also
|
60
|
+
# accepts <tt>{x,y}</tt> pairs). Use IncrementalLinearRegression directly,
|
61
|
+
# or apply this method to an empty _array_, for more control over its input
|
62
|
+
# data.
|
63
|
+
def linear_least_squares_incremental
|
64
|
+
IncrementalLinearRegression.new(*self)
|
65
|
+
end
|
66
|
+
|
67
|
+
alias_method :llsqi, :linear_least_squares_incremental
|
68
|
+
|
69
|
+
# Inspired by {Incremental Simple Linear Regression in Ruby}[http://blog.codewren.ch/post/31378435699].
|
70
|
+
#
|
71
|
+
# Use #push to add a single <tt>{x,y}</tt> pair, #add to add a list of +y+
|
72
|
+
# values, and #<< to add a single +y+ value. Whenever a single +y+ value is
|
73
|
+
# added, it's associated with an +x+ value of its position (rank) in the
|
74
|
+
# data series.
|
75
|
+
#
|
76
|
+
# Call #to_a (or any Enumerable method) to work with the regression points.
|
77
|
+
class IncrementalLinearRegression
|
78
|
+
|
79
|
+
include Enumerable
|
80
|
+
|
81
|
+
def initialize(*ys)
|
82
|
+
clear
|
83
|
+
add(*ys)
|
84
|
+
end
|
85
|
+
|
86
|
+
def clear
|
87
|
+
@x = @y = @xx = @xy = 0.0
|
88
|
+
@cnt, @slope = 0, nil
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
def push(x, y)
|
93
|
+
cnt, @slope = @cnt += 1, nil
|
94
|
+
|
95
|
+
@x += (x - @x) / cnt
|
96
|
+
@y += (y - @y) / cnt
|
97
|
+
@xx += (x * x - @xx) / cnt
|
98
|
+
@xy += (x * y - @xy) / cnt
|
99
|
+
|
100
|
+
self
|
101
|
+
end
|
102
|
+
|
103
|
+
def add(*ys)
|
104
|
+
ys.each { |y| self << y }
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
def <<(y)
|
109
|
+
push(@cnt + 1, y)
|
110
|
+
end
|
111
|
+
|
112
|
+
def slope
|
113
|
+
@slope ||= @cnt < 2 ? 0 : (@xy - @x * @y) / (@xx - @x * @x)
|
114
|
+
end
|
115
|
+
|
116
|
+
def intercept
|
117
|
+
at(0)
|
118
|
+
end
|
119
|
+
|
120
|
+
def at(x)
|
121
|
+
@y + slope * (x - @x)
|
122
|
+
end
|
123
|
+
|
124
|
+
alias_method :[], :at
|
125
|
+
|
126
|
+
def each
|
127
|
+
@cnt.times { |i| yield [x = i + 1, at(x)] }
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
131
|
+
def to_a(range = nil)
|
132
|
+
range ? range.map { |x| [x, at(x)] } : super()
|
133
|
+
end
|
134
|
+
|
135
|
+
def to_s
|
136
|
+
s, i = slope, intercept
|
137
|
+
|
138
|
+
y = s == 0 ? i : begin
|
139
|
+
x = s.abs == 1 ? "#{'-' if s < 0}x" : "#{s} * x"
|
140
|
+
i == 0 ? x : "#{x} #{i < 0 ? '-' : '+'} #{i.abs}"
|
141
|
+
end
|
142
|
+
|
143
|
+
"y := #{y}".gsub(/\.0\b/, '')
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
68
148
|
end
|
69
149
|
end
|
70
150
|
end
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -64,7 +64,7 @@ module Nuggets
|
|
64
64
|
return sx if empty?
|
65
65
|
|
66
66
|
target = first.respond_to?(:to_ary) ? self :
|
67
|
-
|
67
|
+
self.class.new(size) { |i| [i + 1, at(i)] }
|
68
68
|
|
69
69
|
target.each { |x, y|
|
70
70
|
sx += x
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2012 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
data/lib/nuggets/hash/at.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|
@@ -7,7 +7,7 @@
|
|
7
7
|
# Copyright (C) 2007-2011 Jens Wille #
|
8
8
|
# #
|
9
9
|
# Authors: #
|
10
|
-
# Jens Wille <jens.wille@
|
10
|
+
# Jens Wille <jens.wille@gmail.com> #
|
11
11
|
# #
|
12
12
|
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
13
|
# under the terms of the GNU Affero General Public License as published by #
|