Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/base/ndarray/gfill-by/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<!--

@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.

-->

# gfillBy

> Fill a one-dimensional ndarray according to a provided callback function.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## 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 => <ndarray>[ 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 => <ndarray>[ 0.0, 1.0, 6.0, -15.0, 16.0, -30.0 ]

var count = ctx.count;
// returns 6
```

</section>

<!-- /.usage -->

<section class="notes">

## 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.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```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 ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/blas/ext/base/dfill]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dfill

[@stdlib/blas/ext/base/sfill]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/sfill

[@stdlib/blas/ext/base/gfill]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/gfill

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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<ndarray>
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 )
<ndarray>[ 5.0, 5.0, 5.0 ]

See Also
--------

Loading