Skip to content

Commit 3b4d986

Browse files
committed
docs: update README to desired API
1 parent 83203af commit 3b4d986

File tree

1 file changed

+97
-14
lines changed
  • lib/node_modules/@stdlib/ndarray/fill-slice

1 file changed

+97
-14
lines changed

lib/node_modules/@stdlib/ndarray/fill-slice/README.md

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# fillSlice
2222

23-
> Fill an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi].
23+
> Fill an input [`ndarray`][@stdlib/ndarray/ctor] slice with a specified value.
2424
2525
<section class="intro">
2626

@@ -36,9 +36,9 @@ limitations under the License.
3636
var fillSlice = require( '@stdlib/ndarray/fill-slice' );
3737
```
3838

39-
#### fillSlice( x, s, value )
39+
#### fillSlice( x, value, ...s\[, options] )
4040

41-
Fills an input [`ndarray`][@stdlib/ndarray/ctor] with a specified value in the region defined by a [`MultiSlice`][@stdlib/slice/multi].
41+
Fills an input [`ndarray`][@stdlib/ndarray/ctor] slice with a specified value.
4242

4343
```javascript
4444
var zeros = require( '@stdlib/ndarray/zeros' );
@@ -50,27 +50,111 @@ var x = zeros( [ 3, 4 ], {
5050
'dtype': 'float64'
5151
});
5252

53-
// Create a MultiSlice to specify the fill region:
53+
// Define the fill region:
5454
var s0 = new Slice( 1, 3 );
5555
var s1 = new Slice( 2, 4 );
5656
var s = new MultiSlice( s0, s1 );
5757

58-
// Fill the ndarray with a scalar value:
59-
var y = fillSlice( x, s, 5.0 );
58+
// Fill the region with a scalar value:
59+
var y = fillSlice( x, 5.0, s );
6060
// returns <ndarray>
6161

6262
var bool = ( y === x );
6363
// returns true
6464

6565
var arr = ndarray2array( x );
66-
// returns [ [ 0, 0, 0, 0 ], [ 0, 0, 5, 5 ], [ 0, 0, 5, 5 ] ]
66+
67+
// 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 ] ]
6768
```
6869

6970
The function accepts the following arguments:
7071

7172
- **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].
7373
- **value**: scalar value.
74+
- **s**: [`MultiSlice`][@stdlib/slice/multi] instance, an array of slice arguments, or slice arguments as separate arguments.
75+
- **options**: function options.
76+
77+
The function supports three (mutually exclusive) means for providing slice arguments:
78+
79+
1. providing a single [`MultiSlice`][@stdlib/slice/multi] instance.
80+
2. providing a single array of slice arguments.
81+
3. providing slice arguments as separate arguments.
82+
83+
The following example demonstrates each invocation style achieving equivalent results.
84+
85+
```javascript
86+
var zeros = require( '@stdlib/ndarray/zeros' );
87+
var MultiSlice = require( '@stdlib/slice/multi' );
88+
var Slice = require( '@stdlib/slice/ctor' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
91+
var opts = {
92+
'dtype': 'float64'
93+
};
94+
95+
// 1. Using a MultiSlice:
96+
var x = zeros( [ 3, 4 ], opts );
97+
98+
var s0 = new Slice( 1, 3 );
99+
var s1 = new Slice( 2, 4 );
100+
var s = new MultiSlice( s0, s1 );
101+
102+
var out = fillSlice( x, 5.0, s );
103+
// returns <ndarray>
104+
105+
var arr = ndarray2array( out );
106+
// 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 ] ]
107+
108+
// 2. Using an array of slice arguments:
109+
x = zeros( [ 3, 4 ], opts );
110+
111+
out = fillSlice( x, 5.0, [ s0, s1 ] );
112+
// returns <ndarray>
113+
114+
arr = ndarray2array( out );
115+
// 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 ] ]
116+
117+
// 3. Providing separate arguments:
118+
x = zeros( [ 3, 4 ], opts );
119+
120+
out = fillSlice( x, 5.0, s0, s1 );
121+
// returns <ndarray>
122+
123+
arr = ndarray2array( out );
124+
// 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 ] ]
125+
```
126+
127+
The function supports the following options:
128+
129+
- **strict**: boolean indicating whether to enforce strict bounds checking.
130+
131+
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`.
132+
133+
```javascript
134+
var zeros = require( '@stdlib/ndarray/zeros' );
135+
var MultiSlice = require( '@stdlib/slice/multi' );
136+
var Slice = require( '@stdlib/slice/ctor' );
137+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
138+
139+
var x = zeros( [ 3, 4 ], {
140+
'dtype': 'float64'
141+
});
142+
143+
// Define the fill region:
144+
var s0 = new Slice( 1, null, 1 );
145+
var s1 = new Slice( 10, 20, 1 );
146+
var s = new MultiSlice( s0, s1 );
147+
148+
// Fill the region with a scalar value:
149+
var y = fillSlice( x, 5.0, s, {
150+
'strict': false
151+
});
152+
// returns <ndarray>
153+
154+
var arr = ndarray2array( x );
155+
156+
// 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 ] ]
157+
```
74158

75159
</section>
76160

@@ -81,10 +165,9 @@ The function accepts the following arguments:
81165
## Notes
82166

83167
- 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]).
168+
- If an input `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.
169+
- An input `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]).
86170
- 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.
88171

89172
</section>
90173

@@ -109,14 +192,14 @@ var x = zeros( [ 2, 3, 4 ], {
109192
});
110193
console.log( ndarray2array( x ) );
111194

112-
// Create a MultiSlice to specify the fill region:
195+
// Define a fill region:
113196
var s0 = new Slice( 1, 2 );
114197
var s1 = new Slice( null, null );
115198
var s2 = new Slice( 2, 4 );
116199
var s = new MultiSlice( s0, s1, s2 );
117200

118-
// Fill the ndarray with a scalar value:
119-
fillSlice( x, s, 10.0 );
201+
// Fill the region with a scalar value:
202+
fillSlice( x, 10.0, s );
120203
console.log( ndarray2array( x ) );
121204
```
122205

0 commit comments

Comments
 (0)