Skip to content

Commit d29b55f

Browse files
committed
feat: add ndarray/base/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 266a064 commit d29b55f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6828
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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 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/base/fill-by' );
37+
```
38+
39+
#### fillBy( x, fcn\[, thisArg] )
40+
41+
Fills an input ndarray according to a callback function.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
function fcn( value ) {
47+
return value * 10.0;
48+
}
49+
50+
// Create a data buffer:
51+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
52+
53+
// Define the shape of the input array:
54+
var shape = [ 3, 1, 2 ];
55+
56+
// Define the array strides:
57+
var sx = [ 2, 2, 1 ];
58+
59+
// Define the index offset:
60+
var ox = 0;
61+
62+
// Create the input ndarray-like object:
63+
var x = {
64+
'dtype': 'float64',
65+
'data': xbuf,
66+
'shape': shape,
67+
'strides': sx,
68+
'offset': ox,
69+
'order': 'row-major'
70+
};
71+
72+
fillBy( x, fcn );
73+
74+
console.log( x.data );
75+
// => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0 ]
76+
```
77+
78+
The function accepts the following arguments:
79+
80+
- **x**: array-like object containing an input ndarray.
81+
- **fcn**: callback function.
82+
- **thisArg**: callback function execution context (_optional_).
83+
84+
To set the callback function execution context, provide a `thisArg`.
85+
86+
<!-- eslint-disable no-invalid-this -->
87+
88+
```javascript
89+
var Float64Array = require( '@stdlib/array/float64' );
90+
91+
function fcn( value ) {
92+
return value * this.factor;
93+
}
94+
95+
// Create a data buffer:
96+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
97+
98+
// Define the shape of the input array:
99+
var shape = [ 3, 1, 2 ];
100+
101+
// Define the array strides:
102+
var sx = [ 2, 2, 1 ];
103+
104+
// Define the index offset:
105+
var ox = 0;
106+
107+
// Create the input ndarray-like object:
108+
var x = {
109+
'dtype': 'float64',
110+
'data': xbuf,
111+
'shape': shape,
112+
'strides': sx,
113+
'offset': ox,
114+
'order': 'row-major'
115+
};
116+
117+
var ctx = {
118+
'factor': 10.0
119+
};
120+
fillBy( x, fcn, ctx );
121+
122+
console.log( x.data );
123+
// => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0 ]
124+
```
125+
126+
A provided ndarray should be an object with the following properties:
127+
128+
- **dtype**: data type.
129+
- **data**: data buffer.
130+
- **shape**: dimensions.
131+
- **strides**: stride lengths.
132+
- **offset**: index offset.
133+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
134+
135+
</section>
136+
137+
<!-- /.usage -->
138+
139+
<section class="notes">
140+
141+
## Notes
142+
143+
- The function **mutates** the input ndarray.
144+
145+
</section>
146+
147+
<!-- /.notes -->
148+
149+
<section class="examples">
150+
151+
## Examples
152+
153+
<!-- eslint no-undef: "error" -->
154+
155+
```javascript
156+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
157+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
158+
var zeros = require( '@stdlib/ndarray/zeros' );
159+
var fillBy = require( '@stdlib/ndarray/base/fill-by' );
160+
161+
// Create a zero-filled ndarray:
162+
var x = zeros( [ 5, 2 ], {
163+
'dtype': 'generic'
164+
});
165+
console.log( ndarray2array( x ) );
166+
167+
// Fill the ndarray with random values:
168+
fillBy( x, discreteUniform( -100, 100 ) );
169+
console.log( ndarray2array( x ) );
170+
```
171+
172+
</section>
173+
174+
<!-- /.examples -->
175+
176+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
177+
178+
<section class="related">
179+
180+
</section>
181+
182+
<!-- /.related -->
183+
184+
<section class="links">
185+
186+
<!-- <related-links> -->
187+
188+
<!-- </related-links> -->
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
30+
var pkg = require( './../package.json' ).name;
31+
var fillBy = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var types = [ 'float64' ];
37+
var order = [ 'column-major' ];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Callback function.
44+
*
45+
* @private
46+
* @param {*} value - input value
47+
* @returns {*} input value
48+
*/
49+
function fcn( value ) {
50+
return value;
51+
}
52+
53+
/**
54+
* Creates a benchmark function.
55+
*
56+
* @private
57+
* @param {PositiveInteger} len - ndarray length
58+
* @param {NonNegativeIntegerArray} shape - ndarray shape
59+
* @param {string} xtype - input ndarray data type
60+
* @returns {Function} benchmark function
61+
*/
62+
function createBenchmark( len, shape, xtype ) {
63+
var x;
64+
65+
x = filledarrayBy( len, xtype, discreteUniform( -100, 100 ) );
66+
x = {
67+
'dtype': xtype,
68+
'data': x,
69+
'shape': shape,
70+
'strides': shape2strides( shape, order ),
71+
'offset': 0,
72+
'order': order
73+
};
74+
return benchmark;
75+
76+
/**
77+
* Benchmark function.
78+
*
79+
* @private
80+
* @param {Benchmark} b - benchmark instance
81+
*/
82+
function benchmark( b ) {
83+
var i;
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
fillBy( x, fcn );
88+
if ( isnan( x.data[ i%len ] ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
}
92+
b.toc();
93+
if ( isnan( x.data[ i%len ] ) ) {
94+
b.fail( 'should not return NaN' );
95+
}
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
}
99+
}
100+
101+
102+
// MAIN //
103+
104+
/**
105+
* Main execution sequence.
106+
*
107+
* @private
108+
*/
109+
function main() {
110+
var len;
111+
var min;
112+
var max;
113+
var sh;
114+
var t1;
115+
var f;
116+
var i;
117+
var j;
118+
119+
min = 1; // 10^min
120+
max = 6; // 10^max
121+
122+
for ( j = 0; j < types.length; j++ ) {
123+
t1 = types[ j ];
124+
for ( i = min; i <= max; i++ ) {
125+
len = pow( 10, i );
126+
127+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
128+
f = createBenchmark( len, sh, t1 );
129+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
130+
131+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
132+
f = createBenchmark( len, sh, t1 );
133+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
134+
135+
len = floor( pow( len, 1.0/10.0 ) );
136+
sh = [ len, len, len, len, len, len, len, len, len, len ];
137+
len *= pow( len, 9 );
138+
f = createBenchmark( len, sh, t1 );
139+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
140+
}
141+
}
142+
}
143+
144+
main();

0 commit comments

Comments
 (0)