Skip to content

Commit 3a79543

Browse files
committed
refactor: add writable parameter
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 50367fe commit 3a79543

10 files changed

Lines changed: 91 additions & 218 deletions

File tree

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

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ limitations under the License.
4242
var prependSingletonDimensions = require( '@stdlib/ndarray/base/prepend-singleton-dimensions' );
4343
```
4444

45-
#### prependSingletonDimensions( x, n )
45+
#### prependSingletonDimensions( x, n, writable )
4646

4747
Returns an ndarray with a specified number of prepended singleton dimensions (i.e., dimensions whose size is equal to `1`).
4848

@@ -53,16 +53,19 @@ var array = require( '@stdlib/ndarray/array' );
5353

5454
// Create a 2x2 ndarray:
5555
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
56-
// returns <ndarray>
56+
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
5757

5858
// Prepend singleton dimensions:
59-
var y = prependSingletonDimensions( x, 3 );
60-
// returns <ndarray>
61-
62-
var sh = y.shape;
63-
// returns [ 1, 1, 1, 2, 2 ]
59+
var y = prependSingletonDimensions( x, 3, false );
60+
// returns <ndarray>[ [ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ] ]
6461
```
6562

63+
The function accepts the following arguments:
64+
65+
- **x**: input ndarray.
66+
- **n**: number of singleton dimensions to prepend.
67+
- **writable**: boolean indicating whether a returned ndarray should be writable.
68+
6669
</section>
6770

6871
<!-- /.usage -->
@@ -86,31 +89,15 @@ var sh = y.shape;
8689
<!-- eslint no-undef: "error" -->
8790

8891
```javascript
89-
var array = require( '@stdlib/ndarray/array' );
90-
var numel = require( '@stdlib/ndarray/base/numel' );
91-
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
92-
var prependSingletonDimensions = require( '@stdlib/ndarray/base/prepend-singleton-dimensions' );
93-
94-
// Create a 2-dimensional array:
95-
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
96-
// returns <ndarray>
97-
98-
// Prepend singleton dimensions:
99-
var y = prependSingletonDimensions( x, 3 );
100-
// returns <ndarray>
101-
102-
// Retrieve the shape:
103-
var sh = y.shape;
104-
// returns [ 1, 1, 1, 2, 2 ]
92+
var uniform = require( '@stdlib/random/uniform' );
93+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
94+
var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' );
10595

106-
// Retrieve the number of elements:
107-
var N = numel( sh );
96+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
97+
console.log( ndarray2array( x ) );
10898

109-
// Loop through the array elements...
110-
var i;
111-
for ( i = 0; i < N; i++ ) {
112-
console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) );
113-
}
99+
var y = prependSingletonDimensions( 5, x, [ 0, 1, 2 ] );
100+
console.log( ndarray2array( y ) );
114101
```
115102

116103
</section>

lib/node_modules/@stdlib/ndarray/base/prepend-singleton-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 = prependSingletonDimensions( values[ i%values.length ], 1 );
62+
out = prependSingletonDimensions( 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 = prependSingletonDimensions( values[ i%values.length ], 1 );
103+
out = prependSingletonDimensions( 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/prepend-singleton-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 = prependSingletonDimensions( x, ndims );
55+
out = prependSingletonDimensions( x, ndims, false );
5656
if ( typeof out !== 'object' ) {
5757
b.fail( 'should return an object' );
5858
}

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

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

2-
{{alias}}( x, n )
2+
{{alias}}( x, n, writable )
33
Returns an array with a specified number of prepended singleton dimensions.
44

55
Parameters
@@ -10,6 +10,9 @@
1010
n: integer
1111
Number of singleton dimensions to prepend.
1212

13+
writable: boolean
14+
Boolean indicating whether a returned array should be writable.
15+
1316
Returns
1417
-------
1518
out: ndarray
@@ -18,21 +21,9 @@
1821
Examples
1922
--------
2023
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
21-
<ndarray>
22-
> var sh = x.shape
23-
[ 2, 2 ]
24-
> var y = {{alias}}( x, 3 )
25-
<ndarray>
26-
> sh = y.shape
27-
[ 1, 1, 1, 2, 2 ]
28-
> var v = y.get( 0, 0, 0, 0, 0 )
29-
1
30-
> v = y.get( 0, 0, 0, 0, 1 )
31-
2
32-
> v = y.get( 0, 0, 0, 1, 0 )
33-
3
34-
> v = y.get( 0, 0, 0, 1, 1 )
35-
4
24+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
25+
> var y = {{alias}}( x, 3, false )
26+
<ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
3627

3728
See Also
3829
--------

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

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,19 @@ import { ndarray } from '@stdlib/types/ndarray';
2727
*
2828
* @param x - input array
2929
* @param n - number of singleton dimensions to prepend
30+
* @param writable - boolean indicating whether a returned array should be writable
3031
* @returns output array
3132
*
3233
* @example
3334
* var array = require( '@stdlib/ndarray/array' );
3435
*
3536
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
36-
* // returns <ndarray>
37+
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
3738
*
38-
* var shx = x.shape;
39-
* // returns [ 2, 2 ]
40-
*
41-
* var y = prependSingletonDimensions( x, 3 );
42-
* // returns <ndarray>
43-
*
44-
* var shy = y.shape;
45-
* // returns [ 1, 1, 1, 2, 2 ]
46-
*
47-
* var v = y.get( 0, 0, 0, 0, 0 );
48-
* // returns 1
49-
*
50-
* v = y.get( 0, 0, 0, 0, 1 );
51-
* // returns 2
52-
*
53-
* v = y.get( 0, 0, 0, 1, 0 );
54-
* // returns 3
55-
*
56-
* v = y.get( 0, 0, 0, 1, 1 );
57-
* // returns 4
39+
* var y = prependSingletonDimensions( x, 3, false );
40+
* // returns <ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
5841
*/
59-
declare function prependSingletonDimensions( x: ndarray, n: number ): ndarray;
42+
declare function prependSingletonDimensions( x: ndarray, n: number, writable: boolean ): ndarray;
6043

6144

6245
// EXPORTS //

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,44 @@ import prependSingletonDimensions = require( './index' );
2626
{
2727
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
2828

29-
prependSingletonDimensions( x, 3 ); // $ExpectType ndarray
29+
prependSingletonDimensions( x, 3, false ); // $ExpectType ndarray
3030
}
3131

3232
// The compiler throws an error if the function is not provided a first argument which is an ndarray...
3333
{
34-
prependSingletonDimensions( '5', 3 ); // $ExpectError
35-
prependSingletonDimensions( 5, 3 ); // $ExpectError
36-
prependSingletonDimensions( true, 3 ); // $ExpectError
37-
prependSingletonDimensions( false, 3 ); // $ExpectError
38-
prependSingletonDimensions( null, 3 ); // $ExpectError
39-
prependSingletonDimensions( {}, 3 ); // $ExpectError
40-
prependSingletonDimensions( [ '5' ], 3 ); // $ExpectError
41-
prependSingletonDimensions( ( x: number ): number => x, 3 ); // $ExpectError
34+
prependSingletonDimensions( '5', 3, false ); // $ExpectError
35+
prependSingletonDimensions( 5, 3, false ); // $ExpectError
36+
prependSingletonDimensions( true, 3, false ); // $ExpectError
37+
prependSingletonDimensions( false, 3, false ); // $ExpectError
38+
prependSingletonDimensions( null, 3, false ); // $ExpectError
39+
prependSingletonDimensions( {}, 3, false ); // $ExpectError
40+
prependSingletonDimensions( [ '5' ], 3, false ); // $ExpectError
41+
prependSingletonDimensions( ( x: number ): number => x, 3, false ); // $ExpectError
4242
}
4343

4444
// The compiler throws an error if the function is not provided a second argument which is a number...
4545
{
4646
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
4747

48-
prependSingletonDimensions( x, '5' ); // $ExpectError
49-
prependSingletonDimensions( x, true ); // $ExpectError
50-
prependSingletonDimensions( x, false ); // $ExpectError
51-
prependSingletonDimensions( x, null ); // $ExpectError
52-
prependSingletonDimensions( x, {} ); // $ExpectError
53-
prependSingletonDimensions( x, [ '5' ] ); // $ExpectError
54-
prependSingletonDimensions( x, ( x: number ): number => x ); // $ExpectError
48+
prependSingletonDimensions( x, '5', false ); // $ExpectError
49+
prependSingletonDimensions( x, true, false ); // $ExpectError
50+
prependSingletonDimensions( x, false, false ); // $ExpectError
51+
prependSingletonDimensions( x, null, false ); // $ExpectError
52+
prependSingletonDimensions( x, {}, false ); // $ExpectError
53+
prependSingletonDimensions( x, [ '5' ], false ); // $ExpectError
54+
prependSingletonDimensions( x, ( x: number ): number => x, false ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is not provided a third argument which is a boolean...
58+
{
59+
const x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
60+
61+
prependSingletonDimensions( x, 3, '5' ); // $ExpectError
62+
prependSingletonDimensions( x, 3, 5 ); // $ExpectError
63+
prependSingletonDimensions( x, 3, null ); // $ExpectError
64+
prependSingletonDimensions( x, 3, {} ); // $ExpectError
65+
prependSingletonDimensions( x, 3, [ '5' ] ); // $ExpectError
66+
prependSingletonDimensions( x, 3, ( x: number ): number => x ); // $ExpectError
5567
}
5668

5769
// The compiler throws an error if the function is provided an unsupported number of arguments...
@@ -60,5 +72,6 @@ import prependSingletonDimensions = require( './index' );
6072

6173
prependSingletonDimensions(); // $ExpectError
6274
prependSingletonDimensions( x ); // $ExpectError
63-
prependSingletonDimensions( x, 3, [ 1, 2, 3 ], [ 2, 3 ] ); // $ExpectError
75+
prependSingletonDimensions( x, 3 ); // $ExpectError
76+
prependSingletonDimensions( x, 3, false, [ 1, 2, 3 ], [ 2, 3 ] ); // $ExpectError
6477
}

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

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,12 @@
1818

1919
'use strict';
2020

21-
var array = require( '@stdlib/ndarray/array' );
22-
var numel = require( '@stdlib/ndarray/base/numel' );
23-
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
24-
var prependSingletonDimensions = require( './../lib' ); // eslint-disable-line id-length
21+
var uniform = require( '@stdlib/random/uniform' );
22+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
23+
var prependSingletonDimensions = require( './../lib' );
2524

26-
// Create a 2-dimensional array:
27-
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
28-
// returns <ndarray>
25+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
26+
console.log( ndarray2array( x ) );
2927

30-
// Prepend singleton dimensions:
31-
var y = prependSingletonDimensions( x, 3 );
32-
// returns <ndarray>
33-
34-
// Retrieve the shape:
35-
var sh = y.shape;
36-
// returns [ 1, 1, 1, 2, 2 ]
37-
38-
// Retrieve the number of elements:
39-
var N = numel( sh );
40-
41-
// Loop through the array elements...
42-
var i;
43-
for ( i = 0; i < N; i++ ) {
44-
console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) );
45-
}
28+
var y = prependSingletonDimensions( 5, x, [ 0, 1, 2 ] );
29+
console.log( ndarray2array( y ) );

lib/node_modules/@stdlib/ndarray/base/prepend-singleton-dimensions/lib/index.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,10 @@
2828
* var prependSingletonDimensions = require( '@stdlib/ndarray/base/prepend-singleton-dimensions' );
2929
*
3030
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
31-
* // returns <ndarray>
31+
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
3232
*
33-
* var shx = x.shape;
34-
* // returns [ 2, 2 ]
35-
*
36-
* var y = prependSingletonDimensions( x, 3 );
37-
* // returns <ndarray>
38-
*
39-
* var shy = y.shape;
40-
* // returns [ 1, 1, 1, 2, 2 ]
41-
*
42-
* var v = y.get( 0, 0, 0, 0, 0 );
43-
* // returns 1
44-
*
45-
* v = y.get( 0, 0, 0, 0, 1 );
46-
* // returns 2
47-
*
48-
* v = y.get( 0, 0, 0, 1, 0 );
49-
* // returns 3
50-
*
51-
* v = y.get( 0, 0, 0, 1, 1 );
52-
* // returns 4
33+
* var y = prependSingletonDimensions( x, 3, false );
34+
* // returns <ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
5335
*/
5436

5537
// MODULES //

lib/node_modules/@stdlib/ndarray/base/prepend-singleton-dimensions/lib/main.js

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
// MODULES //
2222

23-
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
2423
var getDType = require( '@stdlib/ndarray/base/dtype' );
2524
var getShape = require( '@stdlib/ndarray/base/shape' );
2625
var getStrides = require( '@stdlib/ndarray/base/strides' );
@@ -36,36 +35,19 @@ var getData = require( '@stdlib/ndarray/base/data-buffer' );
3635
*
3736
* @param {ndarray} x - input array
3837
* @param {NonNegativeInteger} n - number of singleton dimensions to prepend
38+
* @param {boolean} writable - boolean indicating whether a returned array should be writable
3939
* @returns {ndarray} output array
4040
*
4141
* @example
4242
* var array = require( '@stdlib/ndarray/array' );
4343
*
4444
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
45-
* // returns <ndarray>
45+
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
4646
*
47-
* var shx = x.shape;
48-
* // returns [ 2, 2 ]
49-
*
50-
* var y = prependSingletonDimensions( x, 3 );
51-
* // returns <ndarray>
52-
*
53-
* var shy = y.shape;
54-
* // returns [ 1, 1, 1, 2, 2 ]
55-
*
56-
* var v = y.get( 0, 0, 0, 0, 0 );
57-
* // returns 1
58-
*
59-
* v = y.get( 0, 0, 0, 0, 1 );
60-
* // returns 2
61-
*
62-
* v = y.get( 0, 0, 0, 1, 0 );
63-
* // returns 3
64-
*
65-
* v = y.get( 0, 0, 0, 1, 1 );
66-
* // returns 4
47+
* var y = prependSingletonDimensions( x, 3, false );
48+
* // returns <ndarray>[ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ]
6749
*/
68-
function prependSingletonDimensions( x, n ) { // eslint-disable-line id-length
50+
function prependSingletonDimensions( x, n, writable ) { // eslint-disable-line id-length
6951
var strides;
7052
var shape;
7153
var sh;
@@ -90,13 +72,9 @@ function prependSingletonDimensions( x, n ) { // eslint-disable-line id-length
9072
shape.push( sh[ i ] );
9173
strides.push( st[ i ] );
9274
}
93-
if ( isReadOnly( x ) ) {
94-
// If provided a read-only view, the returned array should also be read-only...
95-
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len
96-
'readonly': true
97-
});
98-
}
99-
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ) ); // eslint-disable-line max-len
75+
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), getOrder( x ), { // eslint-disable-line max-len
76+
'readonly': !writable
77+
});
10078
}
10179

10280

0 commit comments

Comments
 (0)