From df02e15f64e359e1d96ace728605d082fb4e8a15 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 13 Aug 2026 23:21:50 +0500 Subject: [PATCH 1/8] feat: add blas/ext/copy-within --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/blas/ext/copy-within/README.md | 154 ++ .../ext/copy-within/benchmark/benchmark.js | 103 ++ .../blas/ext/copy-within/docs/repl.txt | 62 + .../ext/copy-within/docs/types/index.d.ts | 132 ++ .../blas/ext/copy-within/docs/types/test.ts | 190 +++ .../blas/ext/copy-within/examples/index.js | 37 + .../@stdlib/blas/ext/copy-within/lib/base.js | 127 ++ .../ext/copy-within/lib/broadcast_index.js | 70 + .../@stdlib/blas/ext/copy-within/lib/index.js | 61 + .../@stdlib/blas/ext/copy-within/lib/main.js | 189 +++ .../ext/copy-within/lib/normalize_indices.js | 96 ++ .../@stdlib/blas/ext/copy-within/package.json | 65 + .../@stdlib/blas/ext/copy-within/test/test.js | 1433 +++++++++++++++++ 13 files changed, 2719 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md new file mode 100644 index 000000000000..6e420d8de2e9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md @@ -0,0 +1,154 @@ + + +# copyWithin + +> Perform an in-place copy of elements within an [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +## Usage + +```javascript +var copyWithin = require( '@stdlib/blas/ext/copy-within' ); +``` + +#### copyWithin( x, target, start\[, end]\[, options] ) + +Performs an in-place copy of elements within an [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] + +var y = copyWithin( x, 3, 1, 4 ); +// returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] + +var bool = ( x === y ); +// returns true +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **target**: target index. May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] target index must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] target index must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. +- **start**: source start index (inclusive). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. +- **end**: source end index (exclusive) (_optional_). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. By default, the source end index is the number of indexed elements. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. + +Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds (i.e., the function follows the same conventions as [`Array.prototype.copyWithin`][mdn-array-copywithin]). + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + +var y = copyWithin( x, 0, -2 ); +// returns [ 5.0, 6.0, 3.0, 4.0, 5.0, 6.0 ] +``` + +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ] ); + +var y = copyWithin( x, 2, 0, { + 'dims': [ 1 ] +}); +// returns [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 5.0, 6.0 ] ] +``` + +
+ + + +
+ +## Notes + +- The input [ndarray][@stdlib/ndarray/ctor] is copied **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**). +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before performing the operation. +- If the `start` and `target` index ranges overlap, the function internally allocates a temporary workspace in order to avoid overwriting source elements before they are copied. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copyWithin = require( '@stdlib/blas/ext/copy-within' ); + +// Generate an ndarray of random numbers: +var x = discreteUniform( [ 5, 5 ], 0, 20, { + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +copyWithin( x, -2, 0, 2, { + 'dims': [ 1 ] +}); + +// Print the results: +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js new file mode 100644 index 000000000000..c30d34baae44 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/uniform' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var copyWithin = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( [ len ], -50.0, 50.0, options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = copyWithin( x, 1, 0, len-1 ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt new file mode 100644 index 000000000000..af6d6ddf184f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt @@ -0,0 +1,62 @@ + +{{alias}}( x, target, start[, end][, options] ) + Performs an in-place copy of elements within an ndarray along one or more + ndarray dimensions. + + Negative indices are resolved relative to the number of indexed elements, + with resolved indices clamped to index bounds. + + The function copies elements within an input ndarray in-place and thus + mutates an input ndarray. + + Parameters + ---------- + x: ndarray + Input array. + + target: ndarray|integer + Target index. May be either an integer or an ndarray having an integer + index or "generic" data type. If provided an ndarray, the value must + have a shape which is broadcast compatible with the complement of the + shape defined by `options.dims`. For example, given the input shape + `[2, 3, 4]` and `options.dims=[0]`, an ndarray target index must have + a shape which is broadcast compatible with the shape `[3, 4]`. + Similarly, when performing the operation over all elements in a + provided input ndarray, an ndarray target index must be a + zero-dimensional ndarray. + + start: ndarray|integer + Source start index (inclusive). May be either an integer or an ndarray + having an integer index or "generic" data type and following the same + broadcasting semantics as the `target` argument. + + end: ndarray|integer (optional) + Source end index (exclusive). May be either an integer or an ndarray + having an integer index or "generic" data type and following the same + broadcasting semantics as the `target` argument. Default: the number of + indexed elements. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform operation. If not provided, the + function performs the operation over all elements in a provided input + ndarray. + + Returns + ------- + out: ndarray + Input array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var y = {{alias}}( x, 2, 0 ) + [ 1.0, 2.0, 1.0, 2.0 ] + > var bool = ( x === y ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts new file mode 100644 index 000000000000..947df18abe01 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts @@ -0,0 +1,132 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; +import { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Index argument. +*/ +type IndexArgument = typedndarray | number; + +/** +* Interface defining options. +*/ +interface Options { + /** + * List of dimensions over which to perform operation. + */ + dims?: ArrayLike; +} + +/** +* Interface for performing an in-place copy of elements within an ndarray. +*/ +interface CopyWithin { + /** + * Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. + * + * ## Notes + * + * - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). + * - Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds. + * + * @param x - input ndarray + * @param target - target index + * @param start - source start index (inclusive) + * @param options - function options + * @returns input ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); + * + * var y = copyWithin( x, 2, 0 ); + * // returns [ 1.0, 2.0, 1.0, 2.0 ] + * + * var bool = ( x === y ); + * // returns true + */ + >( x: T, target: IndexArgument, start: IndexArgument, options?: Options ): T; + + /** + * Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. + * + * ## Notes + * + * - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). + * - Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds. + * + * @param x - input ndarray + * @param target - target index + * @param start - source start index (inclusive) + * @param end - source end index (exclusive) + * @param options - function options + * @returns input ndarray + * + * @example + * var array = require( '@stdlib/ndarray/array' ); + * + * var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + * + * var y = copyWithin( x, 3, 1, 4 ); + * // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] + * + * var bool = ( x === y ); + * // returns true + */ + >( x: T, target: IndexArgument, start: IndexArgument, end: IndexArgument, options?: Options ): T; +} + +/** +* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* +* ## Notes +* +* - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). +* - Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds. +* +* @param x - input ndarray +* @param target - target index +* @param start - source start index (inclusive) +* @param end - source end index (exclusive) +* @param options - function options +* @returns input ndarray +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* var y = copyWithin( x, 3, 1, 4 ); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( x === y ); +* // returns true +*/ +declare const copyWithin: CopyWithin; + + +// EXPORTS // + +export = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts new file mode 100644 index 000000000000..49ee55142fd0 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts @@ -0,0 +1,190 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +/// + +import zeros = require( '@stdlib/ndarray/zeros' ); +import copyWithin = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0 ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, {} ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, 4 ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), 2, 0, 4, {} ); // $ExpectType float64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float32' } ), 2, 0 ); // $ExpectType float32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'float32' } ), 2, 0, {} ); // $ExpectType float32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'int32' } ), 2, 0 ); // $ExpectType int32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'int32' } ), 2, 0, {} ); // $ExpectType int32ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex128' } ), 2, 0 ); // $ExpectType complex128ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex128' } ), 2, 0, {} ); // $ExpectType complex128ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex64' } ), 2, 0 ); // $ExpectType complex64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'complex64' } ), 2, 0, {} ); // $ExpectType complex64ndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'generic' } ), 2, 0 ); // $ExpectType genericndarray + copyWithin( zeros( [ 2, 2 ], { 'dtype': 'generic' } ), 2, 0, {} ); // $ExpectType genericndarray +} + +// The function returns an ndarray when provided ndarray index arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, x, x ); // $ExpectType float64ndarray + copyWithin( x, x, x, {} ); // $ExpectType float64ndarray + copyWithin( x, x, x, x ); // $ExpectType float64ndarray + copyWithin( x, x, x, x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + copyWithin( '5', 2, 0 ); // $ExpectError + copyWithin( 5, 2, 0 ); // $ExpectError + copyWithin( true, 2, 0 ); // $ExpectError + copyWithin( false, 2, 0 ); // $ExpectError + copyWithin( null, 2, 0 ); // $ExpectError + copyWithin( void 0, 2, 0 ); // $ExpectError + copyWithin( {}, 2, 0 ); // $ExpectError + copyWithin( ( x: number ): number => x, 2, 0 ); // $ExpectError + + copyWithin( '5', 2, 0, {} ); // $ExpectError + copyWithin( 5, 2, 0, {} ); // $ExpectError + copyWithin( true, 2, 0, {} ); // $ExpectError + copyWithin( false, 2, 0, {} ); // $ExpectError + copyWithin( null, 2, 0, {} ); // $ExpectError + copyWithin( void 0, 2, 0, {} ); // $ExpectError + copyWithin( {}, 2, 0, {} ); // $ExpectError + copyWithin( ( x: number ): number => x, 2, 0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a target index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, '5', 0 ); // $ExpectError + copyWithin( x, true, 0 ); // $ExpectError + copyWithin( x, false, 0 ); // $ExpectError + copyWithin( x, null, 0 ); // $ExpectError + copyWithin( x, [], 0 ); // $ExpectError + copyWithin( x, ( x: number ): number => x, 0 ); // $ExpectError + + copyWithin( x, '5', 0, {} ); // $ExpectError + copyWithin( x, true, 0, {} ); // $ExpectError + copyWithin( x, false, 0, {} ); // $ExpectError + copyWithin( x, null, 0, {} ); // $ExpectError + copyWithin( x, [], 0, {} ); // $ExpectError + copyWithin( x, ( x: number ): number => x, 0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a start index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, '5' ); // $ExpectError + copyWithin( x, 2, true ); // $ExpectError + copyWithin( x, 2, false ); // $ExpectError + copyWithin( x, 2, null ); // $ExpectError + copyWithin( x, 2, [] ); // $ExpectError + copyWithin( x, 2, ( x: number ): number => x ); // $ExpectError + + copyWithin( x, 2, '5', {} ); // $ExpectError + copyWithin( x, 2, true, {} ); // $ExpectError + copyWithin( x, 2, false, {} ); // $ExpectError + copyWithin( x, 2, null, {} ); // $ExpectError + copyWithin( x, 2, [], {} ); // $ExpectError + copyWithin( x, 2, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an end index argument which is not an ndarray or integer... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, '5', {} ); // $ExpectError + copyWithin( x, 2, 0, true, {} ); // $ExpectError + copyWithin( x, 2, 0, false, {} ); // $ExpectError + copyWithin( x, 2, 0, null, {} ); // $ExpectError + copyWithin( x, 2, 0, [], {} ); // $ExpectError + copyWithin( x, 2, 0, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, '5' ); // $ExpectError + copyWithin( x, 2, 0, true ); // $ExpectError + copyWithin( x, 2, 0, false ); // $ExpectError + copyWithin( x, 2, 0, null ); // $ExpectError + copyWithin( x, 2, 0, [] ); // $ExpectError + copyWithin( x, 2, 0, ( x: number ): number => x ); // $ExpectError + + copyWithin( x, 2, 0, 4, '5' ); // $ExpectError + copyWithin( x, 2, 0, 4, true ); // $ExpectError + copyWithin( x, 2, 0, 4, false ); // $ExpectError + copyWithin( x, 2, 0, 4, null ); // $ExpectError + copyWithin( x, 2, 0, 4, [] ); // $ExpectError + copyWithin( x, 2, 0, 4, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + copyWithin( x, 2, 0, { 'dims': '5' } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': 5 } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': true } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': false } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': null } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': {} } ); // $ExpectError + copyWithin( x, 2, 0, { 'dims': ( x: number ): number => x } ); // $ExpectError + + copyWithin( x, 2, 0, 4, { 'dims': '5' } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': 5 } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': true } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': false } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': null } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': {} } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dims': ( 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 ], { + 'dtype': 'float64' + }); + + copyWithin(); // $ExpectError + copyWithin( x ); // $ExpectError + copyWithin( x, 2 ); // $ExpectError + copyWithin( x, 2, 0, 4, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js new file mode 100644 index 000000000000..eac594d1a303 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var copyWithin = require( './../lib' ); + +// Generate an ndarray of random numbers: +var x = discreteUniform( [ 5, 5 ], 0, 20, { + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +// Perform operation: +copyWithin( x, -2, 0, 2, { + 'dims': [ 1 ] +}); + +// Print the results: +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js new file mode 100644 index 000000000000..fa33cc9bda4a --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js @@ -0,0 +1,127 @@ +/** +* @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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var gcopyWithin = require( '@stdlib/blas/ext/base/ndarray/gcopy-within' ); +var dcopyWithin = require( '@stdlib/blas/ext/base/ndarray/dcopy-within' ); +var scopyWithin = require( '@stdlib/blas/ext/base/ndarray/scopy-within' ); +var zcopyWithin = require( '@stdlib/blas/ext/base/ndarray/zcopy-within' ); +var ccopyWithin = require( '@stdlib/blas/ext/base/ndarray/ccopy-within' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); + + +// VARIABLES // + +var idtypes0 = dtypes( 'integer_index_and_generic' ); // target/start/end ndarrays +var idtypes1 = dtypes( 'all' ); // workspace ndarray +var odtypes = dtypes( 'all' ); +var table = { + 'types': [ + 'float64', // input/output + 'float32', // input/output + 'complex128', // input/output + 'complex64' // input/output + ], + 'fcns': [ + dcopyWithin, + scopyWithin, + zcopyWithin, + ccopyWithin + ], + 'default': gcopyWithin +}; +var options = { + 'strictTraversalOrder': true +}; + + +// MAIN // + +/** +* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* +* @private +* @name copyWithin +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {ndarray} target - ndarray containing the target index +* @param {ndarray} start - ndarray containing the source start index (inclusive) +* @param {ndarray} end - ndarray containing the source end index (exclusive) +* @param {ndarray} workspace - workspace ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} index arguments must be ndarray-like objects +* @throws {TypeError} workspace argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create ndarrays containing the target, start, and end indices: +* var opts = { +* 'dtype': 'int32' +* }; +* var target = scalar2ndarray( 3, opts ); +* var start = scalar2ndarray( 1, opts ); +* var end = scalar2ndarray( 4, opts ); +* +* // Create a workspace ndarray: +* var w = zeros( [ 6 ], { +* 'dtype': 'float64' +* }); +* +* // Perform operation: +* var out = copyWithin( x, target, start, end, w ); +* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* +* var bool = ( out === x ); +* // returns true +*/ +var copyWithin = factory( table, [ idtypes0, idtypes0, idtypes0, idtypes1 ], odtypes, options ); // eslint-disable-line max-len + + +// EXPORTS // + +module.exports = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js new file mode 100644 index 000000000000..14dbbc488bec --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); +var getDefault = require( '@stdlib/ndarray/defaults' ).get; +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var DEFAULT_DTYPE = getDefault( 'dtypes.integer_index' ); + + +// MAIN // + +/** +* Broadcasts an index argument to match the shape of the non-core dimensions. +* +* ## Notes +* +* - When a list of dimensions is not provided, the operation is performed across all dimensions, and, thus, an ndarray index argument is expected to be a zero-dimensional ndarray. +* +* @private +* @param {(ndarray|NonNegativeInteger)} idx - index argument +* @param {string} name - argument ordinal name +* @param {NonNegativeIntegerArray} shape - shape of the non-core dimensions +* @param {string} order - memory layout order +* @param {boolean} hasDims - boolean indicating whether a list of dimensions was provided +* @throws {TypeError} index argument must be a zero-dimensional ndarray +* @returns {ndarray} ndarray containing index values +*/ +function broadcastIndex( idx, name, shape, order, hasDims ) { + if ( isInteger( idx ) ) { + return broadcastScalar( idx, DEFAULT_DTYPE, shape, order ); + } + if ( hasDims ) { + return maybeBroadcastArray( idx, shape ); + } + if ( ndims( idx ) === 0 ) { + return idx; + } + throw new TypeError( format( 'invalid argument. %s argument must be a zero-dimensional ndarray.', name ) ); +} + + +// EXPORTS // + +module.exports = broadcastIndex; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js new file mode 100644 index 000000000000..286f8e9ec69c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js @@ -0,0 +1,61 @@ +/** +* @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'; + +/** +* Perform an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* +* @module @stdlib/blas/ext/copy-within +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var copyWithin = require( '@stdlib/blas/ext/copy-within' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = copyWithin( x, 3, 1, 4 ); +* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* +* var bool = ( out === x ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js new file mode 100644 index 000000000000..5a21eafdbf98 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -0,0 +1,189 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var empty = require( '@stdlib/ndarray/base/empty' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var format = require( '@stdlib/string/format' ); +var normalizeIndices = require( './normalize_indices.js' ); +var broadcastIndex = require( './broadcast_index.js' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* +* @param {ndarrayLike} x - input ndarray +* @param {(ndarrayLike|integer)} target - target index +* @param {(ndarrayLike|integer)} start - source start index (inclusive) +* @param {(ndarrayLike|integer)} [end] - source end index (exclusive) +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} index arguments must be either ndarray-like objects or integers +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 3, 1, 2 ]; +* +* // Define the array strides: +* var sx = [ 2, 2, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Perform operation: +* var out = copyWithin( x, 3, 1, 4 ); +* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* +* var bool = ( out === x ); +* // returns true +*/ +function copyWithin( x, target, start ) { + var hasOptions; + var options; + var hasDims; + var hasEnd; + var nargs; + var end; + var lsh; + var len; + var ord; + var sh; + var dt; + var d; + var e; + var s; + var t; + var w; + + nargs = arguments.length; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( target ) && !isndarrayLike( target ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray or an integer. Value: `%s`.', target ) ); + } + if ( !isInteger( start ) && !isndarrayLike( start ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray or an integer. Value: `%s`.', start ) ); + } + // Resolve input ndarray meta data: + dt = getDType( x ); + ord = getOrder( x ); + sh = getShape( x ); + + // Initialize flags indicating whether `end` and `options` arguments were provided: + hasOptions = false; + hasEnd = false; + + // Case: copyWithin( x, target, start, end, options ) + if ( nargs > 4 ) { + end = arguments[ 3 ]; + if ( !isInteger( end ) && !isndarrayLike( end ) ) { + throw new TypeError( format( 'invalid argument. Fourth argument must be either an ndarray or an integer. Value: `%s`.', end ) ); + } + hasEnd = true; + options = arguments[ 4 ]; + hasOptions = true; + } + // Case: copyWithin( x, target, start, ??? ) + else if ( nargs === 4 ) { + // Case: copyWithin( x, target, start, end ) + if ( isInteger( arguments[ 3 ] ) || isndarrayLike( arguments[ 3 ] ) ) { + end = arguments[ 3 ]; + hasEnd = true; + } + // Case: copyWithin( x, target, start, options ) + else { + options = arguments[ 3 ]; + hasOptions = true; + } + } + if ( hasOptions && !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + hasDims = ( hasOptions && hasOwnProp( options, 'dims' ) ); + + // Resolve the shape of the non-core dimensions and the number of indexed elements... + if ( hasDims ) { + lsh = nonCoreShape( sh, options.dims ); + d = toUniqueNormalizedIndices( options.dims, sh.length-1 ); + if ( d === null ) { + // The `dims` option contains invalid dimension indices, so we defer validation to `base`: + len = 1; + } else { + len = ( d.length === 0 ) ? 1 : numel( takeIndexed( sh, d ) ); + } + } else { + lsh = []; + len = ( sh.length === 0 ) ? 1 : numel( sh ); + } + // Normalize the provided indices, defaulting to the number of indexed elements when not provided an ending index: + t = normalizeIndices( target, len ); + s = normalizeIndices( start, len ); + e = ( hasEnd ) ? normalizeIndices( end, len ) : len; + + // Broadcast the indices to match the shape of the non-core dimensions... + t = broadcastIndex( t, 'Second', lsh, ord, hasDims ); + s = broadcastIndex( s, 'Third', lsh, ord, hasDims ); + e = broadcastIndex( e, 'Fourth', lsh, ord, hasDims ); + + // Allocate a workspace ndarray which is broadcast across the non-core dimensions, thus allowing each one-dimensional slice to reuse the same workspace buffer: + w = broadcastArray( empty( dt, [ len ], ord ), lsh.concat( [ len ] ) ); + + // Perform operation: + if ( hasOptions ) { + return base( x, t, s, e, w, options ); + } + return base( x, t, s, e, w ); +} + + +// EXPORTS // + +module.exports = copyWithin; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js new file mode 100644 index 000000000000..fda4b4f5dadb --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/normalize_indices.js @@ -0,0 +1,96 @@ +/** +* @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 unary = require( '@stdlib/ndarray/base/unary' ); +var baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); + + +// FUNCTIONS // + +/** +* Normalizes a relative index. +* +* @private +* @param {integer} idx - relative index +* @param {NonNegativeInteger} len - number of indexed elements +* @returns {NonNegativeInteger} normalized index +*/ +function normalize( idx, len ) { + if ( idx < 0 ) { + idx += len; + if ( idx < 0 ) { + return 0; + } + return idx; + } + if ( idx > len ) { + return len; + } + return idx; +} + + +// MAIN // + +/** +* Normalizes an index argument. +* +* ## Notes +* +* - Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds. +* - If provided an ndarray, the function normalizes each element and returns a new ndarray having the same shape and data type as the provided ndarray. +* +* @private +* @param {(ndarray|integer)} idx - index argument +* @param {NonNegativeInteger} len - number of indexed elements +* @returns {(ndarray|NonNegativeInteger)} normalized index argument +*/ +function normalizeIndices( idx, len ) { + var out; + + if ( isInteger( idx ) ) { + return normalize( idx, len ); + } + out = baseEmpty( getDType( idx ), getShape( idx, false ), getOrder( idx ) ); + unary( [ idx, out ], clbk ); + return out; + + /** + * Normalizes a relative index. + * + * @private + * @param {integer} v - relative index + * @returns {NonNegativeInteger} normalized index + */ + function clbk( v ) { + return normalize( v, len ); + } +} + + +// EXPORTS // + +module.exports = normalizeIndices; diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/package.json b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json new file mode 100644 index 000000000000..d608b63e8267 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/blas/ext/copy-within", + "version": "0.0.0", + "description": "Perform an in-place copy of elements within an ndarray along one or more ndarray dimensions.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "copy", + "copywithin", + "within", + "move", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js new file mode 100644 index 000000000000..be66f685bc28 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js @@ -0,0 +1,1433 @@ +/** +* @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 isSameArray = require( '@stdlib/assert/is-same-array' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var copyWithin = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof copyWithin, '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() { + copyWithin( value, 1, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (end)', 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() { + copyWithin( value, 1, 0, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', 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() { + copyWithin( value, 1, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (end, options)', 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() { + copyWithin( value, 1, 0, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, value, 0, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer (end, options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, value, 0, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, value, 2 ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not an ndarray-like object or an integer (end, options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, value, 2, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not an ndarray-like object or an integer (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (dims)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, value, 0, { + 'dims': [ 0 ] + }); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `start` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not broadcast-compatible', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an `end` argument which is not broadcast-compatible (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + opts = { + 'dtype': 'generic' + }; + x = zeros( [ 2, 2 ], opts ); + + values = [ + zeros( [ 4 ], opts ), + zeros( [ 2, 2, 2 ], opts ), + zeros( [ 0 ], opts ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 3.14, + 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() { + copyWithin( x, 1, 0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + 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() { + copyWithin( x, 1, 0, 2, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + [ 'a' ], + {}, + 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() { + copyWithin( x, 1, 0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + [ 'a' ], + {}, + 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() { + copyWithin( x, 1, 0, 2, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + 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() { + copyWithin( x, 1, 0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ -10 ], + [ 0, 20 ], + [ 20 ] + ]; + 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() { + copyWithin( x, 1, 0, 2, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + 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() { + copyWithin( x, 1, 0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 2 ], + [ 0, 1, 2, 3 ] + ]; + 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() { + copyWithin( x, 1, 0, 2, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices (end)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, 1, 0 ], + [ 1, 0, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + copyWithin( x, 1, 0, 2, { + 'dims': value + }); + }; + } +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (default, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 2, 0 ); + expected = [ 1.0, 2.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (default, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 2, 0 ); + expected = [ 1.0, 2.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (all dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 2, 0, { + 'dims': [ 0, 1 ] + }); + expected = [ 1.0, 2.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (all dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 2, 0, { + 'dims': [ 0, 1 ] + }); + expected = [ 1.0, 2.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (no dimensions, row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [] + }); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (no dimensions, column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [] + }); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [ 0 ] + }); + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [ 1 ] + }); + expected = [ [ 1.0, 1.0 ], [ 3.0, 3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [ 0 ] + }); + expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + actual = copyWithin( x, 1, 0, { + 'dims': [ 1 ] + }); + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an ending index', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'column-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); + t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing negative indices', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 0, -2 ); + expected = [ 5.0, 6.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, -2, 0, 2 ); + expected = [ 1.0, 2.0, 3.0, 4.0, 1.0, 2.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 0, -3, -1 ); + expected = [ 4.0, 5.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function clamps out-of-bounds indices', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 10, 0, 3 ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 2, 0, 100 ); + expected = [ 1.0, 2.0, 1.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, -100, 2, 4 ); + expected = [ 3.0, 4.0, 3.0, 4.0, 5.0, 6.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as zero-dimensional ndarrays', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + opts = { + 'dtype': 'int32' + }; + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, scalar2ndarray( 3, opts ), scalar2ndarray( 1, opts ), scalar2ndarray( 4, opts ) ); // eslint-disable-line max-len + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + opts = { + 'dtype': 'generic' + }; + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, scalar2ndarray( 3, opts ), scalar2ndarray( 1, opts ), scalar2ndarray( 4, opts ) ); // eslint-disable-line max-len + expected = [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as zero-dimensional ndarrays (broadcasted)', function test( t ) { + var expected; + var actual; + var xbuf; + var opts; + var x; + + opts = { + 'dtype': 'int32' + }; + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + actual = copyWithin( x, scalar2ndarray( 2, opts ), 0, { + 'dims': [ 1 ] + }); + expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 5.0, 6.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as ndarrays', function test( t ) { + var expected; + var target; + var actual; + var xbuf; + var tbuf; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + x = new ndarray( 'generic', xbuf, [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + tbuf = new Int32Array( [ 0, 2 ] ); + target = new ndarray( 'int32', tbuf, [ 2 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, target, 2, 4, { + 'dims': [ 1 ] + }); + expected = [ [ 3.0, 4.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing index arguments as ndarrays containing negative indices', function test( t ) { + var expected; + var target; + var actual; + var x; + + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ], [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); + + target = new ndarray( 'generic', [ -2, 0 ], [ 2 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, target, 0, 2, { + 'dims': [ 1 ] + }); + expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (float64)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new ndarray( 'float64', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = new Float64Array( [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function performs an in-place copy of elements within an ndarray (complex dtype)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Complex128Array([ + 1.0, // re + 1.0, // im + 2.0, // re + 2.0, // im + 3.0, // re + 3.0, // im + 4.0, // re + 4.0, // im + 5.0, // re + 5.0, // im + 6.0, // re + 6.0 // im + ]); + x = new ndarray( 'complex128', xbuf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = copyWithin( x, 3, 1, 4 ); + expected = new Complex128Array([ + 1.0, // re + 1.0, // im + 2.0, // re + 2.0, // im + 3.0, // re + 3.0, // im + 2.0, // re + 2.0, // im + 3.0, // re + 3.0, // im + 4.0, // re + 4.0 // im + ]); + + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), 'complex128', 'returns expected value' ); + t.strictEqual( isSameComplex128Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); From c532968f7f6134a146720bfd89d1a62c022dbcdd Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 14 Aug 2026 13:56:10 +0500 Subject: [PATCH 2/8] fix: cleanup --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: 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: passed - task: lint_license_headers status: passed --- --- .../@stdlib/blas/ext/copy-within/README.md | 4 +- .../blas/ext/copy-within/docs/repl.txt | 5 +- .../ext/copy-within/docs/types/index.d.ts | 6 +- .../ext/copy-within/lib/normalize_indices.js | 2 +- .../@stdlib/blas/ext/copy-within/test/test.js | 163 +++--------------- 5 files changed, 31 insertions(+), 149 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md index 6e420d8de2e9..8120dd100f97 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md @@ -59,7 +59,7 @@ The function accepts the following options: - **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. -Negative indices are resolved relative to the number of indexed elements, with resolved indices clamped to index bounds (i.e., the function follows the same conventions as [`Array.prototype.copyWithin`][mdn-array-copywithin]). +When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with out-of-bounds indices clamped to index bounds. ```javascript var array = require( '@stdlib/ndarray/array' ); @@ -141,8 +141,6 @@ console.log( ndarray2array( x ) ); @@ -118,7 +118,7 @@ console.log( ndarray2array( x ) ); // Perform operation: copyWithin( x, -2, 0, 2, { - 'dims': [ 1 ] + 'dim': 0 }); // Print the results: diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt index 56c6554965c2..26986b047da0 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt @@ -1,7 +1,7 @@ {{alias}}( x, target, start[, end][, options] ) - Performs an in-place copy of elements within an ndarray along one or more - ndarray dimensions. + Performs an in-place copy of elements within an ndarray along an ndarray + dimension. When a `target`, `start`, and/or `end` index is negative, the respective index is determined relative to the last indexed element, with @@ -19,12 +19,9 @@ Target index. May be either an integer or an ndarray having an integer index or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast compatible with the complement of the - shape defined by `options.dims`. For example, given the input shape - `[2, 3, 4]` and `options.dims=[0]`, an ndarray target index must have - a shape which is broadcast compatible with the shape `[3, 4]`. - Similarly, when performing the operation over all elements in a - provided input ndarray, an ndarray target index must be a - zero-dimensional ndarray. + shape defined by `options.dim`. For example, given the input shape + `[2, 3, 4]` and `options.dim=0`, an ndarray target index must have a + shape which is broadcast compatible with the shape `[3, 4]`. start: ndarray|integer Source start index (inclusive). May be either an integer or an ndarray @@ -40,10 +37,11 @@ options: Object (optional) Function options. - options.dims: Array (optional) - List of dimensions over which to perform operation. If not provided, the - function performs the operation over all elements in a provided input - ndarray. + options.dim: integer (optional) + Dimension over which to perform operation. If provided a negative + integer, the dimension along which to perform the operation is + determined by counting backward from the last dimension (where `-1` + refers to the last dimension). Default: `-1`. Returns ------- diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts index fb5d34f7732e..c4e6cddd4c8f 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts @@ -20,7 +20,6 @@ /// -import { ArrayLike } from '@stdlib/types/array'; import { typedndarray } from '@stdlib/types/ndarray'; /** @@ -33,9 +32,9 @@ type IndexArgument = typedndarray | number; */ interface Options { /** - * List of dimensions over which to perform operation. + * Dimension over which to perform operation. Default: `-1`. */ - dims?: ArrayLike; + dim?: number; } /** @@ -43,7 +42,7 @@ interface Options { */ interface CopyWithin { /** - * Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. + * Performs an in-place copy of elements within an ndarray along an ndarray dimension. * * ## Notes * @@ -70,7 +69,7 @@ interface CopyWithin { >( x: T, target: IndexArgument, start: IndexArgument, options?: Options ): T; /** - * Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. + * Performs an in-place copy of elements within an ndarray along an ndarray dimension. * * ## Notes * @@ -99,7 +98,7 @@ interface CopyWithin { } /** -* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. * * ## Notes * diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts index 49ee55142fd0..ac6d3f8d11e8 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/test.ts @@ -154,27 +154,27 @@ import copyWithin = require( './index' ); copyWithin( x, 2, 0, 4, ( x: number ): number => x ); // $ExpectError } -// The compiler throws an error if the function is provided an invalid `dims` option... +// The compiler throws an error if the function is provided an invalid `dim` option... { const x = zeros( [ 2, 2 ], { 'dtype': 'float64' }); - copyWithin( x, 2, 0, { 'dims': '5' } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': 5 } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': true } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': false } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': null } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': {} } ); // $ExpectError - copyWithin( x, 2, 0, { 'dims': ( x: number ): number => x } ); // $ExpectError - - copyWithin( x, 2, 0, 4, { 'dims': '5' } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': 5 } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': true } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': false } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': null } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': {} } ); // $ExpectError - copyWithin( x, 2, 0, 4, { 'dims': ( x: number ): number => x } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': '5' } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': true } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': false } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': null } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': [] } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': {} } ); // $ExpectError + copyWithin( x, 2, 0, { 'dim': ( x: number ): number => x } ); // $ExpectError + + copyWithin( x, 2, 0, 4, { 'dim': '5' } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': true } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': false } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': null } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': [] } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': {} } ); // $ExpectError + copyWithin( x, 2, 0, 4, { 'dim': ( x: number ): number => x } ); // $ExpectError } // The compiler throws an error if the function is provided an unsupported number of arguments... diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js index eac594d1a303..85cb042df2a8 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/examples/index.js @@ -30,7 +30,7 @@ console.log( ndarray2array( x ) ); // Perform operation: copyWithin( x, -2, 0, 2, { - 'dims': [ 1 ] + 'dim': 0 }); // Print the results: diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js index fa33cc9bda4a..74f613196580 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/base.js @@ -57,7 +57,7 @@ var options = { // MAIN // /** -* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. * * @private * @name copyWithin @@ -74,7 +74,6 @@ var options = { * @throws {TypeError} workspace argument must be an ndarray-like object * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension indices must not exceed input ndarray bounds -* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions * @throws {Error} must provide valid options * @returns {ndarray} input ndarray * @@ -88,10 +87,10 @@ var options = { * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * // Define the shape of the input array: -* var sh = [ 3, 1, 2 ]; +* var sh = [ 6 ]; * * // Define the array strides: -* var sx = [ 2, 2, 1 ]; +* var sx = [ 1 ]; * * // Define the index offset: * var ox = 0; @@ -113,8 +112,10 @@ var options = { * }); * * // Perform operation: -* var out = copyWithin( x, target, start, end, w ); -* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* var out = copyWithin( x, target, start, end, w, { +* 'dims': [ 0 ] +* }); +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] * * var bool = ( out === x ); * // returns true diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js index 14dbbc488bec..1bdbb12ce481 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/broadcast_index.js @@ -23,9 +23,7 @@ var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); -var ndims = require( '@stdlib/ndarray/base/ndims' ); var getDefault = require( '@stdlib/ndarray/defaults' ).get; -var format = require( '@stdlib/string/format' ); // VARIABLES // @@ -38,30 +36,17 @@ var DEFAULT_DTYPE = getDefault( 'dtypes.integer_index' ); /** * Broadcasts an index argument to match the shape of the non-core dimensions. * -* ## Notes -* -* - When a list of dimensions is not provided, the operation is performed across all dimensions, and, thus, an ndarray index argument is expected to be a zero-dimensional ndarray. -* * @private * @param {(ndarray|NonNegativeInteger)} idx - index argument -* @param {string} name - argument ordinal name * @param {NonNegativeIntegerArray} shape - shape of the non-core dimensions * @param {string} order - memory layout order -* @param {boolean} hasDims - boolean indicating whether a list of dimensions was provided -* @throws {TypeError} index argument must be a zero-dimensional ndarray * @returns {ndarray} ndarray containing index values */ -function broadcastIndex( idx, name, shape, order, hasDims ) { +function broadcastIndex( idx, shape, order ) { if ( isInteger( idx ) ) { return broadcastScalar( idx, DEFAULT_DTYPE, shape, order ); } - if ( hasDims ) { - return maybeBroadcastArray( idx, shape ); - } - if ( ndims( idx ) === 0 ) { - return idx; - } - throw new TypeError( format( 'invalid argument. %s argument must be a zero-dimensional ndarray.', name ) ); + return maybeBroadcastArray( idx, shape ); } diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js index 286f8e9ec69c..a2aba9c0ca06 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Perform an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* Perform an in-place copy of elements within an ndarray along an ndarray dimension. * * @module @stdlib/blas/ext/copy-within * @@ -32,10 +32,10 @@ * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * // Define the shape of the input array: -* var sh = [ 3, 1, 2 ]; +* var sh = [ 6 ]; * * // Define the array strides: -* var sx = [ 2, 2, 1 ]; +* var sx = [ 1 ]; * * // Define the index offset: * var ox = 0; @@ -45,7 +45,7 @@ * * // Perform operation: * var out = copyWithin( x, 3, 1, 4 ); -* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] * * var bool = ( out === x ); * // returns true diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js index 5a21eafdbf98..f145a6a374eb 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -27,8 +27,6 @@ var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' ); var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); -var takeIndexed = require( '@stdlib/array/base/take-indexed' ); -var numel = require( '@stdlib/ndarray/base/numel' ); var empty = require( '@stdlib/ndarray/base/empty' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); @@ -42,19 +40,19 @@ var base = require( './base.js' ); // MAIN // /** -* Performs an in-place copy of elements within an ndarray along one or more ndarray dimensions. +* Performs an in-place copy of elements within an ndarray along an ndarray dimension. * * @param {ndarrayLike} x - input ndarray * @param {(ndarrayLike|integer)} target - target index * @param {(ndarrayLike|integer)} start - source start index (inclusive) * @param {(ndarrayLike|integer)} [end] - source end index (exclusive) * @param {Options} [options] - function options -* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @param {integer} [options.dim=-1] - dimension over which to perform operation * @throws {TypeError} first argument must be an ndarray-like object * @throws {TypeError} index arguments must be either ndarray-like objects or integers * @throws {TypeError} options argument must be an object -* @throws {RangeError} dimension indices must not exceed input ndarray bounds -* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {RangeError} dimension index must not exceed input ndarray bounds +* @throws {RangeError} first argument must have at least one dimension * @throws {Error} must provide valid options * @returns {ndarray} input ndarray * @@ -66,10 +64,10 @@ var base = require( './base.js' ); * var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); * * // Define the shape of the input array: -* var sh = [ 3, 1, 2 ]; +* var sh = [ 6 ]; * * // Define the array strides: -* var sx = [ 2, 2, 1 ]; +* var sx = [ 1 ]; * * // Define the index offset: * var ox = 0; @@ -79,7 +77,7 @@ var base = require( './base.js' ); * * // Perform operation: * var out = copyWithin( x, 3, 1, 4 ); -* // returns [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 2.0 ] ], [ [ 3.0, 4.0 ] ] ] +* // returns [ 1.0, 2.0, 3.0, 2.0, 3.0, 4.0 ] * * var bool = ( out === x ); * // returns true @@ -87,9 +85,9 @@ var base = require( './base.js' ); function copyWithin( x, target, start ) { var hasOptions; var options; - var hasDims; var hasEnd; var nargs; + var opts; var end; var lsh; var len; @@ -115,7 +113,11 @@ function copyWithin( x, target, start ) { // Resolve input ndarray meta data: dt = getDType( x ); ord = getOrder( x ); - sh = getShape( x ); + + // Initialize an options object: + opts = { + 'dims': [ -1 ] // default behavior is to perform the operation over the last dimension + }; // Initialize flags indicating whether `end` and `options` arguments were provided: hasOptions = false; @@ -144,24 +146,27 @@ function copyWithin( x, target, start ) { hasOptions = true; } } - if ( hasOptions && !isPlainObject( options ) ) { - throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + if ( hasOptions ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + // Resolve provided options... + if ( hasOwnProp( options, 'dim' ) ) { + opts.dims[ 0 ] = options.dim; + } } - hasDims = ( hasOptions && hasOwnProp( options, 'dims' ) ); - // Resolve the shape of the non-core dimensions and the number of indexed elements... - if ( hasDims ) { - lsh = nonCoreShape( sh, options.dims ); - d = toUniqueNormalizedIndices( options.dims, sh.length-1 ); - if ( d === null ) { - // The `dims` option contains invalid dimension indices, so we defer validation to `base`: - len = 1; - } else { - len = ( d.length === 0 ) ? 1 : numel( takeIndexed( sh, d ) ); - } + sh = getShape( x ); + if ( sh.length < 1 ) { + throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); + } + lsh = nonCoreShape( sh, opts.dims ); + d = toUniqueNormalizedIndices( opts.dims, sh.length-1 ); + if ( d === null || !isInteger( d[ 0 ] ) ) { + // The `dim` option is an invalid dimension index, so we defer validation to `base`: + len = 1; } else { - lsh = []; - len = ( sh.length === 0 ) ? 1 : numel( sh ); + len = sh[ d[ 0 ] ]; } // Normalize the provided indices, defaulting to the number of indexed elements when not provided an ending index: t = normalizeIndices( target, len ); @@ -169,18 +174,15 @@ function copyWithin( x, target, start ) { e = ( hasEnd ) ? normalizeIndices( end, len ) : len; // Broadcast the indices to match the shape of the non-core dimensions... - t = broadcastIndex( t, 'Second', lsh, ord, hasDims ); - s = broadcastIndex( s, 'Third', lsh, ord, hasDims ); - e = broadcastIndex( e, 'Fourth', lsh, ord, hasDims ); + t = broadcastIndex( t, lsh, ord ); + s = broadcastIndex( s, lsh, ord ); + e = broadcastIndex( e, lsh, ord ); // Allocate a workspace ndarray which is broadcast across the non-core dimensions, thus allowing each one-dimensional slice to reuse the same workspace buffer: w = broadcastArray( empty( dt, [ len ], ord ), lsh.concat( [ len ] ) ); // Perform operation: - if ( hasOptions ) { - return base( x, t, s, e, w, options ); - } - return base( x, t, s, e, w ); + return base( x, t, s, e, w, opts ); } diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/package.json b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json index d608b63e8267..cc06182be036 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/package.json +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/blas/ext/copy-within", "version": "0.0.0", - "description": "Perform an in-place copy of elements within an ndarray along one or more ndarray dimensions.", + "description": "Perform an in-place copy of elements within an ndarray along an ndarray dimension.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js index f09a894872df..d3ec150a0b8f 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/test/test.js @@ -154,6 +154,27 @@ tape( 'the function throws an error if provided a first argument which is not an } }); +tape( 'the function throws an error if provided a first argument which is a zero-dimensional ndarray', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 10.0 ), + scalar2ndarray( -3.0 ), + scalar2ndarray( 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() { + copyWithin( value, 1, 0 ); + }; + } +}); + tape( 'the function throws an error if provided a `target` argument which is not an ndarray-like object or an integer', function test( t ) { var values; var x; @@ -507,7 +528,7 @@ tape( 'the function throws an error if provided a `target` argument which is not } }); -tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (dims)', function test( t ) { +tape( 'the function throws an error if provided a `target` argument which is not broadcast-compatible (dim)', function test( t ) { var values; var opts; var x; @@ -531,7 +552,7 @@ tape( 'the function throws an error if provided a `target` argument which is not function badValue( value ) { return function badValue() { copyWithin( x, value, 0, { - 'dims': [ 0 ] + 'dim': 0 }); }; } @@ -713,7 +734,7 @@ tape( 'the function throws an error if provided an options argument which is not } }); -tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { +tape( 'the function throws an error if provided a `dim` option which is not an integer', function test( t ) { var values; var x; var i; @@ -724,10 +745,11 @@ tape( 'the function throws an error if provided a `dims` option which is not an values = [ '5', - 5, NaN, true, false, + null, + void 0, [ 'a' ], {}, function noop() {} @@ -740,13 +762,13 @@ tape( 'the function throws an error if provided a `dims` option which is not an function badValue( value ) { return function badValue() { copyWithin( x, 1, 0, { - 'dims': value + 'dim': value }); }; } }); -tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers (end)', function test( t ) { +tape( 'the function throws an error if provided a `dim` option which is not an integer (end)', function test( t ) { var values; var x; var i; @@ -757,10 +779,11 @@ tape( 'the function throws an error if provided a `dims` option which is not an values = [ '5', - 5, NaN, true, false, + null, + void 0, [ 'a' ], {}, function noop() {} @@ -773,69 +796,13 @@ tape( 'the function throws an error if provided a `dims` option which is not an function badValue( value ) { return function badValue() { copyWithin( x, 1, 0, 2, { - 'dims': value - }); - }; - } -}); - -tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 2, 2 ], { - 'dtype': 'generic' - }); - - values = [ - [ -10 ], - [ 0, 20 ], - [ 20 ] - ]; - 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() { - copyWithin( x, 1, 0, { - 'dims': value - }); - }; - } -}); - -tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices (end)', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 2, 2 ], { - 'dtype': 'generic' - }); - - values = [ - [ -10 ], - [ 0, 20 ], - [ 20 ] - ]; - 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() { - copyWithin( x, 1, 0, 2, { - 'dims': value + 'dim': value }); }; } }); -tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { +tape( 'the function throws an error if provided a `dim` option which is out-of-bounds', function test( t ) { var values; var x; var i; @@ -845,8 +812,8 @@ tape( 'the function throws an error if provided a `dims` option which contains t }); values = [ - [ 0, 1, 2 ], - [ 0, 1, 2, 3 ] + -10, + 20 ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); @@ -856,13 +823,13 @@ tape( 'the function throws an error if provided a `dims` option which contains t function badValue( value ) { return function badValue() { copyWithin( x, 1, 0, { - 'dims': value + 'dim': value }); }; } }); -tape( 'the function throws an error if provided a `dims` option which contains too many indices (end)', function test( t ) { +tape( 'the function throws an error if provided a `dim` option which is out-of-bounds (end)', function test( t ) { var values; var x; var i; @@ -872,8 +839,8 @@ tape( 'the function throws an error if provided a `dims` option which contains t }); values = [ - [ 0, 1, 2 ], - [ 0, 1, 2, 3 ] + -10, + 20 ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); @@ -883,65 +850,7 @@ tape( 'the function throws an error if provided a `dims` option which contains t function badValue( value ) { return function badValue() { copyWithin( x, 1, 0, 2, { - 'dims': value - }); - }; - } -}); - -tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 2, 2 ], { - 'dtype': 'generic' - }); - - values = [ - [ 0, 0 ], - [ 1, 1 ], - [ 0, 1, 0 ], - [ 1, 0, 1 ] - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - copyWithin( x, 1, 0, { - 'dims': value - }); - }; - } -}); - -tape( 'the function throws an error if provided a `dims` option which contains duplicate indices (end)', function test( t ) { - var values; - var x; - var i; - - x = zeros( [ 2, 2 ], { - 'dtype': 'generic' - }); - - values = [ - [ 0, 0 ], - [ 1, 1 ], - [ 0, 1, 0 ], - [ 1, 0, 1 ] - ]; - for ( i = 0; i < values.length; i++ ) { - t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); - } - t.end(); - - function badValue( value ) { - return function badValue() { - copyWithin( x, 1, 0, 2, { - 'dims': value + 'dim': value }); }; } @@ -954,8 +863,8 @@ tape( 'the function performs an in-place copy of elements within an ndarray (def x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); - actual = copyWithin( x, 2, 0 ); - expected = [ 1.0, 2.0, 1.0, 2.0 ]; + actual = copyWithin( x, 1, 0 ); + expected = [ 1.0, 1.0, 3.0, 3.0 ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -973,7 +882,7 @@ tape( 'the function performs an in-place copy of elements within an ndarray (def x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); - actual = copyWithin( x, 2, 0 ); + actual = copyWithin( x, 1, 0 ); expected = [ 1.0, 2.0, 1.0, 2.0 ]; t.strictEqual( actual, x, 'returns expected value' ); @@ -985,59 +894,30 @@ tape( 'the function performs an in-place copy of elements within an ndarray (def t.end(); }); -tape( 'the function performs an in-place copy of elements within an ndarray (all dimensions, row-major)', function test( t ) { +tape( 'the function supports specifying the operation dimension (row-major)', function test( t ) { var expected; var actual; var x; x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); - actual = copyWithin( x, 2, 0, { - 'dims': [ 0, 1 ] - }); - expected = [ 1.0, 2.0, 1.0, 2.0 ]; - - t.strictEqual( actual, x, 'returns expected value' ); - t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); - t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); - t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); - t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function performs an in-place copy of elements within an ndarray (all dimensions, column-major)', function test( t ) { - var expected; - var actual; - var x; - - x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); - - actual = copyWithin( x, 2, 0, { - 'dims': [ 0, 1 ] + actual = copyWithin( x, 1, 0, { + 'dim': 0 }); - expected = [ 1.0, 2.0, 1.0, 2.0 ]; + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); - t.strictEqual( isSameArray( getData( actual ), expected ), true, 'returns expected value' ); - - t.end(); -}); - -tape( 'the function performs an in-place copy of elements within an ndarray (no dimensions, row-major)', function test( t ) { - var expected; - var actual; - var x; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); actual = copyWithin( x, 1, 0, { - 'dims': [] + 'dim': 1 }); - expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + expected = [ [ 1.0, 1.0 ], [ 3.0, 3.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -1045,20 +925,12 @@ tape( 'the function performs an in-place copy of elements within an ndarray (no t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function performs an in-place copy of elements within an ndarray (no dimensions, column-major)', function test( t ) { - var expected; - var actual; - var x; - - x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); actual = copyWithin( x, 1, 0, { - 'dims': [] + 'dim': -2 }); - expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -1069,30 +941,17 @@ tape( 'the function performs an in-place copy of elements within an ndarray (no t.end(); }); -tape( 'the function supports specifying operation dimensions (row-major)', function test( t ) { +tape( 'the function supports specifying the operation dimension (column-major)', function test( t ) { var expected; var actual; var x; - x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); - - actual = copyWithin( x, 1, 0, { - 'dims': [ 0 ] - }); - expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; - - t.strictEqual( actual, x, 'returns expected value' ); - t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); - t.deepEqual( getShape( actual ), getShape( x ), 'returns expected value' ); - t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); - t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); - - x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); actual = copyWithin( x, 1, 0, { - 'dims': [ 1 ] + 'dim': 0 }); - expected = [ [ 1.0, 1.0 ], [ 3.0, 3.0 ] ]; + expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -1100,20 +959,12 @@ tape( 'the function supports specifying operation dimensions (row-major)', funct t.strictEqual( getOrder( actual ), getOrder( x ), 'returns expected value' ); t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); - t.end(); -}); - -tape( 'the function supports specifying operation dimensions (column-major)', function test( t ) { - var expected; - var actual; - var x; - x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); actual = copyWithin( x, 1, 0, { - 'dims': [ 0 ] + 'dim': 1 }); - expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -1124,9 +975,9 @@ tape( 'the function supports specifying operation dimensions (column-major)', fu x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0 ], [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); actual = copyWithin( x, 1, 0, { - 'dims': [ 1 ] + 'dim': -2 }); - expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + expected = [ [ 1.0, 3.0 ], [ 1.0, 3.0 ] ]; t.strictEqual( actual, x, 'returns expected value' ); t.strictEqual( String( getDType( actual ) ), 'generic', 'returns expected value' ); @@ -1263,7 +1114,7 @@ tape( 'the function supports providing index arguments as zero-dimensional ndarr x = new ndarray( 'generic', [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ], [ 2, 4 ], [ 4, 1 ], 0, 'row-major' ); actual = copyWithin( x, scalar2ndarray( 2, opts ), 0, { - 'dims': [ 1 ] + 'dim': 1 }); expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 5.0, 6.0 ] ]; @@ -1284,7 +1135,7 @@ tape( 'the function supports providing index arguments as ndarrays', function te target = new ndarray( 'int32', new Int32Array( [ 0, 2 ] ), [ 2 ], [ 1 ], 0, 'row-major' ); actual = copyWithin( x, target, 2, 4, { - 'dims': [ 1 ] + 'dim': 1 }); expected = [ [ 3.0, 4.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; @@ -1305,7 +1156,7 @@ tape( 'the function supports providing index arguments as ndarrays containing ne target = new ndarray( 'int32', new Int32Array( [ -2, 0 ] ), [ 2 ], [ 1 ], 0, 'row-major' ); actual = copyWithin( x, target, 0, 2, { - 'dims': [ 1 ] + 'dim': 1 }); expected = [ [ 1.0, 2.0, 1.0, 2.0 ], [ 5.0, 6.0, 7.0, 8.0 ] ]; From 201692d7efc9315b8156bec0c96ae27f291f67dc Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 14 Aug 2026 21:16:16 -0700 Subject: [PATCH 4/8] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/blas/ext/copy-within/docs/repl.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt index 26986b047da0..f53736859b2e 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/repl.txt @@ -4,8 +4,8 @@ dimension. When a `target`, `start`, and/or `end` index is negative, the respective - index is determined relative to the last indexed element, with - out-of-bounds indices clamped to index bounds. + index is determined relative to the last indexed element, with out-of-bounds + indices clamped to index bounds. The function copies elements within an input ndarray in-place and thus mutates an input ndarray. @@ -31,8 +31,8 @@ end: ndarray|integer (optional) Source end index (exclusive). May be either an integer or an ndarray having an integer index or "generic" data type and following the same - broadcasting semantics as the `target` argument. Default: the number of - indexed elements. + broadcasting semantics as the `target` argument. Default: `N + 1` where + `N` is the number of elements along the operation dimension. options: Object (optional) Function options. From f760929399dc974f4553891ea6ea4034770f4e54 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 14 Aug 2026 21:28:02 -0700 Subject: [PATCH 5/8] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/copy-within/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md index f14e78c9ce78..a02680e95a37 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/README.md +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/README.md @@ -54,7 +54,7 @@ The function has the following parameters: - **x**: input [ndarray][@stdlib/ndarray/ctor]. - **target**: target index. May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dim`. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, an [ndarray][@stdlib/ndarray/ctor] target index must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. - **start**: source start index (inclusive). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. -- **end**: source end index (exclusive) (_optional_). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. By default, the source end index is the number of indexed elements. +- **end**: source end index (exclusive) (_optional_). May be either an integer or an [ndarray][@stdlib/ndarray/ctor] having an integer index or "generic" [data type][@stdlib/ndarray/dtypes] and following the same broadcasting semantics as the `target` argument. By default, the source end index is `N + 1`, where `N` is the number of elements along the dimension specified by `options.dim`. - **options**: function options (_optional_). The function accepts the following options: @@ -72,7 +72,7 @@ var y = copyWithin( x, 0, -2 ); // returns [ 5.0, 6.0, 3.0, 4.0, 5.0, 6.0 ] ``` -By default, the function performs the operation over elements in the last dimension. To perform the operation over a different dimension, provide a `dim` option. +By default, the function performs the operation over elements along the last dimension. To perform the operation over a different dimension, provide a `dim` option. ```javascript var array = require( '@stdlib/ndarray/array' ); From 990acc0e1061de11ccf5196444f04d55ddbc68d3 Mon Sep 17 00:00:00 2001 From: Athan Date: Fri, 14 Aug 2026 21:36:21 -0700 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js index f145a6a374eb..1531c0a83a23 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -168,7 +168,7 @@ function copyWithin( x, target, start ) { } else { len = sh[ d[ 0 ] ]; } - // Normalize the provided indices, defaulting to the number of indexed elements when not provided an ending index: + // Normalize the provided indices... t = normalizeIndices( target, len ); s = normalizeIndices( start, len ); e = ( hasEnd ) ? normalizeIndices( end, len ) : len; From efce193d8e049b5fde3fb5217cd496f6f6658e7f Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 15 Aug 2026 17:14:15 +0500 Subject: [PATCH 7/8] fix: 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_pkg_readmes status: na - task: lint_markdown_docs status: na - 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: na - 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: passed - task: lint_license_headers status: passed --- --- .../ext/copy-within/docs/types/index.d.ts | 4 ++++ .../@stdlib/blas/ext/copy-within/lib/main.js | 20 ++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts index c4e6cddd4c8f..12929fd10acb 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/docs/types/index.d.ts @@ -33,6 +33,10 @@ type IndexArgument = typedndarray | number; interface Options { /** * Dimension over which to perform operation. Default: `-1`. + * + * ## Notes + * + * - If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). */ dim?: number; } diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js index 1531c0a83a23..be8500197caa 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -26,7 +26,7 @@ var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var broadcastArray = require( '@stdlib/ndarray/base/broadcast-array' ); var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); -var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); var empty = require( '@stdlib/ndarray/base/empty' ); var getDType = require( '@stdlib/ndarray/dtype' ); var getShape = require( '@stdlib/ndarray/shape' ); @@ -53,7 +53,7 @@ var base = require( './base.js' ); * @throws {TypeError} options argument must be an object * @throws {RangeError} dimension index must not exceed input ndarray bounds * @throws {RangeError} first argument must have at least one dimension -* @throws {Error} must provide valid options +* @throws {TypeError} must provide valid options * @returns {ndarray} input ndarray * * @example @@ -152,6 +152,9 @@ function copyWithin( x, target, start ) { } // Resolve provided options... if ( hasOwnProp( options, 'dim' ) ) { + if ( !isInteger( options.dim ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'dim', options.dim ) ); + } opts.dims[ 0 ] = options.dim; } } @@ -160,14 +163,13 @@ function copyWithin( x, target, start ) { if ( sh.length < 1 ) { throw new RangeError( 'invalid argument. First argument must have at least one dimension.' ); } - lsh = nonCoreShape( sh, opts.dims ); - d = toUniqueNormalizedIndices( opts.dims, sh.length-1 ); - if ( d === null || !isInteger( d[ 0 ] ) ) { - // The `dim` option is an invalid dimension index, so we defer validation to `base`: - len = 1; - } else { - len = sh[ d[ 0 ] ]; + d = normalizeIndex( opts.dims[ 0 ], sh.length-1 ); + if ( d === -1 ) { + throw new RangeError( format( 'invalid option. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', sh.length, opts.dims[ 0 ] ) ); } + lsh = nonCoreShape( sh, opts.dims ); + len = sh[ d ]; + // Normalize the provided indices... t = normalizeIndices( target, len ); s = normalizeIndices( start, len ); From 4b2ebe90f5e0177a9c6a11567b8fc81ca07dcaca Mon Sep 17 00:00:00 2001 From: Athan Date: Sat, 15 Aug 2026 06:01:59 -0700 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js index be8500197caa..6c98d63fed4c 100644 --- a/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/copy-within/lib/main.js @@ -167,6 +167,7 @@ function copyWithin( x, target, start ) { if ( d === -1 ) { throw new RangeError( format( 'invalid option. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', sh.length, opts.dims[ 0 ] ) ); } + opts.dims[ 0 ] = d; lsh = nonCoreShape( sh, opts.dims ); len = sh[ d ];