From 945f942f3a0b742691e78035215e8a93eec7babc Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Thu, 18 Dec 2025 20:19:42 +0530 Subject: [PATCH 1/4] feat: add blas/ext/base/ndarray/sapx --- .../blas/ext/base/ndarray/sapx/README.md | 130 ++++++++++++++++ .../base/ndarray/sapx/benchmark/benchmark.js | 101 ++++++++++++ .../blas/ext/base/ndarray/sapx/docs/repl.txt | 29 ++++ .../base/ndarray/sapx/docs/types/index.d.ts | 49 ++++++ .../ext/base/ndarray/sapx/docs/types/test.ts | 62 ++++++++ .../ext/base/ndarray/sapx/examples/index.js | 34 ++++ .../blas/ext/base/ndarray/sapx/lib/index.js | 47 ++++++ .../blas/ext/base/ndarray/sapx/lib/main.js | 72 +++++++++ .../blas/ext/base/ndarray/sapx/package.json | 76 +++++++++ .../blas/ext/base/ndarray/sapx/test/test.js | 145 ++++++++++++++++++ 10 files changed, 745 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md new file mode 100644 index 000000000000..dd3f777cd6da --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md @@ -0,0 +1,130 @@ + + +# sapx + +> Add a scalar constant to each element in a single-precision floating-point ndarray. + +
+ +## Usage + +```javascript +var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); +``` + +#### sapx( x, alpha ) + +Adds a scalar constant to each element in a single-precision floating-point ndarray. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + +sapx( 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 Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); + +// Initial array: +var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + +// Create an ndarray view: +var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); + +sapx( 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. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); + +var xbuf = discreteUniform( 10, 0, 100, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); + +console.log( x.data ); + +sapx( x, 5.0 ); + +console.log( x.data ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js new file mode 100644 index 000000000000..5cb71140a779 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/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 isnanf = require( '@stdlib/math/base/assert/is-nanf' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var sapx = 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( 'float32', discreteUniform( len, -100, 100, { + 'dtype': 'float32' + }), [ 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 = sapx( x, 5.0 ); + if ( isnanf( z.data[ i % len ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnanf( 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/sapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt new file mode 100644 index 000000000000..2aeb3a4090ab --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt @@ -0,0 +1,29 @@ +{{alias}}( x, alpha ) + Adds a scalar constant to each element in a single-precision floating- + point ndarray. + + Parameters + ---------- + x: float32ndarray + Input ndarray. + + alpha: number + Scalar constant. + + Returns + ------- + out: float32ndarray + Input ndarray. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var x = {{alias:@stdlib/ndarray/ctor}}( 'float32', 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/sapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts new file mode 100644 index 000000000000..8ddf86ac342d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @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 { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Adds a scalar constant to each element in a single-precision floating-point ndarray. +* +* @param x - input ndarray +* @param alpha - scalar constant +* @returns input ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* sapx( x, 5.0 ); +* +* var y = x.data; +* // returns [ 6.0, 7.0, 8.0, 9.0 ] +*/ +declare function sapx( x: float32ndarray, alpha: number ): float32ndarray; + + +// EXPORTS // + +export = sapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts new file mode 100644 index 000000000000..dc65c070e6f0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/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 sapx = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x: any = null; + sapx( x, 5.0 ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + sapx( 123, 5.0 ); // $ExpectError + sapx( true, 5.0 ); // $ExpectError + sapx( false, 5.0 ); // $ExpectError + sapx( null, 5.0 ); // $ExpectError + sapx( undefined, 5.0 ); // $ExpectError + sapx( '5', 5.0 ); // $ExpectError + sapx( [ '1', '2' ], 5.0 ); // $ExpectError + sapx( {}, 5.0 ); // $ExpectError + sapx( ( 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; + sapx( x, '5' ); // $ExpectError + sapx( x, true ); // $ExpectError + sapx( x, false ); // $ExpectError + sapx( x, null ); // $ExpectError + sapx( x, undefined ); // $ExpectError + sapx( x, [ '1' ] ); // $ExpectError + sapx( x, {} ); // $ExpectError + sapx( 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; + sapx(); // $ExpectError + sapx( x ); // $ExpectError + sapx( x, 5.0, 10 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js new file mode 100644 index 000000000000..87935dcf91ff --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/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 sapx = require( './../lib' ); + +var xbuf = discreteUniform( 10, 0, 100, { + 'dtype': 'float32' +}); +var x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); + +console.log( x.data ); + +sapx( x, 5.0 ); + +console.log( x.data ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js new file mode 100644 index 000000000000..5c0f5d961a1c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js @@ -0,0 +1,47 @@ +/** +* @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 a single-precision floating-point ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/sapx +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); +* +* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* sapx( 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/sapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js new file mode 100644 index 000000000000..5d95516d86df --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js @@ -0,0 +1,72 @@ +/** +* @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/sapx' ).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 a single-precision floating-point ndarray. +* +* @param {float32ndarray} x - input ndarray +* @param {number} alpha - scalar constant +* @returns {float32ndarray} input ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); +* +* sapx( x, 5.0 ); +* +* var y = x.data; +* // returns [ 6.0, 7.0, 8.0, 9.0 ] +*/ +function sapx( 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 = sapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json new file mode 100644 index 000000000000..feca46b066e0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json @@ -0,0 +1,76 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/sapx", + "version": "0.0.0", + "description": "Add a scalar constant to each element in a single-precision floating-point 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", + "sapx", + "linear", + "algebra", + "subroutines", + "ndarray", + "vector", + "array", + "multidimensional", + "add", + "constant", + "plus", + "increment", + "augment", + "float32", + "single", + "float32array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js new file mode 100644 index 000000000000..8e744ca0aacf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js @@ -0,0 +1,145 @@ +/** +* @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 Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var sapx = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function adds a scalar constant to each element in a single-precision floating-point ndarray', function test( t ) { + var expected; + var xbuf; + var x; + + xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float32Array( [ 6.0, 7.0, 8.0, 9.0 ] ); + + sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + y = sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ -1 ], 3, 'row-major' ); + + expected = new Float32Array( [ 6.0, 7.0, 8.0, 9.0 ] ); + + sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); + x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); + + expected = new Float32Array( [ 1.0, 2.0, 8.0, 9.0, 10.0 ] ); + + sapx( 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 Float32Array( [] ); + x = new ndarray( 'float32', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); + + expected = new Float32Array( [] ); + + sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + + sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + expected = new Float32Array( [ -4.0, -3.0, -2.0, -1.0 ] ); + + sapx( x, -5.0 ); + + t.deepEqual( x.data, expected, 'returns expected value' ); + t.end(); +}); From 051ebdc9b6a6652a060b269896ca0cd3c1db03c1 Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Fri, 19 Dec 2025 18:18:51 +0530 Subject: [PATCH 2/4] docs: update return annotations to use ndarray instance notation --- .../@stdlib/blas/ext/base/ndarray/sapx/README.md | 12 ++++-------- .../@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt | 4 +--- .../blas/ext/base/ndarray/sapx/docs/types/index.d.ts | 6 ++---- .../@stdlib/blas/ext/base/ndarray/sapx/lib/index.js | 6 ++---- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md index dd3f777cd6da..1f2474b8cefb 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md @@ -41,10 +41,8 @@ var ndarray = require( '@stdlib/ndarray/ctor' ); var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); -sapx( x, 5.0 ); - -var y = x.data; -// returns [ 6.0, 7.0, 8.0, 9.0 ] +var out = sapx( x, 5.0 ); +// returns [ 6.0, 7.0, 8.0, 9.0 ] ``` The function has the following parameters: @@ -64,10 +62,8 @@ var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); // Create an ndarray view: var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); -sapx( x, 5.0 ); - -var y = x.data; -// returns [ 1.0, 2.0, 8.0, 9.0, 10.0 ] +var out = sapx( x, 5.0 ); +// returns [ 8.0, 9.0, 10.0 ] ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt index 2aeb3a4090ab..10084fb05323 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt @@ -20,9 +20,7 @@ > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0 ] ); > var x = {{alias:@stdlib/ndarray/ctor}}( 'float32', 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/sapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts index 8ddf86ac342d..b1358072c89e 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts @@ -36,10 +36,8 @@ import { float32ndarray } from '@stdlib/types/ndarray'; * var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* sapx( x, 5.0 ); -* -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = sapx( x, 5.0 ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ declare function sapx( x: float32ndarray, alpha: number ): float32ndarray; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js index 5c0f5d961a1c..2dec432194c2 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js @@ -31,10 +31,8 @@ * var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* sapx( x, 5.0 ); -* -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = sapx( x, 5.0 ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ // MODULES // From d7c1e9c0f183c6549a19b0b895dd498f85bc160b Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Sun, 21 Dec 2025 10:34:04 +0530 Subject: [PATCH 3/4] fix: refactor sapx to use arrays API pattern --- .../blas/ext/base/ndarray/sapx/README.md | 56 ++++++----- .../base/ndarray/sapx/benchmark/benchmark.js | 18 +++- .../blas/ext/base/ndarray/sapx/docs/repl.txt | 22 +++-- .../base/ndarray/sapx/docs/types/index.d.ts | 20 ++-- .../ext/base/ndarray/sapx/docs/types/test.ts | 47 ++++----- .../ext/base/ndarray/sapx/examples/index.js | 17 +++- .../blas/ext/base/ndarray/sapx/lib/index.js | 15 ++- .../blas/ext/base/ndarray/sapx/lib/main.js | 38 ++++---- .../blas/ext/base/ndarray/sapx/package.json | 4 +- .../blas/ext/base/ndarray/sapx/test/test.js | 95 +++++++------------ 10 files changed, 165 insertions(+), 167 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md index 1f2474b8cefb..7ec8353cf06c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/README.md @@ -20,7 +20,7 @@ limitations under the License. # sapx -> Add a scalar constant to each element in a single-precision floating-point ndarray. +> Add a scalar constant to each element in a one-dimensional single-precision floating-point ndarray.
@@ -30,39 +30,51 @@ limitations under the License. var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); ``` -#### sapx( x, alpha ) +#### sapx( arrays ) -Adds a scalar constant to each element in a single-precision floating-point ndarray. +Adds a scalar constant to each element in a one-dimensional single-precision floating-point ndarray. ```javascript var Float32Array = require( '@stdlib/array/float32' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); -var out = sapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); + +var out = sapx( [ x, alpha ] ); // returns [ 6.0, 7.0, 8.0, 9.0 ] + +var bool = ( out === x ); +// returns true + +var arr = ndarray2array( out ); +// 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. +- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a scalar constant. ```javascript var Float32Array = require( '@stdlib/array/float32' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); // Initial array: var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); // Create an ndarray view: var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); - -var out = sapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); +var out = sapx( [ x, alpha ] ); // returns [ 8.0, 9.0, 10.0 ] ``` @@ -70,16 +82,6 @@ var out = sapx( x, 5.0 ); -
- -## Notes - -- The function **mutates** the input ndarray. - -
- - -
## Examples @@ -89,18 +91,22 @@ var out = sapx( 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 sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); var xbuf = discreteUniform( 10, 0, 100, { 'dtype': 'float32' }); var x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); -console.log( x.data ); - -sapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); -console.log( x.data ); +sapx( [ x, alpha ] ); +console.log( ndarray2array( x ) ); ```
@@ -119,8 +125,6 @@ console.log( x.data ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js index 5cb71140a779..e16b92bc95cf 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js @@ -25,10 +25,18 @@ var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); 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 sapx = require( './../lib' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -39,11 +47,13 @@ var sapx = require( './../lib' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { + var alpha; + var xbuf; var x; - x = new ndarray( 'float32', discreteUniform( len, -100, 100, { - 'dtype': 'float32' - }), [ 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,7 +69,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - z = sapx( x, 5.0 ); + z = sapx( [ x, alpha ] ); if ( isnanf( z.data[ i % len ] ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt index 10084fb05323..28189e842f92 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/repl.txt @@ -1,26 +1,28 @@ -{{alias}}( x, alpha ) - Adds a scalar constant to each element in a single-precision floating- - point ndarray. + +{{alias}}( arrays ) + Adds a scalar constant to each element in a one-dimensional single- + precision floating-point ndarray. Parameters ---------- - x: float32ndarray - 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 ------- - out: float32ndarray + out: ndarray Input ndarray. Examples -------- > var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0 ] ); > var x = {{alias:@stdlib/ndarray/ctor}}( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - > {{alias}}( x, 5.0 ) + > var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, { 'dtype': 'float32' } ); + > {{alias}}( [ x, alpha ] ) [ 6.0, 7.0, 8.0, 9.0 ] + > var data = x.data + [ 6.0, 7.0, 8.0, 9.0 ] See Also -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts index b1358072c89e..aa9fe111a570 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/index.d.ts @@ -23,23 +23,31 @@ import { float32ndarray } from '@stdlib/types/ndarray'; /** -* Adds a scalar constant to each element in a single-precision floating-point ndarray. +* Adds a scalar constant to each element in a one-dimensional single-precision floating-point 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 Float32Array = require( '@stdlib/array/float32' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * * var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* var out = sapx( x, 5.0 ); -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var out = sapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ -declare function sapx( x: float32ndarray, alpha: number ): float32ndarray; +declare function sapx( arrays: [ float32ndarray, float32ndarray ] ): float32ndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts index dc65c070e6f0..aa3243ad1298 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/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 sapx = require( './index' ); @@ -23,40 +25,29 @@ import sapx = require( './index' ); // The function returns an ndarray... { - const x: any = null; - sapx( x, 5.0 ); // $ExpectType float32ndarray + const x = zeros( 'float32', [ 3 ], 'row-major' ); + const alpha = scalar2ndarray( 5.0, { 'dtype': 'float32' } ); + sapx( [ x, alpha ] ); // $ExpectType float32ndarray } -// 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... { - sapx( 123, 5.0 ); // $ExpectError - sapx( true, 5.0 ); // $ExpectError - sapx( false, 5.0 ); // $ExpectError - sapx( null, 5.0 ); // $ExpectError - sapx( undefined, 5.0 ); // $ExpectError - sapx( '5', 5.0 ); // $ExpectError - sapx( [ '1', '2' ], 5.0 ); // $ExpectError - sapx( {}, 5.0 ); // $ExpectError - sapx( ( 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; - sapx( x, '5' ); // $ExpectError - sapx( x, true ); // $ExpectError - sapx( x, false ); // $ExpectError - sapx( x, null ); // $ExpectError - sapx( x, undefined ); // $ExpectError - sapx( x, [ '1' ] ); // $ExpectError - sapx( x, {} ); // $ExpectError - sapx( x, ( x: number ): number => x ); // $ExpectError + const alpha = scalar2ndarray( 5.0, { 'dtype': 'float32' } ); + sapx( 123 ); // $ExpectError + sapx( true ); // $ExpectError + sapx( false ); // $ExpectError + sapx( null ); // $ExpectError + sapx( undefined ); // $ExpectError + sapx( '5' ); // $ExpectError + sapx( [ '1', '2' ] ); // $ExpectError + sapx( {} ); // $ExpectError + sapx( ( 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( 'float32', [ 3 ], 'row-major' ); + const alpha = scalar2ndarray( 5.0, { 'dtype': 'float32' } ); sapx(); // $ExpectError - sapx( x ); // $ExpectError - sapx( x, 5.0, 10 ); // $ExpectError + sapx( [ x, alpha ], 10 ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js index 87935dcf91ff..ea66e789d4dc 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/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 sapx = require( './../lib' ); + +// MAIN // + var xbuf = discreteUniform( 10, 0, 100, { 'dtype': 'float32' }); var x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); -console.log( x.data ); - -sapx( x, 5.0 ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); -console.log( x.data ); +sapx( [ x, alpha ] ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js index 2dec432194c2..ea79e4105f53 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/index.js @@ -19,20 +19,29 @@ 'use strict'; /** -* Add a scalar constant to each element in a single-precision floating-point ndarray. +* Add a scalar constant to each element in a one-dimensional single-precision floating-point ndarray. * * @module @stdlib/blas/ext/base/ndarray/sapx * * @example * var Float32Array = require( '@stdlib/array/float32' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' ); * * var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* var out = sapx( x, 5.0 ); -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var out = sapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ // MODULES // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js index 5d95516d86df..00e42a949723 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/lib/main.js @@ -25,44 +25,40 @@ var strided = require( '@stdlib/blas/ext/base/sapx' ).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 a single-precision floating-point ndarray. +* Adds a scalar constant to each element in a one-dimensional single-precision floating-point ndarray. * -* @param {float32ndarray} x - input ndarray -* @param {number} alpha - scalar constant -* @returns {float32ndarray} input ndarray +* @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 Float32Array = require( '@stdlib/array/float32' ); * var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); * * var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); * var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); * -* sapx( x, 5.0 ); +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); * -* var y = x.data; -* // returns [ 6.0, 7.0, 8.0, 9.0 ] +* var out = sapx( [ x, alpha ] ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 6.0, 7.0, 8.0, 9.0 ] */ -function sapx( 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 sapx( 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/sapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json index feca46b066e0..f4554b531c7d 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/ext/base/ndarray/sapx", "version": "0.0.0", - "description": "Add a scalar constant to each element in a single-precision floating-point ndarray.", + "description": "Add a scalar constant to each element in a one-dimensional single-precision floating-point ndarray.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -64,10 +64,8 @@ "array", "multidimensional", "add", - "constant", "plus", "increment", - "augment", "float32", "single", "float32array" diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js index 8e744ca0aacf..164a01d2a66b 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/test/test.js @@ -23,6 +23,8 @@ var tape = require( 'tape' ); var Float32Array = require( '@stdlib/array/float32' ); var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); var sapx = require( './../lib' ); @@ -34,112 +36,81 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function adds a scalar constant to each element in a single-precision floating-point ndarray', 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - expected = new Float32Array( [ 6.0, 7.0, 8.0, 9.0 ] ); - - sapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + sapx( [ 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - y = sapx( x, 5.0 ); - + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + y = sapx( [ 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); x = new ndarray( 'float32', xbuf, [ 4 ], [ -1 ], 3, 'row-major' ); - expected = new Float32Array( [ 6.0, 7.0, 8.0, 9.0 ] ); - - sapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + sapx( [ 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' ); - expected = new Float32Array( [ 1.0, 2.0, 8.0, 9.0, 10.0 ] ); - - sapx( x, 5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + sapx( [ 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 Float32Array( [] ); x = new ndarray( 'float32', xbuf, [ 0 ], [ 1 ], 0, 'row-major' ); - expected = new Float32Array( [] ); - - sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - - sapx( 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 Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); - x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); - - expected = new Float32Array( [ -4.0, -3.0, -2.0, -1.0 ] ); - - sapx( x, -5.0 ); - - t.deepEqual( x.data, expected, 'returns expected value' ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + sapx( [ x, alpha ] ); + t.deepEqual( getData( x ), expected, 'returns expected value' ); t.end(); }); From 0fb51129e3c4914335c9774e478238559211a9ea Mon Sep 17 00:00:00 2001 From: Aman Singh Date: Mon, 22 Dec 2025 08:11:56 +0530 Subject: [PATCH 4/4] fix: sapx benchmark - use @stdlib/string/format per RFC #8647 --- .../@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js index e16b92bc95cf..570421fbb449 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sapx/benchmark/benchmark.js @@ -26,6 +26,7 @@ var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); 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 sapx = require( './../lib' ); @@ -104,7 +105,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 ); } }