You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Broadcasts an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
430
444
*
445
+
* ## Notes
446
+
*
447
+
* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension.
// Verify that we've been provided a list of unique dimension indices...
503
522
dl = idx.length;
504
-
idx = normalizeIndices( idx, N-1 );
523
+
idx = normalizeIndices( idx, M-1 );
505
524
if ( idx === null ) <spanclass="branch-0 cbranch-no" title="branch not covered" >{</span>
506
525
<spanclass="cstat-no" title="statement not covered" > throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );</span>
507
526
<spanclass="cstat-no" title="statement not covered" > }</span>
508
-
idx.sort();
527
+
idx.sort(); // sort in ascending order
509
528
if ( idx.length !== dl ) <spanclass="branch-0 cbranch-no" title="branch not covered" >{</span>
510
529
<spanclass="cstat-no" title="statement not covered" > throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );</span>
511
530
<spanclass="cstat-no" title="statement not covered" > }</span>
512
-
di = idx.length - 1;
531
+
k = idx.length - 1;
513
532
514
533
// Determine the output array strides...
515
534
st = getStrides( arr, false );
516
535
for ( i = N-1; i >= 0; i-- ) {
517
-
j = M - N + i;
518
-
if ( di >= 0 && idx[ di ] === i ) {
519
-
if ( j >= 0 ) {
520
-
shape[ i ] = sh[ j ];
521
-
strides[ i ] = st[ j ];
522
-
}
523
-
di -= 1;
536
+
// Moving from right-to-left, resolve an index into the input array shape:
537
+
j = M - N + i; // M-1, M-2, M-3, ..., M-M, ..., M-N with N >= M
538
+
539
+
// For prepended singleton dimensions, the stride is zero...
540
+
if ( j < 0 ) {
524
541
continue;
525
542
}
526
-
if ( j < 0 ) {
527
-
// Prepended singleton dimension; stride is zero...
0 commit comments