Module: Measurable::Minkowski

Included in:
Measurable
Defined in:
lib/measurable/minkowski.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:


26
27
28
29
30
31
32
# File 'lib/measurable/minkowski.rb', line 26

def self.extended(base) # :nodoc:
  base.instance_eval do
    alias :cityblock :minkowski
    alias :manhattan :minkowski
  end
  super
end

.included(base) ⇒ Object

:nodoc:


34
35
36
37
38
39
40
# File 'lib/measurable/minkowski.rb', line 34

def self.included(base) # :nodoc:
  base.class_eval do
    alias :cityblock :minkowski
    alias :manhattan :minkowski
  end
  super
end

Instance Method Details

#minkowski(u, v) ⇒ Object

call-seq:

minkowski(u, v) -> Numeric

Calculate the sum of the absolute value of the differences between each coordinate of u and v.

Arguments:

  • u -> An array of Numeric objects.

  • v -> An array of Numeric objects.

Returns:

  • The Minkowski (or L1) distance between u and v.

Raises:

  • ArgumentError -> The sizes of u and v don't match.

Raises:

  • (ArgumentError)

17
18
19
20
21
22
23
24
# File 'lib/measurable/minkowski.rb', line 17

def minkowski(u, v)
  # TODO: Change this to a more specific, custom-made exception.
  raise ArgumentError if u.size != v.size

  u.zip(v).reduce(0) do |acc, elem|
    acc += (elem[0] - elem[1]).abs
  end
end