testcase_matrix.cpp

来自「c++ 实现的矩阵运算库」· C++ 代码 · 共 1,295 行 · 第 1/3 页

CPP
1,295
字号
{
  cout << "\nTestCase_Matrix::Test_Zero" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  double re,im;
  bool result;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Zero();          // Set A back to zeros.
  CPPUNIT_ASSERT(result);  
  result = A.PrintStdout();   // Print Matrix A. A = [0 0 0; 0 0 0; 0 0 0].
  CPPUNIT_ASSERT(result);  
  result = A.GetStats_MaxVal(re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 0.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );
}


void TestCase_Matrix::Test_ZeroColumn()
{
  cout << "\nTestCase_Matrix::Test_ZeroColumn" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  double re,im;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.ZeroColumn(1);   // Set the second column of A back to zeros.
  CPPUNIT_ASSERT(result);  
  result = A.PrintStdout();   // Print Matrix A. A = [1 0 3; 4 0 6; 7 0 9].
  CPPUNIT_ASSERT(result);  
  result = A.GetStats_ColumnSum(1,re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 0.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );  
}

void TestCase_Matrix::Test_ZeroRow()
{
  cout << "\nTestCase_Matrix::Test_ZeroRow" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  double re,im;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.ZeroRow(1);      // Set the second row of A back to zeros.
  CPPUNIT_ASSERT(result);  
  result = A.PrintStdout();   // Print Matrix A. A = [1 2 3; 0 0 0; 7 8 9].
  CPPUNIT_ASSERT(result);  
  result = A.GetStats_RowSum(1,re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 0.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );      
}


void TestCase_Matrix::Test_Fill()
{
  cout << "\nTestCase_Matrix::Test_Fill" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  double re,im;  
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Fill(7);         // Fill the matrix with 7.0.
  CPPUNIT_ASSERT(result);  
  result = A.PrintStdout();   // Print Matrix A. A = [7 7 7; 7 7 7; 7 7 7].
  CPPUNIT_ASSERT(result);  
  result = A.GetStats_MaxVal(re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 7.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );      
}

void TestCase_Matrix::Test_FillColumn()
{
  cout << "\nTestCase_Matrix::Test_FillColumn" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  double re,im;  
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.FillColumn(1,7); // Fill the second column with 7.0.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();   // Print Matrix A. A = [1 7 3; 4 7 6; 7 7 9].
  CPPUNIT_ASSERT(result);  
  result = A.GetStats_MaxColVal(1,re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 7.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );      
}

void TestCase_Matrix::Test_FillRow()
{
  cout << "\nTestCase_Matrix::Test_FillRow" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  double re,im;  
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.FillRow(1,7);    // Fill the second row with 7.0.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();   // Print Matrix A. A = [1 2 3; 7 7 7; 7 8 9].
  CPPUNIT_ASSERT(result);      
  result = A.GetStats_MaxRowVal(1,re,im);
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 7.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( im, 0.0, 1e-06 );      
}

void TestCase_Matrix::Test_FlipColumn()
{
  cout << "\nTestCase_Matrix::Test_FlipColumn" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.FlipColumn(1);   // Flip the second column
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();   // Print Matrix A. A = [1 8 3; 4 5 6; 7 2 9].
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][1], 8.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][1], 2.0, 1e-06 );      
}

void TestCase_Matrix::Test_FlipRow()
{
  cout << "\nTestCase_Matrix::Test_FlipRow" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.FlipRow(1);   // Flip the second column
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();   // Print Matrix A. A = [1 2 3; 6 5 4; 7 8 9].
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][0], 6.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][2], 4.0, 1e-06 );      
}

void TestCase_Matrix::Test_Identity()
{
  cout << "\nTestCase_Matrix::Test_Identity" << endl;
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();   // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Identity();      // Set A to identity.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();   // Print Matrix A. A = [1 0 0; 0 1 0; 0 0 1].
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 1.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][2], 0.0, 1e-06 );

  cout << endl;
  Matrix B;
  result = B.Identity(3);     // Set A to identity, 3x3.
  cout << endl;
  result = B.PrintStdout();   // Print Matrix A. A = [1 0 0; 0 1 0; 0 0 1].
  CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2][2], 1.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1][2], 0.0, 1e-06 );    
}  

void TestCase_Matrix::Test_Inplace_Transpose()
{
  cout << "\nTestCase_Matrix::Test_Inplace_Transpose" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();         // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_transpose();   // Make A = transpose(A).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();         // Print Matrix A. A = [1 4 7; 2 5 8; 3 6 9].
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][0], 2.0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][1], 4.0, 1e-06 );        
}

void TestCase_Matrix::Test_Inplace_Round()
{
  cout << "\nTestCase_Matrix::Test_Inplace_Round" << endl;  
  Matrix A;
  A = "[1.09 2.08 3.07; 4.06 5.05 6.04; 7.03 8.02 9.01]";
  bool result;
  result = A.PrintStdout();     // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_Round(1);  // Make A = round(A) to the 1st decimal place.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();     // Print Matrix A. A = "[1.1 2.1 3.1; 4.1 5.1 6.0; 7.0 8.0 9.0]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 1.1, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 5.1, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 9.0, 1e-06 );            
}

void TestCase_Matrix::Test_Inplace_Floor()
{
  cout << "\nTestCase_Matrix::Test_Inplace_Floor" << endl;  
  Matrix A;
  A = "[1.9 2.8 3.7; -4.6 -5.5 -6.4; 7.3 8.2 9.1]";
  bool result;
  result = A.PrintStdout();     // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_Floor();   // Make A = floor(A).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();     // Print Matrix A. A = "[1 2 3; -5 -6 -7; 7 8 9]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 1, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], -6, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 9, 1e-06 );            
}

void TestCase_Matrix::Test_Inplace_Ceil()
{
  cout << "\nTestCase_Matrix::Test_Inplace_Ceil" << endl;  
  Matrix A;
  A = "[1.9 2.8 3.7; -4.6 -5.5 -6.4; 7.3 8.2 9.1]";
  bool result;
  result = A.PrintStdout();     // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_Ceil();    // Make A = ceil(A).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();     // Print Matrix A. A = "[2 3 4; -4 -5 -6; 8 9 10]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 2, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], -5, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 10, 1e-06 );            
}

void TestCase_Matrix::Test_Inplace_Fix()
{
  cout << "\nTestCase_Matrix::Test_Inplace_Fix" << endl;  
  Matrix A;
  A = "[1.9 2.8 3.7; -4.6 -5.5 -6.4; 7.3 8.2 9.1]";
  bool result;
  result = A.PrintStdout();     // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_Fix();     // Make A = fix(A).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();     // Print Matrix A. A = "[1 2 3; -4 -5 -6; 7 8 9]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 1, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], -5, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 9, 1e-06 );            
}

void TestCase_Matrix::Test_Inplace_AddScalar()
{
  cout << "\nTestCase_Matrix::Test_Inplace_AddScalar" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();        // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_AddScalar(1); // A += 1.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();        // Print Matrix A. A = "[2 3 4; 5 6 7; 8 9 10]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 2, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 6, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 10, 1e-06 );            
}
    
void TestCase_Matrix::Test_Inplace_SubtractScalar()
{
  cout << "\nTestCase_Matrix::Test_Inplace_SubtractScalar" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();        // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_SubtractScalar(1); // A -= 1.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();        // Print Matrix A. A = "[0 1 2; 3 4 5; 6 7 8]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 0, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 4, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 8, 1e-06 );            
}
 
void TestCase_Matrix::Test_Inplace_MultiplyScalar()
{
  cout << "\nTestCase_Matrix::Test_Inplace_MultiplyScalar" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();        // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_MultiplyScalar(5); // A *= 5.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();        // Print Matrix A. A = "[5 10 15; 20 25 30; 35 40 45]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 5, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 25, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 45, 1e-06 );            
}
 
void TestCase_Matrix::Test_Inplace_DivideScalar()
{
  cout << "\nTestCase_Matrix::Test_Inplace_DivideScalar" << endl;  
  Matrix A;
  A = "[5 10 15; 20 25 30; 35 40 45]";
  bool result;
  result = A.PrintStdout();        // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_DivideScalar(5); // A /= 5.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();        // Print Matrix A. A = "[1 2 3; 4 5 6; 7 8 9]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 1, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 5, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 9, 1e-06 );            
}

void TestCase_Matrix::Test_Inplace_PowerScalar()
{
  cout << "\nTestCase_Matrix::Test_Inplace_PowerScalar" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();           // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  result = A.Inplace_PowerScalar(2);  // A = A.^2. Not A*A! Each element is raised.
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();           // Print Matrix A. A = "[1 4 9; 16 25 36; 49 64 81]";
  CPPUNIT_ASSERT(result);  
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 1, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 25, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A[2][2], 81, 1e-06 );            
}    

void TestCase_Matrix::Test_Inplace_AddScalarComplex()
{
  cout << "\nTestCase_Matrix::Test_Inplace_AddScalarComplex" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();           // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  std::complex<double> cplx(4.0,2.0);
  result = A.Inplace_AddScalarComplex(cplx);  // A += (4+2i).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();           // Print Matrix A. A = "[5+2i 6+2i 7+2i; 8+2i 9+2i 10+2i; 11+2i 12+2i 13+2i]";
  CPPUNIT_ASSERT(result);  
  cout << "A(0,0) = " << A(0,0).real() << "+" << A(0,0).imag() << "i " << endl;
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(0,0).real(), 5, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(1,1).real(), 9, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(2,2).real(), 13, 1e-06 );            
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(0,0).imag(), 2, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(1,1).imag(), 2, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(2,2).imag(), 2, 1e-06 );              
}


void TestCase_Matrix::Test_Inplace_SubtractScalarComplex()
{
  cout << "\nTestCase_Matrix::Test_Inplace_SubtractScalarComplex" << endl;  
  Matrix A;
  A = "[1 2 3; 4 5 6; 7 8 9]";
  bool result;
  result = A.PrintStdout();           // Print Matrix A.
  CPPUNIT_ASSERT(result);  
  std::complex<double> cplx(5.0,2.0);
  result = A.Inplace_SubtractScalarComplex(cplx);  // A -= (5+2i).
  CPPUNIT_ASSERT(result);  
  cout << endl;
  result = A.PrintStdout();           // Print Matrix A. A = "[-4-2i -3-2i -2-2i; -1-2i 0-2i 1-2i; 2-2i 3-2i 4-2i]";
  CPPUNIT_ASSERT(result);  
  cout << "A(0,0) = " << A(0,0).real() << "+" << A(0,0).imag() << "i " << endl;
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(0,0).real(), -4, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(1,1).real(), 0, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(2,2).real(), 4, 1e-06 );            
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(0,0).imag(), -2, 1e-06 );
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(1,1).imag(), -2, 1e-06 );        
  CPPUNIT_ASSERT_DOUBLES_EQUAL( A(2,2).imag(), -2, 1e-06 );              
}


void TestCase_Matrix::Test_Plot()
{
  Matrix T; // time
  Matrix S; // sin(time)
  Matrix C; // cos(time)
  Matrix Sinc; // tan(time)
  Matrix F; 
  bool result;
  double pi = 3.1415926535897;
  result = T.Inplace_colon( -2*pi+0.01, 0.01, 2*pi );
  CPPUNIT_ASSERT(result);  

  S = T;
  result = S.Inplace_sin();
  CPPUNIT_ASSERT(result);  
  C = T;
  result = C.Inplace_cos();
  CPPUNIT_ASSERT(result);  
  Sinc = T;
  result = Sinc.Inplace_sinc();
  CPPUNIT_ASSERT(result);  
  
  result = Plot( "PlotFunctionTest1.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)" );   
  CPPUNIT_ASSERT(result);   

  result = Plot( "PlotFunctionTest2.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)", T, C, "cosine", "(V)" );   
  CPPUNIT_ASSERT(result);   

  result = Plot( "PlotFunctionTest3.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)", T, C, "cosine", "(V)", T, Sinc, "sinc", "(V)" );   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?