-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,162 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
export default function add(x, y) { return x + y }; | ||
import cosineSimilarity from './utils/cosineSimilarity'; | ||
|
||
const x = ''; | ||
|
||
console.log('x'); | ||
|
||
export default function add(x, y) { | ||
return typeof cosineSimilarity === 'function' ? x + y : 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Computes the cosine similarity between two numerical vectors. | ||
* | ||
* @param {number[]} a - The first input vector. | ||
* @param {number[]} b - The second input vector. | ||
* @returns {number} - The cosine similarity between vectors `a` and `b`. | ||
* Range: [-1, 1]. Returns NaN if both vectors are zero. | ||
* | ||
* @complexity | ||
* Time Complexity: O(n) | ||
* - The function processes each element of the vectors `a` and `b` exactly once in a single loop. | ||
* | ||
* Space Complexity: O(1) | ||
* - The function uses a constant amount of extra space regardless of the input size. | ||
*/ | ||
export default function cosineSimilarity(a: number[], b: number[]): number { | ||
// Initialize variables for dot product and squared norms | ||
let dotProduct = 0; // Sum of element-wise products of a and b | ||
let normASq = 0; // Sum of squares of elements in a | ||
let normBSq = 0; // Sum of squares of elements in b | ||
|
||
// Loop through the elements of both vectors | ||
for (let i = 0; i < a.length; i += 1) { | ||
// Update dot product with the product of corresponding elements | ||
dotProduct += a[i] * b[i]; | ||
// Update the squared norm for vector a | ||
normASq += a[i] * a[i]; | ||
// Update the squared norm for vector b | ||
normBSq += b[i] * b[i]; | ||
} | ||
|
||
// Calculate the Euclidean norms of both vectors | ||
const normA = Math.sqrt(normASq); // Magnitude of vector a | ||
const normB = Math.sqrt(normBSq); // Magnitude of vector b | ||
|
||
// Return cosine similarity: | ||
// If both norms are non-zero, calculate the similarity as: | ||
// dotProduct / (normA * normB). | ||
// If either norm is zero, this will return NaN. | ||
return dotProduct / (normA * normB); | ||
} |
Oops, something went wrong.