class AgglomerativeClustering::DistanceMatrix

Public Class Methods

new(matrix) click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 5
def initialize matrix
  @matrix_array = matrix.to_a
end

Public Instance Methods

add_edge(weights) click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 22
def add_edge weights
  matrix_array.each_with_index { |row, index|  row << weights[index] }
  matrix_array << weights
  @matrix = build_matrix
end
matrix() click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 9
def matrix
  @matrix ||= build_matrix
end
print_matrix() click to toggle source
remove_edge(index) click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 17
def remove_edge index
  matrix_array.delete_at(index)
  matrix_array.each { |row| row.delete_at(index) }
end
shortest_distance() click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 28
def shortest_distance
  min_dist = 1.0/0
  indexes = []
  matrix.each_with_index do |index, row, column|
    distance = matrix[row, column]
    if distance < min_dist && (row != column)
      min_dist = distance
      indexes = [row, column]
    end
  end
  indexes
end

Private Instance Methods

build_matrix() click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 47
def build_matrix
  Matrix.build(matrix_array.size, matrix_array.first.size) do |row, column|
    matrix_array[row][column]
  end
end
matrix_array() click to toggle source
# File lib/agglomerative_clustering/distance_matrix.rb, line 43
def matrix_array
  @matrix_array ||= []
end