diff --git a/lib/node_modules/@stdlib/ndarray/unshift/README.md b/lib/node_modules/@stdlib/ndarray/unshift/README.md new file mode 100644 index 000000000000..0e3aaef8ec43 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/README.md @@ -0,0 +1,155 @@ + + +# unshift + +> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by prepending the provided scalar values to the input ndarray. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var unshift = require( '@stdlib/ndarray/unshift' ); +``` + +#### unshift( x, ...values ) + +Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by prepending the provided scalar values to the input ndarray. + +```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 = unshift( x, -2.0, -1.0, 0.0 ); +// returns + +var arr = ndarray2array( out ); +// returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **...values**: scalar values to prepend. + +#### unshift.assign( x, ...values, out ) + +Prepends 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 = unshift.assign( x, -2.0, -1.0, 0.0, y ); +// returns + +var bool = ( out === y ); +// returns true + +var arr = ndarray2array( y ); +// returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **...values**: scalar values to prepend. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unshift = require( '@stdlib/ndarray/unshift' ); + +var opts = { + 'dtype': 'generic' +}; +var x = array( discreteUniform( 6, 0, 10, opts ), opts ); +console.log( ndarray2array( x ) ); + +var out = unshift( x, -2.0, -1.0, 0.0 ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..e90d239fd5c7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.assign.js @@ -0,0 +1,115 @@ +/** +* @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 format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unshift = require( './../lib/assign.js' ); + + +// MAIN // + +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 = unshift( 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; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeros( [ 3 ], opts ); + out = zeros( [ 7 ], opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = unshift( 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 = unshift( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.js new file mode 100644 index 000000000000..8917e5759ac3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/benchmark/benchmark.js @@ -0,0 +1,109 @@ +/** +* @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 format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unshift = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::two 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 = unshift( 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 = unshift( 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 = unshift( x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/unshift/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/unshift/docs/repl.txt new file mode 100644 index 000000000000..5a318cc50b69 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/docs/repl.txt @@ -0,0 +1,60 @@ + +{{alias}}( x, ...values ) + Returns a one-dimensional ndarray formed by prepending the provided scalar + values to the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + values: ...any + Scalar values to prepend. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var out = {{alias}}( x, -1.0, 0.0 ) + + > var arr = {{alias:@stdlib/ndarray/to-array}}( out ) + [ -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] + + +{{alias}}.assign( x, ...values, out ) + Prepends the provided scalar values to the input ndarray and assigns results + to an output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + values: ...any + Scalar values to prepend. + + 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, -1.0, 0.0, y ) + + > var bool = ( out === y ) + true + > var arr = {{alias:@stdlib/ndarray/to-array}}( y ) + [ -1.0, 0.0, 1.0, 2.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/unshift/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/unshift/docs/types/index.d.ts new file mode 100644 index 000000000000..043ca97b6036 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 `unshift`. +*/ +interface Unshift { + /** + * Returns a one-dimensional ndarray formed by prepending 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 = unshift( x, -2.0, -1.0, 0.0 ); + * // returns + * + * var arr = ndarray2array( out ); + * // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] + */ + ( x: typedndarray, ...values: Array ): typedndarray; + + /** + * Prepends 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 = unshift.assign( x, -2.0, -1.0, 0.0, y ); + * // returns + * + * var bool = ( out === y ); + * // returns true + * + * var arr = ndarray2array( y ); + * // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] + */ + assign = typedndarray>( x: typedndarray, ...values: Array, out: V ): V; +} + +/** +* Returns a one-dimensional ndarray formed by prepending 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 = unshift( x, -2.0, -1.0, 0.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.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 = unshift.assign( x, -2.0, -1.0, 0.0, y ); +* // returns +* +* var bool = ( out === y ); +* // returns true +* +* var arr = ndarray2array( y ); +* // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] +*/ +declare var unshift: Unshift; + + +// EXPORTS // + +export = unshift; diff --git a/lib/node_modules/@stdlib/ndarray/unshift/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/unshift/docs/types/test.ts new file mode 100644 index 000000000000..21b00fd54f13 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 unshift = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ] ); + + unshift( x, 1.0 ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + unshift( '10', 1.0 ); // $ExpectError + unshift( 10, 1.0 ); // $ExpectError + unshift( false, 1.0 ); // $ExpectError + unshift( true, 1.0 ); // $ExpectError + unshift( null, 1.0 ); // $ExpectError + unshift( [], 1.0 ); // $ExpectError + unshift( {}, 1.0 ); // $ExpectError + unshift( ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + unshift(); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2 ] ); + const out = zeros( [ 3 ] ); + + unshift.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 ] ); + + unshift.assign( '10', 1.0, out ); // $ExpectError + unshift.assign( 10, 1.0, out ); // $ExpectError + unshift.assign( false, 1.0, out ); // $ExpectError + unshift.assign( true, 1.0, out ); // $ExpectError + unshift.assign( null, 1.0, out ); // $ExpectError + unshift.assign( [], 1.0, out ); // $ExpectError + unshift.assign( {}, 1.0, out ); // $ExpectError + unshift.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 ] ); + + unshift.assign( x, 1.0, '10' ); // $ExpectError + unshift.assign( x, 1.0, 10 ); // $ExpectError + unshift.assign( x, 1.0, false ); // $ExpectError + unshift.assign( x, 1.0, true ); // $ExpectError + unshift.assign( x, 1.0, null ); // $ExpectError + unshift.assign( x, 1.0, [] ); // $ExpectError + unshift.assign( x, 1.0, {} ); // $ExpectError + unshift.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 ] ); + + unshift.assign(); // $ExpectError + unshift.assign( x ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/unshift/examples/index.js b/lib/node_modules/@stdlib/ndarray/unshift/examples/index.js new file mode 100644 index 000000000000..97c9e6eb3f9e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 unshift = require( './../lib' ); + +var opts = { + 'dtype': 'generic' +}; +var x = array( discreteUniform( 6, 0, 10, opts ), opts ); +console.log( ndarray2array( x ) ); + +var out = unshift( x, -2.0, -1.0, 0.0 ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/unshift/lib/assign.js b/lib/node_modules/@stdlib/ndarray/unshift/lib/assign.js new file mode 100644 index 000000000000..0c691e89d6d7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/lib/assign.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var concat1d = require( '@stdlib/ndarray/concat1d' ).assign; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Prepends 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 {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 +* 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, -2.0, -1.0, 0.0, y ); +* // returns +* +* var bool = ( y === out ); +* // returns true +* +* var arr = ndarray2array( out ); +* // throws [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] +*/ +function assign( x ) { + var nargs; + var dtype; + var args; + var out; + var i; + + 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 < 3 ) { + throw new Error( format( 'invalid argument. The function must be provided at least three arguments. Value: `%s`.', nargs ) ); + } + 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 ) ); + } + args = []; + dtype = getDType( x ); + for ( i = 1; i < nargs - 1; i++ ) { + args.push( scalar2ndarray( arguments[ i ], { + 'dtype': dtype + })); + } + args.push( x ); + return concat1d( args, out ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/ndarray/unshift/lib/index.js b/lib/node_modules/@stdlib/ndarray/unshift/lib/index.js new file mode 100644 index 000000000000..ee2f074e0a23 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 prepending the provided scalar values to the input ndarray. +* +* @module @stdlib/ndarray/unshift +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var unshift = require( '@stdlib/ndarray/unshift' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); +* +* var out = unshift( x, -2.0, -1.0, 0.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.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/unshift/lib/main.js b/lib/node_modules/@stdlib/ndarray/unshift/lib/main.js new file mode 100644 index 000000000000..54a832523622 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/lib/main.js @@ -0,0 +1,81 @@ +/** +* @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 ndims = require( '@stdlib/ndarray/base/ndims' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var concat1d = require( '@stdlib/ndarray/concat1d' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a one-dimensional ndarray formed by prepending the provided scalar values to the input ndarray. +* +* @param {ndarray} x - input ndarray +* @param {...*} values - scalar values +* @throws {TypeError} first argument must a be one-dimensional ndarray +* @throws {Error} must be provided at least two arguments +* @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 = unshift( x, -2.0, -1.0, 0.0 ); +* // returns +* +* var arr = ndarray2array( out ); +* // returns [ -2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0 ] +*/ +function unshift( x ) { + var nargs; + var dtype; + var args; + var i; + + 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 ) ); + } + args = []; + dtype = getDType( x ); + for ( i = 1; i < nargs; i++ ) { + args.push( scalar2ndarray( arguments[ i ], { + 'dtype': dtype + })); + } + args.push( x ); + return concat1d( args ); +} + + +// EXPORTS // + +module.exports = unshift; diff --git a/lib/node_modules/@stdlib/ndarray/unshift/package.json b/lib/node_modules/@stdlib/ndarray/unshift/package.json new file mode 100644 index 000000000000..e7b4cdef1997 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/unshift", + "version": "0.0.0", + "description": "Return a one-dimensional ndarray formed by prepending 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", + "base", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "add", + "prepend", + "concat", + "join" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/unshift/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/unshift/test/test.assign.js new file mode 100644 index 000000000000..a15ff50cf0ef --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 unshift = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unshift, '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() { + unshift( 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() { + unshift( 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() { + unshift( 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() { + unshift( zeros( [ 2 ] ), 0.0, value ); + }; + } +}); + +tape( 'the function prepends 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; + 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 = unshift( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 5.0, 6.0, 1.0, 2.0, 3.0, 4.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 prepends 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; + 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 = unshift( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 5.0, 6.0, 1.0, 2.0, 3.0, 4.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 prepends 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; + 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 = unshift( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 5.0, 6.0, 1.0, 3.0, 5.0, 7.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 prepends 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; + 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 = unshift( x, 5.0, 6.0, y ); + + actual = ndarray2array( y ); + expected = [ 5.0, 6.0, 1.0, 3.0, 5.0, 7.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/unshift/test/test.js b/lib/node_modules/@stdlib/ndarray/unshift/test/test.js new file mode 100644 index 000000000000..d6ffca59013e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 unshift = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unshift, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( unshift, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/unshift/test/test.main.js b/lib/node_modules/@stdlib/ndarray/unshift/test/test.main.js new file mode 100644 index 000000000000..3c4a28e4d6c9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/unshift/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 unshift = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unshift, '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() { + unshift( 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() { + unshift( value, 0.0 ); + }; + } +}); + +tape( 'the function returns a one-dimensional ndarray formed by prepending 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 = unshift( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 5.0, 6.0, 1.0, 2.0, 3.0, 4.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 prepending 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 = unshift( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 5.0, 6.0, 1.0, 2.0, 3.0, 4.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 prepending 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 = unshift( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 5.0, 6.0, 1.0, 3.0, 5.0, 7.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 prepending 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 = unshift( x, 5.0, 6.0 ); + + actual = ndarray2array( out ); + expected = [ 5.0, 6.0, 1.0, 3.0, 5.0, 7.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(); +});