advanced_math 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/Manifest.txt +13 -0
- data/README.rdoc +0 -0
- data/advanced_math.gemspec +11 -0
- data/lib/advanced_math.rb +56 -4
- data/pkg/adbanced_math-0.borked.gem +0 -0
- data/pkg/adbanced_math-0.borked.tgz +0 -0
- data/pkg/adbanced_math-0.borked/Manifest.txt +13 -0
- data/pkg/adbanced_math-0.borked/advanced_math.gemspec +11 -0
- data/{lib/simple_moving_average.rb → pkg/adbanced_math-0.borked/lib/advanced_math.rb} +13 -13
- data/pkg/adbanced_math-0.borked/test/test_simple_moving_average.rb +51 -0
- data/test/test_simple_moving_average.rb +51 -0
- metadata +16 -6
data/History.txt
ADDED
File without changes
|
data/Manifest.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
advanced_math.gemspec
|
2
|
+
History.txt
|
3
|
+
lib/advanced_math.rb
|
4
|
+
Manifest.txt
|
5
|
+
pkg/adbanced_math-0.borked
|
6
|
+
pkg/adbanced_math-0.borked/advanced_math.gemspec
|
7
|
+
pkg/adbanced_math-0.borked/lib/advanced_math.rb
|
8
|
+
pkg/adbanced_math-0.borked/Manifest.txt
|
9
|
+
pkg/adbanced_math-0.borked/test/test_simple_moving_average.rb
|
10
|
+
pkg/adbanced_math-0.borked.gem
|
11
|
+
pkg/adbanced_math-0.borked.tgz
|
12
|
+
README.rdoc
|
13
|
+
test/test_simple_moving_average.rb
|
data/README.rdoc
ADDED
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'advanced_math'
|
3
|
+
s.version = '0.0.4'
|
4
|
+
s.date = '2011-08-25'
|
5
|
+
s.summary = "A simple gem for advanced and financial math calcualtions."
|
6
|
+
s.description = "A simple gem for advanced and financial math calcualtions."
|
7
|
+
s.authors = ["G Nagel"]
|
8
|
+
s.email = 'glenn@mercury-wireless.com'
|
9
|
+
s.files = Dir['**/*.*']
|
10
|
+
s.homepage = 'https://github.com/gnagel/mercury-wireless-public'
|
11
|
+
end
|
data/lib/advanced_math.rb
CHANGED
@@ -1,5 +1,57 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
module AdvancedMath
|
3
|
+
|
4
|
+
# Simple Moving Average (SMA) calculator
|
5
|
+
# Created: 2011-06-24
|
6
|
+
# Author: G Nagel
|
7
|
+
# Company: Mercury Wireless Software LLC
|
8
|
+
class SimpleMovingAverage
|
9
|
+
###
|
10
|
+
# Initialize the members:
|
11
|
+
# range:
|
12
|
+
# number of values to average
|
13
|
+
# sum:
|
14
|
+
# current sum of all values in the array
|
15
|
+
# values:
|
16
|
+
# array of values used as temporary storage
|
17
|
+
###
|
18
|
+
def initialize(range)
|
19
|
+
raise ArgumentError, "Range is nil" unless (range);
|
20
|
+
raise ArgumentError, "Range must be >= 1" unless range.to_i >= 1;
|
21
|
+
@range = range.to_i;
|
22
|
+
@sum = 0;
|
23
|
+
@values = Array.new();
|
24
|
+
end
|
25
|
+
|
26
|
+
###
|
27
|
+
# Add a value to the list.
|
28
|
+
# If the list is < @range, then return nil.
|
29
|
+
# Otherwise compute the SMA and return the value.
|
30
|
+
###
|
31
|
+
def add(value)
|
32
|
+
raise ArgumentError, "Value is nil" unless (value);
|
33
|
+
|
34
|
+
# add the value to the end of the array.
|
35
|
+
@values.push(value);
|
36
|
+
|
37
|
+
# Calculate the sum of the array
|
38
|
+
@sum += value.to_f;
|
39
|
+
|
40
|
+
# Is the array less than the range?
|
41
|
+
return nil if (@values.length() < @range)
|
42
|
+
|
43
|
+
# Is the array larger than the range?
|
44
|
+
@sum -= @values.shift.to_f() if (@values.length() > @range)
|
45
|
+
|
46
|
+
# Compute the average
|
47
|
+
return @sum.to_f / @range.to_f;
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
###
|
52
|
+
# SMA is just an alias to SimpleMovingAverage
|
53
|
+
###
|
54
|
+
class SMA < SimpleMovingAverage
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
Binary file
|
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
advanced_math.gemspec
|
2
|
+
History.txt
|
3
|
+
lib/advanced_math.rb
|
4
|
+
Manifest.txt
|
5
|
+
pkg/adbanced_math-0.borked
|
6
|
+
pkg/adbanced_math-0.borked/advanced_math.gemspec
|
7
|
+
pkg/adbanced_math-0.borked/lib/advanced_math.rb
|
8
|
+
pkg/adbanced_math-0.borked/Manifest.txt
|
9
|
+
pkg/adbanced_math-0.borked/test/test_simple_moving_average.rb
|
10
|
+
pkg/adbanced_math-0.borked.gem
|
11
|
+
pkg/adbanced_math-0.borked.tgz
|
12
|
+
README.rdoc
|
13
|
+
test/test_simple_moving_average.rb
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'advanced_math'
|
3
|
+
s.version = '0.0.4'
|
4
|
+
s.date = '2011-08-25'
|
5
|
+
s.summary = "A simple gem for advanced and financial math calcualtions."
|
6
|
+
s.description = "A simple gem for advanced and financial math calcualtions."
|
7
|
+
s.authors = ["G Nagel"]
|
8
|
+
s.email = 'glenn@mercury-wireless.com'
|
9
|
+
s.files = Dir['**/*.*']
|
10
|
+
s.homepage = 'https://github.com/gnagel/mercury-wireless-public'
|
11
|
+
end
|
@@ -1,12 +1,11 @@
|
|
1
|
-
# Simple Moving Average (SMA) calculator
|
2
|
-
# Created: 2011-06-24
|
3
|
-
# Author: G Nagel
|
4
|
-
# Company: Mercury Wireless Software LLC
|
5
1
|
|
6
|
-
module
|
2
|
+
module AdvancedMath
|
7
3
|
|
4
|
+
# Simple Moving Average (SMA) calculator
|
5
|
+
# Created: 2011-06-24
|
6
|
+
# Author: G Nagel
|
7
|
+
# Company: Mercury Wireless Software LLC
|
8
8
|
class SimpleMovingAverage
|
9
|
-
|
10
9
|
###
|
11
10
|
# Initialize the members:
|
12
11
|
# range:
|
@@ -39,19 +38,20 @@ module Math
|
|
39
38
|
@sum += value.to_f;
|
40
39
|
|
41
40
|
# Is the array less than the range?
|
42
|
-
|
43
|
-
if (length < @range)
|
44
|
-
return nil;
|
45
|
-
end
|
41
|
+
return nil if (@values.length() < @range)
|
46
42
|
|
47
43
|
# Is the array larger than the range?
|
48
|
-
if (length > @range)
|
49
|
-
@sum -= @values.shift.to_f();
|
50
|
-
end
|
44
|
+
@sum -= @values.shift.to_f() if (@values.length() > @range)
|
51
45
|
|
52
46
|
# Compute the average
|
53
47
|
return @sum.to_f / @range.to_f;
|
54
48
|
end
|
55
49
|
end
|
50
|
+
|
51
|
+
###
|
52
|
+
# SMA is just an alias to SimpleMovingAverage
|
53
|
+
###
|
54
|
+
class SMA < SimpleMovingAverage
|
55
|
+
end
|
56
56
|
|
57
57
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + "/../lib/advanced_math.rb"
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class SimpleMovingAverageTest < Test::Unit::TestCase
|
7
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
8
|
+
def test_sma_initialize_nil
|
9
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(nil); }
|
10
|
+
end
|
11
|
+
|
12
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
13
|
+
def test_sma_initialize_negative
|
14
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(-1); }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
18
|
+
def test_sma_initialize_zero
|
19
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(0); }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Verify the constructor doesn't raise an exception if the value is positive
|
23
|
+
def test_sma_initialize_positive
|
24
|
+
(1...1000).each do |i|
|
25
|
+
assert_nothing_raised(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(i); }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Verify "add" raises an ArgumentError if the "value" is invalid
|
30
|
+
def test_sma_add_nil
|
31
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(1).add(nil); }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Verify SMA of 1, always returns the same value
|
35
|
+
def test_sma_add_1
|
36
|
+
sma = AdvancedMath::SimpleMovingAverage.new(1);
|
37
|
+
(1...1000).each do |i|
|
38
|
+
assert_equal(i, sma.add(i));
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Verify SMA of 2, always returns the same value - 0.5
|
43
|
+
def test_sma_add_2
|
44
|
+
sma = AdvancedMath::SimpleMovingAverage.new(2);
|
45
|
+
assert_equal(nil, sma.add(1));
|
46
|
+
(2...1000).each do |i|
|
47
|
+
assert_equal(i - 0.5, sma.add(i));
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + "/../lib/advanced_math.rb"
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class SimpleMovingAverageTest < Test::Unit::TestCase
|
7
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
8
|
+
def test_sma_initialize_nil
|
9
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(nil); }
|
10
|
+
end
|
11
|
+
|
12
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
13
|
+
def test_sma_initialize_negative
|
14
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(-1); }
|
15
|
+
end
|
16
|
+
|
17
|
+
# Verify the constructor raises an ArgumentError if the "range" is invalid
|
18
|
+
def test_sma_initialize_zero
|
19
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(0); }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Verify the constructor doesn't raise an exception if the value is positive
|
23
|
+
def test_sma_initialize_positive
|
24
|
+
(1...1000).each do |i|
|
25
|
+
assert_nothing_raised(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(i); }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Verify "add" raises an ArgumentError if the "value" is invalid
|
30
|
+
def test_sma_add_nil
|
31
|
+
assert_raise(ArgumentError) { AdvancedMath::SimpleMovingAverage.new(1).add(nil); }
|
32
|
+
end
|
33
|
+
|
34
|
+
# Verify SMA of 1, always returns the same value
|
35
|
+
def test_sma_add_1
|
36
|
+
sma = AdvancedMath::SimpleMovingAverage.new(1);
|
37
|
+
(1...1000).each do |i|
|
38
|
+
assert_equal(i, sma.add(i));
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Verify SMA of 2, always returns the same value - 0.5
|
43
|
+
def test_sma_add_2
|
44
|
+
sma = AdvancedMath::SimpleMovingAverage.new(2);
|
45
|
+
assert_equal(nil, sma.add(1));
|
46
|
+
(2...1000).each do |i|
|
47
|
+
assert_equal(i - 0.5, sma.add(i));
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: advanced_math
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- G Nagel
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-25 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: A simple gem for advanced and financial math calcualtions.
|
@@ -27,8 +27,18 @@ extensions: []
|
|
27
27
|
extra_rdoc_files: []
|
28
28
|
|
29
29
|
files:
|
30
|
-
-
|
30
|
+
- advanced_math.gemspec
|
31
|
+
- History.txt
|
31
32
|
- lib/advanced_math.rb
|
33
|
+
- Manifest.txt
|
34
|
+
- pkg/adbanced_math-0.borked/advanced_math.gemspec
|
35
|
+
- pkg/adbanced_math-0.borked/lib/advanced_math.rb
|
36
|
+
- pkg/adbanced_math-0.borked/Manifest.txt
|
37
|
+
- pkg/adbanced_math-0.borked/test/test_simple_moving_average.rb
|
38
|
+
- pkg/adbanced_math-0.borked.gem
|
39
|
+
- pkg/adbanced_math-0.borked.tgz
|
40
|
+
- README.rdoc
|
41
|
+
- test/test_simple_moving_average.rb
|
32
42
|
homepage: https://github.com/gnagel/mercury-wireless-public
|
33
43
|
licenses: []
|
34
44
|
|
@@ -58,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
68
|
requirements: []
|
59
69
|
|
60
70
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.8.
|
71
|
+
rubygems_version: 1.8.6
|
62
72
|
signing_key:
|
63
73
|
specification_version: 3
|
64
74
|
summary: A simple gem for advanced and financial math calcualtions.
|