testComplex method

void testComplex()

Implementation

void testComplex(){
  startTest( "[testing complex]" );

  // complexの性質のテスト
  MathComplex fc0 = MathComplex();
  MathComplex fc1 = MathComplex.floatToComplex( 1 );
  MathComplex fc2 = MathComplex( 2, 2 );
  test( "fc0=${_toString( fc0 )}", fc0.real() == 0 && fc0.imag() == 0 );
  test( "fc1=${_toString( fc1 )}", fc1.real() == 1 && fc1.imag() == 0 );
  test( "fc2=${_toString( fc2 )}", fc2.real() == 2 && fc2.imag() == 2 );
  fc0.addAndAss( fc2 ); test( "addAndAss", fc0.real() ==  2 && fc0.imag() == 2 );
  fc0.subAndAss( fc1 ); test( "subAndAss", fc0.real() ==  1 && fc0.imag() == 2 );
  fc0.mulAndAss( fc2 ); test( "mulAndAss", fc0.real() == -2 && fc0.imag() == 6 );
  fc0.divAndAss( fc2 ); test( "divAndAss", fc0.real() ==  1 && fc0.imag() == 2 );

  // 算術のテスト
  fc0.ass( MathComplex( -4, -5 ) );                   test( "ass"  , fc0.real() == -4 && fc0.imag() == -5 );
  fc0.ass( MathComplex.floatToComplex( 2 ).add( fc2 ).add( 3 ) ); test( "add"  , fc0.real() ==  7 && fc0.imag() ==  2 );
  fc0.ass( MathComplex.floatToComplex( 2 ).sub( fc2 ).sub( 3 ) ); test( "sub"  , fc0.real() == -3 && fc0.imag() == -2 );
  fc0.ass( MathComplex.floatToComplex( 2 ).mul( fc2 ).mul( 3 ) ); test( "mul"  , fc0.real() == 12 && fc0.imag() == 12 );
  fc0.ass( MathComplex.floatToComplex( 8 ).div( fc2 ).div( 2 ) ); test( "div"  , fc0.real() ==  1 && fc0.imag() == -1 );
  fc0.ass( fc1.add( fc2.minus() ) );                  test( "minus", fc0.real() == -1 && fc0.imag() == -2 );
  test( "equal"   , fc2.equal   ( fc2 ) && fc1.equal   ( 1 ) && MathComplex.floatToComplex( 1 ).equal   ( fc1 ) );
  test( "notEqual", fc1.notEqual( fc2 ) && fc1.notEqual( 0 ) && MathComplex.floatToComplex( 3 ).notEqual( fc1 ) );

  // 数学関数のテスト
  double e      = 2.7182818284590452353602875;
  double ln2    = 0.6931471805599453094172321;
  double piby4  = 0.7853981633974483096156608;
  double rthalf = 0.7071067811865475244008444;
  double c1 = rthalf * (e + 1 / e) / 2;
  double s1 = rthalf * (e - 1 / e) / 2;
  test( "fabs", ClipMath.approx( MathComplex( 5, -12 ).fabs(), 13 ) );
  test( "farg", fc1.farg() == 0 && ClipMath.approx( fc2.farg(), piby4 ) );
  test( "conjg", fc2.conjg().equal( MathComplex( 2, -2 ) ) );
  fc0.ass( MathComplex( piby4, -1 ).cos() );  test( "cos" , ClipMath.approx( fc0.real(), c1 ) && ClipMath.approx( fc0.imag(), s1 ) );
  fc0.ass( MathComplex( -1, piby4 ).cosh() ); test( "cosh", ClipMath.approx( fc0.real(), c1 ) && ClipMath.approx( fc0.imag(), -s1 ) );
  fc0.ass( fc1.exp() );                       test( "exp" , ClipMath.approx( fc0.real(), e ) && fc0.imag() == 0 );
  fc0.ass( MathComplex( 1, -piby4 ).exp() );  test( "exp" , ClipMath.approx( fc0.real(), e * rthalf ) && ClipMath.approx( fc0.imag(), -e * rthalf ) );
  fc0.ass( MathComplex( 1, -1 ).log() );      test( "log" , ClipMath.approx( fc0.real(), ln2 / 2 ) && ClipMath.approx( fc0.imag(), -piby4 ) );
  test( "norm", MathComplex( 3, -4 ).fnorm() == 25 && fc2.fnorm() == 8 );
  fc0.polar( 1, -piby4 ); test( "polar", ClipMath.approx( fc0.real(), rthalf ) && ClipMath.approx( fc0.imag(), -rthalf ) );
  fc0.ass( fc2.pow( fc2 ) );
  fc0.ass( fc2.pow( 5 ) ); test( "pow", ClipMath.approx( fc0.real(), -128 ) && ClipMath.approx( fc0.imag(), -128 ) );
debugPrint( _toString( fc0 ) );
  fc0.ass( fc2.pow( 2 ) ); test( "pow", ClipMath.approx( fc0.real(), 0 ) && ClipMath.approx( fc0.imag(), 8 ) );
debugPrint( _toString( fc0 ) );
  fc0.ass( MathComplex.floatToComplex( 2 ).pow( fc2 ) );
  fc0.ass( MathComplex( piby4, -1 ).sin() );       test( "sin" , ClipMath.approx( fc0.real(), c1 ) && ClipMath.approx( fc0.imag(), -s1 ) );
  fc0.ass( MathComplex( -1, piby4 ).sinh() );      test( "sinh", ClipMath.approx( fc0.real(), -s1 ) && ClipMath.approx( fc0.imag(), c1 ) );
  fc0.ass( MathComplex( rthalf, -rthalf ).sqr() ); test( "sqr" , ClipMath.approx( fc0.real(), 0 ) && ClipMath.approx( fc0.imag(), -1 ) );
  fc0.ass( MathComplex( 0, -1 ).sqrt() );          test( "sqrt", ClipMath.approx( fc0.real(), rthalf ) && ClipMath.approx( fc0.imag(), -rthalf ) );

  endTest();
}