license import { Tensor2D } from '../tensor' ; import { DataType } from '../types' ; import { buffer } from './buffer' ; import { expandDims } from './expand_dims' ; import { op } from './operation' ; import { reshape } from './reshape' ; import { tile } from './tile' ; param numRows param numColumns param batchShape param dtype returns doc function eye_ ( numRows , numColumns ? , batchShape ? , dtype = 'float32' ) { if ( numColumns == null ) { numColumns = numRows ; } const buff = buffer ( [ numRows , numColumns ] , dtype ) ; const n = numRows <= numColumns ? numRows : numColumns ; for ( let i = 0 ; i < n ; ++ i ) { buff . set ( 1 , i , i ) ; } const out = reshape ( buff . toTensor ( ) , [ numRows , numColumns ] ) ; if ( batchShape == null ) { return out ; } else { if ( batchShape . length === 1 ) { return tile ( expandDims ( out , 0 ) , [ batchShape [ 0 ] , 1 , 1 ] ) as Tensor2D ; } else if ( batchShape . length === 2 ) { return tile ( expandDims ( expandDims ( out , 0 ) , 0 ) , [ batchShape [ 0 ] , batchShape [ 1 ] , 1 , 1 ] ) as Tensor2D ; } else if ( batchShape . length === 3 ) { return tile ( expandDims ( expandDims ( expandDims ( out , 0 ) , 0 ) , 0 ) , [ batchShape [ 0 ] , batchShape [ 1 ] , batchShape [ 2 ] , 1 , 1 ] ) as Tensor2D ; } else { throw new Error ( `eye() currently supports only 1D and 2D ` + `batchShapes, but received ${ ( batchShape as any ) . length }D.` ) ; } } } export const eye = op ( { eye_ } ) ;