Skip to content

Commit bd34d7a

Browse files
committed
refactor: apply suggestions from code review
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - 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: na - task: lint_license_headers status: passed ---
1 parent 67d2674 commit bd34d7a

File tree

7 files changed

+486
-20
lines changed

7 files changed

+486
-20
lines changed

lib/node_modules/@stdlib/ndarray/concat/docs/types/index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ interface Concat {
4646
* var ybuf = new Float64Array( [ -5.0, 6.0, -7.0, 8.0, -9.0, 10.0 ] );
4747
* var y = new ndarray( 'float64', ybuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
4848
*
49-
* var out = concat( [ x, y ] );
49+
* var out = concat( [ x, y ], -1 );
5050
* // returns <ndarray>
5151
*
5252
* var arr = ndarray2array( out );
53-
* // returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
53+
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
5454
*/
5555
( arrays: ArrayLike<ndarray>, dim: number ): ndarray;
5656

@@ -75,13 +75,13 @@ interface Concat {
7575
*
7676
* var z = new ndarray( 'float64', new Float64Array( 10 ), [ 2, 5 ], [ 5, 1 ], 0, 'row-major' );
7777
*
78-
* var out = assign( [ x, y ], z, -1 );
78+
* var out = concat.assign( [ x, y ], z, -1 );
7979
*
8080
* var bool = ( out === z );
8181
* // returns true
8282
*
8383
* var arr = ndarray2array( z );
84-
* // returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
84+
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
8585
*/
8686
assign<T extends ndarray>( arrays: ArrayLike<ndarray>, out: T, dim: number ): T;
8787
}
@@ -104,11 +104,11 @@ interface Concat {
104104
* var ybuf = new Float64Array( [ -5.0, 6.0, -7.0, 8.0, -9.0, 10.0 ] );
105105
* var y = new ndarray( 'float64', ybuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
106106
*
107-
* var out = concat( [ x, y ] );
107+
* var out = concat( [ x, y ], -1 );
108108
* // returns <ndarray>
109109
*
110110
* var arr = ndarray2array( out );
111-
* // returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
111+
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
112112
*
113113
* @example
114114
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
@@ -129,7 +129,7 @@ interface Concat {
129129
* // returns true
130130
*
131131
* var arr = ndarray2array( z );
132-
* // returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
132+
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
133133
*/
134134
declare var concat: Concat;
135135

lib/node_modules/@stdlib/ndarray/concat/lib/assign.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function assign( arrays, out, dim ) {
8787
N = arrays.length;
8888
arrs = [];
8989
if ( N < 1 ) {
90-
throw new RangeError( format( 'invalid argument. First argument must have more than one ndarray. Value: `%s`.', N ) );
90+
throw new RangeError( format( 'invalid argument. First argument must have one or more ndarrays. Value: `%s`.', N ) );
9191
}
9292
if ( !isndarrayLike( out ) ) {
9393
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', out ) );
@@ -123,6 +123,7 @@ function assign( arrays, out, dim ) {
123123
continue;
124124
}
125125
arrs[ i ] = broadcastArrayExceptDimensions( arrs[ i ], shapes[ mi ], [ dim ] ); // eslint-disable-line max-len
126+
shapes[ i ] = getShape( arrs[ i ] );
126127
}
127128
// Validate the output ndarray:
128129
err = output( shapes, dtypes, orders, d, out );

lib/node_modules/@stdlib/ndarray/concat/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* // returns <ndarray>
4040
*
4141
* var arr = ndarray2array( out );
42-
* // returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
42+
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
4343
*/
4444

4545
// MODULES //

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2626
var indicesComplement = require( '@stdlib/array/base/indices-complement' );
2727
var nditerStacks = require( '@stdlib/ndarray/iter/stacks' );
2828
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
29+
var isInteger = require( '@stdlib/assert/is-integer' );
2930
var getShape = require( '@stdlib/ndarray/shape' );
3031
var getStrides = require( '@stdlib/ndarray/strides' );
3132
var getDtype = require( '@stdlib/ndarray/dtype' );
@@ -71,7 +72,6 @@ function concat( arrays, dim ) {
7172
var shapes;
7273
var dtypes;
7374
var orders;
74-
var nargs;
7575
var arrs;
7676
var out;
7777
var mi;
@@ -80,9 +80,13 @@ function concat( arrays, dim ) {
8080
var s;
8181
var i;
8282

83-
nargs = arguments.length;
84-
if ( nargs === 2 && isNonNegativeInteger( dim ) ) {
85-
throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
83+
if ( arguments.length > 1 ) {
84+
if ( !isInteger( dim ) ) {
85+
throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
86+
}
87+
if ( isNonNegativeInteger( dim ) ) {
88+
throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
89+
}
8690
}
8791
N = arrays.length;
8892
arrs = [];
@@ -111,16 +115,17 @@ function concat( arrays, dim ) {
111115
mi = i;
112116
}
113117
}
114-
// Normalize dimension index:
115-
d = normalizeIndex( dim, shapes[ mi ].length - 1 );
116-
117118
// Broadcast all ndarrays to shape of max-rank ndarray:
118119
for ( i = 0; i < N; i++ ) {
119120
if ( i === mi ) {
120121
continue;
121122
}
122123
arrs[ i ] = broadcastArrayExceptDimensions( arrs[ i ], shapes[ mi ], [ dim ] ); // eslint-disable-line max-len
124+
shapes[ i ] = getShape( arrs[ i ] );
123125
}
126+
// Normalize dimension index:
127+
d = normalizeIndex( dim, shapes[ mi ].length - 1 );
128+
124129
// Create output ndarray:
125130
out = output( shapes, dtypes, orders, d );
126131

lib/node_modules/@stdlib/ndarray/concat/lib/output.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
24+
var mostlySafeCasts = require( '@stdlib/ndarray/mostly-safe-casts' );
2425
var defaults = require( '@stdlib/ndarray/defaults' );
2526
var empty = require( '@stdlib/ndarray/empty' );
2627
var slice = require( '@stdlib/array/slice' );
@@ -71,24 +72,31 @@ function resolveOutputOrder( orders ) {
7172
* @example
7273
*/
7374
function output( shapes, dtypes, orders, dim, out ) {
75+
var allowedCasts;
7476
var ord;
7577
var osh;
78+
var odt;
7679
var sh;
7780
var dt;
7881
var s;
7982
var i;
8083

81-
dt = promoteDataTypes( dtypes );
84+
// Resolve the output shape:
8285
sh = slice( shapes[ 0 ] );
8386
for ( i = 1; i < shapes.length; i++ ) {
8487
s = shapes[ i ];
8588
sh[ dim ] += s[ dim ];
8689
}
87-
ord = resolveOutputOrder( orders );
8890
if ( out ) {
89-
if ( getDtype( out ) !== dt ) {
90-
throw new TypeError( format( 'invalid argument. Output ndarray must have a dtype of `%s`. Value: `%s`.', dt, getDtype( out ) ) );
91+
// When output is provided: validate that inputs can cast to output dtype
92+
odt = getDtype( out );
93+
for ( i = 0; i < dtypes.length; i++ ) {
94+
allowedCasts = mostlySafeCasts( dtypes[ i ] );
95+
if ( allowedCasts.indexOf( odt ) === -1 ) {
96+
throw new TypeError( format( 'invalid argument. Input ndarray at index %d has dtype `%s` which cannot be safely cast to output dtype `%s`.', i, dtypes[ i ], odt ) );
97+
}
9198
}
99+
// Validate output shape:
92100
osh = getShape( out );
93101
if ( osh.length !== sh.length ) {
94102
throw new RangeError( format( 'invalid argument. Output ndarray must have %d dimensions. Value: %d.', sh.length, getShape( out ).length ) );
@@ -100,6 +108,8 @@ function output( shapes, dtypes, orders, dim, out ) {
100108
}
101109
return null;
102110
}
111+
dt = promoteDataTypes( dtypes );
112+
ord = resolveOutputOrder( orders );
103113
out = empty( sh, {
104114
'dtype': dt,
105115
'order': ord

0 commit comments

Comments
 (0)