Skip to content

Instantly share code, notes, and snippets.

@whitehat101
Last active May 2, 2016 18:31
Show Gist options
  • Save whitehat101/6bd77dc70ca647ee5275 to your computer and use it in GitHub Desktop.
Save whitehat101/6bd77dc70ca647ee5275 to your computer and use it in GitHub Desktop.
Add :mean to Arrays
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
@whitehat101
Copy link
Author

def variance *data
  sample_size = data.length
  mean = data.inject(0.0, :+)/sample_size
  deviations_squared = data.map {|point| (mean - point)**2 }
  variance = deviations_squared.inject(0.0, :+)/sample_size
  variance2 = deviations_squared.inject(0.0, :+)/(sample_size-1)
  puts "Sample Size: n = #{sample_size}"
  puts "Mean: #{mean}"
  puts "Variance(n): #{variance}"
  puts "Variance(n-1): #{variance2}"
  variance
end

@whitehat101
Copy link
Author

# Returns a angle in degrees 0>angle>360 for a given x,y components
def vector_angle x, y
  angle = Math.atan(y/x.to_f) * 180/Math::PI
  angle += 180 if x < 0
  angle += 360 if angle < 0
  angle
end

@whitehat101
Copy link
Author

# to work these problems
# - first, last, delta
# - first, second, last
# - first, delta, n

class Arithmetic
  attr_accessor :a1, :an, :n, :sum, :delta
  # def initialize; end
  def self.from_a1_a2_an a1, a2, an
    a = new
    a.delta_from_values 1,a1, 2,a2
    a.n_from_first_last a1, an
    a
  end
  def self.from_a1_delta_n a1, delta, n
    a = new
    a.a1 = a1
    a.delta = delta
    a.n = n
    a.an_from_a1_delta_n
    a
  end

  def delta_from_values n1, v1, n2, v2
    @delta = (v2-v1)/(n2-n1).to_f
  end
  def n_from_first_last a1, an
    raise "need delta" unless @delta
    @a1 ||= a1
    @an ||= an
    @n = ((@a1 - @an)/@delta).abs + 1
  end
  def sum
    (a1 + an)/2.0*n
  end
  def an_from_a1_delta_n
    @an = a1 + delta*(n-1)
  end
end

@whitehat101
Copy link
Author

def d x1,y1, x2,y2
  Math.sqrt((x2-x1)**2+(y2-y1)**2)
end

def complex_distance a,ai, b,bi
  (Complex(a,ai) - Complex(b, bi)).abs
end

@whitehat101
Copy link
Author

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

@whitehat101
Copy link
Author

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]]

@whitehat101
Copy link
Author

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