mathematics 0.1.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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +21 -0
- data/README.md +63 -0
- data/compare/trignometry.html +1 -0
- data/compare/trignometry.rb +46 -0
- data/lib/mathematics.rb +200 -0
- data/test/test_mathematics.rb +405 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 00769649216c4d4c750ab6d0bcafafdbd2138e88
|
4
|
+
data.tar.gz: 4bff8cff9ed761df27fbbd5d2f0ba74ffedb5f17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82416847e8b0678ce7dd4478abeb10607f0492abe09a1a853a89d074df15d7cb17d903ac622407a951a6ccf637036b58e75bb633b921014d7e96370d76b4c5ee
|
7
|
+
data.tar.gz: 5f5c74d17c09f62ad511a4ce5afacd33a7febcf62fe1c58f3f4215a77434397a232d3350d9870a92281ba3bf83483ad33c1c836d20c1ee775bfef2096ba339ca
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Naga Vijayapuram
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
mathematics
|
2
|
+
===========
|
3
|
+
Wrapper around Ruby Math module; Has additional methods.<br/>
|
4
|
+
http://rubygems.org/gems/mathematics
|
5
|
+
|
6
|
+
--------------<br/>
|
7
|
+
mathematics.rb<br/>
|
8
|
+
--------------<br/>
|
9
|
+
<br/>
|
10
|
+
IMPORTANT NOTE:<br/>
|
11
|
+
--------------<br/>
|
12
|
+
This program introduces/relies on "Prior Knowledge"<br/>
|
13
|
+
in the domain of mathematics, and avoids<br/>
|
14
|
+
computation where unnecessary.<br/>
|
15
|
+
<br/>
|
16
|
+
Also, resorting to computation sometimes doesn't yield<br/>
|
17
|
+
perfect results as known/anticipated. Example:<br/>
|
18
|
+
(environment: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0])<br/>
|
19
|
+
<br/>
|
20
|
+
$ ruby -e "puts Math.sin(Math::PI) # I expect 0"<br/>
|
21
|
+
1.2246467991473532e-16<br/><br/>
|
22
|
+
Under the hood of "Prior Knowledge" style/paradigm of programming,
|
23
|
+
some characteristic functions can manifest as "Intelligent Functions".<br/>
|
24
|
+
The intelligent function can rely on proven, tested, established<br/>
|
25
|
+
knowledge bases/answers; retrieving the result(s) can be on one or<br/>
|
26
|
+
more of these lines ...<br/>
|
27
|
+
. from cache (in-memory/local/remote/distributed)<br/>
|
28
|
+
. via delegation<br/>
|
29
|
+
. via rpc/rest/*cloud calls<br/>
|
30
|
+
.. depending on the need/nature of the "intelligent function" in the<br/>
|
31
|
+
domain space, and the prior knowledge of the complexity vis-a-vis<br/>
|
32
|
+
the path to take (not resorting to any call based on judgement,<br/>
|
33
|
+
distills down to a normal function,<br/>
|
34
|
+
which is a subset of the intelligent function).<br/>
|
35
|
+
|
36
|
+
install
|
37
|
+
=======
|
38
|
+
gem install mathematics
|
39
|
+
|
40
|
+
use
|
41
|
+
===
|
42
|
+
require 'mathematics'<br/>
|
43
|
+
<br/>
|
44
|
+
Mathematics.add(1,2)<br/>
|
45
|
+
Mathematics.sum(1,2,3)<br/>
|
46
|
+
Mathematics.total(1,2,3,4)<br/>
|
47
|
+
<br/>
|
48
|
+
Mathematics.average(1,2,3)<br/>
|
49
|
+
Mathematics.avg(1,2,3,4)<br/>
|
50
|
+
Mathematics.mean(1,2,3,4,5)<br/>
|
51
|
+
<br/>
|
52
|
+
Mathematics.sin(0)<br/>
|
53
|
+
Mathematics.cos(0)<br/>
|
54
|
+
Mathematics.sin(Math::PI/2)<br/>
|
55
|
+
Mathematics.cos(Math::PI/2)<br/>
|
56
|
+
|
57
|
+
comments
|
58
|
+
========
|
59
|
+
Feel free to provide comments
|
60
|
+
|
61
|
+
LICENSE
|
62
|
+
=======
|
63
|
+
MIT - http://opensource.org/licenses/MIT
|
@@ -0,0 +1 @@
|
|
1
|
+
<html> <head> <title> Trignometry - Ruby Math Module vs Ruby Mathematics Module </title> </head><body bgcolor='lightblue'><center><h3>Trignometry - Ruby Math Module vs Ruby Mathematics Module</h3> <table bgcolor='yellow' cellspacing='5' cellpadding='5' border='5'> <tr><td>0.0</td><td>0.0</td> <tr><td>0.7071067811865475</td><td>0.7071067811865475</td></tr> <tr><td>1.0</td><td>1.0</td></tr> <tr><td>0.7071067811865476</td><td>0.7071067811865476</td></tr> <tr><td>1.2246467991473532e-16</td><td>1.2246467991473532e-16</td></tr> <tr><td>-0.7071067811865475</td><td>-0.7071067811865475</td></tr> <tr><td>-1.0</td><td>-1.0</td></tr> <tr><td>-0.7071067811865477</td><td>-0.7071067811865477</td></tr> <tr><td>-2.4492935982947064e-16</td><td>-2.4492935982947064e-16</td></tr> <tr><td> </td><td> </td> <tr><td>1.0</td><td>1.0</td> <tr><td>0.7071067811865476</td><td>0.7071067811865476</td></tr> <tr><td>6.123233995736766e-17</td><td>6.123233995736766e-17</td></tr> <tr><td>-0.7071067811865475</td><td>-0.7071067811865475</td></tr> <tr><td>-1.0</td><td>-1.0</td></tr> <tr><td>-0.7071067811865477</td><td>-0.7071067811865477</td></tr> <tr><td>-1.8369701987210297e-16</td><td>-1.8369701987210297e-16</td></tr> <tr><td>0.7071067811865475</td><td>0.7071067811865475</td></tr> <tr><td>1.0</td><td>1.0</td></tr> </table></center></body></html>
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'mathematics'
|
2
|
+
|
3
|
+
File.open('trignometry.html', 'w') do |file|
|
4
|
+
file.write("<html>")
|
5
|
+
file.write(" <head>")
|
6
|
+
file.write(" <title>")
|
7
|
+
file.write(" Trignometry - Ruby Math Module vs Ruby Mathematics Module")
|
8
|
+
file.write(" </title>")
|
9
|
+
file.write(" </head>")
|
10
|
+
file.write("<body bgcolor='lightblue'>")
|
11
|
+
file.write("<center>")
|
12
|
+
file.write("<h3>Trignometry - Ruby Math Module vs Ruby Mathematics Module</h3>")
|
13
|
+
file.write(" <table bgcolor='yellow' cellspacing='5' cellpadding='5' border='5'>")
|
14
|
+
file.write(" <tr><th>Trignometric Function</th><th>Using Math Module</th><th>Using Mathematics Module</th>")
|
15
|
+
file.write(" <tr><th>sin</th><td>#{Math.sin(0)}</td><td>#{Mathematics.sin(0)}</td>")
|
16
|
+
file.write(" <tr><td></td><td>#{Math.sin(0.25*Math::PI)}</td><td>#{Mathematics.sin(0.25*Math::PI)}</td></tr>")
|
17
|
+
file.write(" <tr><td></td><td>#{Math.sin(0.5*Math::PI)}</td><td>#{Mathematics.sin(0.5*Math::PI)}</td></tr>")
|
18
|
+
file.write(" <tr><td></td><td>#{Math.sin(0.75*Math::PI)}</td><td>#{Mathematics.sin(0.75*Math::PI)}</td></tr>")
|
19
|
+
file.write(" <tr><td></td><td>#{Math.sin(Math::PI)}</td><td>#{Mathematics.sin(Math::PI)}</td></tr>")
|
20
|
+
file.write(" <tr><td></td><td>#{Math.sin(1.25*Math::PI)}</td><td>#{Mathematics.sin(1.25*Math::PI)}</td></tr>")
|
21
|
+
file.write(" <tr><td></td><td>#{Math.sin(1.5*Math::PI)}</td><td>#{Mathematics.sin(1.5*Math::PI)}</td></tr>")
|
22
|
+
file.write(" <tr><td></td><td>#{Math.sin(1.75*Math::PI)}</td><td>#{Mathematics.sin(1.75*Math::PI)}</td></tr>")
|
23
|
+
file.write(" <tr><td></td><td>#{Math.sin(2*Math::PI)}</td><td>#{Mathematics.sin(2*Math::PI)}</td></tr>")
|
24
|
+
file.write(" <tr><th>cos</th><td>#{Math.cos(0)}</td><td>#{Mathematics.cos(0)}</td>")
|
25
|
+
file.write(" <tr><td></td><td>#{Math.cos(0.25*Math::PI)}</td><td>#{Mathematics.cos(0.25*Math::PI)}</td></tr>")
|
26
|
+
file.write(" <tr><td></td><td>#{Math.cos(0.5*Math::PI)}</td><td>#{Mathematics.cos(0.5*Math::PI)}</td></tr>")
|
27
|
+
file.write(" <tr><td></td><td>#{Math.cos(0.75*Math::PI)}</td><td>#{Mathematics.cos(0.75*Math::PI)}</td></tr>")
|
28
|
+
file.write(" <tr><td></td><td>#{Math.cos(Math::PI)}</td><td>#{Mathematics.cos(Math::PI)}</td></tr>")
|
29
|
+
file.write(" <tr><td></td><td>#{Math.cos(1.25*Math::PI)}</td><td>#{Mathematics.cos(1.25*Math::PI)}</td></tr>")
|
30
|
+
file.write(" <tr><td></td><td>#{Math.cos(1.5*Math::PI)}</td><td>#{Mathematics.cos(1.5*Math::PI)}</td></tr>")
|
31
|
+
file.write(" <tr><td></td><td>#{Math.cos(1.75*Math::PI)}</td><td>#{Mathematics.cos(1.75*Math::PI)}</td></tr>")
|
32
|
+
file.write(" <tr><td></td><td>#{Math.cos(2*Math::PI)}</td><td>#{Mathematics.cos(2*Math::PI)}</td></tr>")
|
33
|
+
file.write(" <tr><th>tan</th><td>#{Math.tan(0)}</td><td>#{Mathematics.tan(0)}</td>")
|
34
|
+
file.write(" <tr><td></td><td>#{Math.tan(0.25*Math::PI)}</td><td>#{Mathematics.tan(0.25*Math::PI)}</td></tr>")
|
35
|
+
file.write(" <tr><td></td><td>#{Math.tan(0.5*Math::PI)}</td><td>#{Mathematics.tan(0.5*Math::PI)}</td></tr>")
|
36
|
+
file.write(" <tr><td></td><td>#{Math.tan(0.75*Math::PI)}</td><td>#{Mathematics.tan(0.75*Math::PI)}</td></tr>")
|
37
|
+
file.write(" <tr><td></td><td>#{Math.tan(Math::PI)}</td><td>#{Mathematics.tan(Math::PI)}</td></tr>")
|
38
|
+
file.write(" <tr><td></td><td>#{Math.tan(1.25*Math::PI)}</td><td>#{Mathematics.tan(1.25*Math::PI)}</td></tr>")
|
39
|
+
file.write(" <tr><td></td><td style='color:red'>#{Math.tan(1.5*Math::PI)}</td><td>#{Mathematics.tan(1.5*Math::PI)}</td></tr>")
|
40
|
+
file.write(" <tr><td></td><td>#{Math.tan(1.75*Math::PI)}</td><td>#{Mathematics.tan(1.75*Math::PI)}</td></tr>")
|
41
|
+
file.write(" <tr><td></td><td>#{Math.tan(2*Math::PI)}</td><td>#{Mathematics.tan(2*Math::PI)}</td></tr>")
|
42
|
+
file.write(" </table>")
|
43
|
+
file.write("</center>")
|
44
|
+
file.write("</body>")
|
45
|
+
file.write("</html>")
|
46
|
+
end
|
data/lib/mathematics.rb
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
#
|
2
|
+
# mathematics.rb
|
3
|
+
#
|
4
|
+
# IMPORTANT NOTE:
|
5
|
+
# --------------
|
6
|
+
# This program introduces/relies on "Prior Knowledge"
|
7
|
+
# in the domain of mathematics, and avoids
|
8
|
+
# computation where unnecessary.
|
9
|
+
#
|
10
|
+
# Also, resorting to computation sometimes doesn't yield
|
11
|
+
# perfect results as known/anticipated. Example:
|
12
|
+
#
|
13
|
+
# $ ruby -e "puts Math.sin(Math::PI) # I expect 0"
|
14
|
+
# 1.2246467991473532e-16
|
15
|
+
#
|
16
|
+
module Mathematics
|
17
|
+
|
18
|
+
extend Math # rely on Math module / build atop Math module
|
19
|
+
|
20
|
+
# .. describe .......................
|
21
|
+
|
22
|
+
def self.describe
|
23
|
+
"Wrapper around Math module; Has additional methods."
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.desc
|
27
|
+
describe
|
28
|
+
end
|
29
|
+
|
30
|
+
# .. min .......................
|
31
|
+
|
32
|
+
def self.min *a
|
33
|
+
a.min
|
34
|
+
end
|
35
|
+
|
36
|
+
# .. max .......................
|
37
|
+
|
38
|
+
def self.max *a
|
39
|
+
a.max
|
40
|
+
end
|
41
|
+
|
42
|
+
# .. minmax .......................
|
43
|
+
|
44
|
+
def self.minmax *a
|
45
|
+
a.minmax
|
46
|
+
end
|
47
|
+
|
48
|
+
# .. sum .......................
|
49
|
+
|
50
|
+
def self.add *a
|
51
|
+
a.reduce :+
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.sum *a
|
55
|
+
add *a
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.total *a
|
59
|
+
add *a
|
60
|
+
end
|
61
|
+
|
62
|
+
# .. mean .......................
|
63
|
+
|
64
|
+
def self.average *a
|
65
|
+
1.0 * (a.reduce :+) / a.size
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.avg *a
|
69
|
+
average *a
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.mean *a
|
73
|
+
average *a
|
74
|
+
end
|
75
|
+
|
76
|
+
# .. median .......................
|
77
|
+
|
78
|
+
def self.median *a
|
79
|
+
a.sort!
|
80
|
+
n = a.size
|
81
|
+
n.odd? ? a[(n-1)/2] : avg(1.0 * a[n/2], a[n/2-1])
|
82
|
+
end
|
83
|
+
|
84
|
+
# .. mode .......................
|
85
|
+
|
86
|
+
def self.mode *a
|
87
|
+
h = Hash.new(0)
|
88
|
+
a.each { |e| h[e] = h[e] + 1 }
|
89
|
+
(h.sort_by &:last).last[0]
|
90
|
+
end
|
91
|
+
|
92
|
+
# .. range .......................
|
93
|
+
|
94
|
+
def self.range *a
|
95
|
+
(max *a) - (min *a)
|
96
|
+
end
|
97
|
+
|
98
|
+
# .. variance .......................
|
99
|
+
|
100
|
+
def self.variance *a
|
101
|
+
m = mean *a
|
102
|
+
b = a.map { |e| (e-m)*(e-m) } # / a.size
|
103
|
+
b.inject { |s,e| s+e } / b.size
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.var *a
|
107
|
+
variance *a
|
108
|
+
end
|
109
|
+
|
110
|
+
# .. standard deviation .......................
|
111
|
+
|
112
|
+
def self.stddev *a
|
113
|
+
Math.sqrt(variance *a)
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.sd *a
|
117
|
+
stddev *a
|
118
|
+
end
|
119
|
+
|
120
|
+
# .. factorial .......................
|
121
|
+
|
122
|
+
def self.factorial n # uses "Prior Knowledge" paradigm
|
123
|
+
h = { 0=>1, 1=>1, 2=>2, 3=>6, 4=>24, 5=>120,
|
124
|
+
6=>720, 7=>5040, 8=>40320, 9=>362880, 10=>3628800 }
|
125
|
+
n < 0 ? nil : (h[n] || n * factorial(n-1))
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.fact n
|
129
|
+
factorial n
|
130
|
+
end
|
131
|
+
|
132
|
+
# .. triangular_number .......................
|
133
|
+
|
134
|
+
def self.triangular_number n # uses "Prior Knowledge" paradigm
|
135
|
+
h = { 1=>1, 2=>3, 3=>6, 4=>10, 5=>15, 6=>21,
|
136
|
+
7=>28, 8=>36, 9=>45, 10=>55, 11=>66, 12=>78, 13=>91 }
|
137
|
+
n < 1 ? nil : (h[n] || n*(n+1)/2)
|
138
|
+
end
|
139
|
+
|
140
|
+
# .. triangle_number .......................
|
141
|
+
|
142
|
+
def self.triangle_number n
|
143
|
+
triangular_number n
|
144
|
+
end
|
145
|
+
|
146
|
+
# .. binomial_coefficient .......................
|
147
|
+
|
148
|
+
def self.binomial_coefficient n, k
|
149
|
+
if k < 0 or k > n
|
150
|
+
0
|
151
|
+
elsif k == 0 or n == k
|
152
|
+
1
|
153
|
+
else
|
154
|
+
c = 1
|
155
|
+
(1..k).each do |i|
|
156
|
+
c *= n - k + i
|
157
|
+
c /= i
|
158
|
+
end
|
159
|
+
c
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
# .. sin .......................
|
164
|
+
|
165
|
+
@sin_hash = { 0.0 => 0, 90.0 => 1, 180.0 => 0, 270.0 => -1 }
|
166
|
+
|
167
|
+
def self.sin radians
|
168
|
+
degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease
|
169
|
+
@sin_hash[degrees] || Math.sin(radians)
|
170
|
+
end
|
171
|
+
|
172
|
+
# .. cos .......................
|
173
|
+
|
174
|
+
@cos_hash = { 0.0 => 1, 90.0 => 0, 180.0 => -1, 270.0 => 0 }
|
175
|
+
|
176
|
+
def self.cos radians
|
177
|
+
degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease
|
178
|
+
@cos_hash[degrees] || Math.cos(radians)
|
179
|
+
end
|
180
|
+
|
181
|
+
# .. tan .......................
|
182
|
+
|
183
|
+
@tan_hash = { 0 => 0, 45 => 1, 90 => 1.0/0, 135 => -1,
|
184
|
+
180 => 0, 225 => 1, 270 => -1.0/0, 315 => -1 }
|
185
|
+
|
186
|
+
def self.tan radians
|
187
|
+
degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease
|
188
|
+
# to deal with 45, 135, 225, 315 ...
|
189
|
+
quantum = 1.0e-12
|
190
|
+
degrees = degrees.ceil if degrees.ceil - degrees < quantum
|
191
|
+
degrees = degrees.floor if degrees - degrees.floor < quantum
|
192
|
+
@tan_hash[degrees] || Math.tan(radians)
|
193
|
+
end
|
194
|
+
|
195
|
+
# == method_missing ========================================================
|
196
|
+
|
197
|
+
def self.method_missing m, *a
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
@@ -0,0 +1,405 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/mathematics'
|
3
|
+
|
4
|
+
class TestMathematics < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# .. describe .......................
|
7
|
+
|
8
|
+
def test_describe
|
9
|
+
assert Mathematics.desc == "Wrapper around Math module; Has additional methods."
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_desc
|
13
|
+
assert Mathematics.desc == "Wrapper around Math module; Has additional methods."
|
14
|
+
end
|
15
|
+
|
16
|
+
# .. min .......................
|
17
|
+
|
18
|
+
def test_min
|
19
|
+
assert Mathematics.min(1,2,3) == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
# .. max .......................
|
23
|
+
|
24
|
+
def test_max
|
25
|
+
assert Mathematics.max(1,2,3) == 3
|
26
|
+
end
|
27
|
+
|
28
|
+
# .. minmax .......................
|
29
|
+
|
30
|
+
def test_minmax
|
31
|
+
assert Mathematics.minmax(1,2,3,4,5) == [1,5]
|
32
|
+
end
|
33
|
+
|
34
|
+
# .. sum .......................
|
35
|
+
|
36
|
+
def test_add
|
37
|
+
assert Mathematics.add(1,2) == 3
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_sum
|
41
|
+
assert Mathematics.sum(1,2,3) == 6
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_total
|
45
|
+
assert Mathematics.total(1,2,3,4) == 10
|
46
|
+
end
|
47
|
+
|
48
|
+
# .. mean .......................
|
49
|
+
|
50
|
+
def test_average
|
51
|
+
assert Mathematics.average(1,2,3) == 2
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_avg
|
55
|
+
assert Mathematics.avg(1,2,3,4) == 2.5
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_mean
|
59
|
+
assert Mathematics.mean(1,2,3,4,5) == 3
|
60
|
+
end
|
61
|
+
|
62
|
+
# .. median .......................
|
63
|
+
|
64
|
+
def test_median
|
65
|
+
assert Mathematics.median(1,2,4,7,11) == 4
|
66
|
+
assert Mathematics.median(1,2,4,7,11,13) == 5.5
|
67
|
+
end
|
68
|
+
|
69
|
+
# .. mode .......................
|
70
|
+
|
71
|
+
def test_mode
|
72
|
+
assert Mathematics.mode(1,2,2,3,3,3) == 3
|
73
|
+
end
|
74
|
+
|
75
|
+
# .. range .......................
|
76
|
+
|
77
|
+
def test_range
|
78
|
+
assert Mathematics.range(0,1,2,3,4,5,6,7,8,9) == 9
|
79
|
+
assert Mathematics.range(3,4,5,6,7,8) == 5
|
80
|
+
end
|
81
|
+
|
82
|
+
# .. variance .......................
|
83
|
+
|
84
|
+
def test_variance
|
85
|
+
assert Mathematics.variance(1,2,3) > 0.66 && Mathematics.variance(1,2,3) < 0.67
|
86
|
+
assert Mathematics.var(1,2,3) > 0.66 && Mathematics.var(1,2,3) < 0.67
|
87
|
+
end
|
88
|
+
|
89
|
+
# .. standard deviation .......................
|
90
|
+
|
91
|
+
def test_stddev
|
92
|
+
assert Mathematics.stddev(1,2,3) > 0.8164 && Mathematics.stddev(1,2,3) < 0.8165
|
93
|
+
assert Mathematics.sd(1,2,3) > 0.8164 && Mathematics.sd(1,2,3) < 0.8165
|
94
|
+
end
|
95
|
+
|
96
|
+
# .. factorial .......................
|
97
|
+
|
98
|
+
def test_factorial
|
99
|
+
result = Mathematics.factorial(-1)
|
100
|
+
assert result == nil, "=> It was #{result}"
|
101
|
+
result = Mathematics.factorial(0)
|
102
|
+
assert result == 1, "=> It was #{result}"
|
103
|
+
result = Mathematics.factorial(1)
|
104
|
+
assert result == 1, "=> It was #{result}"
|
105
|
+
result = Mathematics.factorial(2)
|
106
|
+
assert result == 2, "=> It was #{result}"
|
107
|
+
result = Mathematics.factorial(3)
|
108
|
+
assert result == 6, "=> It was #{result}"
|
109
|
+
result = Mathematics.factorial(4)
|
110
|
+
assert result == 24, "=> It was #{result}"
|
111
|
+
result = Mathematics.factorial(5)
|
112
|
+
assert result == 120, "=> It was #{result}"
|
113
|
+
result = Mathematics.factorial(6)
|
114
|
+
assert result == 720, "=> It was #{result}"
|
115
|
+
result = Mathematics.factorial(7)
|
116
|
+
assert result == 5040, "=> It was #{result}"
|
117
|
+
result = Mathematics.factorial(8)
|
118
|
+
assert result == 40320, "=> It was #{result}"
|
119
|
+
result = Mathematics.factorial(9)
|
120
|
+
assert result == 362880, "=> It was #{result}"
|
121
|
+
result = Mathematics.factorial(10)
|
122
|
+
assert result == 3628800, "=> It was #{result}"
|
123
|
+
result = Mathematics.factorial(11)
|
124
|
+
assert result == 39916800, "=> It was #{result}"
|
125
|
+
result = Mathematics.factorial(12)
|
126
|
+
assert result == 479001600, "=> It was #{result}"
|
127
|
+
end
|
128
|
+
|
129
|
+
# .. fact .......................
|
130
|
+
|
131
|
+
def test_fact
|
132
|
+
result = Mathematics.fact(-1)
|
133
|
+
assert result == nil, "=> It was #{result}"
|
134
|
+
result = Mathematics.fact(0)
|
135
|
+
assert result == 1, "=> It was #{result}"
|
136
|
+
result = Mathematics.fact(1)
|
137
|
+
assert result == 1, "=> It was #{result}"
|
138
|
+
result = Mathematics.fact(2)
|
139
|
+
assert result == 2, "=> It was #{result}"
|
140
|
+
result = Mathematics.fact(3)
|
141
|
+
assert result == 6, "=> It was #{result}"
|
142
|
+
result = Mathematics.fact(4)
|
143
|
+
assert result == 24, "=> It was #{result}"
|
144
|
+
result = Mathematics.fact(5)
|
145
|
+
assert result == 120, "=> It was #{result}"
|
146
|
+
result = Mathematics.fact(6)
|
147
|
+
assert result == 720, "=> It was #{result}"
|
148
|
+
result = Mathematics.fact(7)
|
149
|
+
assert result == 5040, "=> It was #{result}"
|
150
|
+
result = Mathematics.fact(8)
|
151
|
+
assert result == 40320, "=> It was #{result}"
|
152
|
+
result = Mathematics.fact(9)
|
153
|
+
assert result == 362880, "=> It was #{result}"
|
154
|
+
result = Mathematics.fact(10)
|
155
|
+
assert result == 3628800, "=> It was #{result}"
|
156
|
+
result = Mathematics.fact(11)
|
157
|
+
assert result == 39916800, "=> It was #{result}"
|
158
|
+
result = Mathematics.fact(12)
|
159
|
+
assert result == 479001600, "=> It was #{result}"
|
160
|
+
end
|
161
|
+
|
162
|
+
# .. triangular_number .......................
|
163
|
+
|
164
|
+
def test_triangular_number
|
165
|
+
result = Mathematics.triangular_number(-1)
|
166
|
+
assert result == nil, "=> It was #{result}"
|
167
|
+
result = Mathematics.triangular_number(0)
|
168
|
+
assert result == nil, "=> It was #{result}"
|
169
|
+
result = Mathematics.triangular_number(1)
|
170
|
+
assert result == 1, "=> It was #{result}"
|
171
|
+
result = Mathematics.triangular_number(2)
|
172
|
+
assert result == 3, "=> It was #{result}"
|
173
|
+
result = Mathematics.triangular_number(3)
|
174
|
+
assert result == 6, "=> It was #{result}"
|
175
|
+
result = Mathematics.triangular_number(4)
|
176
|
+
assert result == 10, "=> It was #{result}"
|
177
|
+
result = Mathematics.triangular_number(5)
|
178
|
+
assert result == 15, "=> It was #{result}"
|
179
|
+
result = Mathematics.triangular_number(6)
|
180
|
+
assert result == 21, "=> It was #{result}"
|
181
|
+
result = Mathematics.triangular_number(7)
|
182
|
+
assert result == 28, "=> It was #{result}"
|
183
|
+
result = Mathematics.triangular_number(8)
|
184
|
+
assert result == 36, "=> It was #{result}"
|
185
|
+
result = Mathematics.triangular_number(9)
|
186
|
+
assert result == 45, "=> It was #{result}"
|
187
|
+
result = Mathematics.triangular_number(10)
|
188
|
+
assert result == 55, "=> It was #{result}"
|
189
|
+
result = Mathematics.triangular_number(11)
|
190
|
+
assert result == 66, "=> It was #{result}"
|
191
|
+
result = Mathematics.triangular_number(12)
|
192
|
+
assert result == 78, "=> It was #{result}"
|
193
|
+
result = Mathematics.triangular_number(13)
|
194
|
+
assert result == 91, "=> It was #{result}"
|
195
|
+
end
|
196
|
+
|
197
|
+
# .. triangle_number .......................
|
198
|
+
|
199
|
+
def test_triangle_number
|
200
|
+
result = Mathematics.triangle_number(-1)
|
201
|
+
assert result == nil, "=> It was #{result}"
|
202
|
+
result = Mathematics.triangle_number(0)
|
203
|
+
assert result == nil, "=> It was #{result}"
|
204
|
+
result = Mathematics.triangle_number(1)
|
205
|
+
assert result == 1, "=> It was #{result}"
|
206
|
+
result = Mathematics.triangle_number(2)
|
207
|
+
assert result == 3, "=> It was #{result}"
|
208
|
+
result = Mathematics.triangle_number(3)
|
209
|
+
assert result == 6, "=> It was #{result}"
|
210
|
+
result = Mathematics.triangle_number(4)
|
211
|
+
assert result == 10, "=> It was #{result}"
|
212
|
+
result = Mathematics.triangle_number(5)
|
213
|
+
assert result == 15, "=> It was #{result}"
|
214
|
+
result = Mathematics.triangle_number(6)
|
215
|
+
assert result == 21, "=> It was #{result}"
|
216
|
+
result = Mathematics.triangle_number(7)
|
217
|
+
assert result == 28, "=> It was #{result}"
|
218
|
+
result = Mathematics.triangle_number(8)
|
219
|
+
assert result == 36, "=> It was #{result}"
|
220
|
+
result = Mathematics.triangle_number(9)
|
221
|
+
assert result == 45, "=> It was #{result}"
|
222
|
+
result = Mathematics.triangle_number(10)
|
223
|
+
assert result == 55, "=> It was #{result}"
|
224
|
+
result = Mathematics.triangle_number(11)
|
225
|
+
assert result == 66, "=> It was #{result}"
|
226
|
+
result = Mathematics.triangle_number(12)
|
227
|
+
assert result == 78, "=> It was #{result}"
|
228
|
+
result = Mathematics.triangle_number(13)
|
229
|
+
assert result == 91, "=> It was #{result}"
|
230
|
+
end
|
231
|
+
|
232
|
+
# .. binomial_coefficient .......................
|
233
|
+
|
234
|
+
def test_binomial_coefficient
|
235
|
+
assert Mathematics.binomial_coefficient(1,-1) == 0
|
236
|
+
assert Mathematics.binomial_coefficient(1,2) == 0
|
237
|
+
assert Mathematics.binomial_coefficient(3,0) == 1
|
238
|
+
assert Mathematics.binomial_coefficient(3,3) == 1
|
239
|
+
assert Mathematics.binomial_coefficient(3,2) == 3,
|
240
|
+
"It was #{Mathematics.binomial_coefficient(3,2)}"
|
241
|
+
assert Mathematics.binomial_coefficient(4,2) == 6,
|
242
|
+
"It was #{Mathematics.binomial_coefficient(4,2)}"
|
243
|
+
assert Mathematics.binomial_coefficient(5,3) == 10,
|
244
|
+
"It was #{Mathematics.binomial_coefficient(5,3)}"
|
245
|
+
end
|
246
|
+
|
247
|
+
# .. sin .......................
|
248
|
+
|
249
|
+
def test_sin_0
|
250
|
+
assert Mathematics.sin(0) == 0, "It was #{Mathematics.sin(0)}"
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_sin_45
|
254
|
+
assert (Mathematics.sin(0.25*Math::PI) > 0.7071) &&
|
255
|
+
(assert Mathematics.sin(0.25*Math::PI) < 0.7072),
|
256
|
+
"It was #{Mathematics.sin(0.25*Math::PI)}"
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_sin_90
|
260
|
+
assert Mathematics.sin(0.5*Math::PI) == 1, "It was #{Mathematics.sin(0.5*Math::PI)}"
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_sin_180
|
264
|
+
assert Mathematics.sin(Math::PI) == 0, "It was #{Mathematics.sin(Math::PI)}"
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_sin_270
|
268
|
+
assert Mathematics.sin(1.5*Math::PI) == -1, "It was #{Mathematics.sin(1.5*Math::PI)}"
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_sin_360
|
272
|
+
assert Mathematics.sin(2*Math::PI) == 0, "It was #{Mathematics.sin(2*Math::PI)}"
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_sin_450
|
276
|
+
assert Mathematics.sin(2.5*Math::PI) == 1, "It was #{Mathematics.sin(2.5*Math::PI)}"
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_sin_540
|
280
|
+
assert Mathematics.sin(3*Math::PI) == 0, "It was #{Mathematics.sin(3*Math::PI)}"
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_sin_630
|
284
|
+
assert Mathematics.sin(3.5*Math::PI) == -1, "It was #{Mathematics.sin(3.5*Math::PI)}"
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_sin_720
|
288
|
+
assert Mathematics.sin(4*Math::PI) == 0, "It was #{Mathematics.sin(4*Math::PI)}"
|
289
|
+
end
|
290
|
+
|
291
|
+
# .. cos .......................
|
292
|
+
|
293
|
+
def test_cos_0
|
294
|
+
assert Mathematics.cos(0) == 1, "It was #{Mathematics.cos(0)}"
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_cos_45
|
298
|
+
assert (Mathematics.cos(0.25*Math::PI) > 0.7071) &&
|
299
|
+
(assert Mathematics.cos(0.25*Math::PI) < 0.7072),
|
300
|
+
"It was #{Mathematics.cos(0.25*Math::PI)}"
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_cos_90
|
304
|
+
assert Mathematics.cos(0.5*Math::PI) == 0, "It was #{Mathematics.cos(0.5*Math::PI)}"
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_cos_180
|
308
|
+
assert Mathematics.cos(Math::PI) == -1, "It was #{Mathematics.cos(Math::PI)}"
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_cos_270
|
312
|
+
assert Mathematics.cos(1.5*Math::PI) == 0, "It was #{Mathematics.cos(1.5*Math::PI)}"
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_cos_360
|
316
|
+
assert Mathematics.cos(2*Math::PI) == 1, "It was #{Mathematics.cos(2*Math::PI)}"
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_cos_450
|
320
|
+
assert Mathematics.cos(2.5*Math::PI) == 0, "It was #{Mathematics.cos(2.5*Math::PI)}"
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_cos_540
|
324
|
+
assert Mathematics.cos(3*Math::PI) == -1, "It was #{Mathematics.cos(3*Math::PI)}"
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_cos_630
|
328
|
+
assert Mathematics.cos(3.5*Math::PI) == 0, "It was #{Mathematics.cos(3.5*Math::PI)}"
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_cos_720
|
332
|
+
assert Mathematics.cos(4*Math::PI) == 1, "It was #{Mathematics.cos(4*Math::PI)}"
|
333
|
+
end
|
334
|
+
|
335
|
+
# .. tan .......................
|
336
|
+
|
337
|
+
def test_tan_0
|
338
|
+
assert Mathematics.tan(0) == 0, "It was #{Mathematics.tan(0)}"
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_tan_45
|
342
|
+
assert Mathematics.tan(0.25*Math::PI) == 1, "It was #{Mathematics.tan(0.25*Math::PI)}"
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_tan_90
|
346
|
+
assert Mathematics.tan(0.5*Math::PI) == 1.0/0, "It was #{Mathematics.tan(0.5*Math::PI)}"
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_tan_135
|
350
|
+
assert Mathematics.tan(0.75*Math::PI) == -1, "It was #{Mathematics.tan(0.75*Math::PI)}"
|
351
|
+
end
|
352
|
+
|
353
|
+
def test_tan_180
|
354
|
+
assert Mathematics.tan(Math::PI) == 0, "It was #{Mathematics.tan(Math::PI)}"
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_tan_225
|
358
|
+
assert Mathematics.tan(1.25*Math::PI) == 1, "It was #{Mathematics.tan(1.25*Math::PI)}"
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_tan_270
|
362
|
+
assert Mathematics.tan(1.5*Math::PI) == -1.0/0, "It was #{Mathematics.tan(1.5*Math::PI)}"
|
363
|
+
end
|
364
|
+
|
365
|
+
def test_tan_315
|
366
|
+
assert Mathematics.tan(1.75*Math::PI) == -1, "It was #{Mathematics.tan(1.75*Math::PI)}"
|
367
|
+
end
|
368
|
+
|
369
|
+
def test_tan_360
|
370
|
+
assert Mathematics.tan(2*Math::PI) == 0, "It was #{Mathematics.tan(2*Math::PI)}"
|
371
|
+
end
|
372
|
+
|
373
|
+
def test_tan_405
|
374
|
+
assert Mathematics.tan(2.25*Math::PI) == 1, "It was #{Mathematics.tan(2.25*Math::PI)}"
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_tan_450
|
378
|
+
assert Mathematics.tan(2.5*Math::PI) == 1.0/0, "It was #{Mathematics.tan(2.5*Math::PI)}"
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_tan_495
|
382
|
+
assert Mathematics.tan(2.75*Math::PI) == -1, "It was #{Mathematics.tan(2.75*Math::PI)}"
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_tan_540
|
386
|
+
assert Mathematics.tan(3*Math::PI) == 0, "It was #{Mathematics.tan(3*Math::PI)}"
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_tan_585
|
390
|
+
assert Mathematics.tan(3.25*Math::PI) == 1, "It was #{Mathematics.tan(3.25*Math::PI)}"
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_tan_630
|
394
|
+
assert Mathematics.tan(3.5*Math::PI) == -1.0/0, "It was #{Mathematics.tan(3.5*Math::PI)}"
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_tan_675
|
398
|
+
assert Mathematics.tan(3.75*Math::PI) == -1, "It was #{Mathematics.tan(3.75*Math::PI)}"
|
399
|
+
end
|
400
|
+
|
401
|
+
def test_tan_720
|
402
|
+
assert Mathematics.tan(4*Math::PI) == 0, "It was #{Mathematics.tan(4*Math::PI)}"
|
403
|
+
end
|
404
|
+
|
405
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mathematics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Naga Vijayapuram
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Wrapper around Math module; Has additional methods.
|
14
|
+
email: nice.ruby.gems@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- MIT-LICENSE
|
20
|
+
- README.md
|
21
|
+
- lib/mathematics.rb
|
22
|
+
- test/test_mathematics.rb
|
23
|
+
- compare/trignometry.rb
|
24
|
+
- compare/trignometry.html
|
25
|
+
homepage: https://github.com/nvijayap/mathematics
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.0.2
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Mathematics
|
49
|
+
test_files: []
|