Class: Cluster::Centroid

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x) ⇒ Centroid

Returns a new instance of Centroid.



59
60
61
62
# File 'lib/lite/cluster.rb', line 59

def initialize( x )
  @x = x
  @n = 1
end

Instance Attribute Details

#nObject

Returns the value of attribute n.



58
59
60
# File 'lib/lite/cluster.rb', line 58

def n
  @n
end

#xObject

Returns the value of attribute x.



58
59
60
# File 'lib/lite/cluster.rb', line 58

def x
  @x
end

Instance Method Details

#merge!(centroid) ⇒ Object



70
71
72
73
74
# File 'lib/lite/cluster.rb', line 70

def merge!( centroid )
  @x = ( @x.mult_scalar(@n)+centroid.x.mult_scalar(centroid.n) ).mult_scalar( 1.0 / (@n + centroid.n) )
  @n += centroid.n
  self
end

#to_sObject



76
77
78
# File 'lib/lite/cluster.rb', line 76

def to_s
  "Centroid #{x.attr} of #{n} instances"
end

#update!(newX) ⇒ Object



64
65
66
67
68
# File 'lib/lite/cluster.rb', line 64

def update!( newX )
  @x += ( @x.mult_scalar(@n) + newX ).mult_scalar( 1.0/(@n+1) )
  @n +=  1
  self 
end