module Mathematics
mathematics.rb
IMPORTANT NOTE:
This program introduces/relies on “Prior Knowledge” in the domain of mathematics, and avoids computation where unnecessary.
Also, resorting to computation sometimes doesn't yield perfect results as known/anticipated. Example:
$ ruby -e “puts Math.sin(Math::PI) # I expect 0” 1.2246467991473532e-16
Public Class Methods
add(*a)
click to toggle source
.. sum .….….….….……
# File lib/mathematics.rb, line 50 def self.add *a a.reduce :+ end
average(*a)
click to toggle source
.. mean .….….….….……
# File lib/mathematics.rb, line 64 def self.average *a 1.0 * (a.reduce :+) / a.size end
avg(*a)
click to toggle source
# File lib/mathematics.rb, line 68 def self.avg *a average *a end
binomial_coefficient(n, k)
click to toggle source
.. binomial_coefficient
.….….….….……
# File lib/mathematics.rb, line 148 def self.binomial_coefficient n, k if k < 0 or k > n 0 elsif k == 0 or n == k 1 else c = 1 (1..k).each do |i| c *= n - k + i c /= i end c end end
cos(radians)
click to toggle source
# File lib/mathematics.rb, line 176 def self.cos radians degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease @cos_hash[degrees] || Math.cos(radians) end
desc()
click to toggle source
# File lib/mathematics.rb, line 26 def self.desc describe end
describe()
click to toggle source
.. describe .….….….….……
# File lib/mathematics.rb, line 22 def self.describe "Wrapper around Math module; Has additional methods." end
fact(n)
click to toggle source
# File lib/mathematics.rb, line 128 def self.fact n factorial n end
factorial(n)
click to toggle source
.. factorial .….….….….……
# File lib/mathematics.rb, line 122 def self.factorial n # uses "Prior Knowledge" paradigm h = { 0=>1, 1=>1, 2=>2, 3=>6, 4=>24, 5=>120, 6=>720, 7=>5040, 8=>40320, 9=>362880, 10=>3628800 } n < 0 ? nil : (h[n] || n * factorial(n-1)) end
max(*a)
click to toggle source
.. max .….….….….……
# File lib/mathematics.rb, line 38 def self.max *a a.max end
mean(*a)
click to toggle source
# File lib/mathematics.rb, line 72 def self.mean *a average *a end
median(*a)
click to toggle source
.. median .….….….….……
# File lib/mathematics.rb, line 78 def self.median *a a.sort! n = a.size n.odd? ? a[(n-1)/2] : avg(1.0 * a[n/2], a[n/2-1]) end
method_missing(m, *a)
click to toggle source
method_missing
========================================================¶ ↑
# File lib/mathematics.rb, line 197 def self.method_missing m, *a end
min(*a)
click to toggle source
.. min .….….….….……
# File lib/mathematics.rb, line 32 def self.min *a a.min end
minmax(*a)
click to toggle source
.. minmax .….….….….……
# File lib/mathematics.rb, line 44 def self.minmax *a a.minmax end
mode(*a)
click to toggle source
.. mode .….….….….……
# File lib/mathematics.rb, line 86 def self.mode *a h = Hash.new(0) a.each { |e| h[e] = h[e] + 1 } (h.sort_by &:last).last[0] end
range(*a)
click to toggle source
.. range .….….….….……
# File lib/mathematics.rb, line 94 def self.range *a (max *a) - (min *a) end
sd(*a)
click to toggle source
# File lib/mathematics.rb, line 116 def self.sd *a stddev *a end
sin(radians)
click to toggle source
# File lib/mathematics.rb, line 167 def self.sin radians degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease @sin_hash[degrees] || Math.sin(radians) end
stddev(*a)
click to toggle source
.. standard deviation .….….….….……
# File lib/mathematics.rb, line 112 def self.stddev *a Math.sqrt(variance *a) end
sum(*a)
click to toggle source
# File lib/mathematics.rb, line 54 def self.sum *a add *a end
tan(radians)
click to toggle source
# File lib/mathematics.rb, line 186 def self.tan radians degrees = (radians / Math::PI * 180) % 360 # think in degrees for ease # to deal with 45, 135, 225, 315 ... quantum = 1.0e-12 degrees = degrees.ceil if degrees.ceil - degrees < quantum degrees = degrees.floor if degrees - degrees.floor < quantum @tan_hash[degrees] || Math.tan(radians) end
total(*a)
click to toggle source
# File lib/mathematics.rb, line 58 def self.total *a add *a end
triangle_number(n)
click to toggle source
.. triangle_number
.….….….….……
# File lib/mathematics.rb, line 142 def self.triangle_number n triangular_number n end
triangular_number(n)
click to toggle source
.. triangular_number
.….….….….……
# File lib/mathematics.rb, line 134 def self.triangular_number n # uses "Prior Knowledge" paradigm h = { 1=>1, 2=>3, 3=>6, 4=>10, 5=>15, 6=>21, 7=>28, 8=>36, 9=>45, 10=>55, 11=>66, 12=>78, 13=>91 } n < 1 ? nil : (h[n] || n*(n+1)/2) end
var(*a)
click to toggle source
# File lib/mathematics.rb, line 106 def self.var *a variance *a end
variance(*a)
click to toggle source
.. variance .….….….….……
# File lib/mathematics.rb, line 100 def self.variance *a m = mean *a b = a.map { |e| (e-m)*(e-m) } # / a.size b.inject { |s,e| s+e } / b.size end