Class: Pgvector::SparseVector
- Inherits:
-
Object
- Object
- Pgvector::SparseVector
- Defined in:
- lib/pgvector/sparse_vector.rb
Constant Summary collapse
- NO_DEFAULT =
Object.new
Instance Attribute Summary collapse
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(value, dimensions = NO_DEFAULT) ⇒ SparseVector
constructor
A new instance of SparseVector.
- #to_a ⇒ Object
- #to_binary ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value, dimensions = NO_DEFAULT) ⇒ SparseVector
Returns a new instance of SparseVector.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pgvector/sparse_vector.rb', line 7 def initialize(value, dimensions = NO_DEFAULT) if value.is_a?(Hash) if dimensions == NO_DEFAULT raise ArgumentError, "missing dimensions" end from_hash(value, dimensions) else unless dimensions == NO_DEFAULT raise ArgumentError, "extra argument" end from_array(value) end end |
Instance Attribute Details
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
3 4 5 |
# File 'lib/pgvector/sparse_vector.rb', line 3 def dimensions @dimensions end |
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
3 4 5 |
# File 'lib/pgvector/sparse_vector.rb', line 3 def indices @indices end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
3 4 5 |
# File 'lib/pgvector/sparse_vector.rb', line 3 def values @values end |
Class Method Details
.from_binary(string) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/pgvector/sparse_vector.rb', line 76 def from_binary(string) dim, nnz, unused = string[0, 12].unpack("l>l>l>") raise "expected unused to be 0" if unused != 0 indices = string[12, nnz * 4].unpack("l>#{nnz}") values = string[(12 + nnz * 4)..-1].unpack("g#{nnz}") from_parts(dim, indices, values) end |
.from_text(string) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pgvector/sparse_vector.rb', line 64 def from_text(string) elements, dimensions = string.split("/", 2) indices = [] values = [] elements[1..-2].split(",").each do |e| index, value = e.split(":", 2) indices << index.to_i - 1 values << value.to_f end from_parts(dimensions.to_i, indices, values) end |
Instance Method Details
#to_a ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/pgvector/sparse_vector.rb', line 25 def to_a arr = Array.new(dimensions, 0.0) @indices.zip(@values) do |i, v| arr[i] = v end arr end |
#to_binary ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/pgvector/sparse_vector.rb', line 33 def to_binary nnz = @indices.size buffer = [dimensions, nnz, 0].pack("l>l>l>") @indices.pack("l>#{nnz}", buffer: buffer) @values.pack("g#{nnz}", buffer: buffer) buffer end |
#to_s ⇒ Object
21 22 23 |
# File 'lib/pgvector/sparse_vector.rb', line 21 def to_s "{#{@indices.zip(@values).map { |i, v| "#{i.to_i + 1}:#{v.to_f}" }.join(",")}}/#{@dimensions.to_i}" end |