From da92cc971b74303cb27e9e7079f9a6edaf0dd7bd Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 26 Aug 2026 22:13:18 +0500 Subject: [PATCH] feat: add index normalization support to blas/ext/base/ndarray/ccopy-within --- .../ext/base/ndarray/ccopy-within/README.md | 1 + .../base/ndarray/ccopy-within/docs/repl.txt | 4 + .../ccopy-within/docs/types/index.d.ts | 2 + .../ext/base/ndarray/ccopy-within/lib/main.js | 11 ++- .../base/ndarray/ccopy-within/test/test.js | 99 +++++++++++++++++++ 5 files changed, 116 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/README.md index 2b00b48d850c..9b6b6f186e77 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/README.md +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/README.md @@ -87,6 +87,7 @@ The function has the following parameters: ## Notes - The input ndarray is copied **in-place** (i.e., the input ndarray is **mutated**). +- If a `target`, `start`, and/or `end` index is negative, the respective index is resolved by counting backward from the last element (where `-1` refers to the last element). - If the `start` and `target` index ranges do not overlap, the `workspace` ndarray is unused and thus ignored. diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/repl.txt index 3b26f402fee2..568e5cd70d6c 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/repl.txt +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/repl.txt @@ -6,6 +6,10 @@ The input ndarray is copied *in-place* (i.e., the input ndarray is *mutated*). + If a `target`, `start`, and/or `end` index is negative, the respective + index is resolved by counting backward from the last element (where `-1` + refers to the last element). + If the `start` and `target` index ranges do not overlap, the `workspace` ndarray is unused and thus ignored. diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/types/index.d.ts index 71098fb3758c..dc2051f91b42 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/docs/types/index.d.ts @@ -35,6 +35,8 @@ import { typedndarray, complex64ndarray } from '@stdlib/types/ndarray'; * - a zero-dimensional ndarray specifying a source end index (exclusive). * - a one-dimensional workspace ndarray. * +* - If the `start` and `target` index ranges do not overlap, the `workspace` ndarray is unused and thus ignored. +* * @param arrays - array-like object containing ndarrays * @returns input ndarray * diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/lib/main.js index f87e5c01d8c4..4d7899a9bf5f 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/lib/main.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/lib/main.js @@ -22,6 +22,7 @@ var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); var getStride = require( '@stdlib/ndarray/base/stride' ); var getOffset = require( '@stdlib/ndarray/base/offset' ); var getData = require( '@stdlib/ndarray/base/data-buffer' ); @@ -72,24 +73,32 @@ var strided = require( '@stdlib/blas/ext/base/ccopy-within' ).ndarray; * // returns [ [ 1.0, 2.0 ], [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] */ function ccopyWithin( arrays ) { + var target; + var start; + var end; var ws; var wo; var wd; var xs; var xo; var xd; + var N; var x; var w; x = arrays[ 0 ]; w = arrays[ 4 ]; + N = numelDimension( x, 0 ); + target = clipIndex( ndarraylike2scalar( arrays[ 1 ] ), N ); + start = clipIndex( ndarraylike2scalar( arrays[ 2 ] ), N ); + end = clipIndex( ndarraylike2scalar( arrays[ 3 ] ), N ); xd = getData( x ); xs = getStride( x, 0 ); xo = getOffset( x ); wd = getData( w ); ws = getStride( w, 0 ); wo = getOffset( w ); - strided( numelDimension( x, 0 ), ndarraylike2scalar( arrays[ 1 ] ), ndarraylike2scalar( arrays[ 2 ] ), ndarraylike2scalar( arrays[ 3 ] ), xd, xs, xo, wd, ws, wo ); // eslint-disable-line max-len + strided( N, target, start, end, xd, xs, xo, wd, ws, wo ); return x; } diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/test/test.js index c755c8499bad..0b0f9ed69540 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/test/test.js +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/ccopy-within/test/test.js @@ -212,6 +212,105 @@ tape( 'if the `end` index is greater than the number of elements, the function c t.end(); }); +tape( 'the function supports a negative `target` index', function test( t ) { + var expected; + var actual; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + wbuf = new Complex64Array( 4 ); + + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( -2 ), scalar( 0 ), scalar( 2 ), w ] ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + wbuf = new Complex64Array( 3 ); + + x = vector( xbuf, 3, 1, 0 ); + w = vector( wbuf, 3, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( -100 ), scalar( 1 ), scalar( 3 ), w ] ); + expected = new Complex64Array( [ 3.0, 4.0, 5.0, 6.0, 5.0, 6.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a negative `start` index', function test( t ) { + var expected; + var actual; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + wbuf = new Complex64Array( 4 ); + + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( 2 ), scalar( -4 ), scalar( 2 ), w ] ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + wbuf = new Complex64Array( 3 ); + + x = vector( xbuf, 3, 1, 0 ); + w = vector( wbuf, 3, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( 1 ), scalar( -100 ), scalar( 3 ), w ] ); + expected = new Complex64Array( [ 1.0, 2.0, 1.0, 2.0, 3.0, 4.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a negative `end` index', function test( t ) { + var expected; + var actual; + var xbuf; + var wbuf; + var x; + var w; + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + wbuf = new Complex64Array( 4 ); + + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( 2 ), scalar( 0 ), scalar( -2 ), w ] ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + wbuf = new Complex64Array( 4 ); + + x = vector( xbuf, 4, 1, 0 ); + w = vector( wbuf, 4, 1, 0 ); + + actual = ccopyWithin( [ x, scalar( 0 ), scalar( 0 ), scalar( -100 ), w ] ); + expected = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + t.strictEqual( actual, x, 'returns expected value' ); + t.strictEqual( isSameComplex64Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { var expected; var actual;