numeric_array 1.0.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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +18 -0
- data/LICENSE +20 -0
- data/README.md +15 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/numeric_array.rb +79 -0
- data/spec/numeric_array_spec.rb +39 -0
- data/spec/spec_helper.rb +8 -0
- metadata +93 -0
data/.document
ADDED
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.2
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
rspec (2.0.0.beta.20)
|
6
|
+
rspec-core (= 2.0.0.beta.20)
|
7
|
+
rspec-expectations (= 2.0.0.beta.20)
|
8
|
+
rspec-mocks (= 2.0.0.beta.20)
|
9
|
+
rspec-core (2.0.0.beta.20)
|
10
|
+
rspec-expectations (2.0.0.beta.20)
|
11
|
+
diff-lcs (>= 1.1.2)
|
12
|
+
rspec-mocks (2.0.0.beta.20)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
rspec (>= 2.0.0.beta.20)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Evan Sagge
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
numeric_array
|
2
|
+
=
|
3
|
+
|
4
|
+
An extension for the native Array class with support for mathematical/statistical calculations.
|
5
|
+
|
6
|
+
- Array#sum
|
7
|
+
- Array#total
|
8
|
+
- Array#average
|
9
|
+
- Array#avg
|
10
|
+
- Array#mean
|
11
|
+
- Array#variance
|
12
|
+
- Array#standard_deviation
|
13
|
+
- Array#std_dev
|
14
|
+
|
15
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "numeric_array"
|
8
|
+
gem.summary = %Q{Extension methods for manipulating and calculating an array of numbers}
|
9
|
+
gem.description = %Q{Support for Array#sum, Array#mean, Array#variance, Array#standard_deviation. An ArgumentError is raised when at least one array element is non-numeric.}
|
10
|
+
gem.email = "evansagge@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/evansagge/numeric_array"
|
12
|
+
gem.authors = ["Evan Sagge"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 2.0.0.beta.20"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
require 'rspec/core'
|
23
|
+
require 'rspec/core/rake_task'
|
24
|
+
|
25
|
+
task :default => :spec
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
28
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
32
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "mongoid-rspec #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module NumericArray
|
2
|
+
module InstanceMethods
|
3
|
+
|
4
|
+
NUMERIC_REGEX = /^-?\d+.?\d*e?-?\d*$/
|
5
|
+
|
6
|
+
def sum
|
7
|
+
check_numeric_array!
|
8
|
+
a = numerify
|
9
|
+
a.inject(0) {|sum, value| sum + value}
|
10
|
+
end
|
11
|
+
|
12
|
+
# average of an array of numbers
|
13
|
+
def average
|
14
|
+
check_numeric_array!
|
15
|
+
a = numerify
|
16
|
+
a.sum / length.to_f
|
17
|
+
end
|
18
|
+
alias :avg :average
|
19
|
+
alias :mean :average
|
20
|
+
|
21
|
+
# variance of an array of numbers
|
22
|
+
def variance(sample = false)
|
23
|
+
a = numerify
|
24
|
+
avg = a.average
|
25
|
+
sum = a.inject(0) { |sum, value| sum + (value - avg) ** 2}
|
26
|
+
(1 / (a.length.to_f - (sample ? 1 : 0)) * sum)
|
27
|
+
end
|
28
|
+
|
29
|
+
def sample_variance
|
30
|
+
variance(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
# standard deviation of an array of numbers
|
34
|
+
def standard_deviation(sample = false)
|
35
|
+
Math.sqrt(self.variance(sample))
|
36
|
+
end
|
37
|
+
alias :std_dev :standard_deviation
|
38
|
+
|
39
|
+
def sample_standard_deviation
|
40
|
+
standard_deviation(true)
|
41
|
+
end
|
42
|
+
alias :sample_std_dev :sample_standard_deviation
|
43
|
+
|
44
|
+
def check_numeric_array!
|
45
|
+
raise ArgumentError, "Array cannot have the following non-numeric elements: #{non_numeric_elements.collect(&:inspect).join(", ")}" unless numeric_array?
|
46
|
+
end
|
47
|
+
|
48
|
+
def numeric_array?
|
49
|
+
!self.detect{|obj| !(obj.kind_of?(Numeric) or obj.to_s =~ NUMERIC_REGEX) }
|
50
|
+
end
|
51
|
+
alias :numeric? :numeric_array?
|
52
|
+
|
53
|
+
def non_numeric_elements
|
54
|
+
self.select{|obj| !(obj.kind_of?(Numeric) or obj.to_s =~ NUMERIC_REGEX)}
|
55
|
+
end
|
56
|
+
|
57
|
+
def numerify
|
58
|
+
self.collect do |obj|
|
59
|
+
if obj.kind_of? Numeric
|
60
|
+
obj
|
61
|
+
elsif obj.to_s =~ NUMERIC_REGEX
|
62
|
+
obj.to_f
|
63
|
+
else
|
64
|
+
raise ArgumentError, "Array element #{obj.inspect} cannot be converted into a numeric value"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def numerify!
|
70
|
+
self.replace(numerify)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module ClassMethods
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
Array.extend NumericArray::ClassMethods
|
79
|
+
Array.send :include, NumericArray::InstanceMethods
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NumericArray do
|
4
|
+
describe ["1", 2.52, "foo", :bar, "-1.25e45" ] do
|
5
|
+
let(:array) { ["1", 2.52, "foo", :bar, "-1.25e45" ] }
|
6
|
+
it { should_not be_numeric }
|
7
|
+
it { should_not be_a_numeric_array }
|
8
|
+
its(:numerify!) { lambda{ array.numerify! }.should raise_error(ArgumentError) }
|
9
|
+
its(:sum) { lambda{ array.sum }.should raise_error(ArgumentError) }
|
10
|
+
its(:avg) { lambda{ array.avg }.should raise_error(ArgumentError) }
|
11
|
+
its(:variance) { lambda{ array.variance }.should raise_error(ArgumentError) }
|
12
|
+
its(:std_dev) { lambda{ array.std_dev }.should raise_error(ArgumentError) }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe [1, 2.52, -8.65, 20/50.0] do
|
16
|
+
let(:array) { [1, 2.52, -8.65, 20/50.0] }
|
17
|
+
it { should be_numeric }
|
18
|
+
it { should be_a_numeric_array }
|
19
|
+
its(:sum) { should == -4.73 }
|
20
|
+
its(:mean) { should == -1.1825 }
|
21
|
+
its(:variance) { array.variance.round(5).should == 19.18492 }
|
22
|
+
its(:std_dev) { array.std_dev.round(5).should == 4.38006 }
|
23
|
+
its(:sample_variance) { array.sample_variance.round(5).should == 25.57989 }
|
24
|
+
its(:sample_std_dev) { array.sample_std_dev.round(5).should == 5.05766 }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ["1", 2.52, -8.65, "4e-1"] do
|
28
|
+
let(:array) { ["1", 2.52, -8.65, "4e-1"] }
|
29
|
+
its(:numerify!) { should == [1, 2.52, -8.65, 0.4] }
|
30
|
+
it { should be_numeric }
|
31
|
+
it { should be_a_numeric_array }
|
32
|
+
its(:sum) { should == -4.73 }
|
33
|
+
its(:mean) { should == -1.1825 }
|
34
|
+
its(:variance) { array.variance.round(5).should == 19.18492 }
|
35
|
+
its(:std_dev) { array.std_dev.round(5).should == 4.38006 }
|
36
|
+
its(:sample_variance) { array.sample_variance.round(5).should == 25.57989 }
|
37
|
+
its(:sample_std_dev) { array.sample_std_dev.round(5).should == 5.05766 }
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numeric_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Evan Sagge
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-25 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta
|
33
|
+
- 20
|
34
|
+
version: 2.0.0.beta.20
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Support for Array#sum, Array#mean, Array#variance, Array#standard_deviation. An ArgumentError is raised when at least one array element is non-numeric.
|
38
|
+
email: evansagge@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
files:
|
47
|
+
- .document
|
48
|
+
- .gitignore
|
49
|
+
- .rvmrc
|
50
|
+
- Gemfile
|
51
|
+
- Gemfile.lock
|
52
|
+
- LICENSE
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- VERSION
|
56
|
+
- lib/numeric_array.rb
|
57
|
+
- spec/numeric_array_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/evansagge/numeric_array
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Extension methods for manipulating and calculating an array of numbers
|
91
|
+
test_files:
|
92
|
+
- spec/numeric_array_spec.rb
|
93
|
+
- spec/spec_helper.rb
|