From a65bc4363ab3809beec0d42bddc3a206d3620480 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 18 Dec 2025 22:33:31 +0530 Subject: [PATCH 1/5] feat: add blas/ext/base/ndarray/gapx --- .../blas/ext/base/ndarray/gapx/README.md | 129 ++++++++++++++ .../base/ndarray/gapx/benchmark/benchmark.js | 101 +++++++++++ .../blas/ext/base/ndarray/gapx/docs/repl.txt | 28 +++ .../base/ndarray/gapx/docs/types/index.d.ts | 48 ++++++ .../ext/base/ndarray/gapx/docs/types/test.ts | 62 +++++++ .../ext/base/ndarray/gapx/examples/index.js | 34 ++++ .../blas/ext/base/ndarray/gapx/lib/index.js | 46 +++++ .../blas/ext/base/ndarray/gapx/lib/main.js | 71 ++++++++ .../blas/ext/base/ndarray/gapx/package.json | 74 ++++++++ .../blas/ext/base/ndarray/gapx/test/test.js | 161 ++++++++++++++++++ 10 files changed, 754 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md new file mode 100644 index 000000000000..1ca029341454 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md @@ -0,0 +1,129 @@ + + +# gapx + +> Add a scalar constant to each element in an ndarray. + +
+ +## Usage + +```javascript +var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); +``` + +#### gapx( x, alpha ) + +Adds a scalar constant to each element in an ndarray. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; +var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +gapx( x, 5.0 ); + +var y = x.data; +// returns [ 6.0, 7.0, 8.0, 9.0 ] +``` + +The function has the following parameters: + +- **x**: input ndarray. +- **alpha**: scalar constant. + +Note that indexing is relative to the first index. To introduce an offset, use [`ndarray`][@stdlib/ndarray/ctor] view creation. + +```javascript +var ndarray = require( '@stdlib/ndarray/ctor' ); + +// Initial array: +var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + +// Create an ndarray view: +var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); + +gapx( x, 5.0 ); + +var y = x.data; +// returns [ 1.0, 2.0, 8.0, 9.0, 10.0 ] +``` + +
+ + + +
+ +## Notes + +- The function **mutates** the input ndarray. +- The function supports generic arrays and typed arrays. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); + +var xbuf = discreteUniform( 10, 0, 100, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); + +console.log( x.data ); + +gapx( x, 5.0 ); + +console.log( x.data ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js new file mode 100644 index 000000000000..c1079569390f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js @@ -0,0 +1,101 @@ +/** +* @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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var gapx = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + + x = new ndarray( 'generic', discreteUniform( len, -100, 100, { + 'dtype': 'generic' + }), [ len ], [ 1 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = gapx( x, 5.0 ); + if ( isnan( z.data[ i % len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z.data[ i % len ] ) ) { + 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt new file mode 100644 index 000000000000..040d28ecbf44 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt @@ -0,0 +1,28 @@ +{{alias}}( x, alpha ) + Adds a scalar constant to each element in an ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + alpha: number + Scalar constant. + + Returns + ------- + out: ndarray + Input ndarray. + + Examples + -------- + > var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + > var x = {{alias:@stdlib/ndarray/ctor}}( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + > {{alias}}( x, 5.0 ) + + > x.data + [ 6.0, 7.0, 8.0, 9.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts new file mode 100644 index 000000000000..5bca6d2bfd5f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @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 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; + +/** +* Adds a scalar constant to each element in an ndarray. +* +* @param x - input ndarray +* @param alpha - scalar constant +* @returns input ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* gapx( x, 5.0 ); +* +* var y = x.data; +* // returns [ 6.0, 7.0, 8.0, 9.0 ] +*/ +declare function gapx( x: ndarray, alpha: number ): ndarray; + + +// EXPORTS // + +export = gapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts new file mode 100644 index 000000000000..b56e787af233 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts @@ -0,0 +1,62 @@ +/* +* @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 gapx = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x: any = null; + gapx( x, 5.0 ); // $ExpectType ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + gapx( 123, 5.0 ); // $ExpectError + gapx( true, 5.0 ); // $ExpectError + gapx( false, 5.0 ); // $ExpectError + gapx( null, 5.0 ); // $ExpectError + gapx( undefined, 5.0 ); // $ExpectError + gapx( '5', 5.0 ); // $ExpectError + gapx( [ '1', '2' ], 5.0 ); // $ExpectError + gapx( {}, 5.0 ); // $ExpectError + gapx( ( x: number ): number => x, 5.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x: any = null; + gapx( x, '5' ); // $ExpectError + gapx( x, true ); // $ExpectError + gapx( x, false ); // $ExpectError + gapx( x, null ); // $ExpectError + gapx( x, undefined ); // $ExpectError + gapx( x, [ '1' ] ); // $ExpectError + gapx( x, {} ); // $ExpectError + gapx( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x: any = null; + gapx(); // $ExpectError + gapx( x ); // $ExpectError + gapx( x, 5.0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js new file mode 100644 index 000000000000..0c94bba87269 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/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 ndarray = require( '@stdlib/ndarray/ctor' ); +var gapx = require( './../lib' ); + +var xbuf = discreteUniform( 10, 0, 100, { + 'dtype': 'generic' +}); +var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); + +console.log( x.data ); + +gapx( x, 5.0 ); + +console.log( x.data ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js new file mode 100644 index 000000000000..6431a3ce5cba --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* Add a scalar constant to each element in an ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/gapx +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); +* +* var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* gapx( x, 5.0 ); +* +* var y = x.data; +* // returns [ 6.0, 7.0, 8.0, 9.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js new file mode 100644 index 000000000000..ef583ac93740 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js @@ -0,0 +1,71 @@ +/** +* @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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var strided = require( '@stdlib/blas/ext/base/gapx' ).ndarray; +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); + + +// MAIN // + +/** +* Adds a scalar constant to each element in an ndarray. +* +* @param {ndarray} x - input ndarray +* @param {number} alpha - scalar constant +* @returns {ndarray} input ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; +* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* gapx( x, 5.0 ); +* +* var y = x.data; +* // returns [ 6.0, 7.0, 8.0, 9.0 ] +*/ +function gapx( x, alpha ) { + var buf; + var sx; + var ox; + var N; + + N = numelDimension( x, 0 ); + if ( N <= 0 ) { + return x; + } + buf = getData( x ); + sx = getStride( x, 0 ); + ox = getOffset( x ); + + strided( N, alpha, buf, sx, ox ); + return x; +} + + +// EXPORTS // + +module.exports = gapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json new file mode 100644 index 000000000000..48e75fd3771b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/gapx", + "version": "0.0.0", + "description": "Add a scalar constant to each element in an ndarray.", + "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", + "level 1", + "gapx", + "linear", + "algebra", + "subroutines", + "ndarray", + "vector", + "array", + "multidimensional", + "add", + "constant", + "plus", + "increment", + "augment", + "generic" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js new file mode 100644 index 000000000000..560748028f6c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js @@ -0,0 +1,161 @@ +/** +* @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 ndarray = require( '@stdlib/ndarray/ctor' ); +var gapx = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function adds a scalar constant to each element in an ndarray', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0 ] ); + + gapx( x, 5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns the input ndarray', function test( t ) { + var xbuf; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + y = gapx( x, 5.0 ); + + t.strictEqual( y, x, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays with negative strides', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ -1 ], 3, 'row-major' ); + + expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0 ] ); + + gapx( x, 5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays with an offset', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); + + expected = new Float64Array( [ 1.0, 2.0, 8.0, 9.0, 10.0 ] ); + + gapx( x, 5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports zero-length ndarrays', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [] ); + x = new ndarray( 'float64', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); + + expected = new Float64Array( [] ); + + gapx( x, 5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports adding zero', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + + gapx( x, 0.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative constants', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float64Array( [ -4.0, -3.0, -2.0, -1.0 ] ); + + gapx( x, -5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports generic arrays', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = [ 6.0, 7.0, 8.0, 9.0 ]; + + gapx( x, 5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); From 5c7a0d5b0984c4206b68d65f29ea61be6195bdc4 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 19 Dec 2025 17:56:15 +0530 Subject: [PATCH 2/5] docs: update return annotations to use ndarray instance notation --- .../@stdlib/blas/ext/base/ndarray/gapx/README.md | 12 ++++-------- .../@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt | 4 +--- .../blas/ext/base/ndarray/gapx/docs/types/index.d.ts | 6 ++---- .../@stdlib/blas/ext/base/ndarray/gapx/lib/index.js | 6 ++---- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md index 1ca029341454..4ed9e1fc4f27 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md @@ -40,10 +40,8 @@ var ndarray = require( '@stdlib/ndarray/ctor' ); var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); -gapx( x, 5.0 ); - -var y = x.data; -// returns [ 6.0, 7.0, 8.0, 9.0 ] +var out = gapx( x, 5.0 ); +// returns [ 6.0, 7.0, 8.0, 9.0 ] ``` The function has the following parameters: @@ -62,10 +60,8 @@ var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; // Create an ndarray view: var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); -gapx( x, 5.0 ); - -var y = x.data; -// returns [ 1.0, 2.0, 8.0, 9.0, 10.0 ] +var out = gapx( x, 5.0 ); +// returns [ 1.0, 2.0, 8.0, 9.0, 10.0 ] ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt index 040d28ecbf44..ddc69311de33 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt @@ -19,9 +19,7 @@ > var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; > var x = {{alias:@stdlib/ndarray/ctor}}( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); > {{alias}}( x, 5.0 ) - - > x.data - [ 6.0, 7.0, 8.0, 9.0 ] + [ 6.0, 7.0, 8.0, 9.0 ] See Also -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts index 5bca6d2bfd5f..7bd84999a63c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts @@ -35,10 +35,8 @@ import { ndarray } from '@stdlib/types/ndarray'; * var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; * var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* gapx( x, 5.0 ); -* -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = gapx( x, 5.0 ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ declare function gapx( x: ndarray, alpha: number ): ndarray; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js index 6431a3ce5cba..40b3a03842f9 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js @@ -30,10 +30,8 @@ * var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; * var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* gapx( x, 5.0 ); -* -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = gapx( x, 5.0 ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ // MODULES // From 0ac2249bfe07fb00e28aab4bfd3a5bf31f709fcd Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 19 Dec 2025 18:01:55 +0530 Subject: [PATCH 3/5] fix: correct ndarray instance notation for offset example --- lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md index 4ed9e1fc4f27..0ff625e01d33 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md @@ -61,7 +61,7 @@ var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); var out = gapx( x, 5.0 ); -// returns [ 1.0, 2.0, 8.0, 9.0, 10.0 ] +// returns [ 8.0, 9.0, 10.0 ] ``` From a58566f92ec45fa8a42bc553ac73cc8a579313bc Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Sun, 21 Dec 2025 12:00:53 +0530 Subject: [PATCH 4/5] fix: refactor gapx to use arrays API pattern --- .../blas/ext/base/ndarray/gapx/README.md | 51 ++++--- .../base/ndarray/gapx/benchmark/benchmark.js | 27 ++-- .../blas/ext/base/ndarray/gapx/docs/repl.txt | 20 +-- .../base/ndarray/gapx/docs/types/index.d.ts | 20 ++- .../ext/base/ndarray/gapx/docs/types/test.ts | 47 +++--- .../ext/base/ndarray/gapx/examples/index.js | 17 ++- .../blas/ext/base/ndarray/gapx/lib/index.js | 15 +- .../blas/ext/base/ndarray/gapx/lib/main.js | 36 ++--- .../blas/ext/base/ndarray/gapx/package.json | 2 +- .../blas/ext/base/ndarray/gapx/test/test.js | 138 ++++++------------ 10 files changed, 182 insertions(+), 191 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md index 0ff625e01d33..9c4156868a1f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/README.md @@ -20,7 +20,7 @@ limitations under the License. # gapx -> Add a scalar constant to each element in an ndarray. +> Add a scalar constant to each element in a one-dimensional ndarray.
@@ -30,38 +30,50 @@ limitations under the License. var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); ``` -#### gapx( x, alpha ) +#### gapx( arrays ) -Adds a scalar constant to each element in an ndarray. +Adds a scalar constant to each element in a one-dimensional ndarray. ```javascript var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); -var out = gapx( x, 5.0 ); -// returns [ 6.0, 7.0, 8.0, 9.0 ] +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' +}); + +var out = gapx( [ x, alpha ] ); +// returns [ 6, 7, 8, 9 ] + +var bool = ( out === x ); +// returns true + +var arr = ndarray2array( out ); +// returns [ 6, 7, 8, 9 ] ``` The function has the following parameters: -- **x**: input ndarray. -- **alpha**: scalar constant. - -Note that indexing is relative to the first index. To introduce an offset, use [`ndarray`][@stdlib/ndarray/ctor] view creation. +- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a scalar constant. ```javascript var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); // Initial array: var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; // Create an ndarray view: var x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); - -var out = gapx( x, 5.0 ); -// returns [ 8.0, 9.0, 10.0 ] +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' +}); +var out = gapx( [ x, alpha ] ); +// returns [ 8, 9, 10 ] ```
@@ -72,7 +84,6 @@ var out = gapx( x, 5.0 ); ## Notes -- The function **mutates** the input ndarray. - The function supports generic arrays and typed arrays. @@ -88,18 +99,22 @@ var out = gapx( x, 5.0 ); ```javascript var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); var xbuf = discreteUniform( 10, 0, 100, { 'dtype': 'generic' }); var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); -console.log( x.data ); - -gapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' +}); -console.log( x.data ); +gapx( [ x, alpha ] ); +console.log( ndarray2array( x ) ); ``` @@ -118,8 +133,6 @@ console.log( x.data ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js index c1079569390f..1a4cc6093a6a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js @@ -22,13 +22,20 @@ var bench = require( '@stdlib/bench' ); var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pow = require( '@stdlib/math/base/special/pow' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var pkg = require( './../package.json' ).name; var gapx = require( './../lib' ); +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + // FUNCTIONS // /** @@ -39,11 +46,13 @@ var gapx = require( './../lib' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { + var alpha; + var xbuf; var x; - x = new ndarray( 'generic', discreteUniform( len, -100, 100, { - 'dtype': 'generic' - }), [ len ], [ 1 ], 0, 'row-major' ); + xbuf = discreteUniform( len, -100, 100, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + alpha = scalar2ndarray( 5.0, options ); return benchmark; @@ -59,14 +68,14 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - z = gapx( x, 5.0 ); - if ( isnan( z.data[ i % len ] ) ) { - b.fail( 'should not return NaN' ); + z = gapx( [ x, alpha ] ); + if ( typeof z !== 'object' ) { + b.fail( 'should return an ndarray' ); } } b.toc(); - if ( isnan( z.data[ i % len ] ) ) { - b.fail( 'should not return NaN' ); + if ( typeof z !== 'object' ) { + b.fail( 'should return an ndarray' ); } b.pass( 'benchmark finished' ); b.end(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt index ddc69311de33..ac96e86bdd98 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/repl.txt @@ -1,13 +1,12 @@ -{{alias}}( x, alpha ) - Adds a scalar constant to each element in an ndarray. + +{{alias}}( arrays ) + Adds a scalar constant to each element in a one-dimensional ndarray. Parameters ---------- - x: ndarray - Input ndarray. - - alpha: number - Scalar constant. + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray and a + zero-dimensional ndarray containing a scalar constant. Returns ------- @@ -18,8 +17,11 @@ -------- > var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; > var x = {{alias:@stdlib/ndarray/ctor}}( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - > {{alias}}( x, 5.0 ) - [ 6.0, 7.0, 8.0, 9.0 ] + > var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, { 'dtype': 'generic' } ); + > {{alias}}( [ x, alpha ] ) + [ 6, 7, 8, 9 ] + > var data = x.data + [ 6, 7, 8, 9 ] See Also -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts index 7bd84999a63c..d3dcd3089d4d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/index.d.ts @@ -23,22 +23,30 @@ import { ndarray } from '@stdlib/types/ndarray'; /** -* Adds a scalar constant to each element in an ndarray. +* Adds a scalar constant to each element in a one-dimensional ndarray. * -* @param x - input ndarray -* @param alpha - scalar constant +* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a scalar constant * @returns input ndarray * * @example * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * * var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; * var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* var out = gapx( x, 5.0 ); -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); +* +* var out = gapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6, 7, 8, 9 ] */ -declare function gapx( x: ndarray, alpha: number ): ndarray; +declare function gapx( arrays: [ ndarray, ndarray ] ): ndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts index b56e787af233..b3a1de9964bf 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/docs/types/test.ts @@ -16,6 +16,8 @@ * limitations under the License. */ +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); import gapx = require( './index' ); @@ -23,40 +25,29 @@ import gapx = require( './index' ); // The function returns an ndarray... { - const x: any = null; - gapx( x, 5.0 ); // $ExpectType ndarray + const x = zeros( 'generic', [ 3 ], 'row-major' ); + const alpha = scalar2ndarray( 5.0, { 'dtype': 'generic' } ); + gapx( [ x, alpha ] ); // $ExpectType ndarray } -// The compiler throws an error if the function is provided a first argument which is not an ndarray... +// The compiler throws an error if the function is provided a first argument which is not an array-like object containing ndarrays... { - gapx( 123, 5.0 ); // $ExpectError - gapx( true, 5.0 ); // $ExpectError - gapx( false, 5.0 ); // $ExpectError - gapx( null, 5.0 ); // $ExpectError - gapx( undefined, 5.0 ); // $ExpectError - gapx( '5', 5.0 ); // $ExpectError - gapx( [ '1', '2' ], 5.0 ); // $ExpectError - gapx( {}, 5.0 ); // $ExpectError - gapx( ( x: number ): number => x, 5.0 ); // $ExpectError -} - -// The compiler throws an error if the function is provided a second argument which is not a number... -{ - const x: any = null; - gapx( x, '5' ); // $ExpectError - gapx( x, true ); // $ExpectError - gapx( x, false ); // $ExpectError - gapx( x, null ); // $ExpectError - gapx( x, undefined ); // $ExpectError - gapx( x, [ '1' ] ); // $ExpectError - gapx( x, {} ); // $ExpectError - gapx( x, ( x: number ): number => x ); // $ExpectError + const alpha = scalar2ndarray( 5.0, { 'dtype': 'generic' } ); + gapx( 123 ); // $ExpectError + gapx( true ); // $ExpectError + gapx( false ); // $ExpectError + gapx( null ); // $ExpectError + gapx( undefined ); // $ExpectError + gapx( '5' ); // $ExpectError + gapx( [ '1', '2' ] ); // $ExpectError + gapx( {} ); // $ExpectError + gapx( ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... { - const x: any = null; + const x = zeros( 'generic', [ 3 ], 'row-major' ); + const alpha = scalar2ndarray( 5.0, { 'dtype': 'generic' } ); gapx(); // $ExpectError - gapx( x ); // $ExpectError - gapx( x, 5.0, 10 ); // $ExpectError + gapx( [ x, alpha ], 10 ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js index 0c94bba87269..ea189e30179a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/examples/index.js @@ -18,17 +18,26 @@ 'use strict'; +// MODULES // + var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var gapx = require( './../lib' ); + +// MAIN // + var xbuf = discreteUniform( 10, 0, 100, { 'dtype': 'generic' }); var x = new ndarray( 'generic', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); -console.log( x.data ); - -gapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' +}); -console.log( x.data ); +gapx( [ x, alpha ] ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js index 40b3a03842f9..c52de49b34b5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/index.js @@ -19,19 +19,28 @@ 'use strict'; /** -* Add a scalar constant to each element in an ndarray. +* Add a scalar constant to each element in a one-dimensional ndarray. * * @module @stdlib/blas/ext/base/ndarray/gapx * * @example * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var gapx = require( '@stdlib/blas/ext/base/ndarray/gapx' ); * * var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; * var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* var out = gapx( x, 5.0 ); -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); +* +* var out = gapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6, 7, 8, 9 ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js index ef583ac93740..6bbb2d3ed40d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/lib/main.js @@ -25,43 +25,39 @@ var strided = require( '@stdlib/blas/ext/base/gapx' ).ndarray; var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); // MAIN // /** -* Adds a scalar constant to each element in an ndarray. +* Adds a scalar constant to each element in a one-dimensional ndarray. * -* @param {ndarray} x - input ndarray -* @param {number} alpha - scalar constant +* @param {ArrayLikeObject} arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a scalar constant * @returns {ndarray} input ndarray * * @example * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * * var xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; * var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* gapx( x, 5.0 ); +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); * -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = gapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6, 7, 8, 9 ] */ -function gapx( x, alpha ) { - var buf; - var sx; - var ox; - var N; - - N = numelDimension( x, 0 ); - if ( N <= 0 ) { - return x; - } - buf = getData( x ); - sx = getStride( x, 0 ); - ox = getOffset( x ); +function gapx( arrays ) { + var x = arrays[ 0 ]; - strided( N, alpha, buf, sx, ox ); + strided( numelDimension( x, 0 ), ndarraylike2scalar( arrays[ 1 ] ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len return x; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json index 48e75fd3771b..95064334ef22 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/ext/base/ndarray/gapx", "version": "0.0.0", - "description": "Add a scalar constant to each element in an ndarray.", + "description": "Add a scalar constant to each element in a one-dimensional ndarray.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js index 560748028f6c..f0f31b59d4fe 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/test/test.js @@ -21,8 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var Float64Array = require( '@stdlib/array/float64' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); var gapx = require( './../lib' ); @@ -36,126 +37,79 @@ tape( 'main export is a function', function test( t ) { tape( 'the function adds a scalar constant to each element in an ndarray', function test( t ) { var expected; + var alpha; var xbuf; var x; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0 ] ); - - gapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + expected = [ 6, 7, 8, 9 ]; + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + gapx( [ x, alpha ] ); + t.deepEqual( getData( x ), expected, 'returns expected value' ); t.end(); }); tape( 'the function returns the input ndarray', function test( t ) { + var alpha; var xbuf; var x; var y; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - y = gapx( x, 5.0 ); - + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + y = gapx( [ x, alpha ] ); t.strictEqual( y, x, 'returns expected value' ); t.end(); }); tape( 'the function supports ndarrays with negative strides', function test( t ) { var expected; + var alpha; var xbuf; var x; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float64', xbuf, [ 4 ], [ -1 ], 3, 'row-major' ); - - expected = new Float64Array( [ 6.0, 7.0, 8.0, 9.0 ] ); - - gapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 4 ], [ -1 ], 3, 'row-major' ); + expected = [ 6, 7, 8, 9 ]; + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + gapx( [ x, alpha ] ); + t.deepEqual( getData( x ), expected, 'returns expected value' ); t.end(); }); -tape( 'the function supports ndarrays with an offset', function test( t ) { +tape( 'the function supports ndarrays with non-zero offsets', function test( t ) { var expected; + var alpha; var xbuf; var x; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); - x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); - - expected = new Float64Array( [ 1.0, 2.0, 8.0, 9.0, 10.0 ] ); - - gapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]; + x = new ndarray( 'generic', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); + expected = [ 1, 2, 8, 9, 10 ]; + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + gapx( [ x, alpha ] ); + t.deepEqual( getData( x ), expected, 'returns expected value' ); t.end(); }); tape( 'the function supports zero-length ndarrays', function test( t ) { var expected; + var alpha; var xbuf; var x; - - xbuf = new Float64Array( [] ); - x = new ndarray( 'float64', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); - - expected = new Float64Array( [] ); - - gapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function supports adding zero', function test( t ) { - var expected; - var xbuf; - var x; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - - gapx( x, 0.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function supports negative constants', function test( t ) { - var expected; - var xbuf; - var x; - - xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = new Float64Array( [ -4.0, -3.0, -2.0, -1.0 ] ); - - gapx( x, -5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function supports generic arrays', function test( t ) { - var expected; - var xbuf; - var x; - - xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; - x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = [ 6.0, 7.0, 8.0, 9.0 ]; - - gapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + xbuf = []; + x = new ndarray( 'generic', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); + expected = []; + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + gapx( [ x, alpha ] ); + t.deepEqual( getData( x ), expected, 'returns expected value' ); t.end(); }); From b9f7a389b9d4c2dfc64c4afa236eaabb3f481dad Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Mon, 22 Dec 2025 08:15:01 +0530 Subject: [PATCH 5/5] fix: gapx benchmark - use @stdlib/string/format per RFC #8647 --- .../@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js index 1a4cc6093a6a..cb75d033c53a 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gapx/benchmark/benchmark.js @@ -25,6 +25,7 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var pow = require( '@stdlib/math/base/special/pow' ); var ndarray = require( '@stdlib/ndarray/ctor' ); var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var gapx = require( './../lib' ); @@ -103,7 +104,7 @@ function main() { for ( i = min; i <= max; i++ ) { len = pow( 10, i ); f = createBenchmark( len ); - bench( pkg+':len='+len, f ); + bench( format( '%s:len=%d', pkg, len ), f ); } }