From 12c8edae633b9a961cae420ae71fb3f0e66f6efb Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 24 Sep 2026 14:58:25 +0500 Subject: [PATCH 1/2] feat: add blas/ext/base/ndarray/dwhere --- .../blas/ext/base/ndarray/dwhere/README.md | 128 ++++++ .../ndarray/dwhere/benchmark/benchmark.js | 110 +++++ .../ext/base/ndarray/dwhere/docs/repl.txt | 32 ++ .../base/ndarray/dwhere/docs/types/index.d.ts | 57 +++ .../base/ndarray/dwhere/docs/types/test.ts | 72 +++ .../ext/base/ndarray/dwhere/examples/index.js | 46 ++ .../blas/ext/base/ndarray/dwhere/lib/index.js | 47 ++ .../blas/ext/base/ndarray/dwhere/lib/main.js | 76 ++++ .../blas/ext/base/ndarray/dwhere/package.json | 73 ++++ .../blas/ext/base/ndarray/dwhere/test/test.js | 412 ++++++++++++++++++ 10 files changed, 1053 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/README.md new file mode 100644 index 000000000000..96ed983e7c71 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/README.md @@ -0,0 +1,128 @@ + + +# dwhere + +> Take elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dwhere = require( '@stdlib/blas/ext/base/ndarray/dwhere' ); +``` + +#### dwhere( arrays ) + +Takes elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition. + +```javascript +var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); + +var condition = new BooleanVector( [ true, false, true, false, true ] ); +var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +var y = new Float64Vector( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +var out = new Float64Vector( 5 ); + +dwhere( [ condition, x, y, out ] ); +// out => [ 1.0, 7.0, 3.0, 9.0, 5.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional condition ndarray. + - first one-dimensional input ndarray. + - second one-dimensional input ndarray. + - a one-dimensional output ndarray. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dwhere = require( '@stdlib/blas/ext/base/ndarray/dwhere' ); + +var opts = { + 'dtype': 'float64' +}; + +var cbuf = bernoulli( 10, 0.5, { + 'dtype': 'uint8' +}); +var condition = new BooleanVector( cbuf.buffer ); +console.log( ndarray2array( condition ) ); + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var y = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( y ) ); + +var out = discreteUniform( [ 10 ], -100, 100, opts ); + +dwhere( [ condition, x, y, out ] ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js new file mode 100644 index 000000000000..0ba72635518c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/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 bernoulli = require( '@stdlib/random/array/bernoulli' ); +var uniform = require( '@stdlib/random/uniform' ); +var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dwhere = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var cbuf = bernoulli( len, 0.5, { + 'dtype': 'uint8' + }); + var condition = new BooleanVector( cbuf.buffer ); + var out = uniform( [ len ], -100.0, 100.0, options ); + var x = uniform( [ len ], -100.0, 100.0, options ); + var y = uniform( [ len ], -100.0, 100.0, options ); + 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 = dwhere( [ condition, x, y, out ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + 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/blas/ext/base/ndarray/dwhere/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/repl.txt new file mode 100644 index 000000000000..4c5d6d2ab42a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( arrays ) + Takes elements from one of two one-dimensional double-precision floating- + point ndarrays depending on a condition. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional condition ndarray. + - first one-dimensional input ndarray. + - second one-dimensional input ndarray. + - a one-dimensional output ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var c = new {{alias:@stdlib/ndarray/vector/bool}}( [ true, false, true, false ] ); + > var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var y = new {{alias:@stdlib/ndarray/vector/float64}}( [ 5.0, 6.0, 7.0, 8.0 ] ); + > var out = new {{alias:@stdlib/ndarray/vector/float64}}( 4 ); + > {{alias}}( [ c, x, y, out ] ) + [ 1.0, 6.0, 3.0, 8.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/index.d.ts new file mode 100644 index 000000000000..4ab9c4657f33 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @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, boolndarray } from '@stdlib/types/ndarray'; + +/** +* Takes elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional condition ndarray. +* - first one-dimensional input ndarray. +* - second one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* +* var condition = new BooleanVector( [ true, false, true, false, true ] ); +* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float64Vector( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* var out = new Float64Vector( 5 ); +* +* var v = dwhere( [ condition, x, y, out ] ); +* // returns [ 1.0, 7.0, 3.0, 9.0, 5.0 ] +*/ +declare function dwhere( arrays: [ boolndarray, float64ndarray, float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dwhere; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/test.ts new file mode 100644 index 000000000000..eb3bae908ccc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/docs/types/test.ts @@ -0,0 +1,72 @@ +/** +* @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 BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +import dwhere = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const condition = new BooleanVector( 10 ); + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dwhere( [ condition, 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... +{ + dwhere( '10' ); // $ExpectError + dwhere( 5 ); // $ExpectError + dwhere( true ); // $ExpectError + dwhere( false ); // $ExpectError + dwhere( null ); // $ExpectError + dwhere( undefined ); // $ExpectError + dwhere( [] ); // $ExpectError + dwhere( {} ); // $ExpectError + dwhere( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const condition = new BooleanVector( 10 ); + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const y = zeros( [ 10 ], { + 'dtype': 'float64' + }); + const out = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dwhere(); // $ExpectError + dwhere( [ condition, x, y, out ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/examples/index.js new file mode 100644 index 000000000000..0bcc5152a3fd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/examples/index.js @@ -0,0 +1,46 @@ +/** +* @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 bernoulli = require( '@stdlib/random/array/bernoulli' ); +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dwhere = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var cbuf = bernoulli( 10, 0.5, { + 'dtype': 'uint8' +}); +var condition = new BooleanVector( cbuf.buffer ); +console.log( ndarray2array( condition ) ); + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var y = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( y ) ); + +var out = discreteUniform( [ 10 ], -100, 100, opts ); + +dwhere( [ condition, x, y, out ] ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/index.js new file mode 100644 index 000000000000..cbece425eda9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/index.js @@ -0,0 +1,47 @@ +/** +* @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'; + +/** +* Take elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition. +* +* @module @stdlib/blas/ext/base/ndarray/dwhere +* +* @example +* var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var dwhere = require( '@stdlib/blas/ext/base/ndarray/dwhere' ); +* +* var condition = new BooleanVector( [ true, false, true, false, true ] ); +* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float64Vector( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* var out = new Float64Vector( 5 ); +* +* var v = dwhere( [ condition, x, y, out ] ); +* // returns [ 1.0, 7.0, 3.0, 9.0, 5.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/main.js new file mode 100644 index 000000000000..29976aa9cc24 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/lib/main.js @@ -0,0 +1,76 @@ +/** +* @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 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/dwhere' ).ndarray; + + +// MAIN // + +/** +* Takes elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional condition ndarray. +* - first one-dimensional input ndarray. +* - second one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var BooleanVector = require( '@stdlib/ndarray/vector/bool' ); +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* +* var condition = new BooleanVector( [ true, false, true, false, true ] ); +* var x = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); +* var y = new Float64Vector( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); +* var out = new Float64Vector( 5 ); +* +* var v = dwhere( [ condition, x, y, out ] ); +* // returns [ 1.0, 7.0, 3.0, 9.0, 5.0 ] +*/ +function dwhere( arrays ) { + var condition; + var out; + var x; + var y; + + condition = arrays[ 0 ]; + x = arrays[ 1 ]; + y = arrays[ 2 ]; + out = arrays[ 3 ]; + strided( numelDimension( condition, 0 ), getData( condition ), getStride( condition, 0 ), getOffset( condition ), getData( x ), getStride( x, 0 ), getOffset( x ), getData( y ), getStride( y, 0 ), getOffset( y ), getData( out ), getStride( out, 0 ), getOffset( out ) ); // eslint-disable-line max-len + return out; +} + + +// EXPORTS // + +module.exports = dwhere; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/package.json new file mode 100644 index 000000000000..9b72b5b1910c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/dwhere", + "version": "0.0.0", + "description": "Take elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition.", + "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", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "ext", + "dwhere", + "where", + "condition", + "conditional", + "select", + "ternary", + "vector", + "strided", + "array", + "ndarray", + "float64", + "double" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/test/test.js new file mode 100644 index 000000000000..95b4daba8d2d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/test/test.js @@ -0,0 +1,412 @@ +/** +* @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 BooleanArray = require( '@stdlib/array/bool' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var dwhere = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional double-precision floating-point ndarray. +* +* @private +* @param {Float64Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + +/** +* Returns a one-dimensional boolean ndarray. +* +* @private +* @param {BooleanArray} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function bvector( buffer, length, stride, offset ) { + return new ndarray( 'bool', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dwhere, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dwhere.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function selects elements from `x` or `y` based on a condition', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray( [ true, false, true, false, true ] ); + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + ybuf = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + c = bvector( cbuf, 5, 1, 0 ); + x = vector( xbuf, 5, 1, 0 ); + y = vector( ybuf, 5, 1, 0 ); + out = vector( obuf, 5, 1, 0 ); + expected = new Float64Array([ + 1.0, // x[0] + 7.0, // y[1] + 3.0, // x[2] + 9.0, // y[3] + 5.0 // x[4] + ]); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function selects elements from `x` or `y` based on a condition (all true)', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray( [ true, true, true, true, true ] ); + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + ybuf = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + c = bvector( cbuf, 5, 1, 0 ); + x = vector( xbuf, 5, 1, 0 ); + y = vector( ybuf, 5, 1, 0 ); + out = vector( obuf, 5, 1, 0 ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function selects elements from `x` or `y` based on a condition (all false)', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray( [ false, false, false, false, false ] ); + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + ybuf = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + c = bvector( cbuf, 5, 1, 0 ); + x = vector( xbuf, 5, 1, 0 ); + y = vector( ybuf, 5, 1, 0 ); + out = vector( obuf, 5, 1, 0 ); + expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] ); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having non-unit strides', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray([ + true, // 0 + false, + false, // 1 + true, + true, // 2 + false, + false, // 3 + true + ]); + xbuf = new Float64Array([ + 1.0, // 0 + -2.0, + 3.0, // 1 + -4.0, + 5.0, // 2 + -6.0, + 7.0, // 3 + -8.0 + ]); + ybuf = new Float64Array([ + 11.0, // 0 + -12.0, + 13.0, // 1 + -14.0, + 15.0, // 2 + -16.0, + 17.0, // 3 + -18.0 + ]); + obuf = new Float64Array( [ 0.0, 99.0, 0.0, 99.0, 0.0, 99.0, 0.0, 99.0 ] ); + c = bvector( cbuf, 4, 2, 0 ); + x = vector( xbuf, 4, 2, 0 ); + y = vector( ybuf, 4, 2, 0 ); + out = vector( obuf, 4, 2, 0 ); + expected = new Float64Array([ + 1.0, // x[0] + 99.0, + 13.0, // y[2] + 99.0, + 5.0, // x[4] + 99.0, + 17.0, // y[6] + 99.0 + ]); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having negative strides', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray([ + true, // 4 + false, // 3 + true, // 2 + false, // 1 + true // 0 + ]); + xbuf = new Float64Array([ + 1.0, // 4 + 2.0, // 3 + 3.0, // 2 + 4.0, // 1 + 5.0 // 0 + ]); + ybuf = new Float64Array([ + 6.0, // 4 + 7.0, // 3 + 8.0, // 2 + 9.0, // 1 + 10.0 // 0 + ]); + obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + c = bvector( cbuf, 5, -1, 4 ); + x = vector( xbuf, 5, -1, 4 ); + y = vector( ybuf, 5, -1, 4 ); + out = vector( obuf, 5, -1, 4 ); + expected = new Float64Array([ + 1.0, // x[0] + 7.0, // y[1] + 3.0, // x[2] + 9.0, // y[3] + 5.0 // x[4] + ]); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having non-zero offsets', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray([ + false, + false, + true, // 0 + false, // 1 + true, // 2 + false + ]); + xbuf = new Float64Array([ + -1.0, + -2.0, + 3.0, // 0 + 4.0, // 1 + 5.0, // 2 + -6.0 + ]); + ybuf = new Float64Array([ + -11.0, + -12.0, + 13.0, // 0 + 14.0, // 1 + 15.0, // 2 + -16.0 + ]); + obuf = new Float64Array( [ 99.0, 99.0, 0.0, 0.0, 0.0, 99.0 ] ); + c = bvector( cbuf, 3, 1, 2 ); + x = vector( xbuf, 3, 1, 2 ); + y = vector( ybuf, 3, 1, 2 ); + out = vector( obuf, 3, 1, 2 ); + expected = new Float64Array([ + 99.0, + 99.0, + 3.0, // x[2] + 14.0, // y[3] + 5.0, // x[4] + 99.0 + ]); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having different strides and offsets', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray( [ false, true, false, true, false, false ] ); + xbuf = new Float64Array( [ 1.0, 100.0, 2.0, 100.0, 3.0, 100.0, 4.0 ] ); + ybuf = new Float64Array( [ 40.0, 30.0, 20.0, 10.0 ] ); + obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + c = bvector( cbuf, 4, -1, 4 ); + x = vector( xbuf, 4, 2, 0 ); + y = vector( ybuf, 4, -1, 3 ); + out = vector( obuf, 4, 3, 1 ); + expected = new Float64Array([ + 0.0, + 10.0, // cbuf[4] = false => y[3] + 0.0, + 0.0, + 2.0, // cbuf[3] = true => x[2] + 0.0, + 0.0, + 30.0, // cbuf[2] = false => y[1] + 0.0, + 0.0, + 4.0, // cbuf[1] = true => x[6] + 0.0 + ]); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the output ndarray unchanged when the input ndarrays are empty', function test( t ) { + var expected; + var actual; + var cbuf; + var obuf; + var xbuf; + var ybuf; + var out; + var c; + var x; + var y; + + cbuf = new BooleanArray( [] ); + xbuf = new Float64Array( [] ); + ybuf = new Float64Array( [] ); + obuf = new Float64Array( [] ); + c = bvector( cbuf, 0, 1, 0 ); + x = vector( xbuf, 0, 1, 0 ); + y = vector( ybuf, 0, 1, 0 ); + out = vector( obuf, 0, 1, 0 ); + expected = new Float64Array( [] ); + + actual = dwhere( [ c, x, y, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( obuf, expected ), true, 'returns expected value' ); + + t.end(); +}); From 5838489313b4161ba8202d42e99eae81d6c9a763 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 24 Sep 2026 15:03:43 +0500 Subject: [PATCH 2/2] fix: lint err --- .../base/ndarray/dwhere/benchmark/benchmark.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js index 0ba72635518c..8cdf2d004331 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js @@ -47,13 +47,19 @@ var options = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var cbuf = bernoulli( len, 0.5, { + var condition; + var cbuf; + var out; + var x; + var y; + + cbuf = bernoulli( len, 0.5, { 'dtype': 'uint8' }); - var condition = new BooleanVector( cbuf.buffer ); - var out = uniform( [ len ], -100.0, 100.0, options ); - var x = uniform( [ len ], -100.0, 100.0, options ); - var y = uniform( [ len ], -100.0, 100.0, options ); + condition = new BooleanVector( cbuf.buffer ); + out = uniform( [ len ], -100.0, 100.0, options ); + x = uniform( [ len ], -100.0, 100.0, options ); + y = uniform( [ len ], -100.0, 100.0, options ); return benchmark; /**