chris_lib 1.0.2 → 1.0.3
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 +4 -4
- data/lib/chris_lib/chris_math.rb +96 -80
- data/lib/chris_lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32ffed400557360fd6645e75dd29aff6481fe8cc
|
4
|
+
data.tar.gz: a4a9e0b507c5844641d46b3e3a3fd61e7c5f8ed7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00e58b8fecbe9408ba6ece1ff97aefa5da3851e42ef0b20f001923ee03183157f65fc692e24453e6c19df1f84004dcb0f0691dce05c38de34f3c52700c8b1a68
|
7
|
+
data.tar.gz: 2e90db2bc7e07fa5ec828daeb44365a57f91de7f3ec5e6d59935d4005cb602d337fa7985d43ed4bed56063343ae8c1cad6048254047d5fd7a2f925b4c03345f3
|
data/lib/chris_lib/chris_math.rb
CHANGED
@@ -1,89 +1,105 @@
|
|
1
1
|
Integer.class_eval do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
def factorial
|
3
|
+
n = self
|
4
|
+
if n > 20
|
5
|
+
fail 'Number too large'
|
6
|
+
else
|
7
|
+
(1..n).inject {|prod, i| prod * i}
|
8
|
+
end
|
9
|
+
end
|
10
10
|
end
|
11
11
|
Array.class_eval do
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
12
|
+
def mean
|
13
|
+
fail 'Length must be greater than 1.' if count < 2
|
14
|
+
sum = self.inject { |s, v| s + v }
|
15
|
+
sum.to_f / count
|
16
|
+
end
|
17
|
+
|
18
|
+
def var
|
19
|
+
fail 'Length must be greater than 1' if count < 2
|
20
|
+
m = self.mean
|
21
|
+
sum = self.inject { |s,v| s + (v**2 - m**2)}
|
22
|
+
m=count-1
|
23
|
+
sum.to_f/m
|
24
|
+
end
|
25
|
+
|
26
|
+
def median
|
27
|
+
return self[0] if length <= 1
|
28
|
+
sorted = sort
|
29
|
+
n = length
|
30
|
+
if n.odd? # length is odd
|
31
|
+
sorted[n/2]
|
32
|
+
else
|
33
|
+
(sorted[n/2] + sorted[n/2-1]).to_f/2
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def histogram
|
38
|
+
k = Hash.new(0)
|
39
|
+
self.each { |x| k[x] += 1 }
|
40
|
+
k
|
41
|
+
end
|
34
42
|
end
|
35
43
|
|
36
44
|
module ChrisMath
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
|
46
|
+
include Math
|
47
|
+
|
48
|
+
def gaussian_array(n = 1)
|
49
|
+
(1..n).map do
|
50
|
+
u1 = rand()
|
51
|
+
u2 = rand()
|
52
|
+
sqrt(-2*log(u1))*cos(2*PI*u2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def bi_gaussian_rand
|
57
|
+
u1 = rand()
|
58
|
+
u2 = rand()
|
59
|
+
z0 = sqrt(-2*log(u1))*cos(2*PI*u2)
|
60
|
+
z1 = sqrt(-2*log(u1))*sin(2*PI*u2)
|
61
|
+
[z0,z1]
|
62
|
+
end
|
47
63
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
+
def gaussian_rand(mean,std)
|
65
|
+
u1 = rand()
|
66
|
+
u2 = rand()
|
67
|
+
z0 = sqrt(-2*log(u1))*cos(2*PI*u2)
|
68
|
+
z0*std + mean
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def std(values)
|
73
|
+
n = values.count
|
74
|
+
fail 'n = #{n} but must be greater than 1' if n < 2
|
75
|
+
m = mean(values)
|
76
|
+
sum = values.inject { |s,v| s + (v**2 - m**2)}
|
77
|
+
sqrt(sum.to_f/(n - 1))
|
78
|
+
end
|
79
|
+
|
64
80
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
81
|
+
def combinatorial_distribution(n,r,p)
|
82
|
+
# probability that r out n or greater hits with the
|
83
|
+
# probability of one hit is p.
|
84
|
+
if r <= n && r >= 0
|
85
|
+
sum = 0
|
86
|
+
(r..n).each do |k|
|
87
|
+
sum += combinatorial(n,k)*(p)**(k)*(1-p)**(n - k)
|
88
|
+
end
|
89
|
+
sum
|
90
|
+
else
|
91
|
+
fail 'Error, #{r} must be >= 0 and <= #{n}'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def factorial(n)
|
96
|
+
#from rosetta code
|
97
|
+
(1..n).inject {|prod, i| prod * i}
|
98
|
+
end
|
99
|
+
|
100
|
+
def combinatorial(n, k)
|
101
|
+
#from rosetta code
|
102
|
+
(0...k).inject(1) do |m,i| (m * (n - i)) / (i + 1) end
|
103
|
+
end
|
104
|
+
|
89
105
|
end
|
data/lib/chris_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chris_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
version: '0'
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
|
-
rubygems_version: 2.
|
92
|
+
rubygems_version: 2.4.5
|
93
93
|
signing_key:
|
94
94
|
specification_version: 4
|
95
95
|
summary: This an eclectic collection of methods. It include maths, datetime, and rspec
|