From 92a67f975a102a9affed4c6fb7563845752fd032 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 15 Dec 2025 14:56:23 +0500 Subject: [PATCH 1/6] feat: add ndarray/push --- 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 status: passed - 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 --- --- .../@stdlib/ndarray/push/README.md | 155 +++++++++++ .../push/benchmark/benchmark.assign.js | 60 ++++ .../ndarray/push/benchmark/benchmark.js | 58 ++++ .../@stdlib/ndarray/push/docs/repl.txt | 60 ++++ .../ndarray/push/docs/types/index.d.ts | 119 ++++++++ .../@stdlib/ndarray/push/docs/types/test.ts | 93 +++++++ .../@stdlib/ndarray/push/examples/index.js | 33 +++ .../@stdlib/ndarray/push/lib/assign.js | 97 +++++++ .../@stdlib/ndarray/push/lib/index.js | 56 ++++ .../@stdlib/ndarray/push/lib/main.js | 80 ++++++ .../@stdlib/ndarray/push/package.json | 69 +++++ .../@stdlib/ndarray/push/test/test.assign.js | 263 ++++++++++++++++++ .../@stdlib/ndarray/push/test/test.js | 39 +++ .../@stdlib/ndarray/push/test/test.main.js | 186 +++++++++++++ 14 files changed, 1368 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/push/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/push/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/push/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/push/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/push/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/test/test.js create mode 100644 lib/node_modules/@stdlib/ndarray/push/test/test.main.js diff --git a/lib/node_modules/@stdlib/ndarray/push/README.md b/lib/node_modules/@stdlib/ndarray/push/README.md new file mode 100644 index 000000000000..818c6af8d55f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/README.md @@ -0,0 +1,155 @@ + + +# push + +> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by appending the provided scalar values to the input [ndarray][@stdlib/ndarray/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var push = require( '@stdlib/ndarray/push' ); +``` + +#### push( x, ...values ) + +Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by appending the provided scalar values to the input [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + +var out = push( x, 5.0, 6.0, 7.0 ); +// returns + +var arr = ndarray2array( out ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +``` + +The function accepts the following arguments: + +- **arr**: input [ndarray][@stdlib/ndarray/ctor]. +- **...values**: scalar values to append. + +#### push.assign( x, ...values, out ) + +Appends the provided scalar values to the input [ndarray][@stdlib/ndarray/ctor] and assigns the result to a provided one-dimensional output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var y = zeros( [ 7 ] ); + +var out = push.assign( x, 5.0, 6.0, 7.0, y ); +// returns + +var bool = ( out === y ); +// returns true + +var arr = ndarray2array( y ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +``` + +The function accepts the following arguments: + +- **arr**: input [ndarray][@stdlib/ndarray/ctor]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **...values**: scalar values to append. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var push = require( '@stdlib/ndarray/push' ); + +var opts = { + 'dtype': 'generic' +}; +var x = array( discreteUniform( 6, 0, 10, opts ), opts ); +console.log( ndarray2array( x ) ); + +var out = push( x, 12.0, 14.0 ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..f289c96e7bac --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js @@ -0,0 +1,60 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var pkg = require( './../package.json' ).name; +var push = require( './../lib/assign.js' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var opts; + var out; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + values = [ 1.0, 2.0, 3.0, 4.0]; + out = zeros( [ 8 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, values, out ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js new file mode 100644 index 000000000000..643289862a76 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js @@ -0,0 +1,58 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var pkg = require( './../package.json' ).name; +var push = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var opts; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + values = [ 1.0, 2.0, 3.0, 4.0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, values ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/push/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/push/docs/repl.txt new file mode 100644 index 000000000000..8272f0576095 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/docs/repl.txt @@ -0,0 +1,60 @@ + +{{alias}}( x, ...values ) + Returns a one-dimensional ndarray formed by appending the provided scalar + values to the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + values: ...any + Scalar values to append. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var out = {{alias}}( x, 5.0, 6.0 ) + + > var arr = {{alias:@stdlib/ndarray/to-array}}( out ) + [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + + +{{alias}}.assign( x, ...values, out ) + Appends the provided scalar values to the input ndarray and assigns the + result to a provided one-dimensional output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + values: ...any + Scalar values to append. + + out: ndarray + Output ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0 ] ); + > var y = {{alias:@stdlib/ndarray/array}}( [ 0.0, 0.0, 0.0, 0.0 ] ); + > var out = {{alias}}.assign( x, 3.0, 4.0, y ) + + > var bool = ( out === y ) + true + > var arr = {{alias:@stdlib/ndarray/to-array}}( out ) + [ 1.0, 2.0, 3.0, 4.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/push/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/push/docs/types/index.d.ts new file mode 100644 index 000000000000..ca8ca11c5615 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/docs/types/index.d.ts @@ -0,0 +1,119 @@ +/* +* @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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Interface describing `push`. +*/ +interface Push { + /** + * Returns a one-dimensional ndarray formed by appending the provided scalar values to the input ndarray. + * + * @param x - input ndarray + * @param values - scalar values + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var ndarray2array = require( '@stdlib/ndarray/to-array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * + * var out = push( x, 5.0, 6.0, 7.0 ); + * // returns + * + * var arr = ndarray2array( out ); + * // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] + */ + ( x: typedndarray, ...values: Array ): typedndarray; + + /** + * Appends the provided scalar values to the input ndarray and assigns the result to a provided one-dimensional output ndarray. + * + * @param x - input ndarray + * @param values - scalar values + * @param out - output ndarray + * @returns output ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * var empty = require( '@stdlib/ndarray/empty' ); + * var ndarray2array = require( '@stdlib/ndarray/to-array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * var y = empty( [ 7 ] ); + * + * var out = push.assign( x, 5.0, 6.0, 7.0, y ); + * // returns + * + * var bool = ( out === y ); + * // returns true + * + * var arr = ndarray2array( y ); + * // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] + */ + assign = typedndarray>( x: typedndarray, ...values: Array, out: V ): V; +} + +/** +* Returns a one-dimensional ndarray formed by appending the provided scalar values to the input ndarray. +* +* @param x - input ndarray +* @param values - scalar values +* @returns output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = push( x, 5.0, 6.0, 7.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var y = empty( [ 7 ] ); +* +* var out = push.assign( x, 5.0, 6.0, 7.0, y ); +* // returns +* +* var bool = ( out === y ); +* // returns true +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +*/ +declare var push: Push; + + +// EXPORTS // + +export = push; diff --git a/lib/node_modules/@stdlib/ndarray/push/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/push/docs/types/test.ts new file mode 100644 index 000000000000..0f27b7f405c1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/docs/types/test.ts @@ -0,0 +1,93 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +import push = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + push( x, 1.0 ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + push( '10', 1.0 ); // $ExpectError + push( 10, 1.0 ); // $ExpectError + push( false, 1.0 ); // $ExpectError + push( true, 1.0 ); // $ExpectError + push( null, 1.0 ); // $ExpectError + push( [], 1.0 ); // $ExpectError + push( {}, 1.0 ); // $ExpectError + push( ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + push(); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2 ] ); + const out = zeros( [ 3 ] ); + + push.assign( x, 1.0, out ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the assign method is provided a first argument which is not an ndarray... +{ + const out = zeros( [ 3 ] ); + + push.assign( '10', 1.0, out ); // $ExpectError + push.assign( 10, 1.0, out ); // $ExpectError + push.assign( false, 1.0, out ); // $ExpectError + push.assign( true, 1.0, out ); // $ExpectError + push.assign( null, 1.0, out ); // $ExpectError + push.assign( [], 1.0, out ); // $ExpectError + push.assign( {}, 1.0, out ); // $ExpectError + push.assign( ( x: number ): number => x, 1.0, out ); // $ExpectError +} + +// The compiler throws an error if the assign method is provided an output argument which is not an ndarray... +{ + const x = zeros( [ 3 ] ); + + push.assign( x, 1.0, '10' ); // $ExpectError + push.assign( x, 1.0, 10 ); // $ExpectError + push.assign( x, 1.0, false ); // $ExpectError + push.assign( x, 1.0, true ); // $ExpectError + push.assign( x, 1.0, null ); // $ExpectError + push.assign( x, 1.0, [] ); // $ExpectError + push.assign( x, 1.0, {} ); // $ExpectError + push.assign( x, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ] ); + + push.assign(); // $ExpectError + push.assign( x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/push/examples/index.js b/lib/node_modules/@stdlib/ndarray/push/examples/index.js new file mode 100644 index 000000000000..7673e1ca28ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/examples/index.js @@ -0,0 +1,33 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var push = require( './../lib' ); + +var opts = { + 'dtype': 'generic' +}; +var x = array( discreteUniform( 6, 0, 10, opts ), opts ); +console.log( ndarray2array( x ) ); + +var out = push( x, 12.0, 14.0 ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/assign.js b/lib/node_modules/@stdlib/ndarray/push/lib/assign.js new file mode 100644 index 000000000000..db43a6c30cb8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/lib/assign.js @@ -0,0 +1,97 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var concat1d = require( '@stdlib/ndarray/concat1d' ).assign; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Appends the provided scalar values to the input ndarray and assigns the result to a provided one-dimensional output ndarray. +* +* @param {ndarray} x - input ndarray +* @param {...*} values - scalar values +* @param {ndarray} out - output ndarray +* @throws {Error} must provide at least three arguments +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} first argument must be a one-dimensional ndarray +* @throws {TypeError} output argument must be an ndarray +* @throws {TypeError} output argument must be a one-dimensional ndarray +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* var y = zeros( [ 7 ] ); +* +* var out = assign( x, 5.0, 6.0, 7.0, y ); +* // returns +* +* var bool = ( out === y ); +* // returns true +* +* var arr = ndarray2array( y ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +*/ +function assign( x ) { + var nargs; + var dtype; + var args; + var out; + var i; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( ndims( x ) !== 1 ) { + throw new TypeError( format( 'invalid argument. First argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); + } + nargs = arguments.length; + out = arguments[ nargs - 1 ]; + if ( !isndarrayLike( out ) ) { + throw new TypeError( format( 'invalid argument. Output argument must be an ndarray. Value: `%s`.', out ) ); + } + if ( ndims( out ) !== 1 ) { + throw new TypeError( format( 'invalid argument. Output argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); + } + dtype = getDType( x ); + args = [ x ]; + for ( i = 1; i < nargs - 1; i++ ) { + args.push( scalar2ndarray( arguments[ i ], { + 'dtype': dtype + })); + } + return concat1d( args, out ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/index.js b/lib/node_modules/@stdlib/ndarray/push/lib/index.js new file mode 100644 index 000000000000..cd90839458b4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/lib/index.js @@ -0,0 +1,56 @@ +/** +* @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'; + +/** +* Return a one-dimensional ndarray formed by appending the provided scalar values to the input ndarray. +* +* @module @stdlib/ndarray/push +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var push = require( '@stdlib/ndarray/push' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = push( x, 5.0, 6.0, 7.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/main.js b/lib/node_modules/@stdlib/ndarray/push/lib/main.js new file mode 100644 index 000000000000..d249e2518f1b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/lib/main.js @@ -0,0 +1,80 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var concat1d = require( '@stdlib/ndarray/concat1d' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a one-dimensional ndarray formed by appending the provided scalar values to the input ndarray. +* +* @param {ndarray} x - input ndarray +* @param {...*} values - scalar values +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} first argument must be a one-dimensional ndarray +* @returns {ndarray} output ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = push( x, 5.0, 6.0, 7.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ] +*/ +function push( x ) { + var nargs; + var dtype; + var args; + var i; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( ndims( x ) !== 1 ) { + throw new TypeError( format( 'invalid argument. First argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); + } + nargs = arguments.length; + dtype = getDType( x ); + args = [ x ]; + for ( i = 1; i < nargs; i++ ) { + args.push( scalar2ndarray( arguments[ i ], { + 'dtype': dtype + })); + } + return concat1d( args ); +} + + +// EXPORTS // + +module.exports = push; diff --git a/lib/node_modules/@stdlib/ndarray/push/package.json b/lib/node_modules/@stdlib/ndarray/push/package.json new file mode 100644 index 000000000000..4812e8c0bda3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/push", + "version": "0.0.0", + "description": "Return a one-dimensional ndarray formed by appending the provided scalar values to the input 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", + "stdtypes", + "types", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "push", + "append", + "add", + "concat", + "concatenate", + "join", + "merge", + "combine" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js new file mode 100644 index 000000000000..d6d521dab4bb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js @@ -0,0 +1,263 @@ +/** +* @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 resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var push = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof push, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 4 ] ); + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( value, 0.0, out ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is an ndarray with more than one dimension', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 4 ] ); + values = [ + zeros( [ 2, 2 ] ), + zeros( [ 3, 1, 2 ] ), + zeros( [ 2, 2, 2 ] ), + zeros( [ 3, 3, 3, 3 ] ), + zeros( [ 3, 1, 2 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( value, 0.0, out ); + }; + } +}); + +tape( 'the function throws an error if provided an output argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( zeros( [ 2 ]), 0.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an output argument which is an ndarray with more than one dimension', function test( t ) { + var values; + var i; + + values = [ + zeros( [ 2, 2 ] ), + zeros( [ 3, 1, 2 ] ), + zeros( [ 2, 2, 2 ] ), + zeros( [ 3, 3, 3, 3 ] ), + zeros( [ 3, 1, 2 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( zeros( [ 2 ] ), 0.0, value ); + }; + } +}); + +tape( 'the function appends the provided provided scalar values and assigns results to an output ndarray (row-major, contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + y = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + out = push( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function appends the provided provided scalar values and assigns results to an output ndarray (column-major, contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + y = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + out = push( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (row-major, non-contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'row-major' ); + y = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + out = push( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 1.0, 3.0, 5.0, 7.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.strictEqual( out, y, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (column-major, non-contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'column-major' ); + y = zeros( [ 6 ], { + 'dtype': 'float64' + }); + + out = push( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 1.0, 3.0, 5.0, 7.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( y ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.strictEqual( y, out, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/push/test/test.js b/lib/node_modules/@stdlib/ndarray/push/test/test.js new file mode 100644 index 000000000000..704a83f7974e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/test/test.js @@ -0,0 +1,39 @@ +/** +* @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 isMethod = require( '@stdlib/assert/is-method' ); +var push = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof push, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( push, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/push/test/test.main.js b/lib/node_modules/@stdlib/ndarray/push/test/test.main.js new file mode 100644 index 000000000000..27a023321817 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/push/test/test.main.js @@ -0,0 +1,186 @@ +/** +* @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 resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var push = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof push, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( value, 0.0 ); + }; + } +}); + +tape( 'the function throws an error if provided an ndarray with more than one dimension', function test( t ) { + var values; + var i; + + values = [ + zeros( [ 2, 2 ] ), + zeros( [ 3, 1, 2 ] ), + zeros( [ 2, 2, 2 ] ), + zeros( [ 3, 3, 3, 3 ] ), + zeros( [ 3, 1, 2 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + push( value, 0.0 ); + }; + } +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (row-major, contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' ); + + out = push( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( out ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( out ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (column-major, contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'column-major' ); + + out = push( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( out ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( out ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (row-major, non-contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'row-major' ); + + out = push( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 1.0, 3.0, 5.0, 7.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( out ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( out ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (column-major, non-contiguous)', function test( t ) { + var expected; + var actual; + var xbuf; + var out; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + x = new ndarray( 'float64', xbuf, [ 4 ], [ 2 ], 0, 'column-major' ); + + out = push( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 1.0, 3.0, 5.0, 7.0, 5.0, 6.0 ]; + + t.strictEqual( resolveStr( getDType( out ) ), 'float64', 'returns expected value' ); + t.strictEqual( getOrder( out ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( out ), [ 6 ], 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); From e259e4528eb0e69c43c70b4f1d7154262b71c867 Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Mon, 15 Dec 2025 15:10:18 +0500 Subject: [PATCH 2/6] fix: apply suggestions from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- .../@stdlib/ndarray/push/benchmark/benchmark.assign.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js index f289c96e7bac..3ecbc4484fdf 100644 --- a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js +++ b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js @@ -42,7 +42,7 @@ bench( pkg, function benchmark( b ) { }; x = zeros( [ 3 ], opts ); values = [ 1.0, 2.0, 3.0, 4.0]; - out = zeros( [ 8 ], opts ); + out = zeros( [ 7 ], opts ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { From 55a3288209bc751a838eed7704b828b3377b8240 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 15 Dec 2025 15:28:50 +0500 Subject: [PATCH 3/6] bench: refactor benchmarks --- 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - 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: na - task: lint_license_headers status: passed --- --- .../push/benchmark/benchmark.assign.js | 63 +++++++++++++++++-- .../ndarray/push/benchmark/benchmark.js | 59 +++++++++++++++-- 2 files changed, 114 insertions(+), 8 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js index 3ecbc4484fdf..4e9a19ad490e 100644 --- a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js +++ b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.assign.js @@ -23,14 +23,42 @@ var bench = require( '@stdlib/bench' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var zeros = require( '@stdlib/ndarray/zeros' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var push = require( './../lib/assign.js' ); // MAIN // -bench( pkg, function benchmark( b ) { - var values; +bench( format( '%s::two scalar values', pkg ), function benchmark( b ) { + var opts; + var out; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + out = zeros( [ 5 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, 1.0, 2.0, out ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::four scalar values', pkg ), function benchmark( b ) { var opts; var out; var v; @@ -41,12 +69,39 @@ bench( pkg, function benchmark( b ) { 'dtype': 'float64' }; x = zeros( [ 3 ], opts ); - values = [ 1.0, 2.0, 3.0, 4.0]; out = zeros( [ 7 ], opts ); b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = push( x, values, out ); + v = push( x, 1.0, 2.0, 3.0, 4.0, out ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::six scalar values', pkg ), function benchmark( b ) { + var opts; + var out; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + out = zeros( [ 9 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, out ); if ( typeof v !== 'object' ) { b.fail( 'should return an ndarray' ); } diff --git a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js index 643289862a76..3d2923206abc 100644 --- a/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/push/benchmark/benchmark.js @@ -23,14 +23,14 @@ var bench = require( '@stdlib/bench' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var zeros = require( '@stdlib/ndarray/zeros' ); +var format = require( '@stdlib/string/format' ); var pkg = require( './../package.json' ).name; var push = require( './../lib' ); // MAIN // -bench( pkg, function benchmark( b ) { - var values; +bench( format( '%s::two scalar values', pkg ), function benchmark( b ) { var opts; var v; var x; @@ -40,11 +40,62 @@ bench( pkg, function benchmark( b ) { 'dtype': 'float64' }; x = zeros( [ 3 ], opts ); - values = [ 1.0, 2.0, 3.0, 4.0 ]; b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = push( x, values ); + v = push( x, 1.0, 2.0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::four scalar values', pkg ), function benchmark( b ) { + var opts; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, 1.0, 2.0, 3.0, 4.0 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::six scalar values', pkg ), function benchmark( b ) { + var opts; + var v; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = push( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ); if ( typeof v !== 'object' ) { b.fail( 'should return an ndarray' ); } From af7bc5816de172e09ac85ac4c0d371113fb37229 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 15 Dec 2025 21:06:00 +0500 Subject: [PATCH 4/6] refactor: apply suggestions from code 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/push/lib/assign.js | 22 ++++++++----------- .../@stdlib/ndarray/push/lib/main.js | 12 +++++----- .../@stdlib/ndarray/push/test/test.assign.js | 8 +++---- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/assign.js b/lib/node_modules/@stdlib/ndarray/push/lib/assign.js index db43a6c30cb8..28d76a8c54da 100644 --- a/lib/node_modules/@stdlib/ndarray/push/lib/assign.js +++ b/lib/node_modules/@stdlib/ndarray/push/lib/assign.js @@ -37,10 +37,9 @@ var format = require( '@stdlib/string/format' ); * @param {...*} values - scalar values * @param {ndarray} out - output ndarray * @throws {Error} must provide at least three arguments -* @throws {TypeError} first argument must be an ndarray -* @throws {TypeError} first argument must be a one-dimensional ndarray -* @throws {TypeError} output argument must be an ndarray -* @throws {TypeError} output argument must be a one-dimensional ndarray +* @throws {TypeError} first argument must a be one-dimensional ndarray +* @throws {Error} must be provided at least three arguments +* @throws {TypeError} output argument must a be one-dimensional ndarray * @returns {ndarray} output ndarray * * @example @@ -67,19 +66,16 @@ function assign( x ) { var out; var i; - if ( !isndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); - } - if ( ndims( x ) !== 1 ) { + if ( !isndarrayLike( x ) || ndims( x ) !== 1 ) { throw new TypeError( format( 'invalid argument. First argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); } nargs = arguments.length; - out = arguments[ nargs - 1 ]; - if ( !isndarrayLike( out ) ) { - throw new TypeError( format( 'invalid argument. Output argument must be an ndarray. Value: `%s`.', out ) ); + if ( nargs < 3 ) { + throw new Error( format( 'invalid argument. The function must be provided at least three arguments. Value: `%s`.', nargs ) ); } - if ( ndims( out ) !== 1 ) { - throw new TypeError( format( 'invalid argument. Output argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); + out = arguments[ nargs - 1 ]; + if ( !isndarrayLike( out ) || ndims( out ) !== 1 ) { + throw new TypeError( format( 'invalid argument. Output argument must be a one-dimensional ndarray. Value: `%s`.', out ) ); } dtype = getDType( x ); args = [ x ]; diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/main.js b/lib/node_modules/@stdlib/ndarray/push/lib/main.js index d249e2518f1b..14cb154d710b 100644 --- a/lib/node_modules/@stdlib/ndarray/push/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/push/lib/main.js @@ -35,8 +35,8 @@ var format = require( '@stdlib/string/format' ); * * @param {ndarray} x - input ndarray * @param {...*} values - scalar values -* @throws {TypeError} first argument must be an ndarray -* @throws {TypeError} first argument must be a one-dimensional ndarray +* @throws {TypeError} first argument must a be one-dimensional ndarray +* @throws {Error} must be provided at least two arguments * @returns {ndarray} output ndarray * * @example @@ -57,13 +57,13 @@ function push( x ) { var args; var i; - if ( !isndarrayLike( x ) ) { - throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); - } - if ( ndims( x ) !== 1 ) { + if ( !isndarrayLike( x ) | ndims( x ) !== 1 ) { throw new TypeError( format( 'invalid argument. First argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); } nargs = arguments.length; + if ( nargs < 2 ) { + throw new Error( format( 'invalid argument. The function must be provided at least two arguments. Value: `%s`.', nargs ) ); + } dtype = getDType( x ); args = [ x ]; for ( i = 1; i < nargs; i++ ) { diff --git a/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js index d6d521dab4bb..e1556bf56cb6 100644 --- a/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js +++ b/lib/node_modules/@stdlib/ndarray/push/test/test.assign.js @@ -150,7 +150,7 @@ tape( 'the function throws an error if provided an output argument which is an n } }); -tape( 'the function appends the provided provided scalar values and assigns results to an output ndarray (row-major, contiguous)', function test( t ) { +tape( 'the function appends the provided provided scalar values to the input ndarray and assigns results to an output ndarray (row-major, contiguous)', function test( t ) { var expected; var actual; var xbuf; @@ -178,7 +178,7 @@ tape( 'the function appends the provided provided scalar values and assigns resu t.end(); }); -tape( 'the function appends the provided provided scalar values and assigns results to an output ndarray (column-major, contiguous)', function test( t ) { +tape( 'the function appends the provided provided scalar values to the input ndarray and assigns results to an output ndarray (column-major, contiguous)', function test( t ) { var expected; var actual; var xbuf; @@ -206,7 +206,7 @@ tape( 'the function appends the provided provided scalar values and assigns resu t.end(); }); -tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (row-major, non-contiguous)', function test( t ) { +tape( 'the function appends the provided provided scalar values to the input ndarray and assigns results to an output ndarray (row-major, non-contiguous)', function test( t ) { var expected; var actual; var xbuf; @@ -234,7 +234,7 @@ tape( 'the function returns a one-dimensional ndarray formed by appending provid t.end(); }); -tape( 'the function returns a one-dimensional ndarray formed by appending provided scalar values (column-major, non-contiguous)', function test( t ) { +tape( 'the function appends the provided provided scalar values to the input ndarray and assigns results to an output ndarray (column-major, non-contiguous)', function test( t ) { var expected; var actual; var xbuf; From 77614be320c873c46c4b9675f9ca9b7c1774e548 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Dec 2025 20:30:17 -0800 Subject: [PATCH 5/6] fix: use correct operand Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/push/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/main.js b/lib/node_modules/@stdlib/ndarray/push/lib/main.js index 14cb154d710b..fe7b429d1ebf 100644 --- a/lib/node_modules/@stdlib/ndarray/push/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/push/lib/main.js @@ -57,7 +57,7 @@ function push( x ) { var args; var i; - if ( !isndarrayLike( x ) | ndims( x ) !== 1 ) { + if ( !isndarrayLike( x ) || ndims( x ) !== 1 ) { throw new TypeError( format( 'invalid argument. First argument must be a one-dimensional ndarray. Value: `%s`.', x ) ); } nargs = arguments.length; From 7f42e7c1e5738338a9f83cedb43420bb94982da8 Mon Sep 17 00:00:00 2001 From: Athan Date: Mon, 15 Dec 2025 20:31:05 -0800 Subject: [PATCH 6/6] refactor: update error message Signed-off-by: Athan --- lib/node_modules/@stdlib/ndarray/push/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/push/lib/main.js b/lib/node_modules/@stdlib/ndarray/push/lib/main.js index fe7b429d1ebf..6174ef9393e4 100644 --- a/lib/node_modules/@stdlib/ndarray/push/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/push/lib/main.js @@ -62,7 +62,7 @@ function push( x ) { } nargs = arguments.length; if ( nargs < 2 ) { - throw new Error( format( 'invalid argument. The function must be provided at least two arguments. Value: `%s`.', nargs ) ); + throw new Error( 'invalid operation. Must provide at least two arguments.' ); } dtype = getDType( x ); args = [ x ];