Skip to content

Commit a4ab7d6

Browse files
committed
fix: avoid input argument mutation
--- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent df378b7 commit a4ab7d6

File tree

2 files changed

+17
-19
lines changed
  • lib/node_modules/@stdlib/ndarray/base/broadcast-array-except-dimensions/lib

2 files changed

+17
-19
lines changed

lib/node_modules/@stdlib/ndarray/base/broadcast-array-except-dimensions/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Broadcast an input ndarray to a target shape keeping the specified dimensions unchanged.
22+
* Broadcast an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
2323
*
2424
* @module @stdlib/ndarray/base/broadcast-array-except-dimensions
2525
*
@@ -33,7 +33,7 @@
3333
* var shx = x.shape;
3434
* // returns [ 1, 3 ]
3535
*
36-
* var y = broadcastArrayExceptDimensionsExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
36+
* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
3737
* // returns <ndarray>
3838
*
3939
* var shy = y.shape;

lib/node_modules/@stdlib/ndarray/base/broadcast-array-except-dimensions/lib/main.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
// MODULES //
2222

23-
var copy = require( '@stdlib/array/base/copy-indexed' );
23+
var copy = require( '@stdlib/array/base/copy' );
2424
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2525
var getShape = require( '@stdlib/ndarray/base/shape' );
2626
var getStrides = require( '@stdlib/ndarray/base/strides' );
@@ -29,18 +29,19 @@ var getOrder = require( '@stdlib/ndarray/base/order' );
2929
var getDType = require( '@stdlib/ndarray/base/dtype' );
3030
var getData = require( '@stdlib/ndarray/base/data-buffer' );
3131
var normalizeIndices = require( '@stdlib/ndarray/base/normalize-indices' );
32-
var format = require( '@stdlib/string/format' );
3332
var join = require( '@stdlib/array/base/join' );
33+
var zeros = require( '@stdlib/array/base/zeros' );
34+
var format = require( '@stdlib/string/format' );
3435

3536

3637
// MAIN //
3738

3839
/**
39-
* Broadcasts an input ndarray to a target shape keeping the specified dimensions unchanged.
40+
* Broadcasts an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
4041
*
4142
* @param {ndarray} arr - input array
4243
* @param {NonNegativeIntegerArray} shape - desired shape
43-
* @param {IntegerArray} [dims] - list of dimensions to exclude
44+
* @param {IntegerArray} dims - list of dimensions to exclude from broadcasting
4445
* @throws {Error} input array cannot have more dimensions than the desired shape
4546
* @throws {Error} input array and desired shape must be broadcast compatible
4647
* @throws {RangeError} dimension indices must not exceed desired shape bounds
@@ -80,7 +81,7 @@ var join = require( '@stdlib/array/base/join' );
8081
* v = y.get( 1, 0, 2 );
8182
* // returns 3
8283
*/
83-
function broadcastArrayExceptDimensions( arr, shape, dims ) {
84+
function broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-line id-length
8485
var strides;
8586
var dim;
8687
var sh;
@@ -93,26 +94,20 @@ function broadcastArrayExceptDimensions( arr, shape, dims ) {
9394
var i;
9495
var j;
9596

97+
shape = copy( shape ); // perform a copy to avoid unintended mutation
9698
N = shape.length;
99+
97100
sh = getShape( arr, false );
98101
M = sh.length;
99102
if ( N < M ) {
100103
throw new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' );
101104
}
102-
// Initialize a strides array...
103-
strides = [];
104-
for ( i = 0; i < N; i++ ) {
105-
strides.push( 0 );
106-
}
107-
// Determine the output array strides...
108-
st = getStrides( arr, false );
105+
// Initialize a strides array:
106+
strides = zeros( N );
109107

110-
if ( !dims ) {
111-
dims = [ -1 ];
112-
}
113108
// Verify that we've been provided a list of unique dimension indices...
114109
dl = dims.length;
115-
dims = normalizeIndices( dims, N - 1 );
110+
dims = normalizeIndices( dims, N-1 );
116111
if ( dims === null ) {
117112
throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );
118113
}
@@ -121,6 +116,9 @@ function broadcastArrayExceptDimensions( arr, shape, dims ) {
121116
throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );
122117
}
123118
di = dims.length - 1;
119+
120+
// Determine the output array strides...
121+
st = getStrides( arr, false );
124122
for ( i = N-1; i >= 0; i-- ) {
125123
j = M - N + i;
126124
if ( di >= 0 && dims[ di ] === i ) {
@@ -150,7 +148,7 @@ function broadcastArrayExceptDimensions( arr, shape, dims ) {
150148
throw new Error( format( 'invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );
151149
}
152150
}
153-
return ndarray( getDType( arr ), getData( arr ), copy( shape ), strides, getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len
151+
return ndarray( getDType( arr ), getData( arr ), shape, strides, getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len
154152
}
155153

156154

0 commit comments

Comments
 (0)