Skip to content

Commit

Permalink
[GLE-8861] change euclidean to l2;
Browse files Browse the repository at this point in the history
  • Loading branch information
jue-yuan committed Dec 3, 2024
1 parent 7f33d68 commit 870042f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions gds/vector/distance.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CREATE FUNCTION gds.vector.distance(list<double> list1, list<double> list2, stri
string metric:
The distance metric to use. Supported metrics are:
"cosine": Cosine distance
"euclidean": Euclidean distance
"l2": Euclidean distance
"ip": Inner product (dot product)
Returns:
float:
Expand All @@ -41,7 +41,7 @@ CREATE FUNCTION gds.vector.distance(list<double> list1, list<double> list2, stri
Metric Handling:
Cosine Distance:
Calculated as 1 - (inner product of vectors) / (product of magnitudes).
Euclidean Distance:
L2 Distance:
Computes the square root of the sum of squared differences between corresponding elements.
Inner Product:
Directly computes the dot product of the two vectors.
Expand Down Expand Up @@ -69,15 +69,15 @@ CREATE FUNCTION gds.vector.distance(list<double> list1, list<double> list2, stri
CASE lower(metric)
WHEN "cosine" THEN
@@myResult = 1 - inner_product(@@myList1, @@myList2) / (sqrt(inner_product(@@myList1, @@myList1)) * sqrt(inner_product(@@myList2, @@myList2)));
WHEN "euclidean" THEN
WHEN "l2" THEN
FOREACH i IN [0, @@myList1.size() - 1 ] DO
@@sqrSum += (@@myList1.get(i) - @@myList2.get(i)) * (@@myList1.get(i) - @@myList2.get(i));
END;
@@myResult = sqrt(@@sqrSum);
WHEN "ip" THEN
@@myResult = inner_product(@@myList1, @@myList2);
ELSE
RAISE invalid_metric_type ("Invalid metric algorithm provided, currently supported: cosine, euclidean and ip.");
RAISE invalid_metric_type ("Invalid metric algorithm provided, currently supported: cosine, l2 and ip.");
END
;

Expand Down
2 changes: 1 addition & 1 deletion gds/vector/ip_distance.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CREATE FUNCTION gds.vector.ip_distance(list<double> list1, list<double> list2) R
ListAccum<double> @@myList2 = list2;

IF (@@myList1.size() != @@myList2.size()) THEN
RAISE list_size_mismatch ("Two lists provided for gds.vector.euclidean_distance have different sizes.");
RAISE list_size_mismatch ("Two lists provided for gds.vector.ip_distance have different sizes.");
END;

RETURN inner_product(@@myList1, @@myList2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE FUNCTION gds.vector.euclidean_distance(list<double> list1, list<double> list2) RETURNS(float) {
CREATE FUNCTION gds.vector.l2_distance(list<double> list1, list<double> list2) RETURNS(float) {

/*
First Author: Jue Yuan
Expand Down Expand Up @@ -50,7 +50,7 @@ CREATE FUNCTION gds.vector.euclidean_distance(list<double> list1, list<double> l
ListAccum<double> @@myList2 = list2;

IF (@@myList1.size() != @@myList2.size()) THEN
RAISE list_size_mismatch ("Two lists provided for gds.vector.euclidean_distance have different sizes.");
RAISE list_size_mismatch ("Two lists provided for gds.vector.l2_distance have different sizes.");
END;

SumAccum<float> @@sqrSum;
Expand Down
6 changes: 3 additions & 3 deletions gds/vector/norm.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CREATE FUNCTION gds.vector.norm(list<double> list1, string metric) RETURNS(float
The input vector as a list of double values.
string metric:
The norm metric to apply. Supported metrics are:
"euclidean": Euclidean norm (L2 norm).
"l2": Euclidean norm (L2 norm).
"ip": Inner product norm (dot product with the zero vector).

Returns:
Expand Down Expand Up @@ -63,15 +63,15 @@ CREATE FUNCTION gds.vector.norm(list<double> list1, string metric) RETURNS(float
SumAccum<float> @@sqrSum;

CASE lower(metric)
WHEN "euclidean" THEN
WHEN "l2" THEN
FOREACH i IN [0, @@myList1.size() - 1 ] DO
@@sqrSum += (@@myList1.get(i) - @@myList2.get(i)) * (@@myList1.get(i) - @@myList2.get(i));
END;
@@myResult = sqrt(@@sqrSum);
WHEN "ip" THEN
@@myResult = inner_product(@@myList1, @@myList2);
ELSE
RAISE invalid_metric_type ("Invalid metric algorithm provided, currently supported: euclidean and ip.");
RAISE invalid_metric_type ("Invalid metric algorithm provided, currently supported: l2 and ip.");
END
;

Expand Down

0 comments on commit 870042f

Please sign in to comment.