license import { DataType , NumericDataType , TypedArray } from '@tensorflow/tfjs-core' ; import { util } from '@tensorflow/tfjs-core' ; export function transposeImpl ( xVals , xShape , dtype , perm , newShape ) { const xRank = xShape . length ; const xSize = util . sizeFromShape ( xShape ) ; const xStrides = util . computeStrides ( xShape ) ; const newStrides = util . computeStrides ( newShape ) ; const result = util . getTypedArrayFromDType ( dtype as NumericDataType , util . sizeFromShape ( newShape ) ) ; for ( let i = 0 ; i < xSize ; ++ i ) { const loc = util . indexToLoc ( i , xRank , xStrides ) ; const newLoc = new Array ( loc . length ) ; for ( let i = 0 ; i < newLoc . length ; i ++ ) { newLoc [ i ] = loc [ perm [ i ] ] ; } const newIndex = util . locToIndex ( newLoc , xRank , newStrides ) ; result [ newIndex ] = xVals [ i ] ; } return result ; }