Skip to content

Commit 4b805c3

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/fill-slice
PR-URL: #7911 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 1aebcbc commit 4b805c3

File tree

11 files changed

+4077
-0
lines changed

11 files changed

+4077
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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] view 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 fillSlice = require( '@stdlib/ndarray/fill-slice' );
37+
```
38+
39+
#### fillSlice( x, value, ...s\[, options] )
40+
41+
Fills an input [`ndarray`][@stdlib/ndarray/ctor] view with a specified value.
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+
// Define 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 region with a scalar value:
59+
var y = fillSlice( x, 5.0, s );
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, 0.0 ], [ 0.0, 0.0, 5.0, 5.0 ], [ 0.0, 0.0, 5.0, 5.0 ] ]
67+
```
68+
69+
The function accepts the following arguments:
70+
71+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
72+
- **value**: fill value.
73+
- **s**: a [`MultiSlice`][@stdlib/slice/multi] instance, an array of slice arguments, or slice arguments as separate arguments.
74+
- **options**: function options.
75+
76+
The function supports three (mutually exclusive) means for providing slice arguments:
77+
78+
1. providing a single [`MultiSlice`][@stdlib/slice/multi] instance.
79+
2. providing a single array of slice arguments.
80+
3. providing slice arguments as separate arguments.
81+
82+
The following example demonstrates each invocation style achieving equivalent results.
83+
84+
```javascript
85+
var zeros = require( '@stdlib/ndarray/zeros' );
86+
var MultiSlice = require( '@stdlib/slice/multi' );
87+
var Slice = require( '@stdlib/slice/ctor' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
90+
var opts = {
91+
'dtype': 'float64'
92+
};
93+
94+
// 1. Using a MultiSlice:
95+
var x = zeros( [ 3, 4 ], opts );
96+
97+
var s0 = new Slice( 1, 3 );
98+
var s1 = new Slice( 2, 4 );
99+
var s = new MultiSlice( s0, s1 );
100+
101+
var out = fillSlice( x, 5.0, s );
102+
// returns <ndarray>
103+
104+
var arr = ndarray2array( out );
105+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 5.0, 5.0 ], [ 0.0, 0.0, 5.0, 5.0 ] ]
106+
107+
// 2. Using an array of slice arguments:
108+
x = zeros( [ 3, 4 ], opts );
109+
110+
out = fillSlice( x, 6.0, [ s0, s1 ] );
111+
// returns <ndarray>
112+
113+
arr = ndarray2array( out );
114+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 6.0, 6.0 ], [ 0.0, 0.0, 6.0, 6.0 ] ]
115+
116+
// 3. Providing separate arguments:
117+
x = zeros( [ 3, 4 ], opts );
118+
119+
out = fillSlice( x, 7.0, s0, s1 );
120+
// returns <ndarray>
121+
122+
arr = ndarray2array( out );
123+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 7.0, 7.0 ], [ 0.0, 0.0, 7.0, 7.0 ] ]
124+
```
125+
126+
The function supports the following options:
127+
128+
- **strict**: boolean indicating whether to enforce strict bounds checking.
129+
130+
By default, the function throws an error when provided a slice which exceeds array bounds. To ignore slice indices exceeding array bounds, set the `strict` option to `false`.
131+
132+
```javascript
133+
var zeros = require( '@stdlib/ndarray/zeros' );
134+
var MultiSlice = require( '@stdlib/slice/multi' );
135+
var Slice = require( '@stdlib/slice/ctor' );
136+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
137+
138+
var x = zeros( [ 3, 4 ], {
139+
'dtype': 'float64'
140+
});
141+
142+
// Define the fill region:
143+
var s0 = new Slice( 1, null, 1 );
144+
var s1 = new Slice( 10, 20, 1 );
145+
var s = new MultiSlice( s0, s1 );
146+
147+
// Fill the region with a scalar value:
148+
var y = fillSlice( x, 5.0, s, {
149+
'strict': false
150+
});
151+
// returns <ndarray>
152+
153+
var arr = ndarray2array( x );
154+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0 ] ]
155+
```
156+
157+
</section>
158+
159+
<!-- /.usage -->
160+
161+
<section class="notes">
162+
163+
## Notes
164+
165+
- An input [`ndarray`][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function throws an error.
166+
- A **slice argument** must be either a [`Slice`][@stdlib/slice/ctor], an integer, `null`, or `undefined`.
167+
- If a fill 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 fill `value` and whose imaginary component is zero.
168+
- A fill value must be able to safely cast to the input [`ndarray`][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Fill 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]).
169+
- The function **mutates** the input [`ndarray`][@stdlib/ndarray/ctor].
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
## Examples
178+
179+
<!-- eslint no-undef: "error" -->
180+
181+
```javascript
182+
var zeros = require( '@stdlib/ndarray/zeros' );
183+
var MultiSlice = require( '@stdlib/slice/multi' );
184+
var Slice = require( '@stdlib/slice/ctor' );
185+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
186+
var fillSlice = require( '@stdlib/ndarray/fill-slice' );
187+
188+
// Create a zero-filled ndarray:
189+
var x = zeros( [ 2, 3, 4 ], {
190+
'dtype': 'generic'
191+
});
192+
console.log( ndarray2array( x ) );
193+
194+
// Specify the fill region:
195+
var s0 = new Slice( 1, 2 );
196+
var s1 = new Slice( null, null );
197+
var s2 = new Slice( 2, 4 );
198+
var s = new MultiSlice( s0, s1, s2 );
199+
200+
// Fill a slice with a scalar value:
201+
fillSlice( x, 10.0, s );
202+
console.log( ndarray2array( x ) );
203+
```
204+
205+
</section>
206+
207+
<!-- /.examples -->
208+
209+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
210+
211+
<section class="related">
212+
213+
</section>
214+
215+
<!-- /.related -->
216+
217+
<section class="links">
218+
219+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
220+
221+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
222+
223+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
224+
225+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
226+
227+
</section>
228+
229+
<!-- /.links -->
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 getData = require( '@stdlib/ndarray/data-buffer' );
29+
var pkg = require( './../package.json' ).name;
30+
var fillSlice = 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 xbuf;
53+
var x;
54+
55+
x = zeros( shape, {
56+
'dtype': xtype,
57+
'order': order
58+
});
59+
xbuf = getData( x );
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var s;
70+
var i;
71+
72+
s = new MultiSlice( null );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
fillSlice( x, i, s );
77+
if ( isnan( xbuf[ i%len ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( xbuf[ i%len ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var ord;
103+
var sh;
104+
var t1;
105+
var f;
106+
var i;
107+
var j;
108+
var k;
109+
110+
min = 1; // 10^min
111+
max = 6; // 10^max
112+
113+
for ( k = 0; k < orders.length; k++ ) {
114+
ord = orders[ k ];
115+
for ( j = 0; j < types.length; j++ ) {
116+
t1 = types[ j ];
117+
for ( i = min; i <= max; i++ ) {
118+
len = pow( 10, i );
119+
120+
sh = [ len ];
121+
f = createBenchmark( len, sh, t1, ord );
122+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',xtype='+t1, f );
123+
}
124+
}
125+
}
126+
}
127+
128+
main();

0 commit comments

Comments
 (0)