Class: FeldtRuby::FastMap::PivotNode

Inherits:
Object
  • Object
show all
Defined in:
lib/feldtruby/statistics/fastmap.rb

Overview

A PivotNode has two pivot objects, a map from each object to its coordinate on the line for these pivots, a distance function and a child pointing to the next dimension. It maps a multi-variate object to a k-dimensional coordinate.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distance, pivot1, pivot2, map = nil, child = nil) ⇒ PivotNode

Returns a new instance of PivotNode.



13
14
15
16
17
# File 'lib/feldtruby/statistics/fastmap.rb', line 13

def initialize(distance, pivot1, pivot2, map = nil, child = nil)
  @distance, @pivot1, @pivot2, @map, @child = distance, pivot1, pivot2, map, child
  @d_1_2 = distance.calc(pivot1, pivot2)
  @d_1_2_squared, @d_1_2_doubled = @d_1_2 * @d_1_2, 2 * @d_1_2
end

Instance Attribute Details

#child=(value) ⇒ Object (writeonly)

Sets the attribute child

Parameters:

  • value

    the value to set the attribute child to.



11
12
13
# File 'lib/feldtruby/statistics/fastmap.rb', line 11

def child=(value)
  @child = value
end

#map=(value) ⇒ Object (writeonly)

Sets the attribute map

Parameters:

  • value

    the value to set the attribute map to.



11
12
13
# File 'lib/feldtruby/statistics/fastmap.rb', line 11

def map=(value)
  @map = value
end

Instance Method Details

#[](object) ⇒ Object



34
35
36
# File 'lib/feldtruby/statistics/fastmap.rb', line 34

def [](object)
  coordinate(object)
end

#coordinate(o) ⇒ Object



30
31
32
# File 'lib/feldtruby/statistics/fastmap.rb', line 30

def coordinate(o)
  [map_object_to_coordinate(o)] + (@child ? @child.coordinate(o) : [])
end

#depthObject



21
22
23
# File 'lib/feldtruby/statistics/fastmap.rb', line 21

def depth
  @depth ||= 1 + (@child ? @child.depth : 0)
end

#fastmap_coordinate(o) ⇒ Object

Map an object to its coordinate in the dimension represented by this node.



26
27
28
# File 'lib/feldtruby/statistics/fastmap.rb', line 26

def fastmap_coordinate(o)
  ( @distance.calc(o, @pivot1) + @d_1_2_squared - @distance.calc(o, @pivot2) ) / @d_1_2_doubled
end

#kObject

The number of coordinates that will be returned for an object.



20
# File 'lib/feldtruby/statistics/fastmap.rb', line 20

def k; depth; end

#map_object_to_coordinate(o) ⇒ Object



38
39
40
# File 'lib/feldtruby/statistics/fastmap.rb', line 38

def map_object_to_coordinate(o)
  @map[o] || fastmap_coordinate(o)
end