From 3c37561550b0847c554ad111b88f7fc20727ea94 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 19 Oct 2026 23:16:07 +0500 Subject: [PATCH] feat: add assign method to ndarray/with --- .../@stdlib/ndarray/with/README.md | 36 +++ .../with/benchmark/benchmark.assign.js | 162 ++++++++++ .../@stdlib/ndarray/with/docs/repl.txt | 33 ++ .../ndarray/with/docs/types/index.d.ts | 80 ++++- .../@stdlib/ndarray/with/docs/types/test.ts | 60 ++++ .../@stdlib/ndarray/with/examples/index.js | 6 + .../@stdlib/ndarray/with/lib/assign.js | 93 ++++++ .../@stdlib/ndarray/with/lib/base.js | 99 ++++++ .../@stdlib/ndarray/with/lib/index.js | 13 +- .../@stdlib/ndarray/with/lib/main.js | 36 +-- .../@stdlib/ndarray/with/test/test.assign.js | 281 ++++++++++++++++++ .../@stdlib/ndarray/with/test/test.js | 139 +-------- .../@stdlib/ndarray/with/test/test.main.js | 172 +++++++++++ 13 files changed, 1037 insertions(+), 173 deletions(-) create mode 100644 lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/with/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/with/lib/base.js create mode 100644 lib/node_modules/@stdlib/ndarray/with/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/with/test/test.main.js diff --git a/lib/node_modules/@stdlib/ndarray/with/README.md b/lib/node_modules/@stdlib/ndarray/with/README.md index f25aea958a7b..c1cc25d8d8ab 100644 --- a/lib/node_modules/@stdlib/ndarray/with/README.md +++ b/lib/node_modules/@stdlib/ndarray/with/README.md @@ -62,6 +62,36 @@ The function accepts the following arguments: - **indices**: indices of the element to replace. - **value**: replacement value. +#### ndarrayWith.assign( x, indices, value, out ) + +Copies elements from an input [ndarray][@stdlib/ndarray/ctor] to an output [ndarray][@stdlib/ndarray/ctor] and replaces the element at a specified index with a provided value. + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); + +var x = zeros( [ 2, 2 ], { + 'dtype': 'float64' +}); +// returns + +var y = emptyLike( x ); +// returns + +var out = ndarrayWith.assign( x, [ 0, 0 ], 1.0, y ); +// returns [ [ 1, 0 ], [ 0, 0 ] ] + +var bool = ( out === y ); +// returns true +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **indices**: indices of the element to replace. +- **value**: replacement value. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have the same shape as the input [ndarray][@stdlib/ndarray/ctor]. + @@ -88,6 +118,7 @@ The function accepts the following arguments: ```javascript var zeros = require( '@stdlib/ndarray/zeros' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var ndarrayWith = require( '@stdlib/ndarray/with' ); @@ -97,6 +128,11 @@ var x = zeros( [ 1, 3, 3 ], { var out = ndarrayWith( x, [ 0, 1, 1 ], 10.0 ); console.log( ndarray2array( out ) ); + +var y = emptyLike( x ); + +out = ndarrayWith.assign( x, [ 0, 2, 2 ], 10.0, y ); +console.log( ndarray2array( out ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..154da396fe0d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/benchmark/benchmark.assign.js @@ -0,0 +1,162 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var assign = require( './../lib/assign.js' ); + + +// MAIN // + +bench( format( '%s:assign:ndims=1', pkg ), function benchmark( b ) { + var out; + var v; + var x; + var i; + + x = zeros( [ 5 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = assign( x, [ 4 ], 5, 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:assign:ndims=2', pkg ), function benchmark( b ) { + var out; + var v; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = assign( x, [ 1, 1 ], 5, 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:assign:ndims=3', pkg ), function benchmark( b ) { + var out; + var v; + var x; + var i; + + x = zeros( [ 2, 2, 2 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = assign( x, [ 1, 1, 1 ], 5, 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:assign:ndims=4', pkg ), function benchmark( b ) { + var out; + var v; + var x; + var i; + + x = zeros( [ 2, 2, 2, 2 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = assign( x, [ 1, 1, 1, 1 ], 5, 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:assign:ndims=5', pkg ), function benchmark( b ) { + var out; + var v; + var x; + var i; + + x = zeros( [ 2, 2, 2, 2, 2 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = assign( x, [ 1, 1, 1, 1, 1 ], 5, 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/with/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt index d6924840b942..1e5511473967 100644 --- a/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/with/docs/repl.txt @@ -25,5 +25,38 @@ > var out = {{alias}}( x, [ 0, 0 ], 5 ) [ [ 5, 2 ], [ 3, 4 ] ] + +{{alias}}.assign( x, indices, value, out ) + Copies elements from an input ndarray to an output ndarray and replaces the + element at a specified index with a provided value. + + Parameters + ---------- + x: ndarray + Input array. + + indices: Array + Indices of the value to replace. + + value: any + Replaced value. + + out: ndarray + Output array. Must have the same shape as the input array. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ); + > var y = {{alias:@stdlib/ndarray/empty-like}}( x ); + > var out = {{alias}}.assign( x, [ 0, 0 ], 5, y ) + [ [ 5, 2 ], [ 3, 4 ] ] + > var bool = ( out === y ) + true + See Also -------- diff --git a/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts index 0274983130ff..86e5ae45ed56 100644 --- a/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/with/docs/types/index.d.ts @@ -22,6 +22,65 @@ import { typedndarray } from '@stdlib/types/ndarray'; +/** +* Interface describing `ndarrayWith`. +*/ +interface NdarrayWith { + /** + * Returns a new ndarray with the element at a specified index replaced by a provided value. + * + * @param x - input ndarray + * @param indices - indices of the element to replace + * @param value - value to set + * @returns output ndarray + * + * @example + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * + * var buffer = [ 1, 2, 3, 4 ]; + * var shape = [ 2, 2 ]; + * var order = 'row-major'; + * var strides = [ 2, 1 ]; + * var offset = 0; + * + * var x = ndarray( 'generic', buffer, shape, strides, offset, order ); + * + * var out = ndarrayWith( x, [ 0, 0 ], 5 ); + * // returns [ [ 5, 2 ], [ 3, 4 ] ] + */ + = typedndarray>( x: U, indices: Array, value: T ): U; + + /** + * Copies elements from an input ndarray to an output ndarray and replaces the element at a specified index with a provided value. + * + * @param x - input ndarray + * @param indices - indices of the element to replace + * @param value - value to set + * @param out - output ndarray + * @returns output ndarray + * + * @example + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var emptyLike = require( '@stdlib/ndarray/empty-like' ); + * + * var buffer = [ 1, 2, 3, 4 ]; + * var shape = [ 2, 2 ]; + * var order = 'row-major'; + * var strides = [ 2, 1 ]; + * var offset = 0; + * + * var x = ndarray( 'generic', buffer, shape, strides, offset, order ); + * var y = emptyLike( x ); + * + * var out = ndarrayWith.assign( x, [ 0, 0 ], 5, y ); + * // returns [ [ 5, 2 ], [ 3, 4 ] ] + * + * var bool = ( out === y ); + * // returns true + */ + assign = typedndarray>( x: typedndarray, indices: Array, value: T, out: U ): U; +} + /** * Returns a new ndarray with the element at a specified index replaced by a provided value. * @@ -43,8 +102,27 @@ import { typedndarray } from '@stdlib/types/ndarray'; * * var out = ndarrayWith( x, [ 0, 0 ], 5 ); * // returns [ [ 5, 2 ], [ 3, 4 ] ] +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var emptyLike = require( '@stdlib/ndarray/empty-like' ); +* +* var buffer = [ 1, 2, 3, 4 ]; +* var shape = [ 2, 2 ]; +* var order = 'row-major'; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, order ); +* var y = emptyLike( x ); +* +* var out = ndarrayWith.assign( x, [ 0, 0 ], 5, y ); +* // returns [ [ 5, 2 ], [ 3, 4 ] ] +* +* var bool = ( out === y ); +* // returns true */ -declare function ndarrayWith = typedndarray>( x: U, indices: Array, value: T ): U; +declare var ndarrayWith: NdarrayWith; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts index e3b134a2bbc7..a2b7e66cd60d 100644 --- a/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/with/docs/types/test.ts @@ -61,3 +61,63 @@ import ndarrayWith = require( './index' ); ndarrayWith( x, [ 0, 0 ] ); // $ExpectError ndarrayWith( x, [ 0, 0 ], 5, {} ); // $ExpectError } + +// Attached to the main export is an `assign` method which returns an ndarray... +{ + const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + const out = array( [ [ 0, 0 ], [ 0, 0 ] ] ); + + ndarrayWith.assign( x, [ 0, 0 ], 5, out ); // $ExpectType typedndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + const out = array( [ [ 0, 0 ], [ 0, 0 ] ] ); + + ndarrayWith.assign( 123, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( true, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( false, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( null, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( undefined, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( {}, [ 0, 0 ], 5, out ); // $ExpectError + ndarrayWith.assign( ( x: number ): number => x, [ 0, 0 ], 5, out ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an array of integers... +{ + const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + const out = array( [ [ 0, 0 ], [ 0, 0 ] ] ); + + ndarrayWith.assign( x, 1, 5, out ); // $ExpectError + ndarrayWith.assign( x, true, 5, out ); // $ExpectError + ndarrayWith.assign( x, false, 5, out ); // $ExpectError + ndarrayWith.assign( x, null, 5, out ); // $ExpectError + ndarrayWith.assign( x, undefined, 5, out ); // $ExpectError + ndarrayWith.assign( x, {}, 5, out ); // $ExpectError + ndarrayWith.assign( x, ( y: number ): number => y, 5, out ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not an ndarray... +{ + const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + ndarrayWith.assign( x, [ 0, 0 ], 5, 123 ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, true ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, false ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, null ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, undefined ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, {} ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, ( y: number ): number => y ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + const out = array( [ [ 0, 0 ], [ 0, 0 ] ] ); + + ndarrayWith.assign(); // $ExpectError + ndarrayWith.assign( x ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ] ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5 ); // $ExpectError + ndarrayWith.assign( x, [ 0, 0 ], 5, out, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/with/examples/index.js b/lib/node_modules/@stdlib/ndarray/with/examples/index.js index 49274cddef6b..8af5095515be 100644 --- a/lib/node_modules/@stdlib/ndarray/with/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/with/examples/index.js @@ -19,6 +19,7 @@ 'use strict'; var zeros = require( '@stdlib/ndarray/zeros' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); var ndarray2array = require( '@stdlib/ndarray/to-array' ); var ndarrayWith = require( './../lib' ); @@ -28,3 +29,8 @@ var x = zeros( [ 1, 3, 3 ], { var out = ndarrayWith( x, [ 0, 1, 1 ], 10.0 ); console.log( ndarray2array( out ) ); + +var y = emptyLike( x ); + +out = ndarrayWith.assign( x, [ 0, 2, 2 ], 10.0, y ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/assign.js b/lib/node_modules/@stdlib/ndarray/with/lib/assign.js new file mode 100644 index 000000000000..65df4b539075 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/lib/assign.js @@ -0,0 +1,93 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var hasEqualShape = require( '@stdlib/ndarray/base/assert/has-equal-shape' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Copies elements from an input ndarray to an output ndarray and replaces the element at a specified index with a provided value. +* +* @param {ndarray} x - input ndarray +* @param {IntegerArray} indices - indices of the element to replace +* @param {*} value - value to set +* @param {ndarray} out - output ndarray +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} second argument must be an array of integers +* @throws {RangeError} second argument must be an array of valid indices +* @throws {RangeError} number of indices must equal the number of input ndarray dimensions +* @throws {TypeError} fourth argument must be an ndarray +* @throws {Error} fourth argument must have the same shape as the first argument +* @returns {ndarray} output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var emptyLike = require( '@stdlib/ndarray/empty-like' ); +* +* var buffer = [ 1, 2, 3, 4 ]; +* var shape = [ 2, 2 ]; +* var order = 'row-major'; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, order ); +* var y = emptyLike( x ); +* +* var out = assign( x, [ 0, 0 ], 5, y ); +* // returns [ [ 5, 2 ], [ 3, 4 ] ] +* +* var bool = ( out === y ); +* // returns true +*/ +function assign( x, indices, value, out ) { + var sh; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isCollection( indices ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an array of integers. Value: `%s`.', indices ) ); + } + if ( !isndarrayLike( out ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be an ndarray. Value: `%s`.', out ) ); + } + sh = getShape( x ); + if ( indices.length !== sh.length ) { + throw new RangeError( format( 'invalid argument. Number of indices does not match the number of array dimensions. Array shape: (%s). Number of indices: %u.', join( sh, ',' ), indices.length ) ); + } + if ( !hasEqualShape( x, out ) ) { + throw new Error( format( 'invalid argument. Fourth argument must have the same shape as the first argument. First argument shape: (%s). Fourth argument shape: (%s).', join( sh, ',' ), join( getShape( out ), ',' ) ) ); + } + return base( x, indices, value, out ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/base.js b/lib/node_modules/@stdlib/ndarray/with/lib/base.js new file mode 100644 index 000000000000..86ba7450ae50 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/lib/base.js @@ -0,0 +1,99 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var getShape = require( '@stdlib/ndarray/shape' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Copies elements from an input ndarray to an output ndarray and replaces the element at a specified index with a provided value. +* +* @private +* @param {ndarray} x - input ndarray +* @param {IntegerArray} indices - indices of the element to replace +* @param {*} value - value to set +* @param {ndarray} out - output ndarray +* @throws {TypeError} second argument must be an array of integers +* @throws {RangeError} second argument must be an array of valid indices +* @returns {ndarray} output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var emptyLike = require( '@stdlib/ndarray/empty-like' ); +* +* var buffer = [ 1, 2, 3, 4 ]; +* var shape = [ 2, 2 ]; +* var order = 'row-major'; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, order ); +* +* var out = base( x, [ 0, 0 ], 5, emptyLike( x ) ); +* // returns [ [ 5, 2 ], [ 3, 4 ] ] +*/ +function base( x, indices, value, out ) { + var args; + var idx; + var get; + var sh; + var i; + + sh = getShape( x ); + + // Resolve an element accessor for retrieving indices: + get = resolveGetter( indices ); + + // Normalize the provided indices... + args = []; + for ( i = 0; i < sh.length; i++ ) { + idx = get( indices, i ); + if ( !isInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an array of integers. Value: `%s`.', indices ) ); + } + idx = normalizeIndex( idx, sh[ i ]-1 ); + if ( idx === -1 ) { + throw new RangeError( format( 'invalid argument. Second argument contains an out-of-bounds index. Array shape: (%s). Value: `[%s]`.', join( sh, ',' ), join( indices, ',' ) ) ); + } + args.push( idx ); + } + args.push( value ); + + // Copy the input array: + assign( [ x, out ] ); + + // Set the element at the specified indices: + out.set.apply( out, args ); + return out; +} + + +// EXPORTS // + +module.exports = base; diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/index.js b/lib/node_modules/@stdlib/ndarray/with/lib/index.js index 1a508ec879ee..8eeedbb08579 100644 --- a/lib/node_modules/@stdlib/ndarray/with/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/with/lib/index.js @@ -42,9 +42,18 @@ // MODULES // -var ndarrayWith = require( './main.js' ); +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 = ndarrayWith; +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/with/lib/main.js b/lib/node_modules/@stdlib/ndarray/with/lib/main.js index 15e48eb0d75f..d9e9bc5c51ae 100644 --- a/lib/node_modules/@stdlib/ndarray/with/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/with/lib/main.js @@ -22,14 +22,11 @@ var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var isCollection = require( '@stdlib/assert/is-collection' ); -var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var getShape = require( '@stdlib/ndarray/shape' ); var emptyLike = require( '@stdlib/ndarray/empty-like' ); -var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); -var assign = require( '@stdlib/ndarray/base/assign' ); -var resolveGetter = require( '@stdlib/array/base/resolve-getter' ); var join = require( '@stdlib/array/base/join' ); var format = require( '@stdlib/string/format' ); +var base = require( './base.js' ); // MAIN // @@ -61,12 +58,7 @@ var format = require( '@stdlib/string/format' ); * // returns [ [ 5, 2 ], [ 3, 4 ] ] */ function ndarrayWith( x, indices, value ) { - var args; - var out; - var idx; - var get; var sh; - var i; if ( !isndarrayLike( x ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); @@ -78,31 +70,7 @@ function ndarrayWith( x, indices, value ) { if ( indices.length !== sh.length ) { throw new RangeError( format( 'invalid argument. Number of indices does not match the number of array dimensions. Array shape: (%s). Number of indices: %u.', join( sh, ',' ), indices.length ) ); } - // Copy the input array: - out = emptyLike( x ); - assign( [ x, out ] ); - - // Resolve an element accessor for retrieving indices: - get = resolveGetter( indices ); - - // Normalize the provided indices... - args = []; - for ( i = 0; i < sh.length; i++ ) { - idx = get( indices, i ); - if ( !isInteger( idx ) ) { - throw new TypeError( format( 'invalid argument. Second argument must be an array of integers. Value: `%s`.', indices ) ); - } - idx = normalizeIndex( idx, sh[ i ] ); - if ( idx === -1 ) { - throw new RangeError( format( 'invalid argument. Second argument contains an out-of-bounds index. Array shape: (%s). Value: `[%s]`.', join( sh, ',' ), join( indices, ',' ) ) ); - } - args.push( idx ); - } - args.push( value ); - - // Set the element at the specified indices: - out.set.apply( out, args ); - return out; + return base( x, indices, value, emptyLike( x ) ); } diff --git a/lib/node_modules/@stdlib/ndarray/with/test/test.assign.js b/lib/node_modules/@stdlib/ndarray/with/test/test.assign.js new file mode 100644 index 000000000000..cbfa265c04a3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/test/test.assign.js @@ -0,0 +1,281 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var array = require( '@stdlib/ndarray/array' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var join = require( '@stdlib/array/base/join' ); +var assign = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var out; + var i; + + out = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + 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() { + assign( value, [ 0, 0, 0 ], 5, out ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an array of integers', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 0, '1', 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() { + assign( x, value, 5, out ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument with out-of-bound indices', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + values = [ + [ 3, 0, 0 ], + [ 1, 3, 0 ], + [ 1, 1, 3 ], + [ 3 ], + [ 1, 3 ], + [ 1, 1, 3, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, value, 5, out ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument whose length does not match the number of dimensions', function test( t ) { + var values; + var out; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + out = emptyLike( x ); + values = [ + [], + [ 0 ], + [ 0, 0 ], + [ 0, 0, 0, 0 ], + [ 0, 0, 0, 0, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, value, 5, out ); + }; + } +}); + +tape( 'the function throws an error if provided a fourth argument which is not an ndarray-like object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + 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() { + assign( x, [ 0, 0, 0 ], 5, value ); + }; + } +}); + +tape( 'the function throws an error if provided a fourth argument which does not have the same shape as the first argument', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + values = [ + zeros( [ 3 ] ), + zeros( [ 3, 3 ] ), + zeros( [ 3, 3, 2 ] ), + zeros( [ 2, 3, 3 ] ), + zeros( [ 3, 3, 3, 3 ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided an ndarray having shape (' + join( getShape( values[ i ] ), ',' ) + ')' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, [ 0, 0, 0 ], 5, value ); + }; + } +}); + +tape( 'the function does not modify the output ndarray if provided invalid indices', function test( t ) { + var expected; + var out; + var x; + + x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + out = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + expected = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]; + + t.throws( badValue( [ 2, 0 ] ), RangeError, 'throws an error when provided [2,0]' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.throws( badValue( [ 0, '1' ] ), TypeError, 'throws an error when provided [0,\'1\']' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, value, 5.0, out ); + }; + } +}); + +tape( 'the function copies elements from an input ndarray to an output ndarray and replaces the element at a specified index with a provided value', function test( t ) { + var expected; + var out; + var x; + var y; + + x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + + y = emptyLike( x ); + out = assign( x, [ 0, 0 ], 5.0, y ); + expected = [ [ 5.0, 2.0 ], [ 3.0, 4.0 ] ]; + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( x.get( 0, 0 ), 1.0, 'returns expected value' ); + + y = emptyLike( x ); + out = assign( x, [ 0, 1 ], 5.0, y ); + expected = [ [ 1.0, 5.0 ], [ 3.0, 4.0 ] ]; + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( x.get( 0, 1 ), 2.0, 'returns expected value' ); + + y = emptyLike( x ); + out = assign( x, [ 1, 1 ], 5.0, y ); + expected = [ [ 1.0, 2.0 ], [ 3.0, 5.0 ] ]; + t.strictEqual( out, y, 'returns expected value' ); + t.deepEqual( ndarray2array( out ), expected, 'returns expected value' ); + t.strictEqual( x.get( 1, 1 ), 4.0, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/with/test/test.js b/lib/node_modules/@stdlib/ndarray/with/test/test.js index 88b55478913d..23e413be5385 100644 --- a/lib/node_modules/@stdlib/ndarray/with/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/with/test/test.js @@ -21,7 +21,7 @@ // MODULES // var tape = require( 'tape' ); -var zeros = require( '@stdlib/ndarray/zeros' ); +var isMethod = require( '@stdlib/assert/is-method' ); var ndarrayWith = require( './../lib' ); @@ -33,140 +33,7 @@ tape( 'main export is a function', function test( t ) { t.end(); }); -tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', 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() { - ndarrayWith( value, [ 0, 0, 0 ], 5 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument which is not an array of integers', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 3, 3, 3 ], { - 'dtype': 'float64' - }); - values = [ - '5', - 5, - NaN, - true, - false, - null, - void 0, - [ 0, '1', 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() { - ndarrayWith( x, value, 5 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument with out-of-bound indices', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 3, 3, 3 ], { - 'dtype': 'float64' - }); - values = [ - [ 3, 0, 0 ], - [ 1, 3, 0 ], - [ 1, 1, 3 ], - [ 3 ], - [ 1, 3 ], - [ 1, 1, 3, 0 ] - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - ndarrayWith( x, value, 5 ); - }; - } -}); - -tape( 'the function throws an error if provided a second argument whose length does not match the number of dimensions', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 3, 3, 3 ], { - 'dtype': 'float64' - }); - values = [ - [], - [ 0 ], - [ 0, 0 ], - [ 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0 ] - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - ndarrayWith( x, value, 5 ); - }; - } -}); - -tape( 'the function returns a new ndarray with the element at a specified index replaced by a provided value', function test( t ) { - var out; - var x; - - x = zeros( [ 3, 3, 3 ], { - 'dtype': 'float64' - }); - - out = ndarrayWith( x, [ 0, 0, 0 ], 1.0 ); - t.notEqual( out, x, 'returns expected value' ); - t.strictEqual( out.get( 0, 0, 0 ), 1.0, 'returns expected value' ); - - out = ndarrayWith( x, [ 1, 1, 1 ], 1.0 ); - t.notEqual( out, x, 'returns expected value' ); - t.strictEqual( out.get( 1, 1, 1 ), 1.0, 'returns expected value' ); - - out = ndarrayWith( x, [ 2, 2, 2 ], 1.0 ); - t.notEqual( out, x, 'returns expected value' ); - t.strictEqual( out.get( 2, 2, 2 ), 1.0, 'returns expected value' ); - +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( ndarrayWith, 'assign' ), true, 'returns expected value' ); t.end(); }); diff --git a/lib/node_modules/@stdlib/ndarray/with/test/test.main.js b/lib/node_modules/@stdlib/ndarray/with/test/test.main.js new file mode 100644 index 000000000000..6991d28803fb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/with/test/test.main.js @@ -0,0 +1,172 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarrayWith = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ndarrayWith, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', 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() { + ndarrayWith( value, [ 0, 0, 0 ], 5 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an array of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 0, '1', 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() { + ndarrayWith( x, value, 5 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument with out-of-bound indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + values = [ + [ 3, 0, 0 ], + [ 1, 3, 0 ], + [ 1, 1, 3 ], + [ 3 ], + [ 1, 3 ], + [ 1, 1, 3, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + ndarrayWith( x, value, 5 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument whose length does not match the number of dimensions', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + values = [ + [], + [ 0 ], + [ 0, 0 ], + [ 0, 0, 0, 0 ], + [ 0, 0, 0, 0, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + ndarrayWith( x, value, 5 ); + }; + } +}); + +tape( 'the function returns a new ndarray with the element at a specified index replaced by a provided value', function test( t ) { + var out; + var x; + + x = zeros( [ 3, 3, 3 ], { + 'dtype': 'float64' + }); + + out = ndarrayWith( x, [ 0, 0, 0 ], 1.0 ); + t.notEqual( out, x, 'returns expected value' ); + t.strictEqual( out.get( 0, 0, 0 ), 1.0, 'returns expected value' ); + + out = ndarrayWith( x, [ 1, 1, 1 ], 1.0 ); + t.notEqual( out, x, 'returns expected value' ); + t.strictEqual( out.get( 1, 1, 1 ), 1.0, 'returns expected value' ); + + out = ndarrayWith( x, [ 2, 2, 2 ], 1.0 ); + t.notEqual( out, x, 'returns expected value' ); + t.strictEqual( out.get( 2, 2, 2 ), 1.0, 'returns expected value' ); + + t.end(); +});