Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions lib/node_modules/@stdlib/ndarray/base/expand-dimensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ limitations under the License.
var expandDimensions = require( '@stdlib/ndarray/base/expand-dimensions' );
```

#### expandDimensions( x, axis )
#### expandDimensions( x, axis, writable )

Expands the shape of an array `x` by inserting a new dimension of size one at a specified `axis`.

Expand All @@ -53,27 +53,33 @@ var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]

// Prepend a singleton dimension:
var y = expandDimensions( x, 0 );
var y = expandDimensions( x, 0, false );
// returns <ndarray>[ [ [ 1, 2 ], [ 3, 4 ] ] ]

var sh = getShape( y );
// returns [ 1, 2, 2 ]

// Append a singleton dimension:
y = expandDimensions( x, 2 );
y = expandDimensions( x, 2, false );
// returns <ndarray>[ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ]

sh = getShape( y );
// returns [ 2, 2, 1 ]

// Insert a singleton dimension:
y = expandDimensions( x, 1 );
y = expandDimensions( x, 1, false );
// returns <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]

sh = getShape( y );
// returns [ 2, 1, 2 ]
```

The function accepts the following arguments:

- **x**: input ndarray.
- **axis**: axis at which to insert a singleton dimension
- **writable**: boolean indicating whether a returned ndarray should be writable.

</section>

<!-- /.usage -->
Expand All @@ -85,6 +91,7 @@ sh = getShape( y );
## Notes

- A provided axis must reside on the interval `[-N-1, N]`, where `N` is the rank (i.e., number of dimensions) of the provided input array. If provided a negative `axis`, the axis position at which to insert a singleton dimension is computed as `N + axis + 1`. Hence, if provided `-1`, the resolved axis position is `N` (i.e., a singleton dimension is appended to the input array).
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.

</section>

Expand All @@ -99,32 +106,15 @@ sh = getShape( y );
<!-- eslint no-undef: "error" -->

```javascript
var array = require( '@stdlib/ndarray/array' );
var numel = require( '@stdlib/ndarray/base/numel' );
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
var getShape = require( '@stdlib/ndarray/shape' );
var uniform = require( '@stdlib/random/uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var expandDimensions = require( '@stdlib/ndarray/base/expand-dimensions' );

// Create a 2-dimensional array:
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
// returns <ndarray>

// Insert a singleton dimension:
var y = expandDimensions( x, 1 );
// returns <ndarray>

// Retrieve the shape:
var sh = getShape( y );
// returns [ 2, 1, 2 ]

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

// Loop through the array elements...
var i;
for ( i = 0; i < N; i++ ) {
console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) );
}
var y = expandDimensions( x, 1, false );
console.log( ndarray2array( y ) );
```

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// MAIN //

bench( pkg+'::base_ndarray,2d', function benchmark( b ) {

Check warning on line 34 in lib/node_modules/@stdlib/ndarray/base/expand-dimensions/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var strides;
var values;
var buffer;
Expand Down Expand Up @@ -59,7 +59,7 @@

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = expandDimensions( values[ i%values.length ], 1 );
out = expandDimensions( values[ i%values.length ], 1, false );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
Expand All @@ -72,7 +72,7 @@
b.end();
});

bench( pkg+'::ndarray,2d', function benchmark( b ) {

Check warning on line 75 in lib/node_modules/@stdlib/ndarray/base/expand-dimensions/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
var strides;
var values;
var buffer;
Expand Down Expand Up @@ -100,7 +100,7 @@

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = expandDimensions( values[ i%values.length ], 1 );
out = expandDimensions( values[ i%values.length ], 1, false );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = expandDimensions( x, i%(ndims-1) );
out = expandDimensions( x, i%(ndims-1), false );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
Expand Down Expand Up @@ -87,7 +87,7 @@

for ( i = min; i <= max; i++ ) {
f = createBenchmark( i );
bench( pkg+'::ndarray:ndims='+i, f );

Check warning on line 90 in lib/node_modules/@stdlib/ndarray/base/expand-dimensions/benchmark/benchmark.ndims.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{{alias}}( x, axis )
{{alias}}( x, axis, writable )
Expands the shape of an array by inserting a new dimension of size one at a
specified axis.

Expand All @@ -11,6 +11,9 @@
the resolved axis position is `N` (i.e., a singleton dimension is appended
to the input array).

The `writable` parameter only applies to ndarray constructors supporting
read-only instances.

Parameters
----------
x: ndarray
Expand All @@ -19,6 +22,9 @@
axis: integer
Axis at which to insert a singleton dimension.

writable: boolean
Boolean indicating whether the returned ndarray should be writable.

Returns
-------
out: ndarray
Expand All @@ -28,12 +34,8 @@
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
> var sh = {{alias:@stdlib/ndarray/shape}}( x )
[ 2, 2 ]
> var y = {{alias}}( x, 1 )
> var y = {{alias}}( x, 1, false )
<ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
> sh = {{alias:@stdlib/ndarray/shape}}( y )
[ 2, 1, 2 ]

See Also
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,19 @@ import { typedndarray } from '@stdlib/types/ndarray';
*
* @param x - input array
* @param axis - axis at which to insert a singleton dimension
* @param writable - boolean indicating whether the returned ndarray should be writable
* @returns output array
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
*
* var shx = getShape( x );
* // returns [ 2, 2 ]
*
* var y = expandDimensions( x, 1 );
* var y = expandDimensions( x, 1, false );
* // returns <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
*
* var shy = getShape( y );
* // returns [ 2, 1, 2 ]
*/
declare function expandDimensions<T = unknown>( x: typedndarray<T>, axis: number ): typedndarray<T>;
declare function expandDimensions<T = unknown>( x: typedndarray<T>, axis: number, writable: boolean ): typedndarray<T>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,47 @@ import expandDimensions = require( './index' );
{
const x = zeros( [ 2, 2 ] );

expandDimensions( x, 1 ); // $ExpectType typedndarray<number>
expandDimensions( x, 1, false ); // $ExpectType typedndarray<number>
}

// The compiler throws an error if the function is not provided a first argument which is an ndarray...
{
expandDimensions( '5', 1 ); // $ExpectError
expandDimensions( 5, 1 ); // $ExpectError
expandDimensions( true, 1 ); // $ExpectError
expandDimensions( false, 1 ); // $ExpectError
expandDimensions( null, 1 ); // $ExpectError
expandDimensions( {}, 1 ); // $ExpectError
expandDimensions( [ '5' ], 1 ); // $ExpectError
expandDimensions( ( x: number ): number => x, 1 ); // $ExpectError
expandDimensions( '5', 1, false ); // $ExpectError
expandDimensions( 5, 1, false ); // $ExpectError
expandDimensions( true, 1, false ); // $ExpectError
expandDimensions( false, 1, false ); // $ExpectError
expandDimensions( void 0, 1, false ); // $ExpectError
expandDimensions( null, 1, false ); // $ExpectError
expandDimensions( {}, 1, false ); // $ExpectError
expandDimensions( [ '5' ], 1, false ); // $ExpectError
expandDimensions( ( x: number ): number => x, 1, false ); // $ExpectError
}

// The compiler throws an error if the function is not provided a second argument which is a number...
{
const x = zeros( [ 2, 2 ] );

expandDimensions( x, '5' ); // $ExpectError
expandDimensions( x, true ); // $ExpectError
expandDimensions( x, false ); // $ExpectError
expandDimensions( x, null ); // $ExpectError
expandDimensions( x, {} ); // $ExpectError
expandDimensions( x, [ '5' ] ); // $ExpectError
expandDimensions( x, ( x: number ): number => x ); // $ExpectError
expandDimensions( x, '5', false ); // $ExpectError
expandDimensions( x, true, false ); // $ExpectError
expandDimensions( x, false, false ); // $ExpectError
expandDimensions( x, void 0, false ); // $ExpectError
expandDimensions( x, null, false ); // $ExpectError
expandDimensions( x, {}, false ); // $ExpectError
expandDimensions( x, [ '5' ], false ); // $ExpectError
expandDimensions( x, ( x: number ): number => x, false ); // $ExpectError
}

// The compiler throws an error if the function is not provided a third argument which is a boolean...
{
const x = zeros( [ 2, 2 ] );

expandDimensions( x, 1, '5' ); // $ExpectError
expandDimensions( x, 1, 5 ); // $ExpectError
expandDimensions( x, 1, void 0 ); // $ExpectError
expandDimensions( x, 1, null ); // $ExpectError
expandDimensions( x, 1, {} ); // $ExpectError
expandDimensions( x, 1, [ '5' ] ); // $ExpectError
expandDimensions( x, 1, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
Expand All @@ -60,5 +75,6 @@ import expandDimensions = require( './index' );

expandDimensions(); // $ExpectError
expandDimensions( x ); // $ExpectError
expandDimensions( x, 1, [ 1, 2, 3 ], [ 2, 3 ] ); // $ExpectError
expandDimensions( x, 1 ); // $ExpectError
expandDimensions( x, 1, false, [ 1, 2, 3 ] ); // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,12 @@

'use strict';

var array = require( '@stdlib/ndarray/array' );
var numel = require( '@stdlib/ndarray/base/numel' );
var ind2sub = require( '@stdlib/ndarray/ind2sub' );
var getShape = require( '@stdlib/ndarray/shape' );
var uniform = require( '@stdlib/random/uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var expandDimensions = require( './../lib' );

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

// Insert a singleton dimension:
var y = expandDimensions( x, 1 );
// returns <ndarray>

// Retrieve the shape:
var sh = getShape( y );
// returns [ 2, 1, 2 ]

// Retrieve the number of elements:
var N = numel( sh );

// Loop through the array elements...
var i;
for ( i = 0; i < N; i++ ) {
console.log( 'Y[%s] = %d', ind2sub( sh, i ).join( ', ' ), y.iget( i ) );
}
var y = expandDimensions( x, 1, false );
console.log( ndarray2array( y ) );
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@
* @module @stdlib/ndarray/base/expand-dimensions
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var array = require( '@stdlib/ndarray/array' );
* var expandDimensions = require( '@stdlib/ndarray/base/expand-dimensions' );
*
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
*
* var shx = getShape( x );
* // returns [ 2, 2 ]
*
* var y = expandDimensions( x, 1 );
* var y = expandDimensions( x, 1, false );
* // returns <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
*
* var shy = getShape( y );
* // returns [ 2, 1, 2 ]
*/

// MODULES //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// MODULES //

var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var getShape = require( '@stdlib/ndarray/base/shape' );
Expand All @@ -43,26 +42,20 @@ var format = require( '@stdlib/string/format' );
*
* @param {ndarray} x - input array
* @param {integer} axis - axis at which to insert a singleton dimension
* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable
* @throws {RangeError} must provide a valid axis
* @returns {ndarray} output array
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
*
* var shx = getShape( x );
* // returns [ 2, 2 ]
*
* var y = expandDimensions( x, 1 );
* var y = expandDimensions( x, 1, false );
* // returns <ndarray>[ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ]
*
* var shy = getShape( y );
* // returns [ 2, 1, 2 ]
*/
function expandDimensions( x, axis ) {
function expandDimensions( x, axis, writable ) {
var strides;
var shape;
var isrm;
Expand Down Expand Up @@ -132,13 +125,9 @@ function expandDimensions( x, axis ) {
}
}
}
if ( isReadOnly( x ) ) {
// If provided a read-only view, the returned array should also be read-only...
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), ord, { // eslint-disable-line max-len
'readonly': true
});
}
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), ord ); // eslint-disable-line max-len
return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), ord, { // eslint-disable-line max-len
'readonly': !writable
});
}


Expand Down
Loading