testcase_matrix.cpp
来自「c++ 实现的矩阵运算库」· C++ 代码 · 共 1,295 行 · 第 1/3 页
CPP
1,295 行
CPPUNIT_ASSERT(result);
result = Plot( "PlotFunctionTest4.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)", T, C, "cosine", "(V)", T, Sinc, "sinc", "(V)", T, S+1.0, "sin+1", "(V)" );
CPPUNIT_ASSERT(result);
result = Plot( "PlotFunctionTest5.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)", T, C, "cosine", "(V)", T, Sinc, "sinc", "(V)", T, S+1.0, "sin+1", "(V)", T, C-1.0, "cos-1", "(V)" );
CPPUNIT_ASSERT(result);
result = Plot( "PlotFunctionTest6.bmp", "Testing Plot", "time (s)", "voltage (V)", T, S, "sine", "(V)", T, C, "cosine", "(V)", T, Sinc, "sinc", "(V)", T, S+1.0, "sin+1", "(V)", T, C-1.0, "cos-1", "(V)", T, Sinc^2, "sinc^2", "(V)" );
CPPUNIT_ASSERT(result);
F = T;
result = F.Concatonate( S );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, "plot1test.bmp", "A Sinusoid", "time (s)", "voltage (V)", "sinusoid", "(V)" );
CPPUNIT_ASSERT(result);
result = F.Concatonate( C );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, "plot2test.bmp", "Two Sinusoids", "time (s)", "voltage (V)", "sine", "(V)", "cosine", "(V)" );
CPPUNIT_ASSERT(result);
result = F.Concatonate( Sinc );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, "plot3test.bmp", "sin cos sinc", "time (s)", "voltage (V)", "sine", "(V)", "cosine", "(V)", "sinc", "(V)" );
CPPUNIT_ASSERT(result);
S += 1.0;
result = F.Concatonate( S );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4, "plot4test.bmp", "sin cos sinc sin+1", "time (s)", "voltage (V)", "sine", "(V)", "cosine", "(V)", "sinc", "(V)", "sin+1", "(V)" );
CPPUNIT_ASSERT(result);
C -= 1.0;
result = F.Concatonate( C );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4, 5 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4, 5, "plot5test.bmp", "sin cos sinc sin+1 cos-1", "time (s)", "voltage (V)", "sine", "(V)", "cosine", "(V)", "sinc", "(V)", "sin+1", "(V)", "cos-1", "(V)" );
CPPUNIT_ASSERT(result);
Sinc.Inplace_Sqr();
result = F.Concatonate( Sinc );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4, 5, 6 );
CPPUNIT_ASSERT(result);
result = F.Plot( 0, 1, 2, 3, 4, 5, 6, "plot6test.bmp", "sin cos sinc sin+1 cos-1 sinc^2", "time (s)", "voltage (V)", "sine", "(V)", "cosine", "(V)", "sinc", "(V)", "sin+1", "(V)", "cos-1", "(V)", "sinc^2", "(V)" );
CPPUNIT_ASSERT(result);
Matrix X = "[ 1 2 3 4 5 6 7 8 9 10 ]"; // set the matrix from a string.
Matrix Y = X^2;
result = Plot( "testplot.bmp", "Y=X^2", "X (m)", "Y (m)", X, Y, "Y=X^2", "(m^2)" );
CPPUNIT_ASSERT(result);
X = "[ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; ]"; // set the matrix from a string, column vector.
result = X.Concatonate( X^2 );
result = X.Plot( 0, 1, "testplot2.bmp", "Y=X^2", "X (m)", "Y (m)", "Y=X^2", "(m^2)" );
X = "[ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; ]"; // set the matrix from a string, column vector.
Y = X;
result = X.Concatonate( Y^2 );
result = X.Concatonate( Y^3 );
result = X.Plot( 0, 1, 2, "testplot3.bmp", "Y=X^2", "X (m)", "Y (m)", "Y=X^2", "(m^2)", "Y=X^3", "(m^3)" );
X = "[ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; ]"; // set the matrix from a string, column vector.
Matrix Y1 = X^(0.5);
Matrix Y2 = X;
Matrix Y3 = X^2;
Matrix Y4 = X^3;
result = Plot( "testplot4.bmp", "Y as function of X", "X (m)", "Y (m)", X, Y1, "Y=sqrt(X)", "sqrt(m)", X, Y2, "Y=(X)", "(m)", X, Y3, "Y=X^2", "(m^2)", X, Y4, "Y=X^3", "(m^3)" );
}
void TestCase_Matrix::Test_Negate()
{
unsigned i;
unsigned j;
Matrix A = "[1 2 3 ; 4 5 6; 7 8 9]";
Matrix B = A.Negate();
cout << "\nA = " << endl;
A.PrintStdout();
cout << "-A = " << endl;
B.PrintStdout();
for( i = 0; i < B.nrows(); i++ )
for( j = 0; j < B.ncols(); j++ )
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j], -B[i][j], 1e-06 );
}
void TestCase_Matrix::Test_AddIdentity()
{
unsigned i;
unsigned j;
Matrix A = "[1 2 3 ; 4 5 6; 7 8 9]";
Matrix B = A.AddIdentity();
cout << "\nA = " << endl;
A.PrintStdout();
cout << "A+I = " << endl;
B.PrintStdout();
for( i = 0; i < B.nrows(); i++ )
{
for( j = 0; j < B.ncols(); j++ )
{
if( j == i )
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j]+1.0, B[i][j], 1e-06 );
}
else
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j], B[i][j], 1e-06 );
}
}
}
}
void TestCase_Matrix::Test_MinusIdentity()
{
unsigned i;
unsigned j;
Matrix A = "[1 2 3 ; 4 5 6; 7 8 9]";
Matrix B = A.MinusIdentity();
cout << "\nA = " << endl;
A.PrintStdout();
cout << "A-I = " << endl;
B.PrintStdout();
for( i = 0; i < B.nrows(); i++ )
{
for( j = 0; j < B.ncols(); j++ )
{
if( j == i )
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j]-1.0, B[i][j], 1e-06 );
}
else
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j], B[i][j], 1e-06 );
}
}
}
}
void TestCase_Matrix::Test_IdentityMinusMe()
{
unsigned i;
unsigned j;
Matrix A = "[1 2 3 ; 4 5 6; 7 8 9]";
Matrix B = A.IdentityMinusMe();
cout << "\nA = " << endl;
A.PrintStdout();
cout << "I-A = " << endl;
B.PrintStdout();
for( i = 0; i < B.nrows(); i++ )
{
for( j = 0; j < B.ncols(); j++ )
{
if( j == i )
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0-A[i][j], B[i][j], 1e-06 );
}
else
{
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[i][j], -B[i][j], 1e-06 );
}
}
}
}
void TestCase_Matrix::Test_Inplace_rand()
{
Matrix A;
double re;
double im;
CPPUNIT_ASSERT( A.Inplace_rand(10000,1) );
CPPUNIT_ASSERT( A.GetStats_MaxVal( re, im ) );
CPPUNIT_ASSERT( re <= 1.0 );
CPPUNIT_ASSERT( A.GetStats_MinVal( re, im ) );
CPPUNIT_ASSERT( re >= 0.0 );
}
void TestCase_Matrix::Test_Inplace_randn()
{
Matrix A;
double re;
double im;
CPPUNIT_ASSERT( A.Inplace_randn(10000,1,88) );
CPPUNIT_ASSERT( A.GetStats_Stdev( re ) );
CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 1.0, 0.009 );
CPPUNIT_ASSERT( A.GetStats_Mean( re, im ) );
CPPUNIT_ASSERT_DOUBLES_EQUAL( re, 0.0, 0.009 );
}
void TestCase_Matrix::Test_Sandbox()
{
bool result;
Matrix bMatrix = "[1 2; 3 4]";
Matrix aMatrix;
aMatrix= 0.5*(bMatrix + bMatrix.T());
CPPUNIT_ASSERT_DOUBLES_EQUAL( aMatrix[0][0], 1.0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( aMatrix[0][1], 2.5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( aMatrix[1][0], 2.5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( aMatrix[1][1], 4.0, 1e-09 );
Matrix A = "[1 2 3; 4 5 6; 7 8 9]";
Matrix B = "[1 2; 3 4]";
cout << "A is:" << endl;
result = A.PrintStdout(); // Print Matrix A. A = [1 2; 3 4]
CPPUNIT_ASSERT( result );
cout << "B is:" << endl;
result = B.PrintStdout(); // Print Matrix B. B = [1 2 3; 4 5 6; 7 8 9]
CPPUNIT_ASSERT( result );
result = A.Swap(B);
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( A.nrows() == 2 );
CPPUNIT_ASSERT( A.ncols() == 2 );
CPPUNIT_ASSERT( B.nrows() == 3 );
CPPUNIT_ASSERT( B.ncols() == 3 );
cout << "A is:" << endl;
result = A.PrintStdout(); // Print Matrix A. A = [1 2; 3 4]
CPPUNIT_ASSERT( result );
cout << "B is:" << endl;
result = B.PrintStdout(); // Print Matrix B. B = [1 2 3; 4 5 6; 7 8 9]
CPPUNIT_ASSERT( result );
A = "[1 2 3; 4 5 6; 7 8 9]";
B.Clear();
cout << "A is:" << endl;
result = A.PrintStdout(); // Print Matrix A. A = [1 2; 3 4]
CPPUNIT_ASSERT( result );
cout << "B is:" << endl;
result = B.PrintStdout(); // Print Matrix B. B = [1 2 3; 4 5 6; 7 8 9]
CPPUNIT_ASSERT( result );
result = A.Swap(B);
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( A.nrows() == 0 );
CPPUNIT_ASSERT( A.ncols() == 0 );
CPPUNIT_ASSERT( B.nrows() == 3 );
CPPUNIT_ASSERT( B.ncols() == 3 );
cout << "A is:" << endl;
result = A.PrintStdout(); // Print Matrix A. A = [1 2; 3 4]
CPPUNIT_ASSERT( result );
cout << "B is:" << endl;
result = B.PrintStdout(); // Print Matrix B. B = [1 2 3; 4 5 6; 7 8 9]
CPPUNIT_ASSERT( result );
Matrix C;
B = "[1 2; 3 4]";
C = "[-1 2; -3 4]";
result = A.TransposeMultiply( B, C );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], -10, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][1], 14, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][0], -14, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 20, 1e-09 );
B = "[1 2; 3 4]";
C = "[-1 2; -3 4]";
result = A.MultiplyTranspose( B, C );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][1], 5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][0], 5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 7, 1e-09 );
A = "[1 2; 3 4]";
B = "[5 6; 7 8]";
result = A.Inplace_PostMultiplyTranspose(B);
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][0], 17, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[0][1], 23, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][0], 39, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( A[1][1], 53, 1e-09 );
/*
result = A.Inplace_colon( 1.0, 1.0, 32.0 );
CPPUNIT_ASSERT( result );
B = A*A.Transpose();
result = B.Print( "FFT2Input.txt", 9 );
CPPUNIT_ASSERT( result );
result = B.Inplace_FFT2();
CPPUNIT_ASSERT( result );
result = B.Real().Print( "FFT2Output.txt", 12 );
CPPUNIT_ASSERT( result );
result = B.Inplace_IFFT2();
CPPUNIT_ASSERT( result );
result = B.Real().Print( "IFFT2Output.txt", 12 );
CPPUNIT_ASSERT( result );
// then plot in matlab using image and imagesc and compare
B = A*A.Transpose();
C = B.FFT2();
CPPUNIT_ASSERT( result );
result = C.Real().Print( "FFT2OutputAgain.txt", 12 );
C = C.IFFT2();
result = C.Real().Print( "IFFT2OutputAgain.txt", 12 );
*/
Matrix LDLt = "[3 6;6 16]";
Matrix L;
Matrix d;
result = LDLt.GetLDLt( L, d );
CPPUNIT_ASSERT_DOUBLES_EQUAL( L[0][0], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( L[0][1], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( L[1][0], 2, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( L[1][1], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( d[0], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( d[1], 4, 1e-09 );
Matrix UDUt = "[19 8;8 4]";
Matrix U;
result = UDUt.GetUDUt( U, d );
CPPUNIT_ASSERT_DOUBLES_EQUAL( U[0][0], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( U[0][1], 2, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( U[1][0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( U[1][1], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( d[0], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( d[1], 4, 1e-09 );
A = "[1 2 3; 4 5 6; 7 8 9]";
result = A.ExtractSubMatrix( B, 1, 0, 2, 2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0][0], 4, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0][1], 5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0][2], 6, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1][0], 7, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1][1], 8, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1][2], 9, 1e-09 );
A = "[1 100 1000; 2 200 2000; 3 300 3000]";
result = A.Find_EqualTo( B, 0, 2 );
CPPUNIT_ASSERT( B.nrows() == 1 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 1, 1e-09 );
result = A.Find_EqualTo( B, 1, 200 );
CPPUNIT_ASSERT( B.nrows() == 1 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 1, 1e-09 );
result = A.Find_EqualTo( B, 2, 2000 );
CPPUNIT_ASSERT( B.nrows() == 1 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 1, 1e-09 );
result = A.Find_EqualTo( B, 0, 2000 );
CPPUNIT_ASSERT( B.nrows() == 0 );
A = "[1 100 1000; 2 200 2000; 3 300 3000]";
result = A.Find_NotEqualTo( B, 0, 2 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 2, 1e-09 );
result = A.Find_NotEqualTo( B, 1, 200 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 2, 1e-09 );
result = A.Find_NotEqualTo( B, 2, 2000 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 2 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 2, 1e-09 );
result = A.Find_NotEqualTo( B, 0, 2000 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 3 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2], 2, 1e-09 );
A = "[-1 0 1 2 3 4 5]";
A.Inplace_Transpose();
result = A.Find_LessThan( B, 0, 4 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 5 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2], 2, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[3], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[4], 4, 1e-09 );
B = "[-1 0 100 -2 3 -4 5]";
B.Inplace_Transpose();
result = A.Concatonate( B );
CPPUNIT_ASSERT( result );
result = A.Find_LessThan( B, 1, 4 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 5 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 0, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[3], 4, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[4], 5, 1e-09 );
A = "[-1 0 1 2 3 4 5]";
A.Inplace_Transpose();
result = A.Find_MoreThan( B, 0, 1 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 4 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 3, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 4, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2], 5, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[3], 6, 1e-09 );
B = "[-1 0 100 -2 3 -4 5]";
B.Inplace_Transpose();
result = A.Concatonate( B );
CPPUNIT_ASSERT( result );
result = A.Find_MoreThan( B, 1, -1 );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( result );
CPPUNIT_ASSERT( B.nrows() == 4 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[0], 1, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[1], 2, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[2], 4, 1e-09 );
CPPUNIT_ASSERT_DOUBLES_EQUAL( B[3], 6, 1e-09 );
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?