static calc(a, b, op, inplace, resultType) {
        const outputShape = BroadcastUtil.calcShape(a.dims, b.dims);
        if (outputShape) {
            if (inplace && !ShapeUtil.areEqual(outputShape, a.dims)) {
                // B is not broadcastable to A, failed to calculate inplace.
                return undefined;
            }
            const size = ShapeUtil.size(outputShape);
            const c = inplace ? a : new tensor_1.Tensor(outputShape, resultType || a.type);
            // both inputs are scalars
            if (outputShape.length === 0) {
                c.set([], op(a.get([]), b.get([])));
            }
            // atleast one input is a non-scalar
            else {
                const outputIndices = new Array(outputShape.length);
                const originalIndicesA = new Array(a.dims.length);
                const originalIndicesB = new Array(b.dims.length);
                let valA = 0;
                let valB = 0;
                let isAScalar = false;
                let isBScalar = false;
                if (a.dims.length === 0) {
                    valA = a.get([]);
                    isAScalar = true;
                }
                if (b.dims.length === 0) {
                    valB = b.get([]);
                    isBScalar = true;
                }
                let rest;
                for (let i = 0; i < size; i++) {
                    // traversal indices
                    rest = i;
                    for (let j = outputShape.length - 1; j >= 0; j--) {
                        outputIndices[j] = rest % outputShape[j];
                        rest = Math.floor(rest / outputShape[j]);
                    }
                    if (!isAScalar) {
                        // map outputIndices (which is actually broadcasted) to the originalIndices
                        BroadcastUtil.fillIndex(outputIndices, a.dims, originalIndicesA);
                        valA = a.get(originalIndicesA);
                    }
                    if (!isBScalar) {
                        BroadcastUtil.fillIndex(outputIndices, b.dims, originalIndicesB);
                        valB = b.get(originalIndicesB);
                    }
                    c.set(outputIndices, op(valA, valB));
                }
            }
            return c;
        }
        return undefined;
    }
/**
     * Perform the broadcasting operation on the specific operator
     * @param a The input tensor A
     * @param b The input tensor B
     * @param op The operator lambda function
     * @param inplace Whether to write the result back to A.
     * @returns The result tensor, or undefined if input not broadcastable.
     */
Perform the broadcasting operation on the specific operator
@param a The input tensor A
@param b The input tensor B
@param op The operator lambda function
@param inplace Whether to write the result back to A.
@returns The result tensor, or undefined if input not broadcastable.
Perform the broadcasting operation on the specific operator @param a The input tensor A @param b The input tensor B @param op The operator lambda function @param inplace Whether to write the result back to A . @returns The result tensor or undefined if input not broadcastable .
static calc(a, b, op, inplace, resultType) {
        const outputShape = BroadcastUtil.calcShape(a.dims, b.dims);
        if (outputShape) {
            if (inplace && !ShapeUtil.areEqual(outputShape, a.dims)) {
                return undefined;
            }
            const size = ShapeUtil.size(outputShape);
            const c = inplace ? a : new tensor_1.Tensor(outputShape, resultType || a.type);
            if (outputShape.length === 0) {
                c.set([], op(a.get([]), b.get([])));
            }
            else {
                const outputIndices = new Array(outputShape.length);
                const originalIndicesA = new Array(a.dims.length);
                const originalIndicesB = new Array(b.dims.length);
                let valA = 0;
                let valB = 0;
                let isAScalar = false;
                let isBScalar = false;
                if (a.dims.length === 0) {
                    valA = a.get([]);
                    isAScalar = true;
                }
                if (b.dims.length === 0) {
                    valB = b.get([]);
                    isBScalar = true;
                }
                let rest;
                for (let i = 0; i < size; i++) {
                    rest = i;
                    for (let j = outputShape.length - 1; j >= 0; j--) {
                        outputIndices[j] = rest % outputShape[j];
                        rest = Math.floor(rest / outputShape[j]);
                    }
                    if (!isAScalar) {
                        BroadcastUtil.fillIndex(outputIndices, a.dims, originalIndicesA);
                        valA = a.get(originalIndicesA);
                    }
                    if (!isBScalar) {
                        BroadcastUtil.fillIndex(outputIndices, b.dims, originalIndicesB);
                        valB = b.get(originalIndicesB);
                    }
                    c.set(outputIndices, op(valA, valB));
                }
            }
            return c;
        }
        return undefined;
    }
static calc ( a , b , op , inplace , resultType ) { const outputShape = BroadcastUtil . calcShape ( a . dims , b . dims ) ; if ( outputShape ) { if ( inplace && ! ShapeUtil . areEqual ( outputShape , a . dims ) ) { return undefined ; } const size = ShapeUtil . size ( outputShape ) ; const c = inplace ? a : new tensor_1 . Tensor ( outputShape , resultType || a . type ) ; if ( outputShape . length === 0 ) { c . set ( [ ] , op ( a . get ( [ ] ) , b . get ( [ ] ) ) ) ; } else { const outputIndices = new Array ( outputShape . length ) ; const originalIndicesA = new Array ( a . dims . length ) ; const originalIndicesB = new Array ( b . dims . length ) ; let valA = 0 ; let valB = 0 ; let isAScalar = false ; let isBScalar = false ; if ( a . dims . length === 0 ) { valA = a . get ( [ ] ) ; isAScalar = true ; } if ( b . dims . length === 0 ) { valB = b . get ( [ ] ) ; isBScalar = true ; } let rest ; for ( let i = 0 ; i < size ; i ++ ) { rest = i ; for ( let j = outputShape . length - 1 ; j >= 0 ; j -- ) { outputIndices [ j ] = rest % outputShape [ j ] ; rest = Math . floor ( rest / outputShape [ j ] ) ; } if ( ! isAScalar ) { BroadcastUtil . fillIndex ( outputIndices , a . dims , originalIndicesA ) ; valA = a . get ( originalIndicesA ) ; } if ( ! isBScalar ) { BroadcastUtil . fillIndex ( outputIndices , b . dims , originalIndicesB ) ; valB = b . get ( originalIndicesB ) ; } c . set ( outputIndices , op ( valA , valB ) ) ; } } return c ; } return undefined ; }
Perform the broadcasting operation on the specific operator
 @param a The input tensor A
 @param b The input tensor B
 @param op The operator lambda function
 @param inplace Whether to write the result back to A.
Perform the broadcasting operation on the specific operator @param a The input tensor A @param b The input tensor B @param op The operator lambda function @param inplace Whether to write the result back to A .
// B is not broadcastable to A, failed to calculate inplace. // both inputs are scalars // atleast one input is a non-scalar // traversal indices // map outputIndices (which is actually broadcasted) to the originalIndices