Skip to content

Conversation

@nakul-krishnakumar
Copy link
Member


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 ---

Resolves None.

Description

What is the purpose of this pull request?

This pull request:

  • Adds stats/strided/distances/dcosine-similarity.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • None.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

None.


@stdlib-js/reviewers

---
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-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. Needs Review A pull request which needs code review. labels Dec 20, 2025
@nakul-krishnakumar
Copy link
Member Author

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label Dec 20, 2025
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label Dec 20, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Dec 20, 2025

Coverage Report

Package Statements Branches Functions Lines
stats/strided/distances/dcosine-similarity $\color{green}374/374$
$\color{green}+0.00%$
$\color{green}15/15$
$\color{green}+0.00%$
$\color{green}4/4$
$\color{green}+0.00%$
$\color{green}374/374$
$\color{green}+0.00%$

The above coverage report was generated for the changes in this PR.

@kgryte kgryte added the Feature Issue or pull request for adding a new feature. label Dec 20, 2025

# dcosine-similarity

> Computes the cosine similarity of two double-precision floating-point vectors.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# dcosine-similarity
# dcosineSimilarity


#### dcosineSimilarity( N, x, strideX, y, strideY )

Calculates the cosine similarity of vectors `x` and `y`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 214 to 218
<!--lint ignore maximum-heading-length-->

#### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )

<!--lint ignore maximum-heading-length-->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!--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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

Comment on lines 54 to 64
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"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.",

Copy link
Member

@kgryte kgryte left a 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.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Dec 20, 2025
---
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
---
@kgryte kgryte removed Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review. labels Dec 20, 2025
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>
Copy link
Member

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.

Copy link
Member Author

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. :)

> var out = {{alias}}( x.length, x, 1, y, 1 )
~-0.061

// Strides:
Copy link
Member

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
Copy link
Member

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:
Copy link
Member

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.

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' );
Copy link
Member

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.

Copy link
Member Author

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 ) {
Copy link
Member

@kgryte kgryte Dec 20, 2025

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 ) {
Copy link
Member

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 ) {
Copy link
Member

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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

Copy link
Member

@kgryte kgryte left a 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.

@kgryte kgryte added the Needs Changes Pull request which needs changes before being merged. label Dec 20, 2025
nakul-krishnakumar and others added 2 commits December 21, 2025 12:26
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]>
Comment on lines +211 to +256

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();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();
});

Comment on lines +220 to +265

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();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();
});

Comment on lines +233 to +279

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();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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();
});

Comment on lines +242 to +288

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();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]>
Signed-off-by: Nakul Krishnakumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants