| 1 |  | 
|---|
| 2 | //#define WANT_STREAM | 
|---|
| 3 |  | 
|---|
| 4 | #include "include.h" | 
|---|
| 5 |  | 
|---|
| 6 | #include "newmat.h" | 
|---|
| 7 |  | 
|---|
| 8 | #include "tmt.h" | 
|---|
| 9 |  | 
|---|
| 10 | #ifdef use_namespace | 
|---|
| 11 | using namespace NEWMAT; | 
|---|
| 12 | #endif | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | /**************************** test program ******************************/ | 
|---|
| 16 |  | 
|---|
| 17 | void trymat3() | 
|---|
| 18 | { | 
|---|
| 19 |    Tracer et("Third test of Matrix package"); | 
|---|
| 20 |    Tracer::PrintTrace(); | 
|---|
| 21 |  | 
|---|
| 22 |    { | 
|---|
| 23 |       Tracer et1("Stage 1"); | 
|---|
| 24 |       int i,j; | 
|---|
| 25 |       SymmetricMatrix S(7); | 
|---|
| 26 |       for (i=1;i<=7;i++) for (j=1;j<=i;j++) S(i,j)=i*i+j; | 
|---|
| 27 |                 S=-S+2.0; | 
|---|
| 28 |  | 
|---|
| 29 |       DiagonalMatrix D(7); | 
|---|
| 30 |       for (i=1;i<=7;i++) D(i,i)=S(i,i); | 
|---|
| 31 |  | 
|---|
| 32 |       Matrix M4(7,7); { M4=D+(D+4.0); M4=M4-D*2.0;  M4=M4-4.0; Print(M4); } | 
|---|
| 33 |       SymmetricMatrix S2=D; Matrix M2=S2;  { M2=-D+M2; Print(M2); } | 
|---|
| 34 |       UpperTriangularMatrix U2=D; { M2=U2; M2=D-M2; Print(M2); } | 
|---|
| 35 |       LowerTriangularMatrix L2=D; { M2=L2; M2=D-M2; Print(M2); } | 
|---|
| 36 |       M2=D; M2=M2-D; Print(M2); | 
|---|
| 37 |       for (i=1;i<=7;i++) for (j=1;j<=i;j++) L2(i,j)=2.0-i*i-j; | 
|---|
| 38 |       U2=L2.t(); D=D.t(); S=S.t(); | 
|---|
| 39 |       M4=(L2-1.0)+(U2+1.0)-D-S; Print(M4); | 
|---|
| 40 |       M4=(-L2+1.0)+(-U2-1.0)+D+S; Print(M4); | 
|---|
| 41 |    } | 
|---|
| 42 |  | 
|---|
| 43 |    { | 
|---|
| 44 |       Tracer et1("Stage 2"); | 
|---|
| 45 |       int i,j; | 
|---|
| 46 |       DiagonalMatrix D(6); | 
|---|
| 47 |       for (i=1;i<=6;i++) D(i,i)=i*3.0+i*i+2.0; | 
|---|
| 48 |       UpperTriangularMatrix U2(7); LowerTriangularMatrix L2(7); | 
|---|
| 49 |       for (i=1;i<=7;i++) for (j=1;j<=i;j++) L2(i,j)=2.0-i*i+j; | 
|---|
| 50 |                 { U2=L2.t(); } | 
|---|
| 51 |       DiagonalMatrix D1(7); for (i=1;i<=7;i++) D1(i,i)=(i-2)*(i-4); | 
|---|
| 52 |       Matrix M2(6,7); | 
|---|
| 53 |       for (i=1;i<=6;i++) for (j=1;j<=7;j++) M2(i,j)=2.0+i*j+i*i+2.0*j*j; | 
|---|
| 54 |       Matrix MD=D; SymmetricMatrix MD1(1); MD1=D1; | 
|---|
| 55 |       Matrix MX=MD*M2*MD1 - D*(M2*D1); Print(MX); | 
|---|
| 56 |       MX=MD*M2*MD1 - (D*M2)*D1; Print(MX); | 
|---|
| 57 |       { | 
|---|
| 58 |          D.ReSize(7); for (i=1;i<=7;i++) D(i,i)=i*3.0+i*i+2.0; | 
|---|
| 59 |          LowerTriangularMatrix LD(1); LD=D; | 
|---|
| 60 |          UpperTriangularMatrix UD(1); UD=D; | 
|---|
| 61 |          M2=U2; M2=LD*M2*MD1 - D*(U2*D1); Print(M2); | 
|---|
| 62 |          M2=U2; M2=UD*M2*MD1 - (D*U2)*D1; Print(M2); | 
|---|
| 63 |          M2=L2; M2=LD*M2*MD1 - D*(L2*D1); Print(M2); | 
|---|
| 64 |          M2=L2; M2=UD*M2*MD1 - (D*L2)*D1; Print(M2); | 
|---|
| 65 |       } | 
|---|
| 66 |    } | 
|---|
| 67 |  | 
|---|
| 68 |    { | 
|---|
| 69 |       Tracer et1("Stage 3"); | 
|---|
| 70 |       // test inverse * scalar | 
|---|
| 71 |       DiagonalMatrix D(6); | 
|---|
| 72 |       for (int i=1;i<=6;i++) D(i)=i*i; | 
|---|
| 73 |       DiagonalMatrix E = D.i() * 4.0; | 
|---|
| 74 |       DiagonalMatrix I(6); I = 1.0; | 
|---|
| 75 |       E=D*E-I*4.0; Print(E); | 
|---|
| 76 |       E = D.i() / 0.25; E=D*E-I*4.0; Print(E); | 
|---|
| 77 |    } | 
|---|
| 78 |    { | 
|---|
| 79 |       Tracer et1("Stage 4"); | 
|---|
| 80 |       Matrix sigma(3,3); Matrix sigmaI(3,3); | 
|---|
| 81 |       sigma = 0; sigma(1,1) = 1.0; sigma(2,2) = 1.0; sigma(3,3) = 1.0; | 
|---|
| 82 |       sigmaI = sigma.i(); | 
|---|
| 83 |       sigmaI -= sigma;  Clean(sigmaI, 0.000000001); Print(sigmaI); | 
|---|
| 84 |    } | 
|---|
| 85 |    { | 
|---|
| 86 |       Tracer et1("Stage 5"); | 
|---|
| 87 |       Matrix X(5,5); DiagonalMatrix DM(5); | 
|---|
| 88 |       for (int i=1; i<=5; i++) for (int j=1; j<=5; j++) | 
|---|
| 89 |          X(i,j) = (23*i+59*j) % 43; | 
|---|
| 90 |       DM << 1 << 8 << -7 << 2 << 3; | 
|---|
| 91 |       Matrix Y = X.i() * DM; Y = X * Y - DM; | 
|---|
| 92 |       Clean(Y, 0.000000001); Print(Y); | 
|---|
| 93 |    } | 
|---|
| 94 |    { | 
|---|
| 95 |       Tracer et1("Stage 6");          // test reverse function | 
|---|
| 96 |       ColumnVector CV(10), RCV(10); | 
|---|
| 97 |       CV  <<  2 << 7 << 1  << 6 << -3 <<  1 << 8 << -4 << 0 << 17; | 
|---|
| 98 |       RCV << 17 << 0 << -4 << 8 << 1  << -3 << 6 <<  1 << 7 << 2; | 
|---|
| 99 |       ColumnVector X = CV - RCV.Reverse(); Print(X); | 
|---|
| 100 |       RowVector Y = CV.t() - RCV.t().Reverse(); Print(Y); | 
|---|
| 101 |       DiagonalMatrix D = CV.AsDiagonal() - RCV.AsDiagonal().Reverse(); | 
|---|
| 102 |       Print(D); | 
|---|
| 103 |       X = CV & CV.Rows(1,9).Reverse(); | 
|---|
| 104 |       ColumnVector Z(19); | 
|---|
| 105 |       Z.Rows(1,10) = RCV.Reverse(); Z.Rows(11,19) = RCV.Rows(2,10); | 
|---|
| 106 |       X -= Z; Print(X); Z -= Z.Reverse(); Print(Z); | 
|---|
| 107 |       Matrix A(3,3); A << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9; | 
|---|
| 108 |       Matrix B(3,3); B << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1; | 
|---|
| 109 |       Matrix Diff = A - B.Reverse(); Print(Diff); | 
|---|
| 110 |       Diff = (-A).Reverse() + B; Print(Diff); | 
|---|
| 111 |       UpperTriangularMatrix U; | 
|---|
| 112 |       U << A.Reverse(); Diff = U; U << B; Diff -= U; Print(Diff); | 
|---|
| 113 |       U << (-A).Reverse(); Diff = U; U << B; Diff += U; Print(Diff); | 
|---|
| 114 |    } | 
|---|
| 115 |    { | 
|---|
| 116 |       Tracer et1("Stage 7");           // test IsSingular function | 
|---|
| 117 |       ColumnVector XX(4); | 
|---|
| 118 |       Matrix A(3,3); | 
|---|
| 119 |       A = 0; | 
|---|
| 120 |       CroutMatrix B1 = A; | 
|---|
| 121 |       XX(1) = B1.IsSingular() ? 0 : 1; | 
|---|
| 122 |       A << 1 << 3 << 6 | 
|---|
| 123 |         << 7 << 11 << 13 | 
|---|
| 124 |         << 2 << 4  << 1; | 
|---|
| 125 |       CroutMatrix B2(A); | 
|---|
| 126 |       XX(2) = B2.IsSingular() ? 1 : 0; | 
|---|
| 127 |       BandMatrix C(3,1,1); C.Inject(A); | 
|---|
| 128 |       BandLUMatrix B3(C); | 
|---|
| 129 |       XX(3) = B3.IsSingular() ? 1 : 0; | 
|---|
| 130 |       C = 0; | 
|---|
| 131 |       BandLUMatrix B4(C); | 
|---|
| 132 |       XX(4) = B4.IsSingular() ? 0 : 1; | 
|---|
| 133 |       Print(XX); | 
|---|
| 134 |    } | 
|---|
| 135 |    { | 
|---|
| 136 |       Tracer et1("Stage 8");           // inverse with vector of 0s | 
|---|
| 137 |       Matrix A(3,3); Matrix Z(3,3); ColumnVector X(6); | 
|---|
| 138 |       A <<  1 <<  3 <<  6 | 
|---|
| 139 |         <<  7 << 11 << 13 | 
|---|
| 140 |         <<  2 <<  4 <<  1; | 
|---|
| 141 |       Z = 0; | 
|---|
| 142 |       Matrix B = (A | Z) & (Z | A);   // 6 * 6 matrix | 
|---|
| 143 |       X = 0.0; | 
|---|
| 144 |       X = B.i() * X; | 
|---|
| 145 |       Print(X); | 
|---|
| 146 |       // also check inverse with non-zero Y | 
|---|
| 147 |       Matrix Y(3,3); | 
|---|
| 148 |       Y << 0.0 << 1.0 << 1.0 | 
|---|
| 149 |         << 5.0 << 0.0 << 5.0 | 
|---|
| 150 |         << 3.0 << 3.0 << 0.0; | 
|---|
| 151 |       Matrix YY = Y & Y;        // stack Y matrices | 
|---|
| 152 |       YY = B.i() * YY; | 
|---|
| 153 |       Matrix Y1 = A.i() * Y; | 
|---|
| 154 |       YY -= Y1 & Y1; Clean(YY, 0.000000001); Print(YY); | 
|---|
| 155 |       Y1 = A * Y1 - Y; Clean(Y1, 0.000000001); Print(Y1); | 
|---|
| 156 |    } | 
|---|
| 157 |  | 
|---|
| 158 |  | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|