From 3980c4e2f1154af70fdb34cff4cc19b7873ff9a6 Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Sat, 23 May 2026 20:59:49 -0700 Subject: [PATCH 1/2] feat: add `blas/ext/base/ndarray/saxpb` --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../blas/ext/base/ndarray/saxpb/README.md | 112 ++++++++++ .../base/ndarray/saxpb/benchmark/benchmark.js | 95 +++++++++ .../blas/ext/base/ndarray/saxpb/docs/repl.txt | 34 +++ .../base/ndarray/saxpb/docs/types/index.d.ts | 53 +++++ .../ext/base/ndarray/saxpb/docs/types/test.ts | 77 +++++++ .../ext/base/ndarray/saxpb/examples/index.js | 32 +++ .../blas/ext/base/ndarray/saxpb/lib/index.js | 44 ++++ .../blas/ext/base/ndarray/saxpb/lib/main.js | 64 ++++++ .../blas/ext/base/ndarray/saxpb/package.json | 75 +++++++ .../blas/ext/base/ndarray/saxpb/test/test.js | 201 ++++++++++++++++++ 10 files changed, 787 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md new file mode 100644 index 000000000000..031424fa6608 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md @@ -0,0 +1,112 @@ + + +# saxpb + +> Multiply each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and add a scalar constant to each result. + +
+ +
+ + + +
+ +## Usage + +```javascript +var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); +``` + +#### saxpb( alpha, beta, arrays ) + +Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant `alpha` and adds a scalar constant `beta` to each result. + +```javascript +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); + +var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); +// returns [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] + +saxpb( 5.0, 3.0, [ x ] ); +// x => [ -7.0, 8.0, 18.0, -22.0, 23.0, 3.0, -2.0, -12.0 ] +``` + +The function has the following parameters: + +- **alpha**: first scalar constant. +- **beta**: second scalar constant. +- **arrays**: array-like object containing a one-dimensional input ndarray. + +
+ + + +
+ +## Notes + +- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**). + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); + +var opts = { + 'dtype': 'float32' +}; +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +saxpb( 5.0, 3.0, [ x ] ); +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js new file mode 100644 index 000000000000..b2047302353b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var saxpb = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = new Float32Vector( len ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var out; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = saxpb( 5.0, 3.0, [ x ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt new file mode 100644 index 000000000000..dea6c8a5f274 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( alpha, beta, arrays ) + Multiplies each element in a one-dimensional single-precision floating-point + ndarray by a scalar constant and adds a scalar constant to each result. + + The input ndarray is modified *in-place* (i.e., the input ndarray is + *mutated*). + + Parameters + ---------- + alpha: number + First scalar constant. + + beta: number + Second scalar constant. + + arrays: ArrayLikeObject + Array-like object containing a one-dimensional input ndarray. + + Returns + ------- + out: ndarray + Input ndarray. + + Examples + -------- + > var buf = [ -2.0, 1.0, 3.0, -5.0 ]; + > var x = new {{alias:@stdlib/ndarray/vector/float32}}( buf ); + > {{alias}}( 5.0, 3.0, [ x ] ) + [ -7.0, 8.0, 18.0, -22.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts new file mode 100644 index 000000000000..9c9bc33a1902 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts @@ -0,0 +1,53 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* +* @param alpha - first scalar constant +* @param beta - second scalar constant +* @param arrays - array-like object containing a one-dimensional input ndarray +* @returns input ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* // returns [ -2.0, 1.0, 3.0, -5.0 ] +* +* var out = saxpb( 5.0, 3.0, [ x ] ); +* // returns [ -7.0, 8.0, 18.0, -22.0 ] +*/ +declare function saxpb( alpha: number, beta: number, arrays: [ float32ndarray ] ): float32ndarray; + + +// EXPORTS // + +export = saxpb; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts new file mode 100644 index 000000000000..28c19cbf070c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts @@ -0,0 +1,77 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +import saxpb = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); + saxpb( 5.0, 3.0, [ x ] ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); + saxpb( '5', 3.0, [ x ] ); // $ExpectError + saxpb( true, 3.0, [ x ] ); // $ExpectError + saxpb( false, 3.0, [ x ] ); // $ExpectError + saxpb( null, 3.0, [ x ] ); // $ExpectError + saxpb( undefined, 3.0, [ x ] ); // $ExpectError + saxpb( [], 3.0, [ x ] ); // $ExpectError + saxpb( {}, 3.0, [ x ] ); // $ExpectError + saxpb( ( x: number ): number => x, 3.0, [ x ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); + saxpb( 5.0, '3', [ x ] ); // $ExpectError + saxpb( 5.0, true, [ x ] ); // $ExpectError + saxpb( 5.0, false, [ x ] ); // $ExpectError + saxpb( 5.0, null, [ x ] ); // $ExpectError + saxpb( 5.0, undefined, [ x ] ); // $ExpectError + saxpb( 5.0, [], [ x ] ); // $ExpectError + saxpb( 5.0, {}, [ x ] ); // $ExpectError + saxpb( 5.0, ( x: number ): number => x, [ x ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an array of ndarrays... +{ + saxpb( 5.0, 3.0, '10' ); // $ExpectError + saxpb( 5.0, 3.0, 5 ); // $ExpectError + saxpb( 5.0, 3.0, true ); // $ExpectError + saxpb( 5.0, 3.0, false ); // $ExpectError + saxpb( 5.0, 3.0, null ); // $ExpectError + saxpb( 5.0, 3.0, undefined ); // $ExpectError + saxpb( 5.0, 3.0, [] ); // $ExpectError + saxpb( 5.0, 3.0, {} ); // $ExpectError + saxpb( 5.0, 3.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); + saxpb(); // $ExpectError + saxpb( 5.0 ); // $ExpectError + saxpb( 5.0, 3.0 ); // $ExpectError + saxpb( 5.0, 3.0, [ x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js new file mode 100644 index 000000000000..391481db15c0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js @@ -0,0 +1,32 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var saxpb = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +saxpb( 5.0, 3.0, [ x ] ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js new file mode 100644 index 000000000000..48b55948afba --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Multiply each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and add a scalar constant to each result. +* +* @module @stdlib/blas/ext/base/ndarray/saxpb +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* // returns [ -2.0, 1.0, 3.0, -5.0 ] +* +* var out = saxpb( 5.0, 3.0, [ x ] ); +* // returns [ -7.0, 8.0, 18.0, -22.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js new file mode 100644 index 000000000000..8aebccdc8ac9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/saxpb' ).ndarray; + + +// MAIN // + +/** +* Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* +* @param {number} alpha - first scalar constant +* @param {number} beta - second scalar constant +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} input ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* +* var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); +* // returns [ -2.0, 1.0, 3.0, -5.0 ] +* +* var out = saxpb( 5.0, 3.0, [ x ] ); +* // returns [ -7.0, 8.0, 18.0, -22.0 ] +*/ +function saxpb( alpha, beta, arrays ) { + var x = arrays[ 0 ]; + strided( numelDimension( x, 0 ), alpha, beta, getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len + return x; +} + + +// EXPORTS // + +module.exports = saxpb; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/package.json new file mode 100644 index 000000000000..8a3ba6fa6a37 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/package.json @@ -0,0 +1,75 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/saxpb", + "version": "0.0.0", + "description": "Multiply each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and add a scalar constant to each result.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "ext", + "saxpb", + "axpb", + "multiply", + "add", + "scalar", + "constant", + "linear", + "combination", + "strided", + "array", + "ndarray", + "float32", + "single", + "single-precision" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js new file mode 100644 index 000000000000..586d6dd6bcdb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js @@ -0,0 +1,201 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float32Array = require( '@stdlib/array/float32' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var saxpb = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof saxpb, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 3', function test( t ) { + t.strictEqual( saxpb.length, 3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function multiplies each element by a scalar constant and adds a scalar constant', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); + x = vector( xbuf, 5, 1, 0 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ -7.0, 8.0, 18.0, -22.0, 23.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function adds `beta` to each element when `alpha` equals `1.0`', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); + x = vector( xbuf, 5, 1, 0 ); + + actual = saxpb( 1.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 4.0, 6.0, -2.0, 7.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function multiplies each element by `alpha` when `beta` equals `0.0`', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); + x = vector( xbuf, 5, 1, 0 ); + + actual = saxpb( 5.0, 0.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ -10.0, 5.0, 15.0, -25.0, 20.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); + x = vector( xbuf, 4, 2, 0 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ -7.0, 1.0, 18.0, -5.0, 23.0, 0.0, -2.0, -3.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); + x = vector( xbuf, 5, -1, 4 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ -7.0, 8.0, 18.0, -22.0, 23.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -5.0, 4.0, 0.0 ] ); + x = vector( xbuf, 3, 1, 2 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, -2.0, 18.0, -22.0, 23.0, 0.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a negative stride and non-zero offset', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, 2.0, 6.0 ] ); + x = vector( xbuf, 5, -2, 9 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, -7.0, 3.0, -22.0, 4.0, 3.0, -1.0, -12.0, 2.0, 33.0 ] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the input ndarray unchanged when the input ndarray is empty', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float32Array( [] ); + x = vector( xbuf, 0, 1, 0 ); + + actual = saxpb( 5.0, 3.0, [ x ] ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = new Float32Array( [] ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); +}); From e2b82b55e7ebf043684eac3136400c8b7745762b Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Thu, 28 May 2026 22:53:09 -0700 Subject: [PATCH 2/2] chore: apply suggestions from review --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../blas/ext/base/ndarray/saxpb/README.md | 35 +++-- .../base/ndarray/saxpb/benchmark/benchmark.js | 20 ++- .../blas/ext/base/ndarray/saxpb/docs/repl.txt | 19 +-- .../base/ndarray/saxpb/docs/types/index.d.ts | 22 +++- .../ext/base/ndarray/saxpb/docs/types/test.ts | 77 +++++------ .../ext/base/ndarray/saxpb/examples/index.js | 11 +- .../blas/ext/base/ndarray/saxpb/lib/index.js | 12 +- .../blas/ext/base/ndarray/saxpb/lib/main.js | 27 +++- .../blas/ext/base/ndarray/saxpb/test/test.js | 120 ++++++++---------- 9 files changed, 196 insertions(+), 147 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md index 031424fa6608..ef043614b68f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/README.md @@ -36,25 +36,35 @@ limitations under the License. var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); ``` -#### saxpb( alpha, beta, arrays ) +#### saxpb( arrays ) -Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant `alpha` and adds a scalar constant `beta` to each result. +Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result. ```javascript var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); -// returns [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] -saxpb( 5.0, 3.0, [ x ] ); +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' +}); + +var beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' +}); + +saxpb( [ x, alpha, beta ] ); // x => [ -7.0, 8.0, 18.0, -22.0, 23.0, 3.0, -2.0, -12.0 ] ``` The function has the following parameters: -- **alpha**: first scalar constant. -- **beta**: second scalar constant. -- **arrays**: array-like object containing a one-dimensional input ndarray. +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the scalar constant to multiply. + - a zero-dimensional ndarray containing the scalar constant to add. @@ -78,16 +88,25 @@ The function has the following parameters: ```javascript var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); var opts = { 'dtype': 'float32' }; + var x = discreteUniform( [ 10 ], -100, 100, opts ); console.log( ndarray2array( x ) ); -saxpb( 5.0, 3.0, [ x ] ); +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +var beta = scalar2ndarray( 3.0, opts ); +console.log( 'Beta: %d', ndarraylike2scalar( beta ) ); + +saxpb( [ x, alpha, beta ] ); console.log( ndarray2array( x ) ); ``` diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js index b2047302353b..9ba2bdb1f3ca 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/benchmark/benchmark.js @@ -21,13 +21,21 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +var uniform = require( '@stdlib/random/uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var pow = require( '@stdlib/math/base/special/pow' ); var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var saxpb = require( './../lib' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -38,7 +46,13 @@ var saxpb = require( './../lib' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x = new Float32Vector( len ); + var alpha; + var beta; + var x; + + x = uniform( [ len ], -100.0, 100.0, options ); + alpha = scalar2ndarray( 5.0, options ); + beta = scalar2ndarray( 3.0, options ); return benchmark; /** @@ -53,7 +67,7 @@ function createBenchmark( len ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - out = saxpb( 5.0, 3.0, [ x ] ); + out = saxpb( [ x, alpha, beta ] ); if ( typeof out !== 'object' ) { b.fail( 'should return an ndarray' ); } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt index dea6c8a5f274..86fdce94aa24 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/repl.txt @@ -1,5 +1,5 @@ -{{alias}}( alpha, beta, arrays ) +{{alias}}( arrays ) Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result. @@ -8,14 +8,12 @@ Parameters ---------- - alpha: number - First scalar constant. - - beta: number - Second scalar constant. - arrays: ArrayLikeObject - Array-like object containing a one-dimensional input ndarray. + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the scalar constant to multiply. + - a zero-dimensional ndarray containing the scalar constant to add. Returns ------- @@ -26,7 +24,10 @@ -------- > var buf = [ -2.0, 1.0, 3.0, -5.0 ]; > var x = new {{alias:@stdlib/ndarray/vector/float32}}( buf ); - > {{alias}}( 5.0, 3.0, [ x ] ) + > var opts = { 'dtype': 'float32' }; + > var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts ); + > var beta = {{alias:@stdlib/ndarray/from-scalar}}( 3.0, opts ); + > {{alias}}( [ x, alpha, beta ] ) [ -7.0, 8.0, 18.0, -22.0 ] See Also diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts index 9c9bc33a1902..a659702b1aba 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { float32ndarray } from '@stdlib/types/ndarray'; +import { float32ndarray, typedndarray } from '@stdlib/types/ndarray'; /** * Multiplies each element in a one-dimensional single-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result. @@ -30,22 +30,30 @@ import { float32ndarray } from '@stdlib/types/ndarray'; * - The function expects the following ndarrays: * * - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the scalar constant to multiply. +* - a zero-dimensional ndarray containing the scalar constant to add. * -* @param alpha - first scalar constant -* @param beta - second scalar constant -* @param arrays - array-like object containing a one-dimensional input ndarray +* @param arrays - array-like object containing ndarrays * @returns input ndarray * * @example * var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * * var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); -* // returns [ -2.0, 1.0, 3.0, -5.0 ] * -* var out = saxpb( 5.0, 3.0, [ x ] ); +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var beta = scalar2ndarray( 3.0, { +* 'dtype': 'float32' +* }); +* +* var out = saxpb( [ x, alpha, beta ] ); * // returns [ -7.0, 8.0, 18.0, -22.0 ] */ -declare function saxpb( alpha: number, beta: number, arrays: [ float32ndarray ] ): float32ndarray; +declare function saxpb( arrays: [ float32ndarray, typedndarray, typedndarray ] ): float32ndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts index 28c19cbf070c..60173a2d9217 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/docs/types/test.ts @@ -16,7 +16,10 @@ * limitations under the License. */ -import Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); import saxpb = require( './index' ); @@ -24,54 +27,44 @@ import saxpb = require( './index' ); // The function returns an ndarray... { - const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); - saxpb( 5.0, 3.0, [ x ] ); // $ExpectType float32ndarray -} + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + const beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); -// The compiler throws an error if the function is provided a first argument which is not a number... -{ - const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); - saxpb( '5', 3.0, [ x ] ); // $ExpectError - saxpb( true, 3.0, [ x ] ); // $ExpectError - saxpb( false, 3.0, [ x ] ); // $ExpectError - saxpb( null, 3.0, [ x ] ); // $ExpectError - saxpb( undefined, 3.0, [ x ] ); // $ExpectError - saxpb( [], 3.0, [ x ] ); // $ExpectError - saxpb( {}, 3.0, [ x ] ); // $ExpectError - saxpb( ( x: number ): number => x, 3.0, [ x ] ); // $ExpectError + saxpb( [ x, alpha, beta ] ); // $ExpectType float32ndarray } -// The compiler throws an error if the function is provided a second argument which is not a number... +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... { - const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); - saxpb( 5.0, '3', [ x ] ); // $ExpectError - saxpb( 5.0, true, [ x ] ); // $ExpectError - saxpb( 5.0, false, [ x ] ); // $ExpectError - saxpb( 5.0, null, [ x ] ); // $ExpectError - saxpb( 5.0, undefined, [ x ] ); // $ExpectError - saxpb( 5.0, [], [ x ] ); // $ExpectError - saxpb( 5.0, {}, [ x ] ); // $ExpectError - saxpb( 5.0, ( x: number ): number => x, [ x ] ); // $ExpectError -} - -// The compiler throws an error if the function is provided a third argument which is not an array of ndarrays... -{ - saxpb( 5.0, 3.0, '10' ); // $ExpectError - saxpb( 5.0, 3.0, 5 ); // $ExpectError - saxpb( 5.0, 3.0, true ); // $ExpectError - saxpb( 5.0, 3.0, false ); // $ExpectError - saxpb( 5.0, 3.0, null ); // $ExpectError - saxpb( 5.0, 3.0, undefined ); // $ExpectError - saxpb( 5.0, 3.0, [] ); // $ExpectError - saxpb( 5.0, 3.0, {} ); // $ExpectError - saxpb( 5.0, 3.0, ( x: number ): number => x ); // $ExpectError + saxpb( '10' ); // $ExpectError + saxpb( 5 ); // $ExpectError + saxpb( true ); // $ExpectError + saxpb( false ); // $ExpectError + saxpb( null ); // $ExpectError + saxpb( undefined ); // $ExpectError + saxpb( [] ); // $ExpectError + saxpb( {} ); // $ExpectError + saxpb( ( x: number ): number => x ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... { - const x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + const beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + saxpb(); // $ExpectError - saxpb( 5.0 ); // $ExpectError - saxpb( 5.0, 3.0 ); // $ExpectError - saxpb( 5.0, 3.0, [ x ], {} ); // $ExpectError + saxpb( [ x, alpha, beta ], {} ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js index 391481db15c0..e0b972de37af 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/examples/index.js @@ -19,14 +19,23 @@ 'use strict'; var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var saxpb = require( './../lib' ); var opts = { 'dtype': 'float32' }; + var x = discreteUniform( [ 10 ], -100, 100, opts ); console.log( ndarray2array( x ) ); -saxpb( 5.0, 3.0, [ x ] ); +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +var beta = scalar2ndarray( 3.0, opts ); +console.log( 'Beta: %d', ndarraylike2scalar( beta ) ); + +saxpb( [ x, alpha, beta ] ); console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js index 48b55948afba..654cc49eb543 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/index.js @@ -25,12 +25,20 @@ * * @example * var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * var saxpb = require( '@stdlib/blas/ext/base/ndarray/saxpb' ); * * var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); -* // returns [ -2.0, 1.0, 3.0, -5.0 ] * -* var out = saxpb( 5.0, 3.0, [ x ] ); +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var beta = scalar2ndarray( 3.0, { +* 'dtype': 'float32' +* }); +* +* var out = saxpb( [ x, alpha, beta ] ); * // returns [ -7.0, 8.0, 18.0, -22.0 ] */ diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js index 8aebccdc8ac9..6b7934bb9bf5 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/lib/main.js @@ -25,6 +25,7 @@ var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); var strided = require( '@stdlib/blas/ext/base/saxpb' ).ndarray; +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); // MAIN // @@ -37,23 +38,37 @@ var strided = require( '@stdlib/blas/ext/base/saxpb' ).ndarray; * - The function expects the following ndarrays: * * - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the scalar constant to multiply. +* - a zero-dimensional ndarray containing the scalar constant to add. * -* @param {number} alpha - first scalar constant -* @param {number} beta - second scalar constant * @param {ArrayLikeObject} arrays - array-like object containing ndarrays * @returns {ndarray} input ndarray * * @example * var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); * * var x = new Float32Vector( [ -2.0, 1.0, 3.0, -5.0 ] ); -* // returns [ -2.0, 1.0, 3.0, -5.0 ] * -* var out = saxpb( 5.0, 3.0, [ x ] ); +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'float32' +* }); +* +* var beta = scalar2ndarray( 3.0, { +* 'dtype': 'float32' +* }); +* +* var out = saxpb( [ x, alpha, beta ] ); * // returns [ -7.0, 8.0, 18.0, -22.0 ] */ -function saxpb( alpha, beta, arrays ) { - var x = arrays[ 0 ]; +function saxpb( arrays ) { + var alpha; + var beta; + var x; + + x = arrays[ 0 ]; + alpha = ndarraylike2scalar( arrays[ 1 ] ); + beta = ndarraylike2scalar( arrays[ 2 ] ); strided( numelDimension( x, 0 ), alpha, beta, 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/saxpb/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js index 586d6dd6bcdb..f3b3132bfdb6 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/saxpb/test/test.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Float32Array = require( '@stdlib/array/float32' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); var ndarray = require( '@stdlib/ndarray/base/ctor' ); var saxpb = require( './../lib' ); @@ -51,21 +52,24 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function has an arity of 3', function test( t ) { - t.strictEqual( saxpb.length, 3, 'returns expected value' ); - t.end(); -}); - tape( 'the function multiplies each element by a scalar constant and adds a scalar constant', function test( t ) { var expected; var actual; + var alpha; + var beta; var xbuf; var x; xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); x = vector( xbuf, 5, 1, 0 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + + actual = saxpb( [ x, alpha, beta ] ); t.strictEqual( actual, x, 'returns expected value' ); expected = new Float32Array( [ -7.0, 8.0, 18.0, -22.0, 23.0 ] ); @@ -74,52 +78,24 @@ tape( 'the function multiplies each element by a scalar constant and adds a scal t.end(); }); -tape( 'the function adds `beta` to each element when `alpha` equals `1.0`', function test( t ) { - var expected; - var actual; - var xbuf; - var x; - - xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); - x = vector( xbuf, 5, 1, 0 ); - - actual = saxpb( 1.0, 3.0, [ x ] ); - t.strictEqual( actual, x, 'returns expected value' ); - - expected = new Float32Array( [ 1.0, 4.0, 6.0, -2.0, 7.0 ] ); - t.deepEqual( xbuf, expected, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function multiplies each element by `alpha` when `beta` equals `0.0`', function test( t ) { - var expected; - var actual; - var xbuf; - var x; - - xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); - x = vector( xbuf, 5, 1, 0 ); - - actual = saxpb( 5.0, 0.0, [ x ] ); - t.strictEqual( actual, x, 'returns expected value' ); - - expected = new Float32Array( [ -10.0, 5.0, 15.0, -25.0, 20.0 ] ); - t.deepEqual( xbuf, expected, 'returns expected value' ); - - t.end(); -}); - tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { var expected; var actual; + var alpha; + var beta; var xbuf; var x; xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] ); x = vector( xbuf, 4, 2, 0 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + + actual = saxpb( [ x, alpha, beta ] ); t.strictEqual( actual, x, 'returns expected value' ); expected = new Float32Array( [ -7.0, 1.0, 18.0, -5.0, 23.0, 0.0, -2.0, -3.0 ] ); @@ -131,13 +107,21 @@ tape( 'the function supports an input ndarray having a non-unit stride', functio tape( 'the function supports an input ndarray having a negative stride', function test( t ) { var expected; var actual; + var alpha; + var beta; var xbuf; var x; xbuf = new Float32Array( [ -2.0, 1.0, 3.0, -5.0, 4.0 ] ); x = vector( xbuf, 5, -1, 4 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + + actual = saxpb( [ x, alpha, beta ] ); t.strictEqual( actual, x, 'returns expected value' ); expected = new Float32Array( [ -7.0, 8.0, 18.0, -22.0, 23.0 ] ); @@ -149,13 +133,21 @@ tape( 'the function supports an input ndarray having a negative stride', functio tape( 'the function supports an input ndarray having a non-zero offset', function test( t ) { var expected; var actual; + var alpha; + var beta; var xbuf; var x; xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -5.0, 4.0, 0.0 ] ); x = vector( xbuf, 3, 1, 2 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + + actual = saxpb( [ x, alpha, beta ] ); t.strictEqual( actual, x, 'returns expected value' ); expected = new Float32Array( [ 1.0, -2.0, 18.0, -22.0, 23.0, 0.0 ] ); @@ -164,34 +156,24 @@ tape( 'the function supports an input ndarray having a non-zero offset', functio t.end(); }); -tape( 'the function supports an input ndarray having a negative stride and non-zero offset', function test( t ) { - var expected; - var actual; - var xbuf; - var x; - - xbuf = new Float32Array( [ 1.0, -2.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0, 2.0, 6.0 ] ); - x = vector( xbuf, 5, -2, 9 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); - t.strictEqual( actual, x, 'returns expected value' ); - - expected = new Float32Array( [ 1.0, -7.0, 3.0, -22.0, 4.0, 3.0, -1.0, -12.0, 2.0, 33.0 ] ); - t.deepEqual( xbuf, expected, 'returns expected value' ); - - t.end(); -}); - tape( 'the function returns the input ndarray unchanged when the input ndarray is empty', function test( t ) { var expected; var actual; + var alpha; + var beta; var xbuf; var x; xbuf = new Float32Array( [] ); x = vector( xbuf, 0, 1, 0 ); - - actual = saxpb( 5.0, 3.0, [ x ] ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'float32' + }); + beta = scalar2ndarray( 3.0, { + 'dtype': 'float32' + }); + + actual = saxpb( [ x, alpha, beta ] ); t.strictEqual( actual, x, 'returns expected value' ); expected = new Float32Array( [] );