Last active
May 2, 2016 18:31
-
-
Save whitehat101/6bd77dc70ca647ee5275 to your computer and use it in GitHub Desktop.
Add :mean to Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WhiteHat101 | |
module Mean | |
def mean | |
inject(0.0, :+) / length | |
end | |
alias avg mean | |
alias average mean | |
def mean_absolute_deviation | |
mean = self.mean | |
map {|r| (mean - r).abs }.mean | |
end | |
alias mad mean_absolute_deviation | |
def standard_deviation | |
mean = self.mean | |
Math.sqrt inject(0.0) {|sum, x| sum + (x - mean)**2 } / self.length.to_f | |
end | |
def standard_deviation2 | |
mean = self.mean | |
Math.sqrt inject(0.0) {|sum, x| sum + (x - mean)**2 } / (self.length.to_f - 1) | |
end | |
end | |
end | |
Array.send :include, WhiteHat101::Mean |
Author
whitehat101
commented
Apr 11, 2016
def eulers_method x0, y0, xN, step = 1.0, &block
until x0 > xN
puts "A(#{x0}) = #{y0} (step #{step})"
y0 += step * block.call(x0, y0)
x0 += step
end
end
def dilate origin_x, origin_y, scale=1.0
proc do |x,y|
[origin_x+scale*(x-origin_x), origin_y+scale*(y-origin_y)]
end
end
# [[2,2],[6,2],[2,5]].map &dilate(6,5,3) # => [[-6, -4], [6, -4], [-6, 5]]
Reflect over y = -x + 3
[[-2,5],[3,8],[2,6],[7,4],[7,2]].map{|x,y|[-y+3,-x+3]} # => [[-2, 5], [-5, 0], [-3, 1], [-1, -4], [1, -4]]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment