Skip to content

Commit 27e3f0b

Browse files
feat: add stats/strided/distances/dcosine-similarity
--- 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 ---
1 parent d57045f commit 27e3f0b

33 files changed

+4077
-0
lines changed
Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dcosine-similarity
22+
23+
> Computes the cosine similarity of two double-precision floating-point vectors.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dcosineSimilarity = require( '@stdlib/stats/strided/distances/dcosine-similarity' );
37+
```
38+
39+
#### dcosineSimilarity( N, x, strideX, y, strideY )
40+
41+
Calculates the cosine similarity of vectors `x` and `y`.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] );
47+
var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] );
48+
49+
var z = dcosineSimilarity( x.length, x, 1, y, 1 );
50+
// returns ~-0.061
51+
```
52+
53+
The function has the following parameters:
54+
55+
- **N**: number of indexed elements.
56+
- **x**: input [`Float64Array`][@stdlib/array/float64].
57+
- **strideX**: stride length of `x`.
58+
- **y**: input [`Float64Array`][@stdlib/array/float64].
59+
- **strideY**: stride length of `y`.
60+
61+
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,
62+
63+
```javascript
64+
var Float64Array = require( '@stdlib/array/float64' );
65+
66+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
67+
var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
68+
69+
var z = dcosineSimilarity( 3, x, 2, y, -1 );
70+
// returns ~0.878
71+
```
72+
73+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
74+
75+
<!-- eslint-disable stdlib/capitalized-comments -->
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
80+
// Initial arrays...
81+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
82+
var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
83+
84+
// Create offset views...
85+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
86+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
87+
88+
var z = dcosineSimilarity( 3, x1, 1, y1, 1 );
89+
// returns ~0.982
90+
```
91+
92+
#### dcosineSimilarity.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
93+
94+
Calculates the cosine similarity of `x` and `y` using alternative indexing semantics.
95+
96+
```javascript
97+
var Float64Array = require( '@stdlib/array/float64' );
98+
99+
var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] );
100+
var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] );
101+
102+
var z = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0 );
103+
// returns ~-0.061
104+
```
105+
106+
The function has the following additional parameters:
107+
108+
- **offsetX**: starting index for `x`.
109+
- **offsetY**: starting index for `y`.
110+
111+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the cosine similarity of every other value in `x` starting from the second value with the last 3 elements in `y` in reverse order
112+
113+
```javascript
114+
var Float64Array = require( '@stdlib/array/float64' );
115+
116+
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
117+
var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
118+
119+
var z = dcosineSimilarity.ndarray( 3, x, 2, 1, y, -1, y.length-1 );
120+
// returns ~0.895
121+
```
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<section class="notes">
128+
129+
## Notes
130+
131+
- If `N <= 0`, both functions return `0.0`.
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint no-undef: "error" -->
142+
143+
```javascript
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
145+
var dcosineSimilarity = require( '@stdlib/stats/strided/distances/dcosine-similarity' );
146+
147+
var opts = {
148+
'dtype': 'float64'
149+
};
150+
var x = discreteUniform( 10, 0, 100, opts );
151+
console.log( x );
152+
153+
var y = discreteUniform( x.length, 0, 10, opts );
154+
console.log( y );
155+
156+
var out = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, -1, y.length-1 );
157+
console.log( out );
158+
```
159+
160+
</section>
161+
162+
<!-- /.examples -->
163+
164+
<!-- C interface documentation. -->
165+
166+
* * *
167+
168+
<section class="c">
169+
170+
## C APIs
171+
172+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
173+
174+
<section class="intro">
175+
176+
</section>
177+
178+
<!-- /.intro -->
179+
180+
<!-- C usage documentation. -->
181+
182+
<section class="usage">
183+
184+
### Usage
185+
186+
```c
187+
#include "stdlib/stats/strided/distances/dcosine_similarity.h"
188+
```
189+
190+
#### stdlib_strided_dcosine_similarity( N, \*X, strideX, \*Y, strideY )
191+
192+
Computes the cosine similarity of two double-precision floating-point vectors.
193+
194+
```c
195+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
196+
const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 };
197+
198+
double v = stdlib_strided_dcosine_similarity( 5, x, 1, y, 1 );
199+
// returns ~-0.061
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **X**: `[in] double*` first input array.
206+
- **strideX**: `[in] CBLAS_INT` stride length of `X`.
207+
- **Y**: `[in] double*` second input array.
208+
- **strideY**: `[in] CBLAS_INT` stride length of `Y`.
209+
210+
```c
211+
double stdlib_strided_dcosine_similarity( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
212+
```
213+
214+
<!--lint ignore maximum-heading-length-->
215+
216+
#### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY )
217+
218+
<!--lint ignore maximum-heading-length-->
219+
220+
Computes the cosine similarity of two double-precision floating-point vectors using alternative indexing semantics.
221+
222+
```c
223+
const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 };
224+
const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 };
225+
226+
double v = stdlib_strided_dcosine_similarity_ndarray( 5, x, -1, 4, y, -1, 4 );
227+
// returns ~0.061
228+
```
229+
230+
The function accepts the following arguments:
231+
232+
- **N**: `[in] CBLAS_INT` number of indexed elements.
233+
- **X**: `[in] double*` first input array.
234+
- **strideX**: `[in] CBLAS_INT` stride length of `X`.
235+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
236+
- **Y**: `[in] double*` second input array.
237+
- **strideY**: `[in] CBLAS_INT` stride length of `Y`.
238+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
239+
240+
```c
241+
double stdlib_strided_dcosine_similarity_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
242+
```
243+
244+
</section>
245+
246+
<!-- /.usage -->
247+
248+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
249+
250+
<section class="notes">
251+
252+
</section>
253+
254+
<!-- /.notes -->
255+
256+
<!-- C API usage examples. -->
257+
258+
<section class="examples">
259+
260+
### Examples
261+
262+
```c
263+
#include "stdlib/stats/strided/distances/dcosine_similarity.h"
264+
#include <stdio.h>
265+
266+
int main( void ) {
267+
// Create strided arrays:
268+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
269+
const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
270+
271+
// Specify the number of elements:
272+
const int N = 8;
273+
274+
// Specify strides:
275+
const int strideX = 1;
276+
const int strideY = -1;
277+
278+
// Compute the cosine similarity of `x` and `y`:
279+
double sim = stdlib_strided_dcosine_similarity( N, x, strideX, y, strideY );
280+
281+
// Print the result:
282+
printf( "cosine similarity: %lf\n", sim );
283+
284+
// Compute the cosine similarity of `x` and `y` with offsets:
285+
sim = stdlib_strided_dcosine_similarity_ndarray( N, x, strideX, 0, y, strideY, N-1 );
286+
287+
// Print the result:
288+
printf( "cosine similarity: %lf\n", sim );
289+
}
290+
```
291+
292+
</section>
293+
294+
<!-- /.examples -->
295+
296+
</section>
297+
298+
<!-- /.c -->
299+
300+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
301+
302+
<section class="related">
303+
304+
</section>
305+
306+
<!-- /.related -->
307+
308+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
309+
310+
<section class="links">
311+
312+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
313+
314+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
315+
316+
<!-- <related-links> -->
317+
318+
<!-- </related-links> -->
319+
320+
</section>
321+
322+
<!-- /.links -->

0 commit comments

Comments
 (0)