📘
Embedding Cosine Similarity
Compute the cosine similarity between two embedding vectors.
Cosine similarity is defined as: cos_sim(a,b)=∥a∥⋅∥b∥a⋅b
Input:
- Line 1: space-separated floats (vector a)
- Line 2: space-separated floats (vector b)
Output: Cosine similarity rounded to 4 decimal places.
Example:
Input:
1.0 0.0 0.0 0.0 1.0 0.0
Output:
0.0
Reasoning:
- First, we define the two embedding vectors: a=(1.0,0.0,0.0) and b=(0.0,1.0,0.0)
- Then, we calculate the dot product of a and b: a⋅b=(1.0)(0.0)+(0.0)(1.0)+(0.0)(0.0)=0.0
- Next, we calculate the magnitudes of a and b: ∥a∥=(1.0)2+(0.0)2+(0.0)2=1.0 and ∥b∥=(0.0)2+(1.0)2+(0.0)2=1.0
- The final output is the cosine similarity: cos_sim(a,b)=∥a∥⋅∥b∥a⋅b=1.0⋅1.00.0=0.0
Constraints:
- Vectors have the same dimension (1 <= D <= 100)
- Vectors are not zero vectors
- Output rounded to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.