mad_math 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c72da6443f380dc68cab4a7bfb1e975bad24e17b
4
- data.tar.gz: 6a7e75b4708695ff46698c47c1147110bf47a2e6
3
+ metadata.gz: 9314803b08c93f3d572a81aba873f4b3419f07cb
4
+ data.tar.gz: b821bc41815d01c07a441ab0c1bf3b0d03db6e95
5
5
  SHA512:
6
- metadata.gz: 3b83fe96d3cbbfbc589acfadbc383127ad2a805ce46884bdaa3edac5f115760981cde779ee8fd48def99458605b1b66e9d529f1ad4b3013dcfadb54e8f2c0eae
7
- data.tar.gz: 7c810ab96bab4482f3f8fc85d77b1b7c7907f7230e138dc8ec970bb9d0cf74a8a788928fed5f346b05d51342d158497f0ee0c9103ece15e66d668ce6d34b242a
6
+ metadata.gz: f3cbc60759cbc8320ea28da8ea1638e968c1e9bb4e5c7823065012bea5ebc32bf0a3f8ca8d3db4fb05f65e971aef1ca75b9e187ef7a4fdb2da5cbc5bc34b0c20
7
+ data.tar.gz: 5faad1b54d4d172b46029db5a64ca9e67fa3b9e2e0d638c74f7be654104162b9d190dd3e50fcfe81aa8a176a3b56fa4af033257863e2eedd56d075720dc96360
@@ -1,57 +1 @@
1
- module MadMath
2
- class Stats
3
- def initialize(data)
4
- @data = data
5
- end
6
-
7
- def load
8
- stdev
9
- self
10
- end
11
-
12
- def average
13
- @average ||= sum / count.to_f
14
- end
15
-
16
- # #reduce with the symbol (non-block form) seems most performant
17
- def sum
18
- @sum ||= @data.reduce(:+)
19
- end
20
-
21
- def count
22
- @count ||= @data.size
23
- end
24
-
25
- def sum_of_squares
26
- @sum_of_squares ||= get_sum_of_squares
27
- end
28
-
29
- def stdev
30
- @stdev ||= Math.sqrt(sum_of_squares / (count - 1))
31
- end
32
-
33
- def stdevp
34
- @stdevp ||= Math.sqrt(sum_of_squares / count)
35
- end
36
-
37
- def max
38
- @max ||= @data.max
39
- end
40
-
41
- def min
42
- @min ||= @data.min
43
- end
44
-
45
- private
46
-
47
- def get_sum_of_squares
48
- @max = @data.first
49
- @min = @data.first
50
- @data.map do |num|
51
- @max = num if @max < num
52
- @min = num if @min > num
53
- (num - average) ** 2
54
- end.reduce(:+)
55
- end
56
- end
57
- end
1
+ require_relative 'mad_math/stats'
@@ -0,0 +1 @@
1
+ require_relative 'newton_polynomial'
@@ -0,0 +1,70 @@
1
+ module MadMath
2
+ class NewtonPolynomial
3
+ def initialize(data = [])
4
+ @xs = []
5
+ @ys = []
6
+ @calc = []
7
+
8
+ #pp data
9
+ #puts '-' * 80
10
+
11
+ data.each do |el|
12
+ raise "Element is missing." if el.length != 2
13
+ add el.first, el.last
14
+ end
15
+ end
16
+
17
+ def add(x, y)
18
+ @xs << x
19
+ @ys << y
20
+
21
+ calc(x) if @xs.length > 1
22
+ end
23
+
24
+ def value_for(x)
25
+ y = @ys.first
26
+ #print "#{@ys.first} + "
27
+ @calc.each_with_index do |col, i|
28
+ #print "#{col[0]}"
29
+ xs = (i + 1).times.to_a.map do |j|
30
+ #print " * (#{x} - #{@xs[j]})"
31
+ x - @xs[j]
32
+ end.reduce(:*)
33
+ y += col[0] * xs
34
+ #print " + "
35
+ end
36
+ #puts
37
+ #puts '=' * 80
38
+ y
39
+ end
40
+
41
+ private
42
+
43
+ def solve(x2, y2, x1, y1)
44
+ #puts "(#{y2} - #{y1}) / (#{x2} - #{x1})"
45
+ (y2 - y1) / (x2 - x1).to_f
46
+ end
47
+
48
+ def calc(lx)
49
+ rows = @xs.count
50
+
51
+ @calc << []
52
+
53
+ col = 0
54
+ xn = rows - 2
55
+
56
+ while @calc[col]
57
+ y2 = (@calc[col - 1] && @calc[col - 1][-1]) || @ys[-1]
58
+ y1 = (@calc[col - 1] && @calc[col - 1][-2]) || @ys[-2]
59
+
60
+ @calc[col] << solve(lx, y2, @xs[xn], y1)
61
+
62
+ col += 1
63
+ xn -= 1
64
+ #puts '-' * 80
65
+ end
66
+ #pp @calc
67
+ #puts '-' * 80
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,57 @@
1
+ module MadMath
2
+ class Stats
3
+ def initialize(data)
4
+ @data = data
5
+ end
6
+
7
+ def load
8
+ stdev
9
+ self
10
+ end
11
+
12
+ def average
13
+ @average ||= sum / count.to_f
14
+ end
15
+
16
+ # #reduce with the symbol (non-block form) seems most performant
17
+ def sum
18
+ @sum ||= @data.reduce(:+)
19
+ end
20
+
21
+ def count
22
+ @count ||= @data.size
23
+ end
24
+
25
+ def sum_of_squares
26
+ @sum_of_squares ||= get_sum_of_squares
27
+ end
28
+
29
+ def stdev
30
+ @stdev ||= Math.sqrt(sum_of_squares / (count - 1))
31
+ end
32
+
33
+ def stdevp
34
+ @stdevp ||= Math.sqrt(sum_of_squares / count)
35
+ end
36
+
37
+ def max
38
+ @max ||= @data.max
39
+ end
40
+
41
+ def min
42
+ @min ||= @data.min
43
+ end
44
+
45
+ private
46
+
47
+ def get_sum_of_squares
48
+ @max = @data.first
49
+ @min = @data.first
50
+ @data.map do |num|
51
+ @max = num if @max < num
52
+ @min = num if @min > num
53
+ (num - average) ** 2
54
+ end.reduce(:+)
55
+ end
56
+ end
57
+ end
data/readme.md CHANGED
@@ -32,6 +32,63 @@ Require
32
32
  $ require 'mad_math'
33
33
 
34
34
 
35
+ Classes/Modules
36
+ ---------------
37
+
38
+ To require all classes/modules included in `MadMath`,
39
+ require all of them:
40
+
41
+ require 'mad_math/all'
42
+
43
+
44
+ ### Stats
45
+
46
+ `Stats` is required by default.
47
+
48
+ Basic usage as follows:
49
+
50
+ ```
51
+ stats = MadMath::Stats.new([1, 2, 3, 4])
52
+ stats.load #=> Runs all calculations and caches the values
53
+ stats.average #=> 2.5
54
+ ```
55
+
56
+ The following methods are available on an instance of `Stats`:
57
+
58
+ * `load`
59
+ * calculates all values and caches them
60
+ * this is the same as calling `stdev`
61
+ * `average`
62
+ * this is the mean value
63
+ * `sum`
64
+ * `count`
65
+ * `stdev`
66
+ * standard deviation
67
+ * `stdevp`
68
+ * population standard deviation
69
+ * `max`
70
+ * `min`
71
+
72
+
73
+ NewtonPolynomial
74
+ ----------------
75
+
76
+ To use this, you must either require all classes or require it specifically.
77
+ The assumption is that few users will be utilizing that class.
78
+
79
+ require 'mad_math/newton_polynomial'
80
+
81
+ Data is passed in as an array of arrays
82
+ and may be appended to by using `#add`:
83
+
84
+ ```
85
+ # y = x ** 2 + 1
86
+ np = MadMath::NewtonPolynomial.new([[1, 2], [2, 5], [3, 10]])
87
+ np.add 4, 17
88
+ np.value_for 5 #=> 26.0
89
+ ```
90
+
91
+
35
92
  Gemfile.lock
36
93
  ------------
37
94
 
@@ -70,7 +127,7 @@ It is much harder to get something working if you have no idea
70
127
  what a good starting point is
71
128
  or even whether it was expected to work at some point
72
129
  (maybe the test suite was failing when it was abandoned).
73
- The point is that it very difficult to know the difference
130
+ The point is that it is very difficult to know the difference
74
131
  without a Gemfile.lock.
75
132
 
76
133
  This is one of the dumbest things we do.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mad_math
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Herrick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-08 00:00:00.000000000 Z
11
+ date: 2017-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -117,6 +117,9 @@ extra_rdoc_files:
117
117
  - readme.md
118
118
  files:
119
119
  - lib/mad_math.rb
120
+ - lib/mad_math/all.rb
121
+ - lib/mad_math/newton_polynomial.rb
122
+ - lib/mad_math/stats.rb
120
123
  - readme.md
121
124
  homepage: http://www.bitbucket.org/ToadJamb/gems_mad_math
122
125
  licenses: