babbage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maths.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 geemus
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Babbage
2
+
3
+ Ruby mathematics. Supplements to/for standard library math.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'babbage'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install babbage
18
+
19
+ ## Usage
20
+
21
+ ### Array Methods
22
+
23
+ * `Babbage::Array.average([1, 2, 3, 4, 5]) # => 3`
24
+ * `Babbage::Array.maximum([1, 2, 3, 4, 5]) # => 5`
25
+ * `Babbage::Array.median([1, 2, 3, 4, 5]) # => 3`
26
+ * `Babbage::Array.minimum([1, 2, 3, 4, 5]) # => 1`
27
+ * `Babbage::Array.percentile([1, 2, 3, 4, 5], 80) # => 4.5`
28
+ * `Babbage::Array.standard_deviation([1, 2, 3, 4, 5]) # => 1.4142135623730951`
29
+ * `Babbage::Array.sum([1, 2, 3, 4, 5]) # => 15`
30
+ * `Babbage::Array.variance([1, 2, 3, 4, 5]) # => 2.0`
31
+
32
+ ### Misc Methods
33
+
34
+ * `Babbage.binomial_coefficient(5, 2) # => 10.0`
35
+ * `Babbage.factorial(4) # => 24`
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default do
4
+ Dir.glob('./test/**/*_test.rb') {|path| load(path)}
5
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'babbage/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "babbage"
8
+ gem.version = Babbage::VERSION
9
+ gem.authors = ["geemus"]
10
+ gem.email = ["geemus@gmail.com"]
11
+ gem.description = %q{Ruby mathematics}
12
+ gem.summary = %q{Ruby mathematics}
13
+ gem.homepage = "http://github.com/geemus/babbage"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,19 @@
1
+ require "babbage/array"
2
+ require "babbage/version"
3
+
4
+ module Babbage
5
+
6
+ def self.binomial_coefficient(number, choices)
7
+ factorial(number).to_f / (factorial(choices).to_f * factorial(number - choices).to_f)
8
+ end
9
+
10
+ def self.factorial(number)
11
+ case number
12
+ when 0, 1
13
+ 1
14
+ else
15
+ 1.upto(number).inject(:*)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,66 @@
1
+ module Babbage
2
+
3
+ module Array
4
+
5
+ def self.average(array)
6
+ sum(array).to_f / array.length
7
+ end
8
+
9
+ def self.maximum(array)
10
+ array.max
11
+ end
12
+
13
+ def self.mean(array)
14
+ average(array)
15
+ end
16
+
17
+ def self.median(array)
18
+ percentile(array, 50)
19
+ end
20
+
21
+ def self.minimum(array)
22
+ array.min
23
+ end
24
+
25
+ def self.percentile(array, target)
26
+ target = target.to_f / 100.0
27
+ index = (array.length - 1) * target
28
+ sorted_array = array.sort
29
+ if index == index.to_i
30
+ sorted_array[index.to_i]
31
+ else
32
+ average(
33
+ sorted_array[index.floor, 2]
34
+ )
35
+ end
36
+ end
37
+
38
+ def self.root_mean_square_deviation(array, &estimator)
39
+ Math.sqrt(
40
+ average(
41
+ array.map(&estimator)
42
+ )
43
+ )
44
+ end
45
+
46
+ def self.standard_deviation(array)
47
+ avg = average(array)
48
+ root_mean_square_deviation(array) do |value|
49
+ (value - avg) ** 2
50
+ end
51
+ end
52
+
53
+ def self.sum(array)
54
+ array.inject(0) {|sum,value| sum + value}
55
+ end
56
+
57
+ def self.variance(array)
58
+ avg = average(array)
59
+ average(
60
+ array.map {|value| (value - avg) ** 2 }
61
+ )
62
+ end
63
+
64
+ end
65
+ end
66
+
@@ -0,0 +1,3 @@
1
+ module Babbage
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'helper'
2
+
3
+ class TestArray < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @even = 0.upto(5).to_a
7
+ @odd = 1.upto(5).to_a
8
+ end
9
+
10
+ def test_average
11
+ assert_equal 2.5, Babbage::Array.average(@even)
12
+ assert_equal 3, Babbage::Array.average(@odd)
13
+ end
14
+
15
+ def test_maximum
16
+ assert_equal 5, Babbage::Array.maximum(@even)
17
+ assert_equal 5, Babbage::Array.maximum(@odd)
18
+ end
19
+
20
+ def test_median
21
+ assert_equal 2.5, Babbage::Array.median(@even)
22
+ assert_equal 3, Babbage::Array.median(@odd)
23
+ end
24
+
25
+ def test_minimum
26
+ assert_equal 0, Babbage::Array.minimum(@even)
27
+ assert_equal 1, Babbage::Array.minimum(@odd)
28
+ end
29
+
30
+ def test_percentile
31
+ assert_equal 4, Babbage::Array.percentile(@even, 80)
32
+ assert_equal 4.5, Babbage::Array.percentile(@odd, 80)
33
+ end
34
+
35
+ def test_standard_deviation
36
+ assert_equal 1.707825127659933, Babbage::Array.standard_deviation(@even)
37
+ assert_equal 1.4142135623730951, Babbage::Array.standard_deviation(@odd)
38
+ end
39
+
40
+ def test_sum
41
+ assert_equal 15, Babbage::Array.sum(@even)
42
+ assert_equal 15, Babbage::Array.sum(@odd)
43
+ end
44
+
45
+ def test_variance
46
+ assert_equal 2.9166666666666665, Babbage::Array.variance(@even)
47
+ assert_equal 2.0, Babbage::Array.variance(@odd)
48
+ end
49
+
50
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'helper'
2
+
3
+ class TestBabbage < MiniTest::Unit::TestCase
4
+
5
+ def test_binomial_coefficient
6
+ assert_equal 1.0, Babbage.binomial_coefficient(5, 0)
7
+ assert_equal 5.0, Babbage.binomial_coefficient(5, 1)
8
+ assert_equal 10.0, Babbage.binomial_coefficient(5, 2)
9
+ assert_equal 10.0, Babbage.binomial_coefficient(5, 3)
10
+ assert_equal 5.0, Babbage.binomial_coefficient(5, 4)
11
+ assert_equal 1.0, Babbage.binomial_coefficient(5, 5)
12
+ end
13
+
14
+ def test_factorial
15
+ assert_equal 1, Babbage.factorial(0)
16
+ assert_equal 1, Babbage.factorial(1)
17
+ assert_equal 2, Babbage.factorial(2)
18
+ assert_equal 6, Babbage.factorial(3)
19
+ assert_equal 24, Babbage.factorial(4)
20
+ assert_equal 120, Babbage.factorial(5)
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative '../lib/babbage'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babbage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - geemus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby mathematics
15
+ email:
16
+ - geemus@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - babbage.gemspec
27
+ - lib/babbage.rb
28
+ - lib/babbage/array.rb
29
+ - lib/babbage/version.rb
30
+ - test/array_test.rb
31
+ - test/babbage_test.rb
32
+ - test/helper.rb
33
+ homepage: http://github.com/geemus/babbage
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ segments:
46
+ - 0
47
+ hash: 2140554366665642303
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ segments:
55
+ - 0
56
+ hash: 2140554366665642303
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.23
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Ruby mathematics
63
+ test_files:
64
+ - test/array_test.rb
65
+ - test/babbage_test.rb
66
+ - test/helper.rb