license import { Tensor } from './tensor' ; import { TensorContainer , TensorContainerArray } from './tensor_types' ; import { upcastType } from './types' ; import { assert } from './util' ; export function makeTypesMatch < T extends Tensor > ( a , b ) { if ( a . dtype === b . dtype ) { return [ a , b ] ; } const dtype = upcastType ( a . dtype , b . dtype ) ; return [ a . cast ( dtype ) , b . cast ( dtype ) ] ; } export function assertTypesMatch ( a , b ) { assert ( a . dtype === b . dtype , ( ) => `The dtypes of the first(${ a . dtype }) and` + ` second(${ b . dtype }) input must match` ) ; } export function isTensorInList ( tensor , tensorList ) { return tensorList . some ( x => x . id === tensor . id ) ; } param container returns export function getTensorsInContainer ( result ) { const list = [ ] ; const seen = new Set < { } | void > ( ) ; walkTensorContainer ( result , list , seen ) ; return list ; } function walkTensorContainer ( container , list , seen ) { if ( container == null ) { return ; } if ( container instanceof Tensor ) { list . push ( container ) ; return ; } if ( ! isIterable ( container ) ) { return ; } const iterable = container as TensorContainerArray ; for ( const k in iterable ) { const val = iterable [ k ] ; if ( ! seen . has ( val ) ) { seen . add ( val ) ; walkTensorContainer ( val , list , seen ) ; } } } function isIterable ( obj ) { return Array . isArray ( obj ) || typeof obj === 'object' ; }