Oihana PHP Arango

cosSimilarity.php

Table of Contents

Functions

cosSimilarity()  : string
Return the cosine similarity between two vectors.

Functions

cosSimilarity()

Return the cosine similarity between two vectors.

cosSimilarity(string|int $x, string|int $y) : string

This helper wraps the ArangoDB AQL function COSINE_SIMILARITY(x, y) which calculates the cosine similarity between two vectors. Cosine similarity measures the cosine of the angle between two vectors, ranging from -1 to 1.

Example AQL usage:

COSINE_SIMILARITY([1, 0], [1, 0])     // returns 1 (identical vectors)
COSINE_SIMILARITY([1, 0], [0, 1])     // returns 0 (orthogonal vectors)
COSINE_SIMILARITY([1, 0], [-1, 0])    // returns -1 (opposite vectors)
Parameters
$x : string|int

First input array (vector).

$y : string|int

Second input array (vector).

Tags
example
use function oihana\arango\db\functions\numerics\cosSimilarity;

$expr = cosSimilarity('[1, 0]', '[0, 1]');
// Produces: 'COSINE_SIMILARITY([1, 0], [0, 1])'
see
https://docs.arangodb.com/stable/aql/functions/numeric/#cosine_similarity
since
1.0.0
author

Marc Alcaraz

Return values
string

The formatted AQL expression.

On this page

Search results