Skip to content

Commit daf2cc8

Browse files
headlessNodekgryte
andauthored
refactor: add writable parameter to ndarray/base/spread-dimensions
PR-URL: #9370 Closes: stdlib-js/metr-issue-tracker#137 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 8006652 commit daf2cc8

File tree

23 files changed

+173
-142
lines changed

23 files changed

+173
-142
lines changed

lib/node_modules/@stdlib/ndarray/any-by/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function anyBy( x, options, predicate, thisArg ) {
180180

181181
// Check whether we need to reinsert singleton dimensions which can be useful for broadcasting the returned output array to the shape of the original input array...
182182
if ( opts.keepdims ) {
183-
y = spreadDimensions( N, y, idx );
183+
y = spreadDimensions( N, y, idx, true );
184184
}
185185
return y;
186186
}

lib/node_modules/@stdlib/ndarray/any/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function any( x, options ) {
127127

128128
// Check whether we need to reinsert singleton dimensions which can be useful for broadcasting the returned output array to the shape of the original input array...
129129
if ( opts.keepdims ) {
130-
y = spreadDimensions( N, y, idx );
130+
y = spreadDimensions( N, y, idx, true );
131131
}
132132
return y;
133133
}

lib/node_modules/@stdlib/ndarray/base/binary-reduce-strided1d-dispatch/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ setReadOnly( BinaryStrided1dDispatch.prototype, 'apply', function apply( x, y )
364364

365365
// Check whether we need to reinsert singleton dimensions which can be useful for broadcasting the returned output array to the shape of the original input array...
366366
if ( opts.keepdims ) {
367-
z = spreadDimensions( N, z, idx );
367+
z = spreadDimensions( N, z, idx, true );
368368
}
369369
return z;
370370
});

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ limitations under the License.
4040
var spreadDimensions = require( '@stdlib/ndarray/base/spread-dimensions' );
4141
```
4242

43-
#### spreadDimensions( ndims, x, dims )
43+
#### spreadDimensions( ndims, x, dims, writable )
4444

4545
Expands the shape of an array to a specified dimensionality by spreading its dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions.
4646

@@ -53,7 +53,7 @@ var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
5353
// returns <ndarray>
5454

5555
// Prepend a singleton dimension:
56-
var y = spreadDimensions( 3, x, [ 1, 2 ] );
56+
var y = spreadDimensions( 3, x, [ 1, 2 ], false );
5757
// returns <ndarray>
5858

5959
var sh = y.shape;
@@ -63,7 +63,7 @@ var a = ndarray2array( y );
6363
// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ]
6464

6565
// Append a singleton dimension:
66-
y = spreadDimensions( 3, x, [ 0, 1 ] );
66+
y = spreadDimensions( 3, x, [ 0, 1 ], false );
6767
// returns <ndarray>
6868

6969
sh = y.shape;
@@ -73,7 +73,7 @@ a = ndarray2array( y );
7373
// returns [ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ]
7474

7575
// Insert a singleton dimension:
76-
y = spreadDimensions( 3, x, [ 0, 2 ] );
76+
y = spreadDimensions( 3, x, [ 0, 2 ], false );
7777
// returns <ndarray>
7878

7979
sh = y.shape;
@@ -83,6 +83,13 @@ a = ndarray2array( y );
8383
// returns [ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
8484
```
8585

86+
The function accepts the following arguments:
87+
88+
- **ndims**: number of dimensions in the output ndarray. Must be greater than or equal to the number of dimensions in the input ndarray.
89+
- **x**: input ndarray.
90+
- **dims**: dimension indices specifying where to place the dimensions of the input ndarray. Must resolve to normalized indices arranged in ascending order.
91+
- **writable**: boolean indicating whether a returned ndarray should be writable.
92+
8693
</section>
8794

8895
<!-- /.usage -->
@@ -95,6 +102,7 @@ a = ndarray2array( y );
95102

96103
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. If provided a negative dimension index, the position at which to place a respective dimension is computed as `ndims + index`.
97104
- Provided dimension indices must resolve to normalized dimension indices arranged in ascending order.
105+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
98106

99107
</section>
100108

@@ -121,7 +129,7 @@ var x = array( [ [ 1, 2 ], [ 3, 4 ] ], {
121129
// returns <ndarray>
122130

123131
// Spread dimensions:
124-
var y = spreadDimensions( 5, x, [ 1, 3 ] );
132+
var y = spreadDimensions( 5, x, [ 1, 3 ], false );
125133
// returns <ndarray>
126134

127135
// Retrieve the shape:

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/benchmark/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bench( pkg+'::base_ndarray,2d', function benchmark( b ) {
5959

6060
b.tic();
6161
for ( i = 0; i < b.iterations; i++ ) {
62-
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ] );
62+
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ], false );
6363
if ( typeof out !== 'object' ) {
6464
b.fail( 'should return an object' );
6565
}
@@ -100,7 +100,7 @@ bench( pkg+'::ndarray,2d', function benchmark( b ) {
100100

101101
b.tic();
102102
for ( i = 0; i < b.iterations; i++ ) {
103-
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ] );
103+
out = spreadDimensions( 2, values[ i%values.length ], [ 1 ], false );
104104
if ( typeof out !== 'object' ) {
105105
b.fail( 'should return an object' );
106106
}

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/benchmark/benchmark.ndims.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function createBenchmark( ndims ) {
5252

5353
b.tic();
5454
for ( i = 0; i < b.iterations; i++ ) {
55-
out = spreadDimensions( ndims, x, [ i%ndims ] );
55+
out = spreadDimensions( ndims, x, [ i%ndims ], false );
5656
if ( typeof out !== 'object' ) {
5757
b.fail( 'should return an object' );
5858
}

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/docs/repl.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
{{alias}}( ndims, x, dims )
2+
{{alias}}( ndims, x, dims, writable )
33
Expands the shape of an array to a specified dimensionality by spreading its
44
dimensions to specified dimension indices and inserting dimensions of size
55
one for the remaining dimensions.
@@ -22,6 +22,11 @@
2222
Dimension indices specifying where to place the dimensions of the input
2323
array. Must resolve to normalized indices arranged in ascending order.
2424

25+
writable: boolean
26+
Boolean indicating whether a returned ndarray should be writable. This
27+
parameter only applies to ndarray constructors which support read-only
28+
instances.
29+
2530
Returns
2631
-------
2732
out: ndarray
@@ -33,7 +38,7 @@
3338
<ndarray>
3439
> var sh = x.shape
3540
[ 2, 2 ]
36-
> var y = {{alias}}( 5, x, [ 1, 3 ] )
41+
> var y = {{alias}}( 5, x, [ 1, 3 ], false )
3742
<ndarray>
3843
> sh = y.shape
3944
[ 1, 2, 1, 2, 1 ]

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/docs/types/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Collection } from '@stdlib/types/array';
3434
* @param ndims - number of dimensions in the output array
3535
* @param x - input array
3636
* @param dims - dimension indices
37+
* @param writable - boolean indicating whether a returned array should be writable
3738
* @returns output array
3839
*
3940
* @example
@@ -45,7 +46,7 @@ import { Collection } from '@stdlib/types/array';
4546
* var shx = x.shape;
4647
* // returns [ 2, 2 ]
4748
*
48-
* var y = spreadDimensions( 5, x, [ 1, 3 ] );
49+
* var y = spreadDimensions( 5, x, [ 1, 3 ], false );
4950
* // returns <ndarray>
5051
*
5152
* var shy = y.shape;
@@ -63,7 +64,7 @@ import { Collection } from '@stdlib/types/array';
6364
* v = y.get( 0, 1, 0, 1, 0 );
6465
* // returns 4
6566
*/
66-
declare function spreadDimensions<T extends ndarray>( ndims: number, x: T, dims: Collection<number> ): T;
67+
declare function spreadDimensions<T extends ndarray>( ndims: number, x: T, dims: Collection<number>, writable: boolean ): T;
6768

6869

6970
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/docs/types/test.ts

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,67 @@ import spreadDimensions = require( './index' );
2727
{
2828
const x = zeros( [ 2, 2 ] );
2929

30-
spreadDimensions( 5, x, [ 1, 3 ] ); // $ExpectType float64ndarray
30+
spreadDimensions( 5, x, [ 1, 3 ], false ); // $ExpectType float64ndarray
3131
}
3232

33-
// The compiler throws an error if the function is not provided a first argument which is a number...
33+
// The compiler throws an error if the function is provided a first argument which is not a number...
3434
{
3535
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
3636

37-
spreadDimensions( '5', x, [ 1, 3 ] ); // $ExpectError
38-
spreadDimensions( true, x, [ 1, 3 ] ); // $ExpectError
39-
spreadDimensions( false, x, [ 1, 3 ] ); // $ExpectError
40-
spreadDimensions( null, x, [ 1, 3 ] ); // $ExpectError
41-
spreadDimensions( {}, x, [ 1, 3 ] ); // $ExpectError
42-
spreadDimensions( [ '5' ], x, [ 1, 3 ] ); // $ExpectError
43-
spreadDimensions( ( x: number ): number => x, x, [ 1, 3 ] ); // $ExpectError
37+
spreadDimensions( '5', x, [ 1, 3 ], false ); // $ExpectError
38+
spreadDimensions( true, x, [ 1, 3 ], false ); // $ExpectError
39+
spreadDimensions( false, x, [ 1, 3 ], false ); // $ExpectError
40+
spreadDimensions( null, x, [ 1, 3 ], false ); // $ExpectError
41+
spreadDimensions( {}, x, [ 1, 3 ], false ); // $ExpectError
42+
spreadDimensions( [ '5' ], x, [ 1, 3 ], false ); // $ExpectError
43+
spreadDimensions( ( x: number ): number => x, x, [ 1, 3 ], false ); // $ExpectError
4444
}
4545

46-
// The compiler throws an error if the function is not provided a second argument which is an ndarray...
46+
// The compiler throws an error if the function is provided a second argument which is not an ndarray...
4747
{
48-
spreadDimensions( 5, '5', [ 1, 3 ] ); // $ExpectError
49-
spreadDimensions( 5, 5, [ 1, 3 ] ); // $ExpectError
50-
spreadDimensions( 5, true, [ 1, 3 ] ); // $ExpectError
51-
spreadDimensions( 5, false, [ 1, 3 ] ); // $ExpectError
52-
spreadDimensions( 5, null, [ 1, 3 ] ); // $ExpectError
53-
spreadDimensions( 5, {}, [ 1, 3 ] ); // $ExpectError
54-
spreadDimensions( 5, [ '5' ], [ 1, 3 ] ); // $ExpectError
55-
spreadDimensions( 5, ( x: number ): number => x, [ 1, 3 ] ); // $ExpectError
48+
spreadDimensions( 5, '5', [ 1, 3 ], false ); // $ExpectError
49+
spreadDimensions( 5, 5, [ 1, 3 ], false ); // $ExpectError
50+
spreadDimensions( 5, true, [ 1, 3 ], false ); // $ExpectError
51+
spreadDimensions( 5, false, [ 1, 3 ], false ); // $ExpectError
52+
spreadDimensions( 5, null, [ 1, 3 ], false ); // $ExpectError
53+
spreadDimensions( 5, {}, [ 1, 3 ], false ); // $ExpectError
54+
spreadDimensions( 5, [ '5' ], [ 1, 3 ], false ); // $ExpectError
55+
spreadDimensions( 5, ( x: number ): number => x, [ 1, 3 ], false ); // $ExpectError
5656
}
5757

58-
// The compiler throws an error if the function is not provided a third argument which is an array of numbers...
58+
// The compiler throws an error if the function is provided a third argument which is not an array of numbers...
5959
{
6060
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
6161

62-
spreadDimensions( 5, x, '5' ); // $ExpectError
63-
spreadDimensions( 5, x, true ); // $ExpectError
64-
spreadDimensions( 5, x, false ); // $ExpectError
65-
spreadDimensions( 5, x, null ); // $ExpectError
66-
spreadDimensions( 5, x, {} ); // $ExpectError
67-
spreadDimensions( 5, x, [ '5' ] ); // $ExpectError
68-
spreadDimensions( 5, x, ( x: number ): number => x ); // $ExpectError
62+
spreadDimensions( 5, x, '5', false ); // $ExpectError
63+
spreadDimensions( 5, x, true, false ); // $ExpectError
64+
spreadDimensions( 5, x, false, false ); // $ExpectError
65+
spreadDimensions( 5, x, null, false ); // $ExpectError
66+
spreadDimensions( 5, x, {}, false ); // $ExpectError
67+
spreadDimensions( 5, x, [ '5' ], false ); // $ExpectError
68+
spreadDimensions( 5, x, ( x: number ): number => x, false ); // $ExpectError
6969
}
7070

71+
// The compiler throws an error if the function is provided a fourth argument which is not a boolean...
72+
{
73+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
74+
75+
spreadDimensions( 5, x, '5', false ); // $ExpectError
76+
spreadDimensions( 5, x, true, false ); // $ExpectError
77+
spreadDimensions( 5, x, false, false ); // $ExpectError
78+
spreadDimensions( 5, x, null, false ); // $ExpectError
79+
spreadDimensions( 5, x, {}, false ); // $ExpectError
80+
spreadDimensions( 5, x, [ '5' ], false ); // $ExpectError
81+
spreadDimensions( 5, x, ( x: number ): number => x, false ); // $ExpectError
82+
}
83+
84+
7185
// The compiler throws an error if the function is provided an unsupported number of arguments...
7286
{
7387
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
7488

7589
spreadDimensions(); // $ExpectError
7690
spreadDimensions( 5, x ); // $ExpectError
77-
spreadDimensions( 5, x, [ 1, 3 ], {} ); // $ExpectError
91+
spreadDimensions( 5, x, [ 1, 3 ] ); // $ExpectError
92+
spreadDimensions( 5, x, [ 1, 3 ], false, {} ); // $ExpectError
7893
}

lib/node_modules/@stdlib/ndarray/base/spread-dimensions/examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var x = array( [ [ 1, 2 ], [ 3, 4 ] ], {
3030
// returns <ndarray>
3131

3232
// Spread dimensions:
33-
var y = spreadDimensions( 5, x, [ 1, 3 ] );
33+
var y = spreadDimensions( 5, x, [ 1, 3 ], false );
3434
// returns <ndarray>
3535

3636
// Retrieve the shape:

0 commit comments

Comments
 (0)