Skip to content

Commit 7bcf1ed

Browse files
committed
feat: add ndarray/fill-slice
--- 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 660d87d commit 7bcf1ed

File tree

11 files changed

+1478
-0
lines changed

11 files changed

+1478
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
# fillSlice
22+
23+
> Fill an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi].
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var fillSlice = require( '@stdlib/ndarray/fill-slice' );
37+
```
38+
39+
#### fillSlice( x, s, value )
40+
41+
Fills an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi].
42+
43+
```javascript
44+
var zeros = require( '@stdlib/ndarray/zeros' );
45+
var MultiSlice = require( '@stdlib/slice/multi' );
46+
var Slice = require( '@stdlib/slice/ctor' );
47+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
48+
49+
var x = zeros( [ 3, 4 ], {
50+
'dtype': 'float64'
51+
});
52+
53+
// Create a MultiSlice to specify the fill region:
54+
var s0 = new Slice( 1, 3 );
55+
var s1 = new Slice( 2, 4 );
56+
var s = new MultiSlice( s0, s1 );
57+
58+
// Fill the ndarray with a scalar value:
59+
var y = fillSlice( x, s, 5.0 );
60+
// returns <ndarray>
61+
62+
var bool = ( y === x );
63+
// returns true
64+
65+
var arr = ndarray2array( x );
66+
// returns [ [ 0, 0, 0, 0 ], [ 0, 0, 5, 5 ], [ 0, 0, 5, 5 ] ]
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: array-like object containing an input [`ndarray`][@stdlib/ndarray/ctor].
72+
- **s**: [`MultiSlice`][@stdlib/slice/multi] specifying the fill region of the input [`ndarray`][@stdlib/ndarray/ctor].
73+
- **value**: scalar value.
74+
75+
</section>
76+
77+
<!-- /.usage -->
78+
79+
<section class="notes">
80+
81+
## Notes
82+
83+
- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
84+
- 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.
85+
- A `value` must be able to safely cast to the input [`ndarray`][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Scalar values having floating-point data types (both real and complex) are allowed to downcast to a lower precision data type of the same kind (e.g., a scalar double-precision floating-point number can be used to fill a `'float32'` input [`ndarray`][@stdlib/ndarray/ctor]).
86+
- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor].
87+
- Multi-slice instances have no explicit functionality; however, they are used by [`ndarray`][@stdlib/ndarray/ctor] and other packages for creating views into multi-dimensional data structures.
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var zeros = require( '@stdlib/ndarray/zeros' );
101+
var MultiSlice = require( '@stdlib/slice/multi' );
102+
var Slice = require( '@stdlib/slice/ctor' );
103+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
104+
var fillSlice = require( '@stdlib/ndarray/fill-slice' );
105+
106+
// Create a zero-filled ndarray:
107+
var x = zeros( [ 2, 3, 4 ], {
108+
'dtype': 'generic'
109+
});
110+
console.log( ndarray2array( x ) );
111+
112+
// Create a MultiSlice to specify the fill region:
113+
var s0 = new Slice( 1, 2 );
114+
var s1 = new Slice( null, null );
115+
var s2 = new Slice( 2, 4 );
116+
var s = new MultiSlice( s0, s1, s2 );
117+
118+
// Fill the ndarray with a scalar value:
119+
fillSlice( x, 10.0 );
120+
console.log( ndarray2array( x ) );
121+
```
122+
123+
</section>
124+
125+
<!-- /.examples -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<section class="links">
136+
137+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
138+
139+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
140+
141+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
142+
143+
</section>
144+
145+
<!-- /.links -->
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 MultiSlice = require( '@stdlib/slice/multi' );
28+
var pkg = require( './../package.json' ).name;
29+
var fillSlice = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var types = [ 'float64' ];
35+
var orders = [ 'row-major', 'column-major' ];
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Creates a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - ndarray length
45+
* @param {NonNegativeIntegerArray} shape - ndarray shape
46+
* @param {string} xtype - input ndarray data type
47+
* @param {string} order - memory layout
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len, shape, xtype, order ) {
51+
var x;
52+
53+
x = zeros( shape, {
54+
'dtype': xtype,
55+
'order': order
56+
});
57+
return benchmark;
58+
59+
/**
60+
* Benchmark function.
61+
*
62+
* @private
63+
* @param {Benchmark} b - benchmark instance
64+
*/
65+
function benchmark( b ) {
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
fillSlice( x, new MultiSlice( null ), i );
71+
if ( isnan( x.data[ i%len ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( x.data[ i%len ] ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var ord;
97+
var sh;
98+
var t1;
99+
var f;
100+
var i;
101+
var j;
102+
var k;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( k = 0; k < orders.length; k++ ) {
108+
ord = orders[ k ];
109+
for ( j = 0; j < types.length; j++ ) {
110+
t1 = types[ j ];
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
113+
114+
sh = [ len ];
115+
f = createBenchmark( len, sh, t1, ord );
116+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
117+
}
118+
}
119+
}
120+
}
121+
122+
main();

0 commit comments

Comments
 (0)