Skip to content

Commit 2444ee0

Browse files
committed
chore: clean-up
--- 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: passed - 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: na - 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 f6880d3 commit 2444ee0

12 files changed

Lines changed: 289 additions & 87 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/README.md

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

2121
# unflattenShape
2222

23-
> Unflatten a shape over multiple dimensions.
23+
> Expand a dimension over multiple dimensions.
2424
2525
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
2626

@@ -42,7 +42,7 @@ var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );
4242

4343
#### unflattenShape( shape, dim, sizes )
4444

45-
Unflattens a shape over multiple dimensions.
45+
Expands a dimension over multiple dimensions.
4646

4747
```javascript
4848
var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
@@ -52,12 +52,12 @@ var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
5252
The function accepts the following parameters:
5353

5454
- **shape**: array shape.
55-
- **dim**: dimension to be unflattened.
55+
- **dim**: dimension to be unflattened. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
5656
- **sizes**: new shape of the unflattened dimension.
5757

5858
#### unflattenShape.assign( shape, dim, sizes, out )
5959

60-
Unflattens a shape over multiple dimensions and assigns results to a provided output array.
60+
Expands a dimension over multiple dimensions and assigns results to a provided output array.
6161

6262
```javascript
6363
var o = [ 0, 0, 0, 0 ];
@@ -72,7 +72,7 @@ var bool = ( out === o );
7272
The function accepts the following parameters:
7373

7474
- **shape**: array shape.
75-
- **dim**: dimension to be unflattened.
75+
- **dim**: dimension to be unflattened. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
7676
- **sizes**: new shape of the unflattened dimension.
7777
- **out**: output array.
7878

@@ -102,7 +102,11 @@ var unflattenShape = require( '@stdlib/ndarray/base/unflatten-shape' );
102102
var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] );
103103
// returns [ 2, 2, 2, 1 ]
104104

105-
console.log( 'Unflattened Shape: ', out );
105+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 2 ] );
106+
// returns [ 2, 2, 1, 2, 1 ]
107+
108+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 1, 2 ] );
109+
// returns [ 2, 2, 1, 1, 2, 1 ]
106110
```
107111

108112
</section>

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/repl.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
{{alias}}( shape, dim, sizes )
3-
Unflattens a shape over multiple dimensions.
3+
Expands a dimension over multiple dimensions.
44

55
Parameters
66
----------
7-
shape: ArrayLike
7+
shape: ArrayLike<number>
88
Array shape.
99

1010
dim: integer
11-
Dimension to be unflattened.
11+
Dimension to be unflattened. If less than zero, the index is resolved
12+
relative to the last dimension, with the last dimension corresponding to
13+
the value `-1`.
1214

13-
sizes: ArrayLike
15+
sizes: ArrayLike<number>
1416
New shape of the unflattened dimension.
1517

1618
Returns
1719
-------
18-
out: Array
20+
out: Array<number>
1921
Unflattened shape.
2022

2123
Examples
@@ -27,18 +29,20 @@
2729

2830

2931
{{alias}}.assign( shape, dim, sizes, out )
30-
Unflattens a shape over multiple dimensions and assigns results to a
32+
Expands a dimension over multiple dimensions and assigns results to a
3133
provided output array.
3234

3335
Parameters
3436
----------
35-
shape: ArrayLike
37+
shape: ArrayLike<number>
3638
Array shape.
3739

3840
dim: integer
39-
Dimension to be unflattened.
41+
Dimension to be unflattened. If less than zero, the index is resolved
42+
relative to the last dimension, with the last dimension corresponding to
43+
the value `-1`.
4044

41-
sizes: ArrayLike
45+
sizes: ArrayLike<number>
4246
New shape of the unflattened dimension.
4347

4448
out: Array|TypedArray|Object

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/index.d.ts

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

2121
/// <reference types="@stdlib/types"/>
2222

23+
import { ArrayLike } from '@stdlib/types/array';
24+
2325
/**
24-
* Interface describing `unflattenShape`
26+
* Interface describing `unflattenShape`.
2527
*/
2628
interface Routine {
2729
/**
28-
* Unflattens a shape over multiple dimensions.
30+
* Expands a dimension over multiple dimensions.
2931
*
3032
* @param shape - array shape
3133
* @param dim - dimension to be unflattened
@@ -36,10 +38,10 @@ interface Routine {
3638
* var sh = unflattenShape( [ 6, 2, 1 ], 0, [ 3, 2 ] );
3739
* // returns [ 3, 2, 2, 1 ]
3840
*/
39-
( shape: Array<number>, dim: number, sizes: Array<number> ): Array<number>;
41+
( shape: ArrayLike<number>, dim: number, sizes: ArrayLike<number> ): Array<number>;
4042

4143
/**
42-
* Unflattens a shape over multiple dimensions.
44+
* Expands a dimension over multiple dimensions.
4345
*
4446
* @param shape - array shape
4547
* @param dim - dimension to be unflattened
@@ -56,11 +58,11 @@ interface Routine {
5658
* var bool = ( out === o );
5759
* // returns true
5860
*/
59-
assign( shape: Array<number>, dim: number, sizes: Array<number>, out: Array<number> ): Array<number>;
61+
assign<T extends ArrayLike<number> = ArrayLike<number>>( shape: ArrayLike<number>, dim: number, sizes: ArrayLike<number>, out: T ): T;
6062
}
6163

6264
/**
63-
* Unflattens a shape over multiple dimensions.
65+
* Expands a dimension over multiple dimensions.
6466
*
6567
* @param shape - array shape
6668
* @param dim - dimension to be unflattened

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/docs/types/test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import unflattenShape = require( './index' );
2626
unflattenShape( [ 6, 2, 1 ], 1, [ 3, 2 ] ); // $ExpectType number[]
2727
}
2828

29-
// The compiler throws an error if the function is provided a first argument that is not an array-like object containing numbers...
29+
// The compiler throws an error if the function is provided a first argument which is not an array-like object containing numbers...
3030
{
3131
unflattenShape( true, 1, [ 3, 2 ] ); // $ExpectError
3232
unflattenShape( false, 1, [ 3, 2 ] ); // $ExpectError
@@ -38,7 +38,7 @@ import unflattenShape = require( './index' );
3838
unflattenShape( ( x: number ): number => x, 1, [ 3, 2 ] ); // $ExpectError
3939
}
4040

41-
// The compiler throws an error if the function is provided a second argument that is not a number...
41+
// The compiler throws an error if the function is provided a second argument which is not a number...
4242
{
4343
unflattenShape( [ 6, 2, 1 ], true, [ 3, 2 ] ); // $ExpectError
4444
unflattenShape( [ 6, 2, 1 ], false, [ 3, 2 ] ); // $ExpectError
@@ -50,7 +50,7 @@ import unflattenShape = require( './index' );
5050
unflattenShape( [ 6, 2, 1 ], ( x: number ): number => x, [ 3, 2 ] ); // $ExpectError
5151
}
5252

53-
// The compiler throws an error if the function is provided a third argument that is not an array-like object containing numbers...
53+
// The compiler throws an error if the function is provided a third argument which is not an array-like object containing numbers...
5454
{
5555
unflattenShape( [ 6, 2, 1 ], 1, true ); // $ExpectError
5656
unflattenShape( [ 6, 2, 1 ], 1, false ); // $ExpectError
@@ -70,12 +70,12 @@ import unflattenShape = require( './index' );
7070
unflattenShape( [ 6, 2, 1 ], 1, [ 3, 2 ], {} ); // $ExpectError
7171
}
7272

73-
// The assign method returns an array of numbers...
73+
// The `assign` method returns an array of numbers...
7474
{
7575
unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectType number[]
7676
}
7777

78-
// The compiler throws an error if the assign method is provided a first argument that is not an array-like object containing numbers...
78+
// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object containing numbers...
7979
{
8080
unflattenShape.assign( true, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
8181
unflattenShape.assign( false, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
@@ -87,7 +87,7 @@ import unflattenShape = require( './index' );
8787
unflattenShape.assign( ( x: number ): number => x, 1, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
8888
}
8989

90-
// The compiler throws an error if the assign method is provided a second argument that is not a number...
90+
// The compiler throws an error if the `assign` method is provided a second argument which is not a number...
9191
{
9292
unflattenShape.assign( [ 6, 2, 1 ], true, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
9393
unflattenShape.assign( [ 6, 2, 1 ], false, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
@@ -99,7 +99,7 @@ import unflattenShape = require( './index' );
9999
unflattenShape.assign( [ 6, 2, 1 ], ( x: number ): number => x, [ 3, 2 ], [ 0, 0, 0, 0 ] ); // $ExpectError
100100
}
101101

102-
// The compiler throws an error if the assign method is provided a third argument that is not an array-like object containing numbers...
102+
// The compiler throws an error if the `assign` method is provided a third argument which is not an array-like object containing numbers...
103103
{
104104
unflattenShape.assign( [ 6, 2, 1 ], 1, true, [ 0, 0, 0, 0 ] ); // $ExpectError
105105
unflattenShape.assign( [ 6, 2, 1 ], 1, false, [ 0, 0, 0, 0 ] ); // $ExpectError
@@ -111,7 +111,7 @@ import unflattenShape = require( './index' );
111111
unflattenShape.assign( [ 6, 2, 1 ], 1, ( x: number ): number => x, [ 0, 0, 0, 0 ] ); // $ExpectError
112112
}
113113

114-
// The compiler throws an error if the assign method is provided a fourth argument that is not an array-like object containing numbers...
114+
// The compiler throws an error if the `assign` method is provided a fourth argument which is not an array-like object containing numbers...
115115
{
116116
unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], true ); // $ExpectError
117117
unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], false ); // $ExpectError
@@ -123,7 +123,7 @@ import unflattenShape = require( './index' );
123123
unflattenShape.assign( [ 6, 2, 1 ], 1, [ 3, 2 ], ( x: number ): number => x ); // $ExpectError
124124
}
125125

126-
// The compiler throws an error if the assign method is provided an unsupported number of arguments...
126+
// The compiler throws an error if the `assign` method is provided an unsupported number of arguments...
127127
{
128128
unflattenShape.assign(); // $ExpectError
129129
unflattenShape.assign( [ 6, 2, 1 ] ); // $ExpectError

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/examples/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
var unflattenShape = require( './../lib' );
2222

2323
var out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 2 ] );
24-
// returns [ 2, 2, 2, 1 ]
24+
console.log( out );
25+
// => [ 2, 2, 2, 1 ]
2526

26-
console.log( 'Unflattened Shape: ', out );
27+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 2 ] );
28+
console.log( out );
29+
// => [ 2, 2, 1, 2, 1 ]
30+
31+
out = unflattenShape( [ 2, 4, 1 ], 1, [ 2, 1, 1, 2 ] );
32+
console.log( out );
33+
// => [ 2, 2, 1, 1, 2, 1 ]

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/assign.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020

2121
// MODULES //
2222

23-
var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' );
23+
var numel = require( '@stdlib/ndarray/base/numel' );
24+
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
25+
var join = require( '@stdlib/array/base/join' );
2426
var format = require( '@stdlib/string/format' );
2527

2628

2729
// MAIN //
2830

2931
/**
30-
* Unflattens a shape over multiple dimensions.
32+
* Expands a dimension over multiple dimensions.
3133
*
3234
* @param {NonNegativeIntegerArray} shape - array shape
33-
* @param {NonNegativeInteger} dim - dimension to be unflattened
35+
* @param {integer} dim - dimension to be unflattened
3436
* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension
3537
* @param {NonNegativeIntegerArray} out - output array
36-
* @throws {RangeError} - product of the sizes array must be equal to the dimension to be unflattened
38+
* @throws {RangeError} second argument is out-of-bounds
39+
* @throws {RangeError} product of the sizes must be equal to the size of the dimension to be unflattened
3740
* @returns {NonNegativeIntegerArray} unflattened shape
3841
*
3942
* @example
@@ -46,28 +49,37 @@ var format = require( '@stdlib/string/format' );
4649
* // returns true
4750
*/
4851
function unflattenShape( shape, dim, sizes, out ) {
49-
var s;
52+
var S1;
53+
var S2;
54+
var N;
55+
var d;
5056
var i;
5157
var j;
52-
var k;
5358

54-
s = flattenShape( sizes, sizes.length-1 );
55-
if ( shape[ dim ] !== s[ 0 ] ) {
56-
throw new RangeError( format( 'invalid argument. Product of the sizes array must be equal to the dimension to be unflattened. Dim: `%d`. Product: `%d`.', shape[dim], s[0] ) );
59+
S1 = shape.length;
60+
d = normalizeIndex( dim, S1-1 );
61+
if ( d < 0 ) {
62+
throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', S1, dim ) );
5763
}
58-
for ( i = 0; i < dim; i++ ) {
64+
S2 = sizes.length;
65+
N = numel( sizes );
66+
if ( N !== shape[ d ] ) {
67+
throw new RangeError( format( 'invalid argument. Product of the sizes must be equal to the size of the dimension to be unflattened. Dimension: %d. Size: %d. Value: `[%s]`.', d, shape[ d ], join( sizes, ', ' ) ) );
68+
}
69+
for ( i = 0; i < d; i++ ) {
5970
out[ i ] = shape[ i ];
6071
}
6172
j = 0;
62-
k = dim + sizes.length - 1;
63-
for ( i = dim; i <= k; i++ ) {
73+
for ( ; i < d+S2; i++ ) {
6474
out[ i ] = sizes[ j ];
6575
j += 1;
6676
}
67-
j = shape.length - 1;
68-
for ( i = out.length-1; i > k; i-- ) {
69-
out[ i ] = shape[ j ];
70-
j -= 1;
77+
j = d + 1;
78+
if ( j < S1 ) {
79+
for ( ; i < S1+S2-1; i++ ) {
80+
out[ i ] = shape[ j ];
81+
j += 1;
82+
}
7183
}
7284
return out;
7385
}

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Unflatten a shape over multiple dimensions.
22+
* Expand a dimension over multiple dimensions.
2323
*
2424
* @module @stdlib/ndarray/base/unflatten-shape
2525
*

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/lib/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ var assign = require( './assign.js' );
2727
// MAIN //
2828

2929
/**
30-
* Unflattens a shape over multiple dimensions.
30+
* Expands a dimension over multiple dimensions.
3131
*
3232
* @param {NonNegativeIntegerArray} shape - array shape
33-
* @param {NonNegativeInteger} dim - dimension to be unflattened
33+
* @param {integer} dim - dimension to be unflattened
3434
* @param {NonNegativeIntegerArray} sizes - new shape of the unflattened dimension
35+
* @throws {RangeError} second argument is out-of-bounds
36+
* @throws {RangeError} product of the sizes must be equal to the size of the dimension to be unflattened
3537
* @returns {NonNegativeIntegerArray} unflattened shape
3638
*
3739
* @example

lib/node_modules/@stdlib/ndarray/base/unflatten-shape/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/ndarray/base/unflatten-shape",
33
"version": "0.0.0",
4-
"description": "Unflatten a shape over multiple dimensions.",
4+
"description": "Expand a dimension over multiple dimensions.",
55
"license": "Apache-2.0",
66
"author": {
77
"name": "The Stdlib Authors",

0 commit comments

Comments
 (0)