Skip to content

Commit d197725

Browse files
committed
feat: add ndarray/to-filled-slice
1 parent c5fad82 commit d197725

12 files changed

Lines changed: 2374 additions & 0 deletions

File tree

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# toFilledSlice
22+
23+
> Return a new [`ndarray`][@stdlib/ndarray/ctor] with a specified slice region filled with a provided 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 toFilledSlice = require( '@stdlib/ndarray/to-filled-slice' );
37+
```
38+
39+
#### toFilledSlice( x, value, ...s\[, options] )
40+
41+
Returns a new [`ndarray`][@stdlib/ndarray/ctor] with a specified slice region filled with a provided value.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var array = require( '@stdlib/ndarray/array' );
47+
var MultiSlice = require( '@stdlib/slice/multi' );
48+
var Slice = require( '@stdlib/slice/ctor' );
49+
50+
var x = array( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ] );
51+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ]
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 = toFilledSlice( x, 0.0, s );
60+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 0.0, 0.0 ], [ 9.0, 10.0, 0.0, 0.0 ] ]
61+
62+
var bool = ( y === x );
63+
// returns false
64+
```
65+
66+
The function accepts the following arguments:
67+
68+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
69+
- **value**: fill value.
70+
- **s**: a [`MultiSlice`][@stdlib/slice/multi] instance, an array of slice arguments, or slice arguments as separate arguments.
71+
- **options**: function options.
72+
73+
The function supports three (mutually exclusive) means for providing slice arguments:
74+
75+
1. providing a single [`MultiSlice`][@stdlib/slice/multi] instance.
76+
2. providing a single array of slice arguments.
77+
3. providing slice arguments as separate arguments.
78+
79+
The following example demonstrates each invocation style achieving equivalent results.
80+
81+
<!-- eslint-disable max-len -->
82+
83+
```javascript
84+
var array = require( '@stdlib/ndarray/array' );
85+
var MultiSlice = require( '@stdlib/slice/multi' );
86+
var Slice = require( '@stdlib/slice/ctor' );
87+
88+
var s0 = new Slice( 1, 3 );
89+
var s1 = new Slice( 2, 4 );
90+
var s = new MultiSlice( s0, s1 );
91+
92+
// 1. Using a MultiSlice:
93+
var x = array( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ] );
94+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ]
95+
96+
var out = toFilledSlice( x, 0.0, s );
97+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 0.0, 0.0 ], [ 9.0, 10.0, 0.0, 0.0 ] ]
98+
99+
// 2. Using an array of slice arguments:
100+
out = toFilledSlice( x, 0.0, [ s0, s1 ] );
101+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 0.0, 0.0 ], [ 9.0, 10.0, 0.0, 0.0 ] ]
102+
103+
// 3. Providing separate arguments:
104+
out = toFilledSlice( x, 0.0, s0, s1 );
105+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 0.0, 0.0 ], [ 9.0, 10.0, 0.0, 0.0 ] ]
106+
```
107+
108+
The function supports the following options:
109+
110+
- **strict**: boolean indicating whether to enforce strict bounds checking.
111+
112+
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`.
113+
114+
<!-- eslint-disable max-len -->
115+
116+
```javascript
117+
var array = require( '@stdlib/ndarray/array' );
118+
var MultiSlice = require( '@stdlib/slice/multi' );
119+
var Slice = require( '@stdlib/slice/ctor' );
120+
121+
var x = array( [ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ] );
122+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ]
123+
124+
// Define the fill region:
125+
var s0 = new Slice( 1, null, 1 );
126+
var s1 = new Slice( 10, 20, 1 );
127+
var s = new MultiSlice( s0, s1 );
128+
129+
// Return a copy of `x` (out-of-bounds slice is ignored):
130+
var y = toFilledSlice( x, 0.0, s, {
131+
'strict': false
132+
});
133+
// returns <ndarray>[ [ 1.0, 2.0, 3.0, 4.0 ], [ 5.0, 6.0, 7.0, 8.0 ], [ 9.0, 10.0, 11.0, 12.0 ] ]
134+
```
135+
136+
</section>
137+
138+
<!-- /.usage -->
139+
140+
<section class="notes">
141+
142+
## Notes
143+
144+
- The function does **not** mutate the input [`ndarray`][@stdlib/ndarray/ctor].
145+
- The output [`ndarray`][@stdlib/ndarray/ctor] has the same [data type][@stdlib/ndarray/dtypes], shape, and memory layout as the input [`ndarray`][@stdlib/ndarray/ctor].
146+
- A **slice argument** must be either a [`Slice`][@stdlib/slice/ctor], an integer, `null`, or `undefined`.
147+
- Each slice argument must have an absolute index increment equal to one. Otherwise, the function throws an error.
148+
- If a fill value is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills the specified region with a complex number whose real component equals the provided fill `value` and whose imaginary component is zero.
149+
- 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]).
150+
151+
</section>
152+
153+
<!-- /.notes -->
154+
155+
<section class="examples">
156+
157+
## Examples
158+
159+
<!-- eslint no-undef: "error" -->
160+
161+
```javascript
162+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
163+
var MultiSlice = require( '@stdlib/slice/multi' );
164+
var Slice = require( '@stdlib/slice/ctor' );
165+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
166+
var toFilledSlice = require( '@stdlib/ndarray/to-filled-slice' );
167+
168+
// Create an input ndarray:
169+
var x = discreteUniform( [ 2, 3, 4 ], -10, 10, {
170+
'dtype': 'float64'
171+
});
172+
console.log( ndarray2array( x ) );
173+
174+
// Define the fill region:
175+
var s0 = new Slice( 1, 2 );
176+
var s1 = new Slice( null, null );
177+
var s2 = new Slice( 2, 4 );
178+
var s = new MultiSlice( s0, s1, s2 );
179+
180+
// Fill the region with a scalar value:
181+
var y = toFilledSlice( x, 0.0, s );
182+
console.log( ndarray2array( y ) );
183+
```
184+
185+
</section>
186+
187+
<!-- /.examples -->
188+
189+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190+
191+
<section class="related">
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<section class="links">
198+
199+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
200+
201+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
202+
203+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
204+
205+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
206+
207+
<!-- <related-links> -->
208+
209+
<!-- </related-links> -->
210+
211+
</section>
212+
213+
<!-- /.links -->
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
27+
var MultiSlice = require( '@stdlib/slice/multi' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var toFilledSlice = 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 {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( shape, xtype, order ) {
51+
var x;
52+
var s;
53+
54+
x = discreteUniform( shape, -10, 10, {
55+
'dtype': xtype,
56+
'order': order
57+
});
58+
s = new MultiSlice( null );
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var out;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
out = toFilledSlice( x, i, s );
74+
if ( typeof out !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isndarrayLike( out ) ) {
80+
b.fail( 'should return an ndarray' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var ord;
100+
var sh;
101+
var t1;
102+
var f;
103+
var i;
104+
var j;
105+
var k;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
for ( k = 0; k < orders.length; k++ ) {
111+
ord = orders[ k ];
112+
for ( j = 0; j < types.length; j++ ) {
113+
t1 = types[ j ];
114+
for ( i = min; i <= max; i++ ) {
115+
len = pow( 10, i );
116+
117+
sh = [ len ];
118+
f = createBenchmark( sh, t1, ord );
119+
bench( format( '%s:ndims=%d,len=%d,shape=[%s],xorder=%s,xtype=%s', pkg, sh.length, len, sh.join( ',' ), ord, t1 ), f );
120+
}
121+
}
122+
}
123+
}
124+
125+
main();

0 commit comments

Comments
 (0)