license import { Tensor , } from '../tensor' ; import { convertToTensor } from '../tensor_util_env' ; import { TensorLike } from '../types' ; import * as util from '../util' ; import { matMul } from './mat_mul' ; import { op } from './operation' ; import { reshape } from './reshape' ; param t1 param t2 doc function dot_ ( t1 , t2 ) { const $t1 = convertToTensor ( t1 , 't1' , 'dot' ) ; const $t2 = convertToTensor ( t2 , 't2' , 'dot' ) ; util . assert ( ( $t1 . rank === 1 || $t1 . rank === 2 ) && ( $t2 . rank === 1 || $t2 . rank === 2 ) , ( ) => `Error in dot: inputs must all be rank 1 or 2, but got ranks ` + `${ $t1 . rank } and ${ $t2 . rank }.` ) ; const t1Inner = ( $t1 . rank === 1 ? $t1 . size : $t1 . shape [ 1 ] ) ; const t2Inner = ( $t2 . rank === 1 ? $t2 . size : $t2 . shape [ 0 ] ) ; util . assert ( t1Inner === t2Inner , ( ) => `Error in dot: inner dimensions of inputs must match, but got ` + `${ t1Inner } and ${ t2Inner }.` ) ; if ( $t1 . rank === 1 && $t2 . rank === 1 ) { const t12D = reshape ( $t1 , [ 1 , - 1 ] ) ; const t22D = reshape ( $t2 , [ - 1 , 1 ] ) ; const t1t2 = matMul ( t12D , t22D ) ; return reshape ( t1t2 , [ ] ) ; } else if ( $t1 . rank === 1 && $t2 . rank === 2 ) { const t12D = reshape ( $t1 , [ 1 , - 1 ] ) ; const t22D = reshape ( $t2 , [ $t2 . shape [ 0 ] , $t2 . shape [ 1 ] ] ) ; const t1t2 = matMul ( t12D , t22D ) ; return reshape ( t1t2 , [ t1t2 . size ] ) ; } else if ( $t1 . rank === 2 && $t2 . rank === 1 ) { const t22D = reshape ( $t2 , [ - 1 , 1 ] ) ; const t1t2 = matMul ( $t1 , t22D ) ; return reshape ( t1t2 , [ t1t2 . size ] ) ; } else { const t22D = reshape ( $t2 , [ $t2 . shape [ 0 ] , $t2 . shape [ 1 ] ] ) ; const t1t2 = matMul ( $t1 , t22D ) ; return t1t2 ; } } export const dot = op ( { dot_ } ) ;