testMatrix method

void testMatrix()

Implementation

void testMatrix(){
  startTest( "[testing matrix]" );

  /*
   * 算術のテスト
   */

  MathMatrix matI = MathMatrix.arrayToMatrix( [[ 1, 0, 0 ],[ 0, 1, 0 ],[ 0, 0, 1 ]] );

  debugPrint( "test 1" );
  MathMatrix matA, matB, matC;
  matA = MathMatrix.arrayToMatrix( [[ -4,  6, 3 ],[ 0, 1, 2 ]] );
  matB = MathMatrix.arrayToMatrix( [[  5, -1, 0 ],[ 3, 1, 0 ]] );
  matC = MathMatrix.arrayToMatrix( [[  1,  5, 3 ],[ 3, 2, 2 ]] );
  test( "", matC.equal( matA.add( matB ) ) );

  debugPrint( "test 2" );
  matA = MathMatrix.arrayToMatrix( [[ 2.7, -1.8 ],[ 0.9, 3.6 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 5.4, -3.6 ],[ 1.8, 7.2 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 3  , -2   ],[ 1  , 4   ]] );
  test( "", matA.add( matA ).equal( MathMatrix.floatToMatrix( 2 ).mul( matA ) ) );
  test( "", matA.add( matA ).equal( matB ) );
  test( "", MathMatrix.floatToMatrix( 2 ).mul( matA ).equal( matB ) );
  test( "", ClipMath.approxM( MathMatrix.floatToMatrix( 10 / 9 ).mul( matA ), matC ) );

  debugPrint( "test 3" );
  matA = MathMatrix.arrayToMatrix( [[ 5, -8, 1 ],[ 4, 0, 0 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 5, 4 ],[ -8, 0 ],[ 1, 0 ]] );
  test( "", matA.trans().equal( matB ) );

  debugPrint( "test 4" );
  matA = MathMatrix.arrayToMatrix( [[ 7, 5, -2 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 7 ],[ 5 ],[ -2 ]] );
  test( "", matA.trans().equal( matB ) );

  debugPrint( "test 5" );
  MathMatrix matR, matS;
  matA = MathMatrix.arrayToMatrix( [[ 2,  3 ],[ 5, -1 ]] );
  matR = MathMatrix.arrayToMatrix( [[ 2,  4 ],[ 4, -1 ]] );
  matS = MathMatrix.arrayToMatrix( [[ 0, -1 ],[ 1,  0 ]] );
  test( "", matA.add( matA.trans() ).div( 2 ).equal( matR ) );
  test( "", matA.sub( matA.trans() ).div( 2 ).equal( matS ) );

  debugPrint( "test 6" );
  matA = MathMatrix.arrayToMatrix( [[  2, -3 ],[  0,  4 ]] );
  matB = MathMatrix.arrayToMatrix( [[ -5,  2 ],[  2,  1 ]] );
  matC = MathMatrix.arrayToMatrix( [[  7, -5 ],[ -2,  3 ]] ); test( "", matA.sub( matB ).equal( matC ) );
  matC = MathMatrix.arrayToMatrix( [[ -7,  5 ],[  2, -3 ]] ); test( "", matB.sub( matA ).equal( matC ) );
  matC = MathMatrix.arrayToMatrix( [[  2,  0 ],[ -3,  4 ]] ); test( "", matA.trans().equal( matC ) );
  test( "", matB.trans().equal( matB ) );
  test( "", matB.trans().trans().equal( matB ) );
  matC = MathMatrix.arrayToMatrix( [[  4, -3 ],[ -3, 8 ]] ); test( "", matA.add( matA.trans() ).equal( matC ) );
  matC = MathMatrix.arrayToMatrix( [[  0, -3 ],[  3, 0 ]] ); test( "", matA.sub( matA.trans() ).equal( matC ) );
  matC = MathMatrix.arrayToMatrix( [[ -3,  2 ],[ -1, 5 ]] ); test( "", matA.add( matB ).trans().equal( matC ) );
  test( "", matA.trans().add( matB.trans() ).equal( matC ) );

  debugPrint( "test 7" );
  MathMatrix matK, matM, matN, matL;
  matK = MathMatrix.arrayToMatrix( [[ 1, 3, 5 ],[ 0, 4, 2 ],[ 0, 0, 6 ]] );
  matM = MathMatrix.arrayToMatrix( [[ 2, 0, 0 ],[ -1, 1, 0 ],[ 4, -3, 0 ]] );
  matN = MathMatrix.arrayToMatrix( [[ 6, 7 ],[ 0, -2 ],[ 3, 8 ]] );
  matL = MathMatrix.arrayToMatrix( [[ 18, 0, 9 ],[ 21, -6, 24 ]] );
  test( "", MathMatrix.floatToMatrix( 3 ).mul( matN ).trans().equal( matL ) );
  test( "", MathMatrix.floatToMatrix( 3 ).mul( matN.trans() ).equal( matL ) );
  matL = MathMatrix.arrayToMatrix( [[ -1, 3, 5 ],[ 1, 3, 2 ],[ -4, 3, 6 ]] );
  test( "", matK.sub( matM ).equal( matL ) );
  test( "", matM.sub( matK ).equal( matL.minus() ) );
  matL = MathMatrix.arrayToMatrix( [[ 3, 3, 5 ],[ -1, 5, 2 ],[ 4, -3, 6 ]] );
  test( "", matK.add( matM ).equal( matL ) );
  test( "", matM.add( matK ).equal( matL ) );

  debugPrint( "test 8" );
  matA = MathMatrix.arrayToMatrix( [[ 2,  1 ],[  3, 4 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 1, -2 ],[  5, 3 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 7, -1 ],[ 23, 6 ]] );
  test( "", matA.mul( matB ).equal( matC ) );

  debugPrint( "test 9" );
  matA = MathMatrix.arrayToMatrix( [[ 3, 2, -1 ],[  0,  4,  6 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 1, 0,  2 ],[  5,  3,  1 ],[ 6, 4, 2 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 7, 2,  6 ],[ 56, 36, 16 ]] );
  test( "", matA.mul( matB ).equal( matC ) );

  debugPrint( "test 10" );
  matA = MathMatrix.arrayToMatrix( [[ 3, 4, 2 ],[ 6, 0, -1 ],[ -5, -2, 1 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 1 ],[ 3 ],[ 2 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 19 ],[ 4 ],[ -9 ]] );
  test( "", matA.mul( matB ).equal( matC ) );

  debugPrint( "test 11" );
  matA = MathMatrix.arrayToMatrix( [[ 3, 6, 1 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 1 ],[ 2 ],[ 4 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 3, 6, 1 ],[ 6, 12, 2 ],[ 12, 24, 4 ]] );
  test( "", MathMatrix.floatToMatrix( 3 ).notEqual( matA ) );
  test( "", matA.notEqual( 3 ) );
  test( "", matA.mul( matB ).equal( 19 ) );
  test( "", matB.mul( matA ).equal( matC ) );

  debugPrint( "test 12" );
  matA = MathMatrix.arrayToMatrix( [[ 1, 0 ],[ 0, 0 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 0, 1 ],[ 1, 0 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 0, 1 ],[ 0, 0 ]] ); test( "", matA.mul( matB ).equal( matC ) );
  matC = MathMatrix.arrayToMatrix( [[ 0, 0 ],[ 1, 0 ]] ); test( "", matB.mul( matA ).equal( matC ) );

  debugPrint( "test 13" );
  matA = MathMatrix.arrayToMatrix( [[  1, 1 ],[ 2,  2 ]] );
  matB = MathMatrix.arrayToMatrix( [[ -1, 1 ],[ 1, -1 ]] );
  matC = MathMatrix.arrayToMatrix( [[  0, 0 ],[ 0,  0 ]] ); test( "", matA.mul( matB ).equal( matC ) );

  debugPrint( "test 14" );
  MathMatrix matD;
  matA = MathMatrix.arrayToMatrix( [[ 4, 6, -1 ],[ 3, 0, 2 ],[ 1, -2, 5 ]] );
  matB = MathMatrix.arrayToMatrix( [[ 2, 4 ],[ 0, 1 ],[ -1, 2 ]] );
  matC = MathMatrix.arrayToMatrix( [[ 3 ],[ 1 ],[ 2 ]] );
  matD = MathMatrix.arrayToMatrix( [[ 33, 26, 3 ],[ 14, 14, 7 ],[ 3, -4, 20 ]] );
  test( "", matA.mul( matA ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 4 ],[ 17 ]] );
  test( "", matB.trans().mul( matC ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 4, 17 ]] );
  test( "", matC.trans().mul( matB ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 20, 4, 6 ],[ 4, 1, 2 ],[ 6, 2, 5 ]] );
  test( "", matB.mul( matB.trans() ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 5, 6 ],[ 6, 21 ]] );
  test( "", matB.trans().mul( matB ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 43, 44, 0 ],[ 23, 12, 13 ],[ 6, -10, 33 ]] );
  test( "", matA.mul( matA ).add( MathMatrix.floatToMatrix( 3 ).mul( matA ) ).sub( MathMatrix.floatToMatrix( 2 ).mul( matI ) ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 25, 100 ]] );
  test( "", matC.trans().mul( matA ).mul( matB ).equal( matD ) );
  matD = MathMatrix.arrayToMatrix( [[ 25 ],[ 100 ]] );
  test( "", matA.mul( matB ).trans().mul( matC ).equal( matD ) );

  debugPrint( "test 15" );
  matA = MathMatrix.arrayToMatrix( [[  2, -1,  0 ],[ 0, -2,  1 ],[ 1,  0,  1 ]] );
  matB = MathMatrix.arrayToMatrix( [[ -2,  1, -1 ],[ 1,  2, -2 ],[ 2, -1, -4 ]] );
  matC = MathMatrix.arrayToMatrix( [[ -5,  0,  0 ],[ 0, -5,  0 ],[ 0,  0, -5 ]] );
  test( "", matA.mul( matB ).equal( matB.mul( matA ) ) );
  test( "", matA.mul( matB ).equal( matC ) );
  test( "", matB.mul( matA ).equal( matC ) );

  endTest();
}