Class: Hnswlib::L2Space

Inherits:
Object
  • Object
show all
Defined in:
ext/hnswlib/dummy.rb

Overview

L2Space is a class that calculates squared Euclidean distance for search index. This class is used internally.

Examples:

require 'hnswlib'

n_features = 3
space = Hnswlib::L2Space.new(n_features)

a = [1, 2, 3]
b = [4, 5, 6]
space.distance(a, b)
# => 27.0

Instance Method Summary collapse

Constructor Details

#initialize(dim) ⇒ L2Space

Create a new L2Space.

Parameters:

  • dim (Integer)

    The number of dimensions (features).



23
24
25
# File 'ext/hnswlib/dummy.rb', line 23

def initialize(dim)
  @dim = dim
end

Instance Method Details

#distance(arr_a, arr_b) ⇒ Float

Calculate the squared Euclidean distance between items: d = sum((Ai - Bi)^2)

Parameters:

  • arr_a (Array<Float>)

    The vector of item A.

  • arr_b (Array<Float>)

    The vector of item B.

Returns:

  • (Float)


33
34
35
# File 'ext/hnswlib/dummy.rb', line 33

def distance(arr_a, arr_b)
  a.zip(b).sum { |v| (v[0] - v[1])**2 }
end