From 5905daaba81fea2fe41021f9b5895b457c12d9ea Mon Sep 17 00:00:00 2001 From: Karan Anand Date: Wed, 30 Sep 2026 11:32:13 -0700 Subject: [PATCH] feat: add `blas/ext/base/ndarray/gwapx` --- 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/gwapx/README.md | 124 ++++++ .../base/ndarray/gwapx/benchmark/benchmark.js | 110 +++++ .../blas/ext/base/ndarray/gwapx/docs/repl.txt | 33 ++ .../base/ndarray/gwapx/docs/types/index.d.ts | 58 +++ .../ext/base/ndarray/gwapx/docs/types/test.ts | 70 ++++ .../ext/base/ndarray/gwapx/examples/index.js | 41 ++ .../blas/ext/base/ndarray/gwapx/lib/index.js | 49 +++ .../blas/ext/base/ndarray/gwapx/lib/main.js | 79 ++++ .../blas/ext/base/ndarray/gwapx/package.json | 70 ++++ .../blas/ext/base/ndarray/gwapx/test/test.js | 394 ++++++++++++++++++ 10 files changed, 1028 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/README.md new file mode 100644 index 000000000000..2f8ab854611f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/README.md @@ -0,0 +1,124 @@ + + +# gwapx + +> Add a scalar constant to each element in an input one-dimensional ndarray and assign the results to elements in a one-dimensional output ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var gwapx = require( '@stdlib/blas/ext/base/ndarray/gwapx' ); +``` + +#### gwapx( arrays ) + +Adds a scalar constant to each element in an input one-dimensional ndarray and assigns the results to elements in a one-dimensional output ndarray. + +```javascript +var vector = require( '@stdlib/ndarray/vector/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + +var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ], 'generic' ); +var w = vector( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], 'generic' ); + +var alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' +}); + +gwapx( [ x, w, alpha ] ); +// w => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. + - a zero-dimensional ndarray containing the scalar constant to add. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```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/ndarraylike2scalar' ); +var gwapx = require( '@stdlib/blas/ext/base/ndarray/gwapx' ); + +var opts = { + 'dtype': 'generic' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var w = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( w ) ); + +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +gwapx( [ x, w, alpha ] ); +console.log( ndarray2array( w ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/benchmark/benchmark.js new file mode 100644 index 000000000000..4a7a042e6d29 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/benchmark/benchmark.js @@ -0,0 +1,110 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var 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 gwapx = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var alpha; + var x; + var w; + + x = uniform( [ len ], -100.0, 100.0, options ); + w = zeros( [ len ], options ); + alpha = scalar2ndarray( 5.0, options ); + 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 = gwapx( [ x, w, alpha ] ); + 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/gwapx/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/repl.txt new file mode 100644 index 000000000000..000f480cc811 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( arrays ) + Adds a scalar constant to each element in an input one-dimensional ndarray + and assigns the results to elements in a one-dimensional output ndarray. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional output ndarray. + - a zero-dimensional ndarray containing the scalar constant to add. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var xbuf = [ -2.0, 1.0, 3.0, -5.0 ]; + > var x = {{alias:@stdlib/ndarray/vector/ctor}}( xbuf, 'generic' ); + > var wbuf = [ 0.0, 0.0, 0.0, 0.0 ]; + > var w = {{alias:@stdlib/ndarray/vector/ctor}}( wbuf, 'generic' ); + > var opts = { 'dtype': 'generic' }; + > var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts ); + > {{alias}}( [ x, w, alpha ] ) + [ 3.0, 6.0, 8.0, 0.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/index.d.ts new file mode 100644 index 000000000000..c56fc7dab141 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/index.d.ts @@ -0,0 +1,58 @@ +/* +* @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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Adds a scalar constant to each element in an input one-dimensional ndarray and assigns the results to elements in a one-dimensional output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0 ], 'generic' ); +* var w = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); +* +* var out = gwapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +*/ +declare function gwapx = typedndarray>( arrays: [ typedndarray, T, typedndarray ] ): T; + + +// EXPORTS // + +export = gwapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/test.ts new file mode 100644 index 000000000000..ff10cbd74f2d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/docs/types/test.ts @@ -0,0 +1,70 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import gwapx = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const w = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + gwapx( [ x, w, alpha ] ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + gwapx( '10' ); // $ExpectError + gwapx( 5 ); // $ExpectError + gwapx( true ); // $ExpectError + gwapx( false ); // $ExpectError + gwapx( null ); // $ExpectError + gwapx( undefined ); // $ExpectError + gwapx( [] ); // $ExpectError + gwapx( {} ); // $ExpectError + gwapx( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const w = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + gwapx(); // $ExpectError + gwapx( [ x, w, alpha ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/examples/index.js new file mode 100644 index 000000000000..c74f5377b6aa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/examples/index.js @@ -0,0 +1,41 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' ); +var gwapx = require( './../lib' ); + +var opts = { + 'dtype': 'generic' +}; + +var x = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( x ) ); + +var w = discreteUniform( [ 10 ], -100, 100, opts ); +console.log( ndarray2array( w ) ); + +var alpha = scalar2ndarray( 5.0, opts ); +console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) ); + +gwapx( [ x, w, alpha ] ); +console.log( ndarray2array( w ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/index.js new file mode 100644 index 000000000000..07c10a8f2ae0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/index.js @@ -0,0 +1,49 @@ +/** +* @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'; + +/** +* Add a scalar constant to each element in an input one-dimensional ndarray and assign the results to elements in a one-dimensional output ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/gwapx +* +* @example +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var gwapx = require( '@stdlib/blas/ext/base/ndarray/gwapx' ); +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0 ], 'generic' ); +* var w = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); +* +* var out = gwapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/main.js new file mode 100644 index 000000000000..70536f27def1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/lib/main.js @@ -0,0 +1,79 @@ +/** +* @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 ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/blas/ext/base/gwapx' ).ndarray; + + +// MAIN // + +/** +* Adds a scalar constant to each element in an input one-dimensional ndarray and assigns the results to elements in a one-dimensional output ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional output ndarray. +* - a zero-dimensional ndarray containing the scalar constant to add. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0 ], 'generic' ); +* var w = vector( [ 0.0, 0.0, 0.0, 0.0 ], 'generic' ); +* +* var alpha = scalar2ndarray( 5.0, { +* 'dtype': 'generic' +* }); +* +* var out = gwapx( [ x, w, alpha ] ); +* // returns [ 3.0, 6.0, 8.0, 0.0 ] +* +* var bool = ( out === w ); +* // returns true +*/ +function gwapx( arrays ) { + var alpha; + var x; + var w; + + x = arrays[ 0 ]; + w = arrays[ 1 ]; + alpha = ndarraylike2scalar( arrays[ 2 ] ); + strided( numelDimension( x, 0 ), alpha, getData( x ), getStride( x, 0 ), getOffset( x ), getData( w ), getStride( w, 0 ), getOffset( w ) ); // eslint-disable-line max-len + return w; +} + + +// EXPORTS // + +module.exports = gwapx; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/package.json new file mode 100644 index 000000000000..50d30e24db40 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/gwapx", + "version": "0.0.0", + "description": "Add a scalar constant to each element in an input one-dimensional ndarray and assign the results to elements in a one-dimensional output 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", + "extended", + "ext", + "gwapx", + "wapx", + "add", + "scalar", + "constant", + "strided", + "array", + "ndarray", + "generic" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/test/test.js new file mode 100644 index 000000000000..d119850d3697 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gwapx/test/test.js @@ -0,0 +1,394 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var gwapx = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} 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( 'generic', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gwapx, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( gwapx.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function adds a scalar constant to each element and assigns results to an output ndarray', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]; + wbuf = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]; + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]; + t.deepEqual( wbuf, 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 xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 0 + 1.0, + 3.0, // 1 + -6.0, + 4.0, // 2 + 0.0, + -1.0, // 3 + -3.0 + ]; + wbuf = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 // 3 + ]; + x = vector( xbuf, 4, 2, 0 ); + w = vector( wbuf, 4, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 3.0, // 5.0 + -2.0 + 8.0, // 5.0 + 3.0 + 9.0, // 5.0 + 4.0 + 4.0 // 5.0 + -1.0 + ]; + t.deepEqual( wbuf, 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 alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 4 + 1.0, // 3 + 3.0, // 2 + -6.0, // 1 + 4.0 // 0 + ]; + wbuf = [ + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0, // 3 + 0.0 // 4 + ]; + x = vector( xbuf, 5, -1, 4 ); + w = vector( wbuf, 5, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]; + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0 // 3 + ]; + wbuf = [ + 0.0, // 0 + 0.0, + 0.0, // 1 + 0.0, + 0.0, // 2 + 0.0, + 0.0, // 3 + 0.0 + ]; + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 2, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 3.0, // 5.0 + -2.0 + 0.0, + 6.0, // 5.0 + 1.0 + 0.0, + 8.0, // 5.0 + 3.0 + 0.0, + -1.0, // 5.0 + -6.0 + 0.0 + ]; + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 0 + 1.0, // 1 + 3.0, // 2 + -6.0, // 3 + 4.0 // 4 + ]; + wbuf = [ + 0.0, // 4 + 0.0, // 3 + 0.0, // 2 + 0.0, // 1 + 0.0 // 0 + ]; + x = vector( xbuf, 5, 1, 0 ); + w = vector( wbuf, 5, -1, 4 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 9.0, // 5.0 + 4.0 + -1.0, // 5.0 + -6.0 + 8.0, // 5.0 + 3.0 + 6.0, // 5.0 + 1.0 + 3.0 // 5.0 + -2.0 + ]; + t.deepEqual( wbuf, 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 alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + 1.0, + -2.0, + 3.0, // 0 + -6.0, // 1 + 4.0, // 2 + 0.0 + ]; + wbuf = [ + 0.0, // 0 + 0.0, // 1 + 0.0 // 2 + ]; + x = vector( xbuf, 3, 1, 2 ); + w = vector( wbuf, 3, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 8.0, // 5.0 + 3.0 + -1.0, // 5.0 + -6.0 + 9.0 // 5.0 + 4.0 + ]; + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = [ + -2.0, // 0 + 1.0, // 1 + 3.0 // 2 + ]; + wbuf = [ + 0.0, + 0.0, + 0.0, // 0 + 0.0, // 1 + 0.0, // 2 + 0.0 + ]; + x = vector( xbuf, 3, 1, 0 ); + w = vector( wbuf, 3, 1, 2 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = [ + 0.0, + 0.0, + 3.0, // 5.0 + -2.0 + 6.0, // 5.0 + 1.0 + 8.0, // 5.0 + 3.0 + 0.0 + ]; + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the output ndarray unchanged when the input ndarray is empty', function test( t ) { + var expected; + var actual; + var alpha; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = []; + wbuf = []; + x = vector( xbuf, 0, 1, 0 ); + w = vector( wbuf, 0, 1, 0 ); + alpha = scalar2ndarray( 5.0, { + 'dtype': 'generic' + }); + + actual = gwapx( [ x, w, alpha ] ); + t.strictEqual( actual, w, 'returns expected value' ); + + expected = []; + t.deepEqual( wbuf, expected, 'returns expected value' ); + + t.end(); +});