Module: Math

Included in:
Paleta::Color, Paleta::Palette
Defined in:
lib/paleta/core_ext/math.rb

Instance Method Summary collapse

Instance Method Details

#distance(a, b) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/paleta/core_ext/math.rb', line 2

def distance(a, b)
  unless (a.is_a?(Hash) && b.is_a?(Hash) && a.keys == b.keys) || (a.is_a?(Array) && b.is_a?(Array) && a.size == b.size)
    raise ArgumentError, "Arguments must be Hashes with identical keys or Arrays of the same size"
  end
  sum = 0
  a.keys.each { |k| sum += (a[k] - b[k]) ** 2 } if a.is_a?(Hash)
  a.each_with_index { |v, i| sum += (a[i] - b[i]) ** 2 } if a.is_a?(Array)
  sqrt(sum)
end

#multiple_regression(dx, dy, dz) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/paleta/core_ext/math.rb', line 12

def multiple_regression(dx, dy, dz)
  regression = {}
  regression[:slope], regression[:offset] = {}, {}
  size = dx.size

  raise "arguments not same length!" unless size == dy.size && size == dz.size

  if size == 1
    regression[:slope] = { :x => dx[0], :y => dy[0], :z => dz[0] }
    regression[:offset] = { :x => 0, :y => 0, :z => 0 }
    return regression
  end

  sxx = syy = szz = sxy = szx = syz = sx = sy = sz = 0
  dx.zip(dy, dz).each do |x, y, z|
    sxx += x ** 2
    syy += y ** 2
    szz += z ** 2
    sxy += x * y
    szx += z * x
    syz += y * z
    sx  += x
    sy  += y
    sz  += z
  end

  regression[:slope][:x] = ( size * sxy - sx * sz ) / ( size * sxx - sx ** 2 ).to_f
  regression[:slope][:y] = ( size * syz - sz * sy ) / ( size * syy - sz ** 2 ).to_f
  regression[:slope][:z] = ( size * syz - sz * sy ) / ( size * szz - sy ** 2 ).to_f

  regression[:offset][:x] = (sz - regression[:slope][:x] * sx) / size
  regression[:offset][:y] = (sy - regression[:slope][:y] * sz) / size
  regression[:offset][:z] = (sx - regression[:slope][:z] * sy) / size

  regression
end