-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathip_distance.gsql
58 lines (45 loc) · 1.92 KB
/
ip_distance.gsql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
CREATE FUNCTION gds.vector.ip_distance(list<double> list1, list<double> list2) RETURNS(float) {
/*
First Author: Jue Yuan
First Commit Date: Nov 27, 2024
Recent Author: Jue Yuan
Recent Commit Date: Nov 27, 2024
Maturity:
alpha
Description:
Calculates the inner product (dot product) between two vectors represented as lists of double values.
The inner product is a key measure in linear algebra, indicating the magnitude of the projection of one vector onto another.
This function provides a similarity measure commonly used in machine learning and data analysis.
Parameters:
list<double> list1:
The first vector as a list of double values.
list<double> list2:
The second vector as a list of double values.
Returns:
float:
The inner product (dot product) of the two input vectors.
Exceptions:
list_size_mismatch (90000):
Raised when the input vectors are not of equal size.
Logic Overview:
Input Validation:
Ensures both vectors have the same length.
Inner Product Calculation:
Computes the sum of the element-wise products of the two vectors.
Formula:
Inner Product = (x1 x y1) + (x2 x y2) + ... + (xn x yn)
Where xi and yi are elements of list1 and list2, respectively.
Use Case:
This function is widely used in:
Calculating similarity in machine learning models (e.g., recommendation systems).
Performing vector projections in linear algebra.
Evaluating similarity between embeddings in natural language processing (NLP).
*/
EXCEPTION list_size_mismatch (90000);
ListAccum<double> @@myList1 = list1;
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.");
END;
RETURN inner_product(@@myList1, @@myList2);
}