-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add stats/strided/distances/dcosine-similarity
#9281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
feat: add stats/strided/distances/dcosine-similarity
#9281
Conversation
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown
status: passed
- task: lint_package_json
status: passed
- task: lint_repl_help
status: passed
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: passed
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: passed
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: passed
- task: lint_c_examples
status: passed
- task: lint_c_benchmarks
status: passed
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: passed
- task: lint_license_headers
status: passed
---
|
/stdlib update-copyright-years |
Coverage Report
The above coverage report was generated for the changes in this PR. |
|
|
||
| # dcosine-similarity | ||
|
|
||
| > Computes the cosine similarity of two double-precision floating-point vectors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| > Computes the cosine similarity of two double-precision floating-point vectors. | |
| > Compute the cosine similarity of two double-precision floating-point strided arrays. |
|
|
||
| --> | ||
|
|
||
| # dcosine-similarity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # dcosine-similarity | |
| # dcosineSimilarity |
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/README.md
Show resolved
Hide resolved
|
|
||
| #### dcosineSimilarity( N, x, strideX, y, strideY ) | ||
|
|
||
| Calculates the cosine similarity of vectors `x` and `y`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Calculates the cosine similarity of vectors `x` and `y`. | |
| Computes the cosine similarity of two double-precision floating-point strided arrays. |
| - **y**: input [`Float64Array`][@stdlib/array/float64]. | ||
| - **strideY**: stride length of `y`. | ||
|
|
||
| The `N` and strides parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the cosine similarity of every other value in `x` and the first `N` elements of `y` in reverse order, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| The `N` and strides parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the cosine similarity of every other value in `x` and the first `N` elements of `y` in reverse order, | |
| The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to calculate the cosine similarity of every other value in `x` and the first `N` elements of `y` in reverse order, |
|
|
||
| #### dcosineSimilarity.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) | ||
|
|
||
| Calculates the cosine similarity of `x` and `y` using alternative indexing semantics. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Calculates the cosine similarity of `x` and `y` using alternative indexing semantics. | |
| Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. |
|
|
||
| #### stdlib_strided_dcosine_similarity( N, \*X, strideX, \*Y, strideY ) | ||
|
|
||
| Computes the cosine similarity of two double-precision floating-point vectors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Computes the cosine similarity of two double-precision floating-point vectors. | |
| Computes the cosine similarity of two double-precision floating-point strided arrays. |
|
|
||
| <!--lint ignore maximum-heading-length--> | ||
|
|
||
| Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. | |
| Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. |
| <!--lint ignore maximum-heading-length--> | ||
|
|
||
| #### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY ) | ||
|
|
||
| <!--lint ignore maximum-heading-length--> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| <!--lint ignore maximum-heading-length--> | |
| #### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY ) | |
| <!--lint ignore maximum-heading-length--> | |
| <!--lint disable maximum-heading-length--> | |
| #### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY ) | |
| <!--lint enable maximum-heading-length--> |
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef STDLIB_STATS_STRIDED_DCOSINE_SIMILARITY_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| #ifndef STDLIB_STATS_STRIDED_DCOSINE_SIMILARITY_H | |
| #ifndef STDLIB_STATS_STRIDED_DISTANCES_DCOSINE_SIMILARITY_H |
This needs to match the package path. Applies here and below.
| #endif | ||
|
|
||
| /** | ||
| * Computes the cosine similarity of two double-precision floating-point vectors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * Computes the cosine similarity of two double-precision floating-point vectors. | |
| * Computes the cosine similarity of two double-precision floating-point strided arrays. |
| double API_SUFFIX( stdlib_strided_dcosine_similarity )( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY ); | ||
|
|
||
| /** | ||
| * Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. | |
| * Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. |
| // MAIN // | ||
|
|
||
| /** | ||
| * Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. | |
| * Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. |
This change applies here and everywhere else in this PR.
| /** | ||
| * Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics. | ||
| * | ||
| * @param {integer} N - number of indexed elements |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * @param {integer} N - number of indexed elements | |
| * @param {NonNegativeInteger} N - number of indexed elements |
This is more to signal that users should not be providing negative integers, as they are likely the result of an error in upstream computation.
| var den; | ||
|
|
||
| if (N <= 0) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| num = ddot( N, x, strideX, offsetX, y, strideY, offsetY ); | ||
| xnrm = dnrm2( N, x, strideX, offsetX ); | ||
| ynrm = dnrm2( N, y, strideY, offsetY ); | ||
| den = xnrm * ynrm; | ||
| return num / den; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var den; | |
| if (N <= 0) { | |
| return 0.0; | |
| } | |
| num = ddot( N, x, strideX, offsetX, y, strideY, offsetY ); | |
| xnrm = dnrm2( N, x, strideX, offsetX ); | |
| ynrm = dnrm2( N, y, strideY, offsetY ); | |
| den = xnrm * ynrm; | |
| return num / den; | |
| if ( N <= 0 ) { | |
| return 0.0; | |
| } | |
| dot = ddot( N, x, strideX, offsetX, y, strideY, offsetY ); | |
| xnrm = dnrm2( N, x, strideX, offsetX ); | |
| ynrm = dnrm2( N, y, strideY, offsetY ); | |
| return dot / ( xnrm * ynrm ); |
| function dcosineSimilarity( N, x, strideX, offsetX, y, strideY, offsetY ) { | ||
| var ynrm; | ||
| var xnrm; | ||
| var num; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| var num; | |
| var dot; |
| { | ||
| "name": "@stdlib/stats/strided/distances/dcosine-similarity", | ||
| "version": "0.0.0", | ||
| "description": "Computes the cosine similarity of two double-precision floating-point vectors.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| "description": "Computes the cosine similarity of two double-precision floating-point vectors.", | |
| "description": "Compute the cosine similarity of two double-precision floating-point strided arrays.", |
kgryte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some initial comments.
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown
status: passed
- task: lint_package_json
status: passed
- task: lint_repl_help
status: passed
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: passed
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: passed
- task: lint_license_headers
status: passed
---
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/README.md
Outdated
Show resolved
Hide resolved
| sim = \frac{A \cdot B}{\|A\| \, \|B\|} | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="sim=\frac{A\cdot B}{\|A\|\|B\|} data-equation="eq:cosine_similarity"> <img src="" alt="Cosine similarity equation."> <br> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This equation markup is not correct, but it looks like I will need to update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please point me to the correct markup? I did not really get you. :)
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
| > var out = {{alias}}( x.length, x, 1, y, 1 ) | ||
| ~-0.061 | ||
|
|
||
| // Strides: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example doesn't actually show anything different than the first example.
|
|
||
|
|
||
| {{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) | ||
| Computes the cosine similarity of two double-precision |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your line wrapping is off.
| > var out = {{alias}}.ndarray( x.length, x, 1, 0, y, 1, 0 ) | ||
| ~-0.061 | ||
|
|
||
| // Strides: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment. Doesn't show anything different.
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/lib/ndarray.js
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/main.c
Outdated
Show resolved
Hide resolved
| y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | ||
| t.strictEqual( sim, -0.11028735042288243, 'returns expected value' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nakul-krishnakumar How did you calculate these expected values? Did you compute via a reference implementation? Compute by hand? Something else? What we want to avoid is computing expected values via the implementation itself, in order to avoid circular testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the below reference implementation:
function cosinesim(A, B) {
var dot = 0;
var mA = 0;
var mB = 0;
for(var i = 0; i < A.length; i++) {
dot += A[i] * B[i];
mA += A[i] * A[i];
mB += B[i] * B[i];
}
mA = Math.sqrt(mA);
mB = Math.sqrt(mB);
var sim = dot / (mA * mB);
return sim;
}
var array1 = [ 3.0, -4.0, 1.0 ];
var array2 = [ 1.0, -2.0, 3.0 ];
var p = cosinesim(array1, array2);
console.log(p);| t.end(); | ||
| }); | ||
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test can be removed, as it is not applicable based on the implementation.
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment.
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment.
| t.end(); | ||
| }); | ||
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment.
kgryte
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left another round of comments. This PR is getting there. Just a bit more clean-up needed.
Co-authored-by: Athan <[email protected]> Signed-off-by: Nakul Krishnakumar <[email protected]>
Co-authored-by: Athan <[email protected]> Signed-off-by: Nakul Krishnakumar <[email protected]>
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { | ||
| var expected; | ||
| var xnrm2; | ||
| var ynrm2; | ||
| var sim; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 100 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | ||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 240 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | ||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { | |
| var expected; | |
| var xnrm2; | |
| var ynrm2; | |
| var sim; | |
| var x; | |
| var y; | |
| var i; | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 100 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 240 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| t.end(); | |
| }); |
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { | ||
| var expected; | ||
| var xnrm2; | ||
| var ynrm2; | ||
| var sim; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 100 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | ||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 240 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | ||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| t.end(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { | |
| var expected; | |
| var xnrm2; | |
| var ynrm2; | |
| var sim; | |
| var x; | |
| var y; | |
| var i; | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 100 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 240 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, y, 1 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| t.end(); | |
| }); |
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { | ||
| var expected; | ||
| var xnrm2; | ||
| var ynrm2; | ||
| var sim; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 100 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | ||
|
|
||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 240 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | ||
|
|
||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
| t.end(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', function test( t ) { | |
| var expected; | |
| var xnrm2; | |
| var ynrm2; | |
| var sim; | |
| var x; | |
| var y; | |
| var i; | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 100 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 240 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| t.end(); | |
| }); |
|
|
||
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { | ||
| var expected; | ||
| var xnrm2; | ||
| var ynrm2; | ||
| var sim; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 100 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | ||
|
|
||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
|
|
||
| expected = 0.0; | ||
| xnrm2 = 0.0; | ||
| ynrm2 = 0.0; | ||
| x = new Float64Array( 240 ); | ||
| y = new Float64Array( x.length ); | ||
| for ( i = 0; i < x.length; i++ ) { | ||
| x[ i ] = i; | ||
| y[ i ] = x.length - i; | ||
| expected += x[ i ] * y[ i ]; | ||
| xnrm2 += x[ i ] * x[ i ]; | ||
| ynrm2 += y[ i ] * y[ i ]; | ||
| } | ||
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | ||
|
|
||
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | ||
|
|
||
| t.strictEqual( sim, expected, 'returns expected value' ); | ||
| t.end(); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| tape( 'if both strides are equal to `1`, the function efficiently calculates the cosine similarity', opts, function test( t ) { | |
| var expected; | |
| var xnrm2; | |
| var ynrm2; | |
| var sim; | |
| var x; | |
| var y; | |
| var i; | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 100 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| expected = 0.0; | |
| xnrm2 = 0.0; | |
| ynrm2 = 0.0; | |
| x = new Float64Array( 240 ); | |
| y = new Float64Array( x.length ); | |
| for ( i = 0; i < x.length; i++ ) { | |
| x[ i ] = i; | |
| y[ i ] = x.length - i; | |
| expected += x[ i ] * y[ i ]; | |
| xnrm2 += x[ i ] * x[ i ]; | |
| ynrm2 += y[ i ] * y[ i ]; | |
| } | |
| expected /= sqrt( xnrm2 ) * sqrt( ynrm2 ); | |
| sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); | |
| t.strictEqual( sim, expected, 'returns expected value' ); | |
| t.end(); | |
| }); |
Co-authored-by: Athan <[email protected]> Signed-off-by: Nakul Krishnakumar <[email protected]>
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt
Outdated
Show resolved
Hide resolved
Signed-off-by: Nakul Krishnakumar <[email protected]>
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes. report:
Resolves None.
Description
This pull request:
stats/strided/distances/dcosine-similarity.Related Issues
This pull request has the following related issues:
Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
None.
@stdlib-js/reviewers