Skip to content

Commit 447759d

Browse files
kgryteGirish-Garg
authored andcommitted
feat: add ndarray/fill
--- 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 80d4ebb commit 447759d

File tree

11 files changed

+1254
-0
lines changed

11 files changed

+1254
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
# fill
22+
23+
> Fill an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var fill = require( '@stdlib/ndarray/fill' );
37+
```
38+
39+
#### fill( x, value )
40+
41+
Fills an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value.
42+
43+
```javascript
44+
var zeros = require( '@stdlib/ndarray/zeros' );
45+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
46+
47+
var x = zeros( [ 3, 1, 2 ], {
48+
'dtype': 'float64'
49+
});
50+
51+
var y = fill( x, 10.0 );
52+
// returns <ndarray>
53+
54+
var bool = ( y === x );
55+
// returns true
56+
57+
var arr = ndarray2array( x );
58+
// returns [ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **x**: array-like object containing an input [`ndarray`][@stdlib/ndarray/ctor].
64+
- **value**: scalar value.
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
75+
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input [`ndarray`][@stdlib/ndarray/ctor] with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
76+
- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor].
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var zeros = require( '@stdlib/ndarray/zeros' );
90+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
91+
var fill = require( '@stdlib/ndarray/fill' );
92+
93+
// Create a zero-filled ndarray:
94+
var x = zeros( [ 5, 2 ], {
95+
'dtype': 'generic'
96+
});
97+
console.log( ndarray2array( x ) );
98+
99+
// Fill the ndarray with a scalar value:
100+
fill( x, 10.0 );
101+
console.log( ndarray2array( x ) );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<section class="links">
117+
118+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
119+
120+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
121+
122+
<!-- <related-links> -->
123+
124+
<!-- </related-links> -->
125+
126+
</section>
127+
128+
<!-- /.links -->
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 fill = 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+
* Creates a benchmark function.
41+
*
42+
* @private
43+
* @param {PositiveInteger} len - ndarray length
44+
* @param {NonNegativeIntegerArray} shape - ndarray shape
45+
* @param {string} xtype - input ndarray data type
46+
* @param {string} order - memory layout
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len, shape, xtype, order ) {
50+
var x = zeros( shape, {
51+
'dtype': xtype,
52+
'order': order
53+
});
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
fill( x, i );
68+
if ( isnan( x.data[ i%len ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( x.data[ i%len ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var ord;
94+
var sh;
95+
var t1;
96+
var f;
97+
var i;
98+
var j;
99+
var k;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( k = 0; k < orders.length; k++ ) {
105+
ord = orders[ k ];
106+
for ( j = 0; j < types.length; j++ ) {
107+
t1 = types[ j ];
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
111+
sh = [ len ];
112+
f = createBenchmark( len, sh, t1, ord );
113+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
114+
}
115+
}
116+
}
117+
}
118+
119+
main();
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 floor = require( '@stdlib/math/base/special/floor' );
27+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
28+
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var fill = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var orders = [ 'row-major', 'column-major' ];
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @param {NonNegativeIntegerArray} shape - ndarray shape
47+
* @param {string} xtype - input ndarray data type
48+
* @param {string} order - memory layout
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len, shape, xtype, order ) {
52+
var x = zeros( shape, {
53+
'dtype': xtype,
54+
'order': order
55+
});
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
fill( x, i );
70+
if ( isnan( x.data[ i%len ] ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( x.data[ i%len ] ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var ord;
96+
var sh;
97+
var t1;
98+
var f;
99+
var i;
100+
var j;
101+
var k;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( k = 0; k < orders.length; k++ ) {
107+
ord = orders[ k ];
108+
for ( j = 0; j < types.length; j++ ) {
109+
t1 = types[ j ];
110+
for ( i = min; i <= max; i++ ) {
111+
len = pow( 10, i );
112+
113+
sh = [ len/2, 2 ];
114+
f = createBenchmark( len, sh, t1, ord );
115+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
116+
117+
sh = [ 2, len/2 ];
118+
f = createBenchmark( len, sh, t1, ord );
119+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
120+
121+
len = floor( sqrt( len ) );
122+
sh = [ len, len ];
123+
len *= len;
124+
f = createBenchmark( len, sh, t1, ord );
125+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
126+
}
127+
}
128+
}
129+
}
130+
131+
main();

0 commit comments

Comments
 (0)