diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/README.md b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/README.md new file mode 100644 index 000000000000..ec5817daf035 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/README.md @@ -0,0 +1,338 @@ + + +# dcosineSimilarity + +> Compute the cosine similarity of two double-precision floating-point strided arrays. + +
+ +The [cosine similarity][wikipedia-cosine-similarity] is defined as + + + +```math +sim = \frac{A \cdot B}{\|A\| \, \|B\|} +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var dcosineSimilarity = require( '@stdlib/stats/strided/distances/dcosine-similarity' ); +``` + +#### dcosineSimilarity( N, x, strideX, y, strideY ) + +Computes the cosine similarity of two double-precision floating-point strided arrays. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); +var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + +var z = dcosineSimilarity( x.length, x, 1, y, 1 ); +// returns ~-0.061 +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **x**: input [`Float64Array`][@stdlib/array/float64]. +- **strideX**: stride length of `x`. +- **y**: input [`Float64Array`][@stdlib/array/float64]. +- **strideY**: stride length of `y`. + +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, + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); + +var z = dcosineSimilarity( 3, x, 2, y, -1 ); +// returns ~0.878 +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Initial arrays... +var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y0 = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + +// Create offset views... +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element + +var z = dcosineSimilarity( 3, x1, 1, y1, 1 ); +// returns ~0.982 +``` + +#### dcosineSimilarity.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) + +Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); +var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + +var z = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0 ); +// returns ~-0.061 +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. +- **offsetY**: starting index for `y`. + +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 + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +var y = new Float64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + +var z = dcosineSimilarity.ndarray( 3, x, 2, 1, y, -1, y.length-1 ); +// returns ~0.895 +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions return `0.0`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var dcosineSimilarity = require( '@stdlib/stats/strided/distances/dcosine-similarity' ); + +var opts = { + 'dtype': 'float64' +}; +var x = discreteUniform( 10, 0, 100, opts ); +console.log( x ); + +var y = discreteUniform( x.length, 0, 10, opts ); +console.log( y ); + +var out = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); +console.log( out ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +``` + +#### stdlib_strided_dcosine_similarity( N, \*X, strideX, \*Y, strideY ) + +Computes the cosine similarity of two double-precision floating-point strided arrays. + +```c +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; + +double v = stdlib_strided_dcosine_similarity( 5, x, 1, y, 1 ); +// returns ~-0.061 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] double*` first input array. +- **strideX**: `[in] CBLAS_INT` stride length of `X`. +- **Y**: `[in] double*` second input array. +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. + +```c +double stdlib_strided_dcosine_similarity( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY ); +``` + + + +#### stdlib_strided_dcosine_similarity_ndarray( N, \*X, strideX, offsetX, \*Y, strideY, offsetY ) + + + +Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. + +```c +const double x[] = { 4.0, 2.0, -3.0, 5.0, -1.0 }; +const double y[] = { 2.0, 6.0, -1.0, -4.0, 8.0 }; + +double v = stdlib_strided_dcosine_similarity_ndarray( 5, x, -1, 4, y, -1, 4 ); +// returns ~0.061 +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] double*` first input array. +- **strideX**: `[in] CBLAS_INT` stride length of `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. +- **Y**: `[in] double*` second input array. +- **strideY**: `[in] CBLAS_INT` stride length of `Y`. +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. + +```c +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 ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +#include + +int main( void ) { + // Create strided arrays: + const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + + // Specify the number of elements: + const int N = 8; + + // Specify strides: + const int strideX = 1; + const int strideY = -1; + + // Compute the cosine similarity of `x` and `y`: + double sim = stdlib_strided_dcosine_similarity( N, x, strideX, y, strideY ); + + // Print the result: + printf( "cosine similarity: %lf\n", sim ); + + // Compute the cosine similarity of `x` and `y` with offsets: + sim = stdlib_strided_dcosine_similarity_ndarray( N, x, strideX, 0, y, strideY, N-1 ); + + // Print the result: + printf( "cosine similarity: %lf\n", sim ); +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.js new file mode 100644 index 000000000000..52eb393f23c7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dcosineSimilarity = require( './../lib/dcosine_similarity.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dcosineSimilarity( x.length, x, 1, y, 1 ); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.native.js new file mode 100644 index 000000000000..43e5b60d0046 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.native.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var dcosineSimilarity = tryRequire( resolve( __dirname, './../lib/dcosine_similarity.native.js' ) ); +var opts = { + 'skip': ( dcosineSimilarity instanceof Error ) +}; +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dcosineSimilarity( x.length, x, 1, y, 1 ); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s::native:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..514cba457482 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dcosineSimilarity = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:ndarray:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.native.js new file mode 100644 index 000000000000..28c9902246f5 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/benchmark.ndarray.native.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var tryRequire = require( '@stdlib/utils/try-require' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; + + +// VARIABLES // + +var dcosineSimilarity = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( dcosineSimilarity instanceof Error ) +}; +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -100.0, 100.0, options ); + var y = uniform( len, -100.0, 100.0, options ); + return benchmark; + + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s::native:ndarray:len=%d', pkg, len ), opts, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/Makefile b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/Makefile new file mode 100644 index 000000000000..cce2c865d7ad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.length.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/benchmark.length.c new file mode 100644 index 000000000000..16929f60c541 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/benchmark/c/benchmark.length.c @@ -0,0 +1,203 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +#include +#include +#include +#include +#include + +#define NAME "dcosine_similarity" +#define ITERATIONS 10000000 +#define REPEATS 3 +#define MIN 1 +#define MAX 6 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param iterations number of iterations +* @param elapsed elapsed time in seconds +*/ +static void print_results( int iterations, double elapsed ) { + double rate = (double)iterations / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", iterations ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark1( int iterations, int len ) { + double elapsed; + double *x; + double *y; + double z; + double t; + int i; + + x = (double *) malloc( len * sizeof( double ) ); + y = (double *) malloc( len * sizeof( double ) ); + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_double()*20000.0 ) - 10000.0; + y[ i ] = ( rand_double()*20000.0 ) - 10000.0; + } + z = 0.0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + z = stdlib_strided_dcosine_similarity( len, x, 1, y, 1 ); + if ( z != z ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( z != z ) { + printf( "should not return NaN\n" ); + } + free( x ); + free( y ); + return elapsed; +} + +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int len ) { + double elapsed; + double *x; + double *y; + double z; + double t; + int i; + + x = (double *) malloc( len * sizeof( double ) ); + y = (double *) malloc( len * sizeof( double ) ); + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_double()*20000.0 ) - 10000.0; + y[ i ] = ( rand_double()*20000.0 ) - 10000.0; + } + z = 0.0; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + z = stdlib_strided_dcosine_similarity_ndarray( len, x, 1, 0, y, 1, 0 ); + if ( z != z ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( z != z ) { + printf( "should not return NaN\n" ); + } + free( x ); + free( y ); + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int iter; + int len; + int i; + int j; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + count = 0; + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:len=%d\n", NAME, len ); + elapsed = benchmark1( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:ndarray:len=%d\n", NAME, len ); + elapsed = benchmark2( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/binding.gyp b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/binding.gyp new file mode 100644 index 000000000000..08de71a2020e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/binding.gyp @@ -0,0 +1,265 @@ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Fortran compiler (to override -Dfortran_compiler=): + 'fortran_compiler%': 'gfortran', + + # Fortran compiler flags: + 'fflags': [ + # Specify the Fortran standard to which a program is expected to conform: + '-std=f95', + + # Indicate that the layout is free-form source code: + '-ffree-form', + + # Aggressive optimization: + '-O3', + + # Enable commonly used warning options: + '-Wall', + + # Warn if source code contains problematic language features: + '-Wextra', + + # Warn if a procedure is called without an explicit interface: + '-Wimplicit-interface', + + # Do not transform names of entities specified in Fortran source files by appending underscores (i.e., don't mangle names, thus allowing easier usage in C wrappers): + '-fno-underscoring', + + # Warn if source code contains Fortran 95 extensions and C-language constructs: + '-pedantic', + + # Compile but do not link (output is an object file): + '-c', + ], + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + + # Define custom build actions for particular inputs: + 'rules': [ + { + # Define a rule for processing Fortran files: + 'extension': 'f', + + # Define the pathnames to be used as inputs when performing processing: + 'inputs': [ + # Full path of the current input: + '<(RULE_INPUT_PATH)' + ], + + # Define the outputs produced during processing: + 'outputs': [ + # Store an output object file in a directory for placing intermediate results (only accessible within a single target): + '<(INTERMEDIATE_DIR)/<(RULE_INPUT_ROOT).<(obj)' + ], + + # Define the rule for compiling Fortran based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + + # Rule to compile Fortran on Windows: + { + 'rule_name': 'compile_fortran_windows', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Windows...', + + 'process_outputs_as_sources': 0, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + }, + + # Rule to compile Fortran on non-Windows: + { + 'rule_name': 'compile_fortran_linux', + 'message': 'Compiling Fortran file <(RULE_INPUT_PATH) on Linux...', + + 'process_outputs_as_sources': 1, + + # Define the command-line invocation: + 'action': [ + '<(fortran_compiler)', + '<@(fflags)', + '-fPIC', # generate platform-independent code + '<@(_inputs)', + '-o', + '<@(_outputs)', + ], + } + ], # end condition (OS=="win") + ], # end conditions + }, # end rule (extension=="f") + ], # end rules + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt new file mode 100644 index 000000000000..8f40db2406ad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/repl.txt @@ -0,0 +1,116 @@ + +{{alias}}( N, x, strideX, y, strideY ) + Computes the cosine similarity of two double-precision floating-point + strided arrays. + + The `N` and stride parameters determine which elements in the strided arrays + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use a typed + array view. + + If `N <= 0`, the function returns `0`. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Float64Array + First input array. + + strideX: integer + Index increment for `x`. + + y: Float64Array + Second input array. + + strideY: integer + Index increment for `y`. + + Returns + ------- + out: number + Cosine similarity. + + Examples + -------- + // Standard usage: + > var x = new {{alias:@stdlib/array/float64}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); + > var y = new {{alias:@stdlib/array/float64}}( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + > var out = {{alias}}( x.length, x, 1, y, 1 ) + ~-0.061 + + // Strides: + > x = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); + > var y1 = new Float64Array( y.buffer, y.BYTES_PER_ELEMENT*2 ); + > out = {{alias}}( 3, x, 1, y, -1 ) + ~0.926 + + // Using view offsets: + > x = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float64}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x.buffer, x.BYTES_PER_ELEMENT*1 ); + > var y1 = new {{alias:@stdlib/array/float64}}( y.buffer, y.BYTES_PER_ELEMENT*3 ); + > out = {{alias}}( 3, x1, 1, y1, 1 ) + ~0.982 + + +{{alias}}.ndarray( N, x, strideX, offsetX, y, strideY, offsetY ) + Computes the cosine similarity of two double-precision floating-point + strided arrays using alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameters support indexing based on a starting index. + + Parameters + ---------- + N: integer + Number of indexed elements. + + x: Float64Array + First input array. + + strideX: integer + Index increment for `x`. + + offsetX: integer + Starting index for `x`. + + y: Float64Array + Second input array. + + strideY: integer + Index increment for `y`. + + offsetY: integer + Starting index for `y`. + + Returns + ------- + out: number + Cosine similarity. + + Examples + -------- + // Standard usage: + > var x = new {{alias:@stdlib/array/float64}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); + > var y = new {{alias:@stdlib/array/float64}}( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + > var out = {{alias}}.ndarray( x.length, x, 1, 0, y, 1, 0 ) + ~-0.061 + + // Strides: + > x = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float64}}( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); + > out = {{alias}}.ndarray( 3, x, 1, 0, y, -1, 2 ) + ~0.926 + + // Using offset indices: + > x = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + > y = new {{alias:@stdlib/array/float64}}( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + > out = {{alias}}.ndarray( 3, x, 1, 1, y, 1, 3 ) + ~0.982 + + See Also + -------- diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/index.d.ts new file mode 100644 index 000000000000..f180faecd267 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/index.d.ts @@ -0,0 +1,103 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/** +* Interface describing `dcosineSimilarity`. +*/ +interface Routine { + /** + * Computes the cosine similarity of two double-precision floating-point strided arrays. + * + * @param N - number of indexed elements + * @param x - first input array + * @param strideX - `x` stride length + * @param y - second input array + * @param strideY - `y` stride length + * @returns cosine similarity + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * + * var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); + * var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + * + * var z = dcosineSimilarity( x.length, x, 1, y, 1 ); + * // returns ~-0.061 + */ + ( N: number, x: Float64Array, strideX: number, y: Float64Array, strideY: number ): number; + + /** + * Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. + * + * @param N - number of indexed elements + * @param x - first input array + * @param strideX - `x` stride length + * @param offsetX - starting index for `x` + * @param y - second input array + * @param strideY - `y` stride length + * @param offsetY - starting index for `y` + * @returns cosine similarity + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * + * var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); + * var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); + * + * var z = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0 ); + * // returns ~-0.061 + */ + ndarray( N: number, x: Float64Array, strideX: number, offsetX: number, y: Float64Array, strideY: number, offsetY: number ): number; +} + +/** +* Computes the cosine similarity of two double-precision floating-point strided arrays. +* +* @param N - number of indexed elements +* @param x - first input array +* @param strideX - `x` stride length +* @param y - second input array +* @param strideY - `y` stride length +* @returns cosine similarity +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); +* var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); +* +* var z = dcosineSimilarity( x.length, x, 1, y, 1 ); +* // returns ~-0.061 +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); +* var y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ); +* +* var z = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0 ); +* // returns ~-0.061 +*/ +declare var dcosineSimilarity: Routine; + + +// EXPORTS // + +export = dcosineSimilarity; diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/test.ts b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/test.ts new file mode 100644 index 000000000000..6f931dd9efec --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/docs/types/test.ts @@ -0,0 +1,248 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import dcosineSimilarity = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity( x.length, x, 1, y, 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity( '10', x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( true, x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( false, x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( null, x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( undefined, x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( [], x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( {}, x, 1, y, 1 ); // $ExpectError + dcosineSimilarity( ( x: number ): number => x, x, 1, y, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a Float64Array... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity( x.length, 10, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, '10', 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, true, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, false, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, null, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, undefined, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, [], 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, {}, 1, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, ( x: number ): number => x, 1, y, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity( x.length, x, '10', y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, true, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, false, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, null, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, undefined, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, [], y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, {}, y, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, ( x: number ): number => x, y, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a Float64Array... +{ + const x = new Float64Array( 10 ); + + dcosineSimilarity( x.length, x, 1, 10, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, '10', 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, true, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, false, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, null, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, undefined, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, [], 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, {}, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity( x.length, x, 1, y, '10' ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, true ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, false ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, null ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, undefined ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, [] ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, {} ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity(); // $ExpectError + dcosineSimilarity( x.length ); // $ExpectError + dcosineSimilarity( x.length, x ); // $ExpectError + dcosineSimilarity( x.length, x, 1 ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y ); // $ExpectError + dcosineSimilarity( x.length, x, 1, y, 1, 10 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0 ); // $ExpectType number +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( '10', x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( true, x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( false, x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( null, x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( undefined, x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( [], x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( {}, x, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( ( x: number ): number => x, x, 1, 0, y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a Float64Array... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, 10, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, '10', 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, true, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, false, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, null, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, undefined, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, [], 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, {}, 1, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, ( x: number ): number => x, 1, 0, y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, '10', 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, true, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, false, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, null, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, undefined, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, [], 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, {}, 0, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, ( x: number ): number => x, 0, y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, 1, '10', y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, true, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, false, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, null, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, undefined, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, [], y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, {}, y, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, ( x: number ): number => x, y, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a Float64Array... +{ + const x = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, 1, 0, 10, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, '10', 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, true, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, false, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, null, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, undefined, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, [], 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, {}, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, '10', 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, true, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, false, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, null, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, undefined, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, [], 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, {}, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, '10' ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, true ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, false ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, null ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, undefined ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, [] ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, {} ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + const y = new Float64Array( 10 ); + + dcosineSimilarity.ndarray(); // $ExpectError + dcosineSimilarity.ndarray( x.length ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1 ); // $ExpectError + dcosineSimilarity.ndarray( x.length, x, 1, 0, y, 1, 0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/Makefile b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/example.c b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/example.c new file mode 100644 index 000000000000..4fb46668e825 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/c/example.c @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +#include + +int main( void ) { + // Create strided arrays: + const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 }; + + // Specify the number of elements: + const int N = 8; + + // Specify strides: + const int strideX = 1; + const int strideY = -1; + + // Compute the cosine similarity of `x` and `y`: + double sim = stdlib_strided_dcosine_similarity( N, x, strideX, y, strideY ); + + // Print the result: + printf( "cosine similarity: %lf\n", sim ); + + // Compute the cosine similarity of `x` and `y` with offsets: + sim = stdlib_strided_dcosine_similarity_ndarray( N, x, strideX, 0, y, strideY, N-1 ); + + // Print the result: + printf( "cosine similarity: %lf\n", sim ); +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/index.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/index.js new file mode 100644 index 000000000000..14a056e79760 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/examples/index.js @@ -0,0 +1,34 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var dcosineSimilarity = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; +var x = discreteUniform( 10, 0, 100, opts ); +console.log( x ); + +var y = discreteUniform( x.length, 0, 10, opts ); +console.log( y ); + +var out = dcosineSimilarity.ndarray( x.length, x, 1, 0, y, -1, y.length-1 ); +console.log( out ); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/include.gypi b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/include.gypi new file mode 100644 index 000000000000..4217944b5d20 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/include.gypi @@ -0,0 +1,70 @@ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A GYP include file for building a Node.js native add-on. +# +# Note that nesting variables is required due to how GYP processes a configuration. Any variables defined within a nested 'variables' section is defined in the outer scope. Thus, conditions in the outer variable scope are free to use these variables without running into "variable undefined" errors. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +# +# Variable nesting hacks: +# +# [3]: https://chromium.googlesource.com/external/skia/gyp/+/master/common_variables.gypi +# [4]: https://src.chromium.org/viewvc/chrome/trunk/src/build/common.gypi?revision=127004 +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + 'variables': { + # Host BLAS library (to override -Dblas=): + 'blas%': '', + + # Path to BLAS library (to override -Dblas_dir=): + 'blas_dir%': '', + }, # end variables + + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '<@(blas_dir)', + '=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "cosine", + "similarity", + "distance", + "metric", + "strided", + "strided array", + "typed", + "array", + "float64", + "double", + "float64array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/Makefile b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/Makefile new file mode 100644 index 000000000000..7733b6180cb4 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/addon.c b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/addon.c new file mode 100644 index 000000000000..c3a6b76fb761 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/addon.c @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include "stdlib/napi/argv_int64.h" +#include "stdlib/napi/argv_strided_float64array.h" +#include "stdlib/napi/create_double.h" +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 5 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 4 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Y, N, strideY, argv, 3 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX( stdlib_strided_dcosine_similarity )( N, X, strideX, Y, strideY ), v ); + return v; +} + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 7 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 ); + STDLIB_NAPI_ARGV_INT64( env, strideY, argv, 5 ); + STDLIB_NAPI_ARGV_INT64( env, offsetY, argv, 6 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, Y, N, strideY, argv, 4 ); + STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX( stdlib_strided_dcosine_similarity_ndarray )( N, X, strideX, offsetX, Y, strideY, offsetY ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/main.c b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/main.c new file mode 100644 index 000000000000..e5587b0d7471 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/src/main.c @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/strided/distances/dcosine_similarity.h" +#include "stdlib/blas/base/ddot.h" +#include "stdlib/blas/base/dnrm2.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" + +/** +* Computes the cosine similarity of two double-precision floating-point strided arrays. +* +* @param N number of indexed elements +* @param X first array +* @param strideX X stride length +* @param Y second array +* @param strideY Y stride length +* @return cosine similarity +*/ +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 ) { + CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + CBLAS_INT oy = stdlib_strided_stride2offset( N, strideY ); + return API_SUFFIX( stdlib_strided_dcosine_similarity_ndarray )( N, X, strideX, ox, Y, strideY, oy ); +} + +/** +* Computes the cosine similarity of two double-precision floating-point strided arrays using alternative indexing semantics. +* +* @param N number of indexed elements +* @param X first array +* @param strideX X stride length +* @param offsetX starting index for X +* @param Y second array +* @param strideY Y stride length +* @param offsetY starting index for Y +* @return cosine similarity +*/ +double API_SUFFIX( 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 ) { + double ynrm; + double xnrm; + double dot; + + if ( N <= 0 ) { + return 0.0; + } + + dot = c_ddot_ndarray( N, X, strideX, offsetX, Y, strideY, offsetY ); + xnrm = c_dnrm2_ndarray( N, X, strideX, offsetX ); + ynrm = c_dnrm2_ndarray( N, Y, strideY, offsetY ); + return dot / ( xnrm*ynrm ); +} diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.js new file mode 100644 index 000000000000..df51115eb14e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.js @@ -0,0 +1,256 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var dcosineSimilarity = require( './../lib/dcosine_similarity.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcosineSimilarity, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 5', function test( t ) { + t.strictEqual( dcosineSimilarity.length, 5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function calculates the cosine similarity of two strided arrays', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + 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' ); + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( x.length, x, 1, y, 1 ); + t.strictEqual( sim, 0.7337993857053429, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0`', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( 0, x, 1, y, 1 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + + sim = dcosineSimilarity( -4, x, 1, y, 1 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` stride', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, // 1 + -3.0, // 2 + 3.0, + -4.0, + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, y, 1 ); + t.strictEqual( sim, -0.1696208382576527, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` stride', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, // 1 + -5.0, // 2 + 7.0, + 6.0 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, + -3.0, // 1 + 3.0, + -4.0, // 2 + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 1, y, 2 ); + t.strictEqual( sim, 0.7737946312060225, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, -2, y, -1 ); + t.strictEqual( sim, 0.9277856399363485, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, y, -1 ); + t.strictEqual( sim, 0.8170052650185755, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var sim; + var x0; + var y0; + var x1; + var y1; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y0 = new Float64Array([ + 6.0, + 7.0, + 8.0, + 9.0, // 0 + 10.0, // 1 + 11.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); + + sim = dcosineSimilarity( 3, x1, 2, y1, 1 ); + t.strictEqual( sim, 0.953507690477341, '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, 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(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.native.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.native.js new file mode 100644 index 000000000000..852d47e045ff --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.dcosine_similarity.native.js @@ -0,0 +1,265 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var dcosineSimilarity = tryRequire( resolve( __dirname, './../lib/dcosine_similarity.native.js' ) ); +var opts = { + 'skip': ( dcosineSimilarity instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcosineSimilarity, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 5', opts, function test( t ) { + t.strictEqual( dcosineSimilarity.length, 5, 'returns expected value' ); + t.end(); +}); + +tape( 'the function calculates the cosine similarity of two strided arrays', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + 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' ); + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( x.length, x, 1, y, 1 ); + t.strictEqual( sim, 0.7337993857053429, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0`', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( 0, x, 1, y, 1 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + + sim = dcosineSimilarity( -4, x, 1, y, 1 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` stride', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, // 1 + -3.0, // 2 + 3.0, + -4.0, + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, y, 1 ); + t.strictEqual( sim, -0.1696208382576527, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` stride', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, // 1 + -5.0, // 2 + 7.0, + 6.0 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, + -3.0, // 1 + 3.0, + -4.0, // 2 + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 1, y, 2 ); + t.strictEqual( sim, 0.7737946312060225, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, -2, y, -1 ); + t.strictEqual( sim, 0.9277856399363485, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, y, -1 ); + t.strictEqual( sim, 0.8170052650185755, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', opts, function test( t ) { + var sim; + var x0; + var y0; + var x1; + var y1; + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y0 = new Float64Array([ + 6.0, + 7.0, + 8.0, + 9.0, // 0 + 10.0, // 1 + 11.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); + + sim = dcosineSimilarity( 3, x1, 2, y1, 1 ); + t.strictEqual( sim, 0.953507690477341, '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(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.js new file mode 100644 index 000000000000..54233e9eeec9 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var proxyquire = require( 'proxyquire' ); +var IS_BROWSER = require( '@stdlib/assert/is-browser' ); +var dcosineSimilarity = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': IS_BROWSER +}; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcosineSimilarity, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof dcosineSimilarity.ndarray, 'function', 'method is a function' ); + t.end(); +}); + +tape( 'if a native implementation is available, the main export is the native implementation', opts, function test( t ) { + var dcosineSimilarity = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dcosineSimilarity, mock, 'returns expected value' ); + t.end(); + + function tryRequire() { + return mock; + } + + function mock() { + // Mock... + } +}); + +tape( 'if a native implementation is not available, the main export is a JavaScript implementation', opts, function test( t ) { + var dcosineSimilarity; + var main; + + main = require( './../lib/dcosine_similarity.js' ); + + dcosineSimilarity = proxyquire( './../lib', { + '@stdlib/utils/try-require': tryRequire + }); + + t.strictEqual( dcosineSimilarity, main, 'returns expected value' ); + t.end(); + + function tryRequire() { + return new Error( 'Cannot find module' ); + } +}); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.js new file mode 100644 index 000000000000..a6b8fdab2b12 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.js @@ -0,0 +1,279 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var dcosineSimilarity = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcosineSimilarity, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', function test( t ) { + t.strictEqual( dcosineSimilarity.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function calculates the cosine similarity of two strided arrays', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + 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, 0, y, 1, 0 ); + t.strictEqual( sim, -0.11028735042288243, 'returns expected value' ); + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.7337993857053429, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0`', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( 0, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + + sim = dcosineSimilarity( -4, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` stride', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, // 1 + -3.0, // 2 + 3.0, + -4.0, + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, 1, 0 ); + t.strictEqual( sim, -0.1696208382576527, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` offset', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, // 2 + 9.0, + 10.0, + 11.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 1, y, 1, 0 ); + t.strictEqual( sim, 0.9633753381657649, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` stride', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, // 1 + -5.0, // 2 + 7.0, + 6.0 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, + -3.0, // 1 + 3.0, + -4.0, // 2 + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 1, 0, y, 2, 0 ); + t.strictEqual( sim, 0.7737946312060225, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` offset', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0, // 2 + 6.0 + ]); + y = new Float64Array([ + 6.0, + 7.0, + 8.0, + 9.0, // 0 + 10.0, // 1 + 11.0 // 2 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, 1, 3 ); + t.strictEqual( sim, 0.9143034529972666, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, -2, x.length-1, y, -1, 2 ); + t.strictEqual( sim, 0.9277856399363485, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, + 7.0, // 2 + 8.0, // 1 + 9.0, // 0 + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, -1, y.length-2 ); + t.strictEqual( sim, 0.8252281337630523, '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(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.native.js b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.native.js new file mode 100644 index 000000000000..482d1966b558 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/distances/dcosine-similarity/test/test.ndarray.native.js @@ -0,0 +1,288 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var resolve = require( 'path' ).resolve; +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var dcosineSimilarity = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); +var opts = { + 'skip': ( dcosineSimilarity instanceof Error ) +}; + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcosineSimilarity, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 7', opts, function test( t ) { + t.strictEqual( dcosineSimilarity.length, 7, 'returns expected value' ); + t.end(); +}); + +tape( 'the function calculates the cosine similarity of two strided arrays', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + 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, 0, y, 1, 0 ); + t.strictEqual( sim, -0.11028735042288243, 'returns expected value' ); + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( x.length, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.7337993857053429, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0`', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array( [ 3.0, -4.0, 1.0 ] ); + y = new Float64Array( [ 1.0, -2.0, 3.0 ] ); + + sim = dcosineSimilarity( 0, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + + sim = dcosineSimilarity( -4, x, 1, 0, y, 1, 0 ); + t.strictEqual( sim, 0.0, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` stride', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, // 1 + -3.0, // 2 + 3.0, + -4.0, + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, 1, 0 ); + t.strictEqual( sim, -0.1696208382576527, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `x` offset', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 0 + 7.0, // 1 + 8.0, // 2 + 9.0, + 10.0, + 11.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 1, y, 1, 0 ); + t.strictEqual( sim, 0.9633753381657649, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` stride', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 2.0, // 0 + -3.0, // 1 + -5.0, // 2 + 7.0, + 6.0 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, + -3.0, // 1 + 3.0, + -4.0, // 2 + 1.0 + ]); + + sim = dcosineSimilarity( 3, x, 1, 0, y, 2, 0 ); + t.strictEqual( sim, 0.7737946312060225, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a `y` offset', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0, // 2 + 6.0 + ]); + y = new Float64Array([ + 6.0, + 7.0, + 8.0, + 9.0, // 0 + 10.0, // 1 + 11.0 // 2 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, 1, 3 ); + t.strictEqual( sim, 0.9143034529972666, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0, // 0 + 9.0, + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, -2, x.length-1, y, -1, 2 ); + t.strictEqual( sim, 0.9277856399363485, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports complex access patterns', opts, function test( t ) { + var sim; + var x; + var y; + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, + 7.0, // 2 + 8.0, // 1 + 9.0, // 0 + 10.0 + ]); + + sim = dcosineSimilarity( 3, x, 2, 0, y, -1, y.length-2 ); + t.strictEqual( sim, 0.8252281337630523, '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(); +});