Vettore.Vector (Vettore v0.3.5)

Copy Markdown View Source

Representation-independent dense vector operations.

The same API accepts ordinary numeric lists, row-major little-endian f32 binaries, %Vettore.Vector{} wrappers, and—when the host application has Nx installed—Nx.Tensor values. Native kernels operate directly on f32 binaries, while Nx remains an optional interchange format rather than a runtime requirement.

Little-endian f32 binaries are suitable for persistence and model matrices:

iex> {:ok, binary} = Vettore.Vector.to_f32_binary([3.0, 4.0])
iex> Vettore.Vector.dimensions(binary)
{:ok, 2}
iex> {:ok, normalized} = Vettore.Vector.normalize(binary, :l2, as: :list)
iex> Enum.map(normalized, &Float.round(&1, 1))
[0.6, 0.8]

The wrapper struct is useful at application boundaries where representation and dimensions should travel together:

iex> {:ok, vector} = Vettore.Vector.new([1, 2, 3], as: :f32_binary)
iex> {vector.representation, vector.dimensions}
{:f32_binary, 3}
iex> Vettore.Vector.to_list(vector)
{:ok, [1.0, 2.0, 3.0]}

Summary

Functions

Chebyshev/L-infinity distance over any supported representation.

Chebyshev/L-infinity distance with per-call compute options.

Converts between list, f32-binary, and optional Nx representations.

Cosine similarity, with the same normalization options as Vettore.Distance.cosine/3.

Returns the flattened coordinate count.

Compatibility alias for inner_product/2.

Converts an Nx tensor into a validated Vettore representation.

Hamming distance using Vettore's non-zero coordinate semantics.

Hamming distance with per-call compute options.

Inner/dot product over any supported representation.

Inner/dot product with per-call compute options.

Jaccard distance using Vettore's non-zero coordinate semantics.

Jaccard distance with per-call compute options.

L2/Euclidean distance over any supported representation.

L2/Euclidean distance with per-call compute options.

Squared L2 distance over any supported representation.

Squared L2 distance with per-call compute options.

Manhattan/L1 distance over any supported representation.

Manhattan/L1 distance with per-call compute options.

Returns the row/column shape of a row-major little-endian f32 matrix.

Mean-pools a non-empty list of equally sized vectors.

Mean-pools selected rows from a row-major little-endian f32 matrix.

Computes one named metric over any pair of supported representations.

Negative inner product over any supported representation.

Negative inner product with per-call compute options.

Builds a dimensioned vector wrapper in the requested representation.

Normalizes a vector while allowing the result representation to be selected.

Returns the storage representation used by a supported vector value.

Returns a wrapper with new shape metadata, without changing coordinate order.

Returns the original tensor shape, or a one-dimensional shape for flat values.

Stacks equally sized vectors into a row-major matrix representation.

Selects rows from a row-major f32 matrix without mean pooling them.

Converts any supported vector to a validated little-endian f32 binary.

Converts any supported vector to a validated flat float list.

Converts a vector to an Nx tensor, preserving shape and accepting a host backend.

Checks the vector representation, finite f32 values, and wrapper metadata.

Returns whether a complete row-major f32 matrix is structurally valid and finite.

Validates a complete f32 matrix and returns its shape.

Types

metric()

@type metric() :: Vettore.Distance.metric()

normalization()

@type normalization() :: :none | :l2 | :zscore | :minmax

raw_vector()

@type raw_vector() :: [number()] | binary() | term()

representation()

@type representation() :: :list | :f32_binary | :nx

t()

@type t() :: %Vettore.Vector{
  data: raw_vector(),
  dimensions: non_neg_integer(),
  representation: representation(),
  shape: tuple() | nil
}

target_representation()

@type target_representation() :: representation() | :same

vector()

@type vector() :: raw_vector() | t()

Functions

chebyshev(left, right)

@spec chebyshev(vector(), vector()) :: {:ok, float()} | {:error, term()}

Chebyshev/L-infinity distance over any supported representation.

chebyshev(left, right, opts)

@spec chebyshev(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Chebyshev/L-infinity distance with per-call compute options.

convert(vector, target)

@spec convert(vector(), target_representation()) ::
  {:ok, raw_vector()} | {:error, term()}

Converts between list, f32-binary, and optional Nx representations.

cosine(left, right, opts \\ [])

@spec cosine(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Cosine similarity, with the same normalization options as Vettore.Distance.cosine/3.

dimensions(vector)

@spec dimensions(vector()) :: {:ok, non_neg_integer()} | {:error, term()}

Returns the flattened coordinate count.

dot_product(left, right)

@spec dot_product(vector(), vector()) :: {:ok, float()} | {:error, term()}

Compatibility alias for inner_product/2.

dot_product(left, right, opts)

@spec dot_product(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Compatibility alias for inner_product/3.

from_nx(tensor, target \\ :f32_binary)

@spec from_nx(term(), :list | :f32_binary) :: {:ok, raw_vector()} | {:error, term()}

Converts an Nx tensor into a validated Vettore representation.

hamming(left, right)

@spec hamming(vector(), vector()) :: {:ok, float()} | {:error, term()}

Hamming distance using Vettore's non-zero coordinate semantics.

hamming(left, right, opts)

@spec hamming(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Hamming distance with per-call compute options.

inner_product(left, right)

@spec inner_product(vector(), vector()) :: {:ok, float()} | {:error, term()}

Inner/dot product over any supported representation.

inner_product(left, right, opts)

@spec inner_product(vector(), vector(), keyword()) ::
  {:ok, float()} | {:error, term()}

Inner/dot product with per-call compute options.

jaccard(left, right)

@spec jaccard(vector(), vector()) :: {:ok, float()} | {:error, term()}

Jaccard distance using Vettore's non-zero coordinate semantics.

jaccard(left, right, opts)

@spec jaccard(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Jaccard distance with per-call compute options.

l2(left, right)

@spec l2(vector(), vector()) :: {:ok, float()} | {:error, term()}

L2/Euclidean distance over any supported representation.

l2(left, right, opts)

@spec l2(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

L2/Euclidean distance with per-call compute options.

l2_squared(left, right)

@spec l2_squared(vector(), vector()) :: {:ok, float()} | {:error, term()}

Squared L2 distance over any supported representation.

l2_squared(left, right, opts)

@spec l2_squared(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Squared L2 distance with per-call compute options.

manhattan(left, right)

@spec manhattan(vector(), vector()) :: {:ok, float()} | {:error, term()}

Manhattan/L1 distance over any supported representation.

manhattan(left, right, opts)

@spec manhattan(vector(), vector(), keyword()) :: {:ok, float()} | {:error, term()}

Manhattan/L1 distance with per-call compute options.

matrix_shape_f32(matrix, dimensions)

@spec matrix_shape_f32(binary(), pos_integer()) ::
  {:ok, {non_neg_integer(), pos_integer()}} | {:error, term()}

Returns the row/column shape of a row-major little-endian f32 matrix.

mean_pool(vectors, opts \\ [])

@spec mean_pool(
  [vector()],
  keyword()
) :: {:ok, raw_vector()} | {:error, term()}

Mean-pools a non-empty list of equally sized vectors.

mean_pool_f32(matrix, dimensions, row_indices, opts \\ [])

@spec mean_pool_f32(binary(), pos_integer(), [non_neg_integer()], keyword()) ::
  {:ok, raw_vector()} | {:error, term()}

Mean-pools selected rows from a row-major little-endian f32 matrix.

The result defaults to :f32_binary; pass as: :list or as: :nx when a different interchange representation is needed. Repeated indices are counted repeatedly, matching token-sequence pooling semantics.

metric(left, right, metric, opts \\ [])

@spec metric(vector(), vector(), metric(), keyword()) ::
  {:ok, float()} | {:error, term()}

Computes one named metric over any pair of supported representations.

negative_inner_product(left, right)

@spec negative_inner_product(vector(), vector()) :: {:ok, float()} | {:error, term()}

Negative inner product over any supported representation.

negative_inner_product(left, right, opts)

@spec negative_inner_product(vector(), vector(), keyword()) ::
  {:ok, float()} | {:error, term()}

Negative inner product with per-call compute options.

new(vector, opts \\ [])

@spec new(
  vector(),
  keyword()
) :: {:ok, t()} | {:error, term()}

Builds a dimensioned vector wrapper in the requested representation.

normalize(vector, method \\ :l2, opts \\ [])

@spec normalize(vector(), normalization(), keyword()) ::
  {:ok, raw_vector()} | {:error, term()}

Normalizes a vector while allowing the result representation to be selected.

representation(vector)

@spec representation(term()) :: representation() | :unknown

Returns the storage representation used by a supported vector value.

reshape(vector, shape, opts \\ [])

@spec reshape(vector(), tuple(), keyword()) :: {:ok, t()} | {:error, term()}

Returns a wrapper with new shape metadata, without changing coordinate order.

shape(vector)

@spec shape(vector()) :: {:ok, tuple()} | {:error, term()}

Returns the original tensor shape, or a one-dimensional shape for flat values.

stack(vectors, opts \\ [])

@spec stack(
  [vector()],
  keyword()
) :: {:ok, term()} | {:error, term()}

Stacks equally sized vectors into a row-major matrix representation.

take_rows_f32(matrix, dimensions, row_indices, opts \\ [])

@spec take_rows_f32(binary(), pos_integer(), [non_neg_integer()], keyword()) ::
  {:ok, term()} | {:error, term()}

Selects rows from a row-major f32 matrix without mean pooling them.

to_f32_binary(vector)

@spec to_f32_binary(vector()) :: {:ok, binary()} | {:error, term()}

Converts any supported vector to a validated little-endian f32 binary.

to_list(vector)

@spec to_list(vector()) :: {:ok, [float()]} | {:error, term()}

Converts any supported vector to a validated flat float list.

to_nx(vector, opts \\ [])

@spec to_nx(
  vector(),
  keyword()
) :: {:ok, term()} | {:error, term()}

Converts a vector to an Nx tensor, preserving shape and accepting a host backend.

valid?(vector)

@spec valid?(term()) :: boolean()

Checks the vector representation, finite f32 values, and wrapper metadata.

valid_matrix_f32?(matrix, dimensions)

@spec valid_matrix_f32?(term(), term()) :: boolean()

Returns whether a complete row-major f32 matrix is structurally valid and finite.

validate_matrix_f32(matrix, dimensions)

@spec validate_matrix_f32(binary(), pos_integer()) ::
  {:ok, {non_neg_integer(), pos_integer()}} | {:error, term()}

Validates a complete f32 matrix and returns its shape.