| 1 | |
|---|
| 2 | //#define WANT_STREAM |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | #include "include.h" |
|---|
| 6 | |
|---|
| 7 | #include "newmatap.h" |
|---|
| 8 | |
|---|
| 9 | #include "tmt.h" |
|---|
| 10 | |
|---|
| 11 | #ifdef use_namespace |
|---|
| 12 | using namespace NEWMAT; |
|---|
| 13 | #endif |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /**************************** test program ******************************/ |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | static void process(const GeneralMatrix& A, |
|---|
| 20 | const ColumnVector& X1, const ColumnVector& X2) |
|---|
| 21 | { |
|---|
| 22 | Matrix B = A; |
|---|
| 23 | LinearEquationSolver L(A); |
|---|
| 24 | Matrix Y(4,2); |
|---|
| 25 | Y.Column(1) << L.i() * X1; Y.Column(2) << L.i() * X2; |
|---|
| 26 | Matrix Z(4,2); Z.Column(1) << X1; Z.Column(2) << X2; |
|---|
| 27 | Z = B * Y - Z; Clean(Z,0.00000001); Print(Z); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | void trymata() |
|---|
| 33 | { |
|---|
| 34 | // cout << "\nTenth test of Matrix package\n"; |
|---|
| 35 | Tracer et("Tenth test of Matrix package"); |
|---|
| 36 | Tracer::PrintTrace(); |
|---|
| 37 | int i; int j; |
|---|
| 38 | UpperTriangularMatrix U(8); |
|---|
| 39 | for (i=1;i<=8;i++) for (j=i;j<=8;j++) U(i,j)=i+j*j+5; |
|---|
| 40 | Matrix X(8,6); |
|---|
| 41 | for (i=1;i<=8;i++) for (j=1;j<=6;j++) X(i,j)=i*j+1.0; |
|---|
| 42 | Matrix Y = U.i()*X; Matrix MU=U; |
|---|
| 43 | Y=Y-MU.i()*X; Clean(Y,0.00000001); Print(Y); |
|---|
| 44 | Y = U.t().i()*X; Y=Y-MU.t().i()*X; Clean(Y,0.00000001); Print(Y); |
|---|
| 45 | UpperTriangularMatrix UX(8); |
|---|
| 46 | for (i=1;i<=8;i++) for (j=i;j<=8;j++) UX(i,j)=i+j+1; |
|---|
| 47 | UX(4,4)=0; UX(4,5)=0; |
|---|
| 48 | UpperTriangularMatrix UY = U.i() * UX; |
|---|
| 49 | { X=UX; MU=U; Y=UY-MU.i()*X; Clean(Y,0.000000001); Print(Y); } |
|---|
| 50 | LowerTriangularMatrix LY = U.t().i() * UX.t(); |
|---|
| 51 | { Y=LY-MU.i().t()*X.t(); Clean(Y,0.000000001); Print(Y); } |
|---|
| 52 | DiagonalMatrix D(8); for (i=1;i<=8;i++) D(i,i)=i+1; |
|---|
| 53 | { X=D.i()*MU; } |
|---|
| 54 | { UY=U; UY=D.i()*UY; Y=UY-X; Clean(Y,0.00000001); Print(Y); } |
|---|
| 55 | { UY=D.i()*U; Y=UY-X; Clean(Y,0.00000001); Print(Y); } |
|---|
| 56 | // X=MU.t(); |
|---|
| 57 | // LY=D.i()*U.t(); Y=D*LY-X; Clean(Y,0.00000001); Print(Y); |
|---|
| 58 | // LowerTriangularMatrix L=U.t(); |
|---|
| 59 | // LY=D.i()*L; Y=D*LY-X; Clean(Y,0.00000001); Print(Y); |
|---|
| 60 | U.ReSize(8); |
|---|
| 61 | for (i=1;i<=8;i++) for (j=i;j<=8;j++) U(i,j)=i+j*j+5; |
|---|
| 62 | MU = U; |
|---|
| 63 | MU = U.i() - MU.i(); Clean(MU,0.00000001); Print(MU); |
|---|
| 64 | MU = U.t().i() - U.i().t(); Clean(MU,0.00000001); Print(MU); |
|---|
| 65 | |
|---|
| 66 | // test LINEQ |
|---|
| 67 | { |
|---|
| 68 | ColumnVector X1(4), X2(4); |
|---|
| 69 | X1(1)=1; X1(2)=2; X1(3)=3; X1(4)=4; |
|---|
| 70 | X2(1)=1; X2(2)=10; X2(3)=100; X2(4)=1000; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | Matrix A(4,4); |
|---|
| 74 | A(1,1)=1; A(1,2)=3; A(1,3)=0; A(1,4)=0; |
|---|
| 75 | A(2,1)=3; A(2,2)=2; A(2,3)=5; A(2,4)=0; |
|---|
| 76 | A(3,1)=0; A(3,2)=5; A(3,3)=4; A(3,4)=1; |
|---|
| 77 | A(4,1)=0; A(4,2)=0; A(4,3)=1; A(4,4)=3; |
|---|
| 78 | process(A,X1,X2); |
|---|
| 79 | |
|---|
| 80 | BandMatrix B(4,1,1); B.Inject(A); |
|---|
| 81 | process(B,X1,X2); |
|---|
| 82 | |
|---|
| 83 | UpperTriangularMatrix U(4); |
|---|
| 84 | U(1,1)=1; U(1,2)=2; U(1,3)=3; U(1,4)=4; |
|---|
| 85 | U(2,2)=8; U(2,3)=7; U(2,4)=6; |
|---|
| 86 | U(3,3)=2; U(3,4)=7; |
|---|
| 87 | U(4,4)=1; |
|---|
| 88 | process(U,X1,X2); |
|---|
| 89 | |
|---|
| 90 | // check rowwise load |
|---|
| 91 | UpperTriangularMatrix U1(4); |
|---|
| 92 | U1.Row(1) << 1 << 2 << 3 << 4; |
|---|
| 93 | U1.Row(2) << 8 << 7 << 6; |
|---|
| 94 | U1.Row(3) << 2 << 7; |
|---|
| 95 | U1.Row(4) << 1; |
|---|
| 96 | |
|---|
| 97 | U1 -= U; |
|---|
| 98 | |
|---|
| 99 | Print(U1); |
|---|
| 100 | |
|---|
| 101 | LowerTriangularMatrix L = U.t(); |
|---|
| 102 | process(L,X1,X2); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // test inversion of poorly conditioned matrix |
|---|
| 106 | // a user complained this didn't work under OS9 |
|---|
| 107 | { |
|---|
| 108 | Matrix M(4,4); |
|---|
| 109 | |
|---|
| 110 | M << 8.613057e+00 << 8.693985e+00 << -2.322050e-01 << 0.000000e+00 |
|---|
| 111 | << 8.693985e+00 << 8.793868e+00 << -2.346310e-01 << 0.000000e+00 |
|---|
| 112 | << -2.322050e-01 << -2.346310e-01 << 6.264000e-03 << 0.000000e+00 |
|---|
| 113 | << 0.000000e+00 << 0.000000e+00 << 0.000000e+00 << 3.282806e+03 ; |
|---|
| 114 | Matrix MI = M.i(); |
|---|
| 115 | DiagonalMatrix I(4); I = 1; |
|---|
| 116 | Matrix Diff = MI * M - I; |
|---|
| 117 | Clean(Diff,0.00000001); Print(Diff); |
|---|
| 118 | // Alternatively do Cholesky |
|---|
| 119 | SymmetricMatrix SM; SM << M; |
|---|
| 120 | LowerTriangularMatrix LT = Cholesky(SM).i(); |
|---|
| 121 | MI = LT.t() * LT; Diff = MI * M - I; |
|---|
| 122 | Clean(Diff,0.00000001); Print(Diff); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | // cout << "\nEnd of tenth test\n"; |
|---|
| 126 | } |
|---|