Class: Cluster::Centroid
- Inherits:
-
Object
- Object
- Cluster::Centroid
- Defined in:
- lib/lite/cluster.rb
Instance Attribute Summary collapse
-
#n ⇒ Object
Returns the value of attribute n.
-
#x ⇒ Object
Returns the value of attribute x.
Instance Method Summary collapse
-
#initialize(x) ⇒ Centroid
constructor
A new instance of Centroid.
- #merge!(centroid) ⇒ Object
- #to_s ⇒ Object
- #update!(newX) ⇒ Object
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
#n ⇒ Object
Returns the value of attribute n.
58 59 60 |
# File 'lib/lite/cluster.rb', line 58 def n @n end |
#x ⇒ Object
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_s ⇒ Object
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 |