Class: Pgvector::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/pgvector/vector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Vector

Returns a new instance of Vector.



3
4
5
# File 'lib/pgvector/vector.rb', line 3

def initialize(data)
  @data = data.to_a.map(&:to_f)
end

Class Method Details

.from_binary(string) ⇒ Object



11
12
13
14
15
# File 'lib/pgvector/vector.rb', line 11

def self.from_binary(string)
  dim, unused = string[0, 4].unpack("nn")
  raise "expected unused to be 0" if unused != 0
  Vector.new(string[4..-1].unpack("g#{dim}"))
end

.from_text(string) ⇒ Object



7
8
9
# File 'lib/pgvector/vector.rb', line 7

def self.from_text(string)
  Vector.new(string[1..-2].split(",").map(&:to_f))
end

Instance Method Details

#to_aObject



21
22
23
# File 'lib/pgvector/vector.rb', line 21

def to_a
  @data
end

#to_sObject



17
18
19
# File 'lib/pgvector/vector.rb', line 17

def to_s
  "[#{@data.to_a.map(&:to_f).join(",")}]"
end