license import * as tf from '@tensorflow/tfjs-core' ; import { expectArraysClose } from '@tensorflow/tfjs-core/dist/test_util' ; import { arrayToMatrix4x4 , calculateInverseMatrix , Matrix4x4 , matrix4x4ToArray } from './calculate_inverse_matrix' ; describe ( 'calculateInverseMatrix' , ( ) => { const identity = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 1 ] ] ; it ( 'identity matrix.' , async ( ) => { const inverse = calculateInverseMatrix ( identity ) ; expectArraysClose ( matrix4x4ToArray ( inverse ) , matrix4x4ToArray ( identity ) ) ; } ) ; it ( 'precision.' , async ( ) => { const matrix = [ [ 0.00001 , 0.0 , 0.0 , 0.0 ] , [ 0.0 , 0.00001 , 0.0 , 0.0 ] , [ 0.0 , 0.0 , 1.0 , 0.0 ] , [ 0.0 , 0.0 , 0.0 , 1.0 ] ] ; const inverse = calculateInverseMatrix ( matrix ) ; const expectedInverse = [ [ 100000.0 , 0.0 , 0.0 , 0.0 ] , [ 0.0 , 100000.0 , 0.0 , 0.0 ] , [ 0.0 , 0.0 , 1.0 , 0.0 ] , [ 0.0 , 0.0 , 0.0 , 1.0 ] ] ; expectArraysClose ( matrix4x4ToArray ( inverse ) , matrix4x4ToArray ( expectedInverse ) ) ; } ) ; it ( 'random matrix.' , async ( ) => { for ( let seed = 1 ; seed <= 5 ; ++ seed ) { const matrix = tf . randomUniform ( [ 4 , 4 ] , 0 , 10 , 'float32' , seed ) ; const inverse = calculateInverseMatrix ( arrayToMatrix4x4 ( matrix . dataSync ( ) ) ) ; const product = tf . matMul ( matrix , inverse ) ; expectArraysClose ( product . dataSync ( ) , matrix4x4ToArray ( identity ) ) ; } } ) ; } ) ;