From 3b1d40a126f388c63a681d463f0c2a29ad594292 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 30 Aug 2026 15:19:23 +0500 Subject: [PATCH 1/2] feat: add blas/ext/base/ndarray/dcartesian-product --- .../base/ndarray/dcartesian-product/README.md | 130 ++++++++ .../dcartesian-product/benchmark/benchmark.js | 110 +++++++ .../ndarray/dcartesian-product/docs/repl.txt | 41 +++ .../dcartesian-product/docs/types/index.d.ts | 60 ++++ .../dcartesian-product/docs/types/test.ts | 69 +++++ .../dcartesian-product/examples/index.js | 39 +++ .../ndarray/dcartesian-product/lib/index.js | 51 ++++ .../ndarray/dcartesian-product/lib/main.js | 82 +++++ .../ndarray/dcartesian-product/package.json | 69 +++++ .../ndarray/dcartesian-product/test/test.js | 285 ++++++++++++++++++ 10 files changed, 936 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/README.md new file mode 100644 index 000000000000..f6a6f9b254df --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/README.md @@ -0,0 +1,130 @@ + + +# dcartesianProduct + +> Compute the Cartesian product for two double-precision floating-point ndarrays. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dcartesianProduct = require( '@stdlib/blas/ext/base/ndarray/dcartesian-product' ); +``` + +#### dcartesianProduct( arrays ) + +Computes the Cartesian product for two double-precision floating-point ndarrays. + +```javascript +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = new Float64Vector( [ 1.0, 2.0 ] ); +var y = new Float64Vector( [ 3.0, 4.0 ] ); +var out = zeros( [ 4, 2 ], { + 'dtype': 'float64' +}); + +var v = dcartesianProduct( [ x, y, out ] ); +// returns [ [ 1.0, 3.0 ], [ 1.0, 4.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ] ] + +var bool = ( v === out ); +// returns true +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional input ndarray. + - a two-dimensional output ndarray. + +
+ + + +
+ +## Notes + +- Pairs are stored as rows in the output matrix, where the first column contains the first element of each pair and the second column contains the second element. +- For input arrays `x` of length `M` and `y` of length `N`, the output array should have shape `[M*N, 2]`, where `M*N` is the number of rows and `2` is the number of columns. +- If `M <= 0` or `N <= 0`, the function returns the output ndarray unchanged. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcartesianProduct = require( '@stdlib/blas/ext/base/ndarray/dcartesian-product' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = discreteUniform( [ 3 ], -50, 50, opts ); +console.log( ndarray2array( x ) ); + +var y = discreteUniform( [ 2 ], -50, 50, opts ); +console.log( ndarray2array( y ) ); + +var out = zeros( [ 6, 2 ], opts ); + +var v = dcartesianProduct( [ x, y, out ] ); +console.log( ndarray2array( v ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js new file mode 100644 index 000000000000..f006f3012e19 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +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 dcartesianProduct = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var x; + var y; + + x = uniform( [ len ], -100.0, 100.0, { + 'dtype': 'float64' + }); + y = uniform( [ len ], -100.0, 100.0, { + 'dtype': 'float64' + }); + out = zeros( [ len*len, 2 ], { + 'dtype': 'float64' + }); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dcartesianProduct( [ x, y, out ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( v.get( i%(len*len), i%2 ) ) ) { + 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 = 3; // 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/blas/ext/base/ndarray/dcartesian-product/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/repl.txt new file mode 100644 index 000000000000..6845d815979c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/repl.txt @@ -0,0 +1,41 @@ + +{{alias}}( arrays ) + Computes the Cartesian product for two double-precision floating-point + ndarrays. + + Pairs are stored as rows in the output matrix, where the first column + contains the first element of each pair and the second column contains the + second element. + + For input arrays `x` of length `M` and `y` of length `N`, the output array + should have shape [M*N, 2], where `M*N` is the number of rows and `2` is + the number of columns. + + If `M <= 0` or `N <= 0`, the function returns the output ndarray unchanged. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional input ndarray. + - a two-dimensional output ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0 ] ); + > var y = new {{alias:@stdlib/ndarray/vector/float64}}( [ 3.0, 4.0 ] ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [ 4, 2 ], { 'dtype': 'float64' } ); + > {{alias}}( [ x, y, out ] ); + > out + [ [ 1.0, 3.0 ], [ 1.0, 4.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/index.d.ts new file mode 100644 index 000000000000..684c6a4f7b53 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the Cartesian product for two double-precision floating-point ndarrays. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional input ndarray. +* - a two-dimensional output ndarray. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = new Float64Vector( [ 1.0, 2.0 ] ); +* var y = new Float64Vector( [ 3.0, 4.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float64' +* }); +* +* var v = dcartesianProduct( [ x, y, out ] ); +* // returns [ [ 1.0, 3.0 ], [ 1.0, 4.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +declare function dcartesianProduct( arrays: [ float64ndarray, float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dcartesianProduct; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/test.ts new file mode 100644 index 000000000000..4d46e44a1e9a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dcartesianProduct = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 4, 2 ], { + 'dtype': 'float64' + }); + + dcartesianProduct( [ x, y, out ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dcartesianProduct( '10' ); // $ExpectError + dcartesianProduct( 10 ); // $ExpectError + dcartesianProduct( true ); // $ExpectError + dcartesianProduct( false ); // $ExpectError + dcartesianProduct( null ); // $ExpectError + dcartesianProduct( undefined ); // $ExpectError + dcartesianProduct( [] ); // $ExpectError + dcartesianProduct( {} ); // $ExpectError + dcartesianProduct( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 2 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 4, 2 ], { + 'dtype': 'float64' + }); + + dcartesianProduct(); // $ExpectError + dcartesianProduct( [ x, y, out ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/examples/index.js new file mode 100644 index 000000000000..a117bcd1a9e0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/discrete-uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dcartesianProduct = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = discreteUniform( [ 3 ], -50, 50, opts ); +console.log( ndarray2array( x ) ); + +var y = discreteUniform( [ 2 ], -50, 50, opts ); +console.log( ndarray2array( y ) ); + +var out = zeros( [ 6, 2 ], opts ); + +var v = dcartesianProduct( [ x, y, out ] ); +console.log( ndarray2array( v ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/index.js new file mode 100644 index 000000000000..4fff449b46be --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Compute the Cartesian product for two double-precision floating-point ndarrays. +* +* @module @stdlib/blas/ext/base/ndarray/dcartesian-product +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var dcartesianProduct = require( '@stdlib/blas/ext/base/ndarray/dcartesian-product' ); +* +* var x = new Float64Vector( [ 1.0, 2.0 ] ); +* var y = new Float64Vector( [ 3.0, 4.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float64' +* }); +* +* var v = dcartesianProduct( [ x, y, out ] ); +* // returns [ [ 1.0, 3.0 ], [ 1.0, 4.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/main.js new file mode 100644 index 000000000000..1b3050b324c0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/dcartesian-product' ).ndarray; + + +// MAIN // + +/** +* Computes the Cartesian product for two double-precision floating-point ndarrays. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional input ndarray. +* - a two-dimensional output ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {Object} output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = new Float64Vector( [ 1.0, 2.0 ] ); +* var y = new Float64Vector( [ 3.0, 4.0 ] ); +* var out = zeros( [ 4, 2 ], { +* 'dtype': 'float64' +* }); +* +* var v = dcartesianProduct( [ x, y, out ] ); +* // returns [ [ 1.0, 3.0 ], [ 1.0, 4.0 ], [ 2.0, 3.0 ], [ 2.0, 4.0 ] ] +* +* var bool = ( v === out ); +* // returns true +*/ +function dcartesianProduct( arrays ) { + var out; + var so; + var x; + var y; + + x = arrays[ 0 ]; + y = arrays[ 1 ]; + out = arrays[ 2 ]; + + so = getStrides( out, false ); + + strided( numelDimension( x, 0 ), numelDimension( y, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ), getData( out ), so[ 0 ], so[ 1 ], getOffset( out ) ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = dcartesianProduct; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/package.json new file mode 100644 index 000000000000..2659392304cd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/dcartesian-product", + "version": "0.0.0", + "description": "Compute the Cartesian product for two double-precision floating-point ndarrays.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "blas", + "extended", + "ext", + "linear", + "algebra", + "cartesian", + "product", + "pairs", + "tuples", + "array", + "ndarray", + "float64", + "double", + "float64array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/test/test.js new file mode 100644 index 000000000000..3073159e2c99 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/test/test.js @@ -0,0 +1,285 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var dcartesianProduct = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} N - number of elements +* @param {integer} stride - stride +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, N, stride, offset ) { + return new ndarray( 'float64', buffer, [ N ], [ stride ], offset, 'row-major' ); +} + +/** +* Returns a two-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} M - number of rows +* @param {NonNegativeInteger} N - number of columns +* @param {integer} stride0 - stride of the first dimension +* @param {integer} stride1 - stride of the second dimension +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} two-dimensional ndarray +*/ +function matrix( buffer, M, N, stride0, stride1, offset ) { + return new ndarray( 'float64', buffer, [ M, N ], [ stride0, stride1 ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcartesianProduct, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dcartesianProduct.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function computes the Cartesian product (row-major)', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 2, 1, 0 ); + out = matrix( new Float64Array( 8 ), 4, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 1.0, 3.0, 1.0, 4.0, 2.0, 3.0, 2.0, 4.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian product (row-major, M=3, N=2)', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 2.0, 3.0 ] ), 3, 1, 0 ); + y = vector( new Float64Array( [ 4.0, 5.0 ] ), 2, 1, 0 ); + out = matrix( new Float64Array( 12 ), 6, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array([ + 1.0, + 4.0, + 1.0, + 5.0, + 2.0, + 4.0, + 2.0, + 5.0, + 3.0, + 4.0, + 3.0, + 5.0 + ]); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian product (row-major, M=1, N=1)', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 5.0 ] ), 1, 1, 0 ); + y = vector( new Float64Array( [ 7.0 ] ), 1, 1, 0 ); + out = matrix( new Float64Array( 2 ), 1, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 5.0, 7.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the Cartesian product (column-major)', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 2, 1, 0 ); + out = matrix( new Float64Array( 8 ), 4, 2, 1, 4, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 4.0, 3.0, 4.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a reference to the output ndarray', function test( t ) { + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 2, 1, 0 ); + out = matrix( new Float64Array( 8 ), 4, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided `M` or `N` less than or equal to `0`, the function returns the output ndarray unchanged', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + out = matrix( new Float64Array( [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] ), 4, 2, 2, 1, 0 ); + expected = new Float64Array( [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 0, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 0, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports input ndarrays with strides', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 0.0, 2.0, 0.0 ] ), 2, 2, 0 ); + y = vector( new Float64Array( [ 3.0, 0.0, 4.0, 0.0 ] ), 2, 2, 0 ); + out = matrix( new Float64Array( 8 ), 4, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 1.0, 3.0, 1.0, 4.0, 2.0, 3.0, 2.0, 4.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports input ndarrays with negative strides', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 2.0, 1.0 ] ), 2, -1, 1 ); + y = vector( new Float64Array( [ 4.0, 3.0 ] ), 2, -1, 1 ); + out = matrix( new Float64Array( 8 ), 4, 2, 2, 1, 0 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 1.0, 3.0, 1.0, 4.0, 2.0, 3.0, 2.0, 4.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays having non-zero offsets', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 99.0, 1.0, 2.0 ] ), 2, 1, 1 ); + y = vector( new Float64Array( [ 99.0, 3.0, 4.0 ] ), 2, 1, 1 ); + out = matrix( new Float64Array( 10 ), 4, 2, 2, 1, 2 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 0.0, 0.0, 1.0, 3.0, 1.0, 4.0, 2.0, 3.0, 2.0, 4.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports output ndarrays with negative strides', function test( t ) { + var expected; + var out; + var x; + var y; + var v; + + x = vector( new Float64Array( [ 1.0, 2.0 ] ), 2, 1, 0 ); + y = vector( new Float64Array( [ 3.0, 4.0 ] ), 2, 1, 0 ); + out = matrix( new Float64Array( 8 ), 4, 2, -2, 1, 6 ); + + v = dcartesianProduct( [ x, y, out ] ); + + expected = new Float64Array( [ 2.0, 4.0, 2.0, 3.0, 1.0, 4.0, 1.0, 3.0 ] ); + t.strictEqual( v, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); From 2b18608edcc64305708d6c4127d83bd4078dc4e5 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 30 Aug 2026 15:24:36 +0500 Subject: [PATCH 2/2] refactor: apply suggestions from code review --- .../dcartesian-product/benchmark/benchmark.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js index f006f3012e19..57cdddefd590 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dcartesian-product/benchmark/benchmark.js @@ -30,6 +30,13 @@ var pkg = require( './../package.json' ).name; var dcartesianProduct = require( './../lib' ); +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + // FUNCTIONS // /** @@ -44,15 +51,9 @@ function createBenchmark( len ) { var x; var y; - x = uniform( [ len ], -100.0, 100.0, { - 'dtype': 'float64' - }); - y = uniform( [ len ], -100.0, 100.0, { - 'dtype': 'float64' - }); - out = zeros( [ len*len, 2 ], { - 'dtype': 'float64' - }); + x = uniform( [ len ], -100.0, 100.0, options ); + y = uniform( [ len ], -100.0, 100.0, options ); + out = zeros( [ len*len, 2 ], options ); return benchmark;