From d72f7c9657e3693a7885fe94acd3757e4226df52 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 17 Sep 2026 20:43:37 +0500 Subject: [PATCH 1/2] feat: add blas/ext/base/ndarray/gfill-by --- .../blas/ext/base/ndarray/gfill-by/README.md | 181 ++++++ .../ndarray/gfill-by/benchmark/benchmark.js | 125 ++++ .../ext/base/ndarray/gfill-by/docs/repl.txt | 50 ++ .../ndarray/gfill-by/docs/types/index.d.ts | 111 ++++ .../base/ndarray/gfill-by/docs/types/test.ts | 121 ++++ .../base/ndarray/gfill-by/examples/index.js | 38 ++ .../ext/base/ndarray/gfill-by/lib/index.js | 56 ++ .../ext/base/ndarray/gfill-by/lib/main.js | 111 ++++ .../ext/base/ndarray/gfill-by/package.json | 69 +++ .../ext/base/ndarray/gfill-by/test/test.js | 557 ++++++++++++++++++ 10 files changed, 1419 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/README.md new file mode 100644 index 000000000000..f123479a0a98 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/README.md @@ -0,0 +1,181 @@ + + +# gfillBy + +> Fill a one-dimensional ndarray according to a provided callback function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var gfillBy = require( '@stdlib/blas/ext/base/ndarray/gfill-by' ); +``` + +#### gfillBy( arrays, clbk\[, thisArg] ) + +Fills a one-dimensional ndarray according to a provided callback function. + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var vector = require( '@stdlib/ndarray/vector/ctor' ); + +function fill( v, i ) { + return v * i; +} + +var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, -6.0 ], 'generic' ); + +var start = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var end = scalar2ndarray( 6, { + 'dtype': 'generic' +}); + +gfillBy( [ x, start, end ], fill ); +// x => [ 0.0, 1.0, 6.0, -15.0, 16.0, -30.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the starting index (inclusive). + - a zero-dimensional ndarray containing the ending index (exclusive). + +- **clbk**: callback function. + +- **thisArg**: callback execution context (_optional_). + +The callback function is provided the following arguments: + +- **value**: current array element. +- **idx**: current array element index. +- **array**: the input ndarray. + +To set the callback execution context, provide a `thisArg`. + +```javascript +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var vector = require( '@stdlib/ndarray/vector/ctor' ); + +function fill( v, i ) { + this.count += 1; + return v * i; +} + +var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, -6.0 ], 'generic' ); +var ctx = { + 'count': 0 +}; + +var start = scalar2ndarray( 0, { + 'dtype': 'generic' +}); + +var end = scalar2ndarray( 6, { + 'dtype': 'generic' +}); + +gfillBy( [ x, start, end ], fill, ctx ); +// x => [ 0.0, 1.0, 6.0, -15.0, 16.0, -30.0 ] + +var count = ctx.count; +// returns 6 +``` + +
+ + + +
+ +## Notes + +- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**). +- If a specified `start` or `end` index is negative, the function resolves the respective index by counting backward from the last element (where `-1` refers to the last element). +- When filling a strided array with a scalar constant, prefer using [`dfill`][@stdlib/blas/ext/base/dfill], [`sfill`][@stdlib/blas/ext/base/sfill], and/or [`gfill`][@stdlib/blas/ext/base/gfill], as, depending on the environment, these interfaces are likely to be significantly more performant. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var gfillBy = require( '@stdlib/blas/ext/base/ndarray/gfill-by' ); + +var opts = { + 'dtype': 'generic' +}; + +var x = zeros( [ 10 ], opts ); +console.log( ndarray2array( x ) ); + +var start = scalar2ndarray( 0, opts ); +var end = scalar2ndarray( 10, opts ); + +gfillBy( [ x, start, end ], discreteUniform( -100, 100 ) ); +console.log( ndarray2array( x ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/benchmark/benchmark.js new file mode 100644 index 000000000000..881d001700d5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/benchmark/benchmark.js @@ -0,0 +1,125 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var gfillBy = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} v - array element +* @returns {number} fill value +*/ +function clbk( v ) { + return v * 2.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var start; + var end; + var x; + + x = uniform( [ len ], -100.0, 100.0, options ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( len, { + 'dtype': 'generic' + }); + 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 = gfillBy( [ x, start, end ], clbk ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( 0 ) ) ) { + 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:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/repl.txt new file mode 100644 index 000000000000..c0fc64b59b5c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/repl.txt @@ -0,0 +1,50 @@ + +{{alias}}( arrays, clbk[, thisArg] ) + Fills a one-dimensional ndarray according to a provided callback function. + + The input ndarray is modified *in-place* (i.e., the input ndarray is + *mutated*). + + If a specified `start` or `end` index is negative, the function resolves + the respective index by counting backward from the last element (where `-1` + refers to the last element). + + The callback function is provided three arguments: + + - value: current array element. + - index: current array index. + - array: the input ndarray. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a zero-dimensional ndarray containing the starting index (inclusive). + - a zero-dimensional ndarray containing the ending index (exclusive). + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: ndarray + Input ndarray. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/vector/ctor}}( [ 1.0, -2.0, 3.0 ], 'generic' ); + > var opts = { 'dtype': 'generic' }; + > var st = {{alias:@stdlib/ndarray/from-scalar}}( 0, opts ); + > var en = {{alias:@stdlib/ndarray/from-scalar}}( 3, opts ); + > function f() { return 5.0; }; + > {{alias}}( [ x, st, en ], f ) + [ 5.0, 5.0, 5.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/index.d.ts new file mode 100644 index 000000000000..0fbedde8e669 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/index.d.ts @@ -0,0 +1,111 @@ +/* +* @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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a fill value. +* +* @returns fill value +*/ +type Nullary = ( this: ThisArg ) => any; + +/** +* Returns a fill value. +* +* @param value - current array element +* @returns fill value +*/ +type Unary = ( this: ThisArg, value: T ) => any; + +/** +* Returns a fill value. +* +* @param value - current array element +* @param index - current array element index +* @returns fill value +*/ +type Binary = ( this: ThisArg, value: T, index: number ) => any; + +/** +* Returns a fill value. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns fill value +*/ +type Ternary = ( this: ThisArg, value: T, index: number, array: U ) => any; + +/** +* Returns a fill value. +* +* @param value - current array element +* @param index - current array element index +* @param array - input ndarray +* @returns fill value +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Fills a one-dimensional ndarray according to a provided callback function. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the starting index (inclusive). +* - a zero-dimensional ndarray containing the ending index (exclusive). +* +* @param arrays - array-like object containing ndarrays +* @param clbk - callback function +* @param thisArg - callback execution context +* @returns input ndarray +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* +* function fill() { +* return 5.0; +* } +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, -6.0 ], 'generic' ); +* +* var start = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var end = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var out = gfillBy( [ x, start, end ], fill ); +* // returns [ 5.0, 5.0, 5.0, -5.0, 4.0, -6.0 ] +*/ +declare function gfillBy = typedndarray, ThisArg = unknown>( arrays: [ V, typedndarray, typedndarray ], clbk: Callback, thisArg?: ThisParameterType> ): V; + + +// EXPORTS // + +export = gfillBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts new file mode 100644 index 000000000000..a66c6ce20ac5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts @@ -0,0 +1,121 @@ +/* +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import gfillBy = require( './index' ); + +/** +* Callback function. +* +* @returns fill value +*/ +function clbk(): number { + return 5.0; +} + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + const end = scalar2ndarray( 10, { + 'dtype': 'generic' + }); + + gfillBy( [ x, start, end ], clbk ); // $ExpectType typedndarray + gfillBy( [ x, start, end ], clbk, {} ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + gfillBy( '10', clbk ); // $ExpectError + gfillBy( 10, clbk ); // $ExpectError + gfillBy( true, clbk ); // $ExpectError + gfillBy( false, clbk ); // $ExpectError + gfillBy( null, clbk ); // $ExpectError + gfillBy( undefined, clbk ); // $ExpectError + gfillBy( [], clbk ); // $ExpectError + gfillBy( {}, clbk ); // $ExpectError + gfillBy( ( x: number ): number => x, clbk ); // $ExpectError + + gfillBy( '10', clbk, {} ); // $ExpectError + gfillBy( 10, clbk, {} ); // $ExpectError + gfillBy( true, clbk, {} ); // $ExpectError + gfillBy( false, clbk, {} ); // $ExpectError + gfillBy( null, clbk, {} ); // $ExpectError + gfillBy( undefined, clbk, {} ); // $ExpectError + gfillBy( [], clbk, {} ); // $ExpectError + gfillBy( {}, clbk, {} ); // $ExpectError + gfillBy( ( x: number ): number => x, clbk, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a callback function... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + const end = scalar2ndarray( 10, { + 'dtype': 'generic' + }); + + gfillBy( [ x, start, end ], '10' ); // $ExpectError + gfillBy( [ x, start, end ], 10 ); // $ExpectError + gfillBy( [ x, start, end ], true ); // $ExpectError + gfillBy( [ x, start, end ], false ); // $ExpectError + gfillBy( [ x, start, end ], null ); // $ExpectError + gfillBy( [ x, start, end ], undefined ); // $ExpectError + gfillBy( [ x, start, end ], [] ); // $ExpectError + gfillBy( [ x, start, end ], {} ); // $ExpectError + + gfillBy( [ x, start, end ], '10', {} ); // $ExpectError + gfillBy( [ x, start, end ], 10, {} ); // $ExpectError + gfillBy( [ x, start, end ], true, {} ); // $ExpectError + gfillBy( [ x, start, end ], false, {} ); // $ExpectError + gfillBy( [ x, start, end ], null, {} ); // $ExpectError + gfillBy( [ x, start, end ], undefined, {} ); // $ExpectError + gfillBy( [ x, start, end ], [], {} ); // $ExpectError + gfillBy( [ x, start, end ], {}, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'generic' + }); + const start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + const end = scalar2ndarray( 10, { + 'dtype': 'generic' + }); + + gfillBy(); // $ExpectError + gfillBy( [ x, start, end ], clbk, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/examples/index.js new file mode 100644 index 000000000000..a66d999d73bf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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/base/discrete-uniform' ).factory; +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var gfillBy = require( './../lib' ); + +var opts = { + 'dtype': 'generic' +}; + +var x = zeros( [ 10 ], opts ); +console.log( ndarray2array( x ) ); + +var start = scalar2ndarray( 0, opts ); +var end = scalar2ndarray( 10, opts ); + +gfillBy( [ x, start, end ], discreteUniform( -100, 100 ) ); +console.log( ndarray2array( x ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/index.js new file mode 100644 index 000000000000..5cdd5bdccb44 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/index.js @@ -0,0 +1,56 @@ +/** +* @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'; + +/** +* Fill a one-dimensional ndarray according to a provided callback function. +* +* @module @stdlib/blas/ext/base/ndarray/gfill-by +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* var gfillBy = require( '@stdlib/blas/ext/base/ndarray/gfill-by' ); +* +* function fill() { +* return 5.0; +* } +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, -6.0 ], 'generic' ); +* +* var start = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var end = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var out = gfillBy( [ x, start, end ], fill ); +* // returns [ 5.0, 5.0, 5.0, -5.0, 4.0, -6.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/main.js new file mode 100644 index 000000000000..3715142dfe34 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/lib/main.js @@ -0,0 +1,111 @@ +/** +* @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 ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); +var strided = require( '@stdlib/blas/ext/base/gfill-by' ).ndarray; + + +// MAIN // + +/** +* Fills a one-dimensional ndarray according to a provided callback function. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a zero-dimensional ndarray containing the starting index (inclusive). +* - a zero-dimensional ndarray containing the ending index (exclusive). +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {ndarray} input ndarray +* +* @example +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var vector = require( '@stdlib/ndarray/vector/ctor' ); +* +* function fill() { +* return 5.0; +* } +* +* var x = vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, -6.0 ], 'generic' ); +* +* var start = scalar2ndarray( 0, { +* 'dtype': 'generic' +* }); +* +* var end = scalar2ndarray( 3, { +* 'dtype': 'generic' +* }); +* +* var out = gfillBy( [ x, start, end ], fill ); +* // returns [ 5.0, 5.0, 5.0, -5.0, 4.0, -6.0 ] +*/ +function gfillBy( arrays, clbk, thisArg ) { + var stride; + var offset; + var start; + var end; + var N; + var x; + + x = arrays[ 0 ]; + + N = numelDimension( x, 0 ); + start = clipIndex( ndarraylike2scalar( arrays[ 1 ] ), N ); + end = clipIndex( ndarraylike2scalar( arrays[ 2 ] ), N ); + if ( start >= end ) { + return x; + } + stride = getStride( x, 0 ); + offset = getOffset( x ) + ( stride*start ); + + strided( end-start, getData( x ), stride, offset, wrapper, null ); + return x; + + /** + * Invokes a provided callback. + * + * @private + * @param {*} value - current array element + * @param {NonNegativeInteger} aidx - current array element index + * @param {NonNegativeInteger} sidx - current strided array element index + * @param {Collection} arr - input array + * @returns {*} result + */ + function wrapper( value, aidx ) { + return clbk.call( thisArg, value, aidx + start, x ); + } +} + + +// EXPORTS // + +module.exports = gfillBy; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/package.json new file mode 100644 index 000000000000..5dd6a79e7ffa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/gfill-by", + "version": "0.0.0", + "description": "Fill a one-dimensional ndarray according to a provided callback function.", + "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", + "ext", + "fill", + "callback", + "assign", + "set", + "strided", + "array", + "ndarray", + "generic" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/test/test.js new file mode 100644 index 000000000000..a5e1d98b4f6d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/test/test.js @@ -0,0 +1,557 @@ +/** +* @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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var gfillBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'generic', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gfillBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function fills a one-dimensional ndarray', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0, 6.0 ]; + x = vector( xbuf, 6, 1, 0 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 6, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ + 4.0, // 0 + 2.0, + -3.0, // 1 + 5.0, + -1.0, // 2 + 6.0 + ]; + x = vector( xbuf, 3, 2, 0 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ + 5.0, // 0 + 2.0, + 5.0, // 1 + 5.0, + 5.0, // 2 + 6.0 + ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports an input ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ + 4.0, // 2 + 2.0, + -3.0, // 1 + 5.0, + -1.0, // 0 + 6.0 + ]; + x = vector( xbuf, 3, -2, 4 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ + 5.0, // 2 + 2.0, + 5.0, // 1 + 5.0, + 5.0, // 0 + 6.0 + ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports an input ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ + 4.0, + 2.0, // 0 + -3.0, + 5.0, // 1 + -1.0, + 6.0 // 2 + ]; + x = vector( xbuf, 3, 2, 1 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ + 4.0, + 5.0, // 0 + -3.0, + 5.0, // 1 + -1.0, + 5.0 // 2 + ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports a nonnegative starting index', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + start = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 1.0, 5.0, 5.0, 5.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports a negative starting index', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + start = scalar2ndarray( -3, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 1.0, 5.0, 5.0, 5.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports a nonnegative ending index', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 5.0, 5.0, 5.0, 4.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports a negative ending index', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( -1, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 5.0, 5.0, 5.0, 4.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function clamps out-of-bounds starting and ending indices', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + start = scalar2ndarray( -10, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 10, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 5.0, 5.0, 5.0, 5.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'if a resolved starting index is greater than or equal to a resolved ending index, the function returns the input ndarray unchanged', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, 2.0, 3.0, 4.0 ]; + x = vector( xbuf, 4, 1, 0 ); + expected = [ 1.0, 2.0, 3.0, 4.0 ]; + + start = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + start = scalar2ndarray( 3, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + start = scalar2ndarray( 5, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( -10, { + 'dtype': 'generic' + }); + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function returns the input ndarray unchanged when the input ndarray is empty', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = []; + x = vector( xbuf, 0, 1, 0 ); + start = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 0, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = []; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports specifying a fill range for an input ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var start; + var xbuf; + var end; + var x; + + xbuf = [ 1.0, -2.0, 3.0, 4.0 ]; + x = vector( xbuf, 2, 2, 0 ); + start = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 2, { + 'dtype': 'generic' + }); + + actual = gfillBy( [ x, start, end ], fill ); + t.strictEqual( actual, x, 'returns expected value' ); + + expected = [ 1.0, -2.0, 5.0, 4.0 ]; + t.deepEqual( xbuf, expected, 'returns expected value' ); + + t.end(); + + function fill() { + return 5.0; + } +}); + +tape( 'the function supports providing an execution context', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var actual; + var start; + var xbuf; + var end; + var ctx; + var arr; + + xbuf = [ + 1.0, // 0 + 2.0, + 3.0, // 1 + -2.0, + -7.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + ctx = { + 'count': 0 + }; + + indices = []; + values = []; + arrays = []; + + arr = vector( xbuf, 4, 2, 0 ); + start = scalar2ndarray( 1, { + 'dtype': 'generic' + }); + end = scalar2ndarray( 4, { + 'dtype': 'generic' + }); + actual = gfillBy( [ arr, start, end ], fill, ctx ); + + t.strictEqual( actual, arr, 'returns expected value' ); + t.strictEqual( ctx.count, 3, 'returns expected value' ); + + expected = [ + 3.0, + -7.0, + 4.0 + ]; + t.deepEqual( values, expected, 'returns expected value' ); + + expected = [ + 1, + 2, + 3 + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + arr, + arr, + arr + ]; + t.deepEqual( arrays, expected, 'returns expected value' ); + + t.end(); + + function fill( v, idx, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + values.push( v ); + indices.push( idx ); + arrays.push( arr ); + return 5.0; + } +}); From d80d1ed9efd0880345722eed6b679da09857e162 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 17 Sep 2026 20:55:48 +0500 Subject: [PATCH 2/2] fix: lint err --- .../@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts index a66c6ce20ac5..f5577aadccfc 100644 --- a/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/docs/types/test.ts @@ -46,8 +46,8 @@ function clbk(): number { 'dtype': 'generic' }); - gfillBy( [ x, start, end ], clbk ); // $ExpectType typedndarray - gfillBy( [ x, start, end ], clbk, {} ); // $ExpectType typedndarray + gfillBy( [ x, start, end ], clbk ); // $ExpectType genericndarray + gfillBy( [ x, start, end ], clbk, {} ); // $ExpectType genericndarray } // The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...