Skip to content

Commit fe888da

Browse files
committed
feat: add ndarray/fill-slice-by
1 parent 9f03000 commit fe888da

File tree

11 files changed

+4219
-0
lines changed

11 files changed

+4219
-0
lines changed
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
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+
# fillSliceBy
22+
23+
> Fill an input [ndarray][@stdlib/ndarray/ctor] view 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 fillSliceBy = require( '@stdlib/ndarray/fill-slice-by' );
37+
```
38+
39+
#### fillSliceBy( x, ...s\[, options], fcn\[, thisArg] )
40+
41+
Fills an input [ndarray][@stdlib/ndarray/ctor] view according to a callback function.
42+
43+
```javascript
44+
var zeros = require( '@stdlib/ndarray/zeros' );
45+
var Slice = require( '@stdlib/slice/ctor' );
46+
var MultiSlice = require( '@stdlib/slice/multi' );
47+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
48+
49+
function fcn( value ) {
50+
return value + 10.0;
51+
}
52+
53+
var x = zeros( [ 3, 4 ], {
54+
'dtype': 'float64'
55+
});
56+
57+
// Define the slice region:
58+
var s0 = new Slice( 1, 3 );
59+
var s1 = new Slice( 2, 4 );
60+
var s = new MultiSlice( s0, s1 );
61+
62+
var y = fillSliceBy( x, s, fcn );
63+
// returns <ndarray>
64+
65+
var bool = ( y === x );
66+
// returns true
67+
68+
var arr = ndarray2array( y );
69+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 10.0, 10.0 ], [ 0.0, 0.0, 10.0, 10.0 ] ]
70+
```
71+
72+
The function accepts the following arguments:
73+
74+
- **x**: array-like object containing an input [ndarray][@stdlib/ndarray/ctor].
75+
- **s**: a [`MultiSlice`][@stdlib/slice/multi] instance, an array of slice arguments, or slice arguments as separate arguments.
76+
- **options**: function options.
77+
- **fcn**: callback function.
78+
- **thisArg**: callback function execution context (_optional_).
79+
80+
The function supports three (mutually exclusive) means for providing slice arguments:
81+
82+
1. providing a single [`MultiSlice`][@stdlib/slice/multi] instance.
83+
2. providing a single array of slice arguments.
84+
3. providing slice arguments as separate arguments.
85+
86+
The following example demonstrates each invocation style achieving equivalent results.
87+
88+
```javascript
89+
var zeros = require( '@stdlib/ndarray/zeros' );
90+
var MultiSlice = require( '@stdlib/slice/multi' );
91+
var Slice = require( '@stdlib/slice/ctor' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
94+
function clbk() {
95+
return 5.0;
96+
}
97+
98+
var opts = {
99+
'dtype': 'float64'
100+
};
101+
102+
// 1. Using a MultiSlice:
103+
var x = zeros( [ 3, 4 ], opts );
104+
105+
var s0 = new Slice( 1, 3 );
106+
var s1 = new Slice( 2, 4 );
107+
var s = new MultiSlice( s0, s1 );
108+
109+
var out = fillSliceBy( x, s, clbk );
110+
// returns <ndarray>
111+
112+
var arr = ndarray2array( out );
113+
// 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 ] ]
114+
115+
// 2. Using an array of slice arguments:
116+
x = zeros( [ 3, 4 ], opts );
117+
118+
s0 = new Slice( 1, 3 );
119+
s1 = new Slice( 2, 4 );
120+
s = new MultiSlice( s0, s1 );
121+
122+
out = fillSliceBy( x, [ s0, s1 ], clbk );
123+
// returns <ndarray>
124+
125+
arr = ndarray2array( out );
126+
// 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 ] ]
127+
128+
// 3. Providing separate arguments:
129+
x = zeros( [ 3, 4 ], opts );
130+
131+
s0 = new Slice( 1, 3 );
132+
s1 = new Slice( 2, 4 );
133+
s = new MultiSlice( s0, s1 );
134+
135+
out = fillSliceBy( x, s0, s1, clbk );
136+
// returns <ndarray>
137+
138+
arr = ndarray2array( out );
139+
// 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 ] ]
140+
```
141+
142+
The function supports the following options:
143+
144+
- **strict**: boolean indicating whether to enforce strict bounds checking.
145+
146+
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`.
147+
148+
```javascript
149+
var zeros = require( '@stdlib/ndarray/zeros' );
150+
var MultiSlice = require( '@stdlib/slice/multi' );
151+
var Slice = require( '@stdlib/slice/ctor' );
152+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
153+
154+
function clbk() {
155+
return 5.0;
156+
}
157+
158+
var x = zeros( [ 3, 4 ], {
159+
'dtype': 'float64'
160+
});
161+
162+
// Define the fill region:
163+
var s0 = new Slice( 1, null, 1 );
164+
var s1 = new Slice( 10, 20, 1 );
165+
var s = new MultiSlice( s0, s1 );
166+
167+
var opts = {
168+
'strict': false
169+
};
170+
171+
// Fill the region with a scalar value:
172+
var y = fillSliceBy( x, s, opts, clbk );
173+
// returns <ndarray>
174+
175+
var arr = ndarray2array( x );
176+
// 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 ] ]
177+
```
178+
179+
To set the callback function execution context, provide a `thisArg`.
180+
181+
<!-- eslint-disable no-invalid-this -->
182+
183+
```javascript
184+
var zeros = require( '@stdlib/ndarray/zeros' );
185+
var Slice = require( '@stdlib/slice/ctor' );
186+
var MultiSlice = require( '@stdlib/slice/multi' );
187+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
188+
189+
function fcn( value ) {
190+
return value + this.factor;
191+
}
192+
193+
var x = zeros( [ 3, 4 ], {
194+
'dtype': 'float64'
195+
});
196+
197+
var ctx = {
198+
'factor': 10.0
199+
};
200+
201+
// Define the slice region:
202+
var s0 = new Slice( 1, 3 );
203+
var s1 = new Slice( 2, 4 );
204+
var s = new MultiSlice( s0, s1 );
205+
206+
var y = fillSliceBy( x, s, fcn, ctx );
207+
// returns <ndarray>
208+
209+
var arr = ndarray2array( y );
210+
// returns [ [ 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 10.0, 10.0 ], [ 0.0, 0.0, 10.0, 10.0 ] ]
211+
```
212+
213+
The callback function is provided the following arguments:
214+
215+
- **value**: current array element.
216+
- **indices**: current array element indices.
217+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
218+
219+
</section>
220+
221+
<!-- /.usage -->
222+
223+
<section class="notes">
224+
225+
## Notes
226+
227+
- An input [ndarray][@stdlib/ndarray/ctor] **must** be writable. If provided a **read-only** [ndarray][@stdlib/ndarray/ctor], the function throws an error.
228+
- A **slice argument** must be either a [`Slice`][@stdlib/slice/ctor], an integer, `null`, or `undefined`.
229+
- The function **mutates** the input [ndarray][@stdlib/ndarray/ctor].
230+
- The function assumes that each element in the underlying input [ndarray][@stdlib/ndarray/ctor] data buffer has one, and only one, corresponding element in input [ndarray][@stdlib/ndarray/ctor] view (i.e., a provided [ndarray][@stdlib/ndarray/ctor] is not a broadcasted [ndarray][@stdlib/ndarray/ctor] view).
231+
232+
</section>
233+
234+
<!-- /.notes -->
235+
236+
<section class="examples">
237+
238+
## Examples
239+
240+
<!-- eslint no-undef: "error" -->
241+
242+
```javascript
243+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
244+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
245+
var zeros = require( '@stdlib/ndarray/zeros' );
246+
var MultiSlice = require( '@stdlib/slice/multi' );
247+
var Slice = require( '@stdlib/slice/ctor' );
248+
var fillSliceBy = require( '@stdlib/ndarray/fill-slice-by' );
249+
250+
// Create a zero-filled ndarray:
251+
var x = zeros( [ 2, 3, 4 ], {
252+
'dtype': 'generic'
253+
});
254+
console.log( ndarray2array( x ) );
255+
256+
// Specify the slice region:
257+
var s0 = new Slice( 1, 2 );
258+
var s1 = new Slice( null, null );
259+
var s2 = new Slice( 2, 4 );
260+
var s = new MultiSlice( s0, s1, s2 );
261+
262+
// Fill the slice:
263+
fillSliceBy( x, s, discreteUniform( -100, 100 ) );
264+
console.log( ndarray2array( x ) );
265+
```
266+
267+
</section>
268+
269+
<!-- /.examples -->
270+
271+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
272+
273+
<section class="related">
274+
275+
</section>
276+
277+
<!-- /.related -->
278+
279+
<section class="links">
280+
281+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
282+
283+
[@stdlib/slice/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/ctor
284+
285+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
286+
287+
<!-- <related-links> -->
288+
289+
<!-- </related-links> -->
290+
291+
</section>
292+
293+
<!-- /.links -->

0 commit comments

Comments
 (0)