Skip to content

Commit e5d835c

Browse files
committed
feat: add ndarray/fill-by
--- 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 status: passed - 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 ---
1 parent 54de8ff commit e5d835c

File tree

11 files changed

+1559
-0
lines changed

11 files changed

+1559
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# fillBy
22+
23+
> Fill an input [`ndarray`][@stdlib/ndarray/ctor] according to a callback function.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var fillBy = require( '@stdlib/ndarray/fill-by' );
37+
```
38+
39+
#### fillBy( x, fcn\[, thisArg] )
40+
41+
Fills an input [`ndarray`][@stdlib/ndarray/ctor] according to a callback function.
42+
43+
```javascript
44+
var zeros = require( '@stdlib/ndarray/zeros' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
47+
function fcn( value ) {
48+
return value + 10.0;
49+
}
50+
51+
var x = zeros( [ 3, 1, 2 ], {
52+
'dtype': 'float64'
53+
});
54+
55+
var y = fillBy( x, fcn );
56+
// returns <ndarray>
57+
58+
var bool = ( y === x );
59+
// returns true
60+
61+
var arr = ndarray2array( y );
62+
// returns [ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
63+
```
64+
65+
The function accepts the following arguments:
66+
67+
- **x**: array-like object containing an input [`ndarray`][@stdlib/ndarray/ctor].
68+
- **fcn**: callback function.
69+
- **thisArg**: callback function execution context (_optional_).
70+
71+
To set the callback function execution context, provide a `thisArg`.
72+
73+
<!-- eslint-disable no-invalid-this -->
74+
75+
```javascript
76+
var zeros = require( '@stdlib/ndarray/zeros' );
77+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
78+
79+
function fcn( value ) {
80+
return value + this.factor;
81+
}
82+
83+
var x = zeros( [ 3, 1, 2 ], {
84+
'dtype': 'float64'
85+
});
86+
87+
var ctx = {
88+
'factor': 10.0
89+
};
90+
var y = fillBy( x, fcn, ctx );
91+
// returns <ndarray>
92+
93+
var arr = ndarray2array( y );
94+
// returns [ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
95+
```
96+
97+
</section>
98+
99+
<!-- /.usage -->
100+
101+
<section class="notes">
102+
103+
## Notes
104+
105+
- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
106+
- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor].
107+
108+
</section>
109+
110+
<!-- /.notes -->
111+
112+
<section class="examples">
113+
114+
## Examples
115+
116+
<!-- eslint no-undef: "error" -->
117+
118+
```javascript
119+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
120+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
121+
var zeros = require( '@stdlib/ndarray/zeros' );
122+
var fillBy = require( '@stdlib/ndarray/fill-by' );
123+
124+
// Create a zero-filled ndarray:
125+
var x = zeros( [ 5, 2 ], {
126+
'dtype': 'generic'
127+
});
128+
console.log( ndarray2array( x ) );
129+
130+
// Fill the ndarray with random values:
131+
fillBy( x, discreteUniform( -100, 100 ) );
132+
console.log( ndarray2array( x ) );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<section class="links">
148+
149+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
150+
151+
<!-- <related-links> -->
152+
153+
<!-- </related-links> -->
154+
155+
</section>
156+
157+
<!-- /.links -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var zeros = require( '@stdlib/ndarray/zeros' );
27+
var pkg = require( './../package.json' ).name;
28+
var fillBy = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var types = [ 'float64' ];
34+
var orders = [ 'row-major', 'column-major' ];
35+
36+
37+
// FUNCTIONS //
38+
39+
/**
40+
* Callback function.
41+
*
42+
* @private
43+
* @param {*} value - input value
44+
* @returns {*} input value
45+
*/
46+
function fcn( value ) {
47+
return value;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveInteger} len - ndarray length
55+
* @param {NonNegativeIntegerArray} shape - ndarray shape
56+
* @param {string} xtype - input ndarray data type
57+
* @param {string} order - memory layout
58+
* @returns {Function} benchmark function
59+
*/
60+
function createBenchmark( len, shape, xtype, order ) {
61+
var x = zeros( shape, {
62+
'dtype': xtype,
63+
'order': order
64+
});
65+
return benchmark;
66+
67+
/**
68+
* Benchmark function.
69+
*
70+
* @private
71+
* @param {Benchmark} b - benchmark instance
72+
*/
73+
function benchmark( b ) {
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
fillBy( x, fcn );
79+
if ( isnan( x.data[ i%len ] ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
}
83+
b.toc();
84+
if ( isnan( x.data[ i%len ] ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
}
90+
}
91+
92+
93+
// MAIN //
94+
95+
/**
96+
* Main execution sequence.
97+
*
98+
* @private
99+
*/
100+
function main() {
101+
var len;
102+
var min;
103+
var max;
104+
var ord;
105+
var sh;
106+
var t1;
107+
var f;
108+
var i;
109+
var j;
110+
var k;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( k = 0; k < orders.length; k++ ) {
116+
ord = orders[ k ];
117+
for ( j = 0; j < types.length; j++ ) {
118+
t1 = types[ j ];
119+
for ( i = min; i <= max; i++ ) {
120+
len = pow( 10, i );
121+
122+
sh = [ len ];
123+
f = createBenchmark( len, sh, t1, ord );
124+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
125+
}
126+
}
127+
}
128+
}
129+
130+
main();

0 commit comments

Comments
 (0)