| 1 |  | 
|---|
| 2 | //#define WANT_STREAM | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 | #include "include.h" | 
|---|
| 6 | #include "newmat.h" | 
|---|
| 7 |  | 
|---|
| 8 | #include "tmt.h" | 
|---|
| 9 |  | 
|---|
| 10 | #ifdef use_namespace | 
|---|
| 11 | using namespace NEWMAT; | 
|---|
| 12 | #endif | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | void trymatc() | 
|---|
| 18 | { | 
|---|
| 19 | //   cout << "\nTwelfth test of Matrix package\n"; | 
|---|
| 20 |    Tracer et("Twelfth test of Matrix package"); | 
|---|
| 21 |    Tracer::PrintTrace(); | 
|---|
| 22 |    DiagonalMatrix D(15); D=1.5; | 
|---|
| 23 |    Matrix A(15,15); | 
|---|
| 24 |    int i,j; | 
|---|
| 25 |    for (i=1;i<=15;i++) for (j=1;j<=15;j++) A(i,j)=i*i+j-150; | 
|---|
| 26 |    { A = A + D; } | 
|---|
| 27 |    ColumnVector B(15); | 
|---|
| 28 |    for (i=1;i<=15;i++) B(i)=i+i*i-150.0; | 
|---|
| 29 |    { | 
|---|
| 30 |       Tracer et1("Stage 1"); | 
|---|
| 31 |       ColumnVector B1=B; | 
|---|
| 32 |       B=(A*2.0).i() * B1; | 
|---|
| 33 |       Matrix X = A*B-B1/2.0; | 
|---|
| 34 |       Clean(X, 0.000000001); Print(X); | 
|---|
| 35 |       A.ReSize(3,5); | 
|---|
| 36 |       for (i=1; i<=3; i++) for (j=1; j<=5; j++) A(i,j) = i+100*j; | 
|---|
| 37 |  | 
|---|
| 38 |       B = A.AsColumn()+10000; | 
|---|
| 39 |       RowVector R = (A+10000).AsColumn().t(); | 
|---|
| 40 |       Print( RowVector(R-B.t()) ); | 
|---|
| 41 |    } | 
|---|
| 42 |  | 
|---|
| 43 |    { | 
|---|
| 44 |       Tracer et1("Stage 2"); | 
|---|
| 45 |       B = A.AsColumn()+10000; | 
|---|
| 46 |       Matrix XR = (A+10000).AsMatrix(15,1).t(); | 
|---|
| 47 |       Print( RowVector(XR-B.t()) ); | 
|---|
| 48 |    } | 
|---|
| 49 |  | 
|---|
| 50 |    { | 
|---|
| 51 |       Tracer et1("Stage 3"); | 
|---|
| 52 |       B = (A.AsMatrix(15,1)+A.AsColumn())/2.0+10000; | 
|---|
| 53 |       Matrix MR = (A+10000).AsColumn().t(); | 
|---|
| 54 |       Print( RowVector(MR-B.t()) ); | 
|---|
| 55 |  | 
|---|
| 56 |       B = (A.AsMatrix(15,1)+A.AsColumn())/2.0; | 
|---|
| 57 |       MR = A.AsColumn().t(); | 
|---|
| 58 |       Print( RowVector(MR-B.t()) ); | 
|---|
| 59 |    } | 
|---|
| 60 |  | 
|---|
| 61 |    { | 
|---|
| 62 |       Tracer et1("Stage 4"); | 
|---|
| 63 |       B = (A.AsMatrix(15,1)+A.AsColumn())/2.0; | 
|---|
| 64 |       RowVector R = A.AsColumn().t(); | 
|---|
| 65 |       Print( RowVector(R-B.t()) ); | 
|---|
| 66 |    } | 
|---|
| 67 |  | 
|---|
| 68 |    { | 
|---|
| 69 |       Tracer et1("Stage 5"); | 
|---|
| 70 |       RowVector R = (A.AsColumn()-5000).t(); | 
|---|
| 71 |       B = ((R.t()+10000) - A.AsColumn())-5000; | 
|---|
| 72 |       Print( RowVector(B.t()) ); | 
|---|
| 73 |    } | 
|---|
| 74 |  | 
|---|
| 75 |    { | 
|---|
| 76 |       Tracer et1("Stage 6"); | 
|---|
| 77 |       B = A.AsColumn(); ColumnVector B1 = (A+10000).AsColumn() - 10000; | 
|---|
| 78 |       Print(ColumnVector(B1-B)); | 
|---|
| 79 |    } | 
|---|
| 80 |  | 
|---|
| 81 |    { | 
|---|
| 82 |       Tracer et1("Stage 7"); | 
|---|
| 83 |       Matrix X = B.AsMatrix(3,5); Print(Matrix(X-A)); | 
|---|
| 84 |       for (i=1; i<=3; i++) for (j=1; j<=5; j++) B(5*(i-1)+j) -= i+100*j; | 
|---|
| 85 |       Print(B); | 
|---|
| 86 |    } | 
|---|
| 87 |  | 
|---|
| 88 |    { | 
|---|
| 89 |       Tracer et1("Stage 8"); | 
|---|
| 90 |       A.ReSize(7,7); D.ReSize(7); | 
|---|
| 91 |       for (i=1; i<=7; i++) for (j=1; j<=7; j++) A(i,j) = i*j*j; | 
|---|
| 92 |       for (i=1; i<=7; i++) D(i,i) = i; | 
|---|
| 93 |       UpperTriangularMatrix U; U << A; | 
|---|
| 94 |       Matrix X = A; for (i=1; i<=7; i++) X(i,i) = i; | 
|---|
| 95 |       A.Inject(D); Print(Matrix(X-A)); | 
|---|
| 96 |       X = U; U.Inject(D); A = U; for (i=1; i<=7; i++) X(i,i) = i; | 
|---|
| 97 |       Print(Matrix(X-A)); | 
|---|
| 98 |    } | 
|---|
| 99 |  | 
|---|
| 100 |    { | 
|---|
| 101 |       Tracer et1("Stage 9"); | 
|---|
| 102 |       A.ReSize(7,5); | 
|---|
| 103 |       for (i=1; i<=7; i++) for (j=1; j<=5; j++) A(i,j) = i+100*j; | 
|---|
| 104 |       Matrix Y = A; Y = Y - ((const Matrix&)A); Print(Y); | 
|---|
| 105 |       Matrix X = A; // X.Release(); | 
|---|
| 106 |       Y = A; Y = ((const Matrix&)X) - A; Print(Y); Y = 0.0; | 
|---|
| 107 |       Y = ((const Matrix&)X) - ((const Matrix&)A); Print(Y); | 
|---|
| 108 |    } | 
|---|
| 109 |  | 
|---|
| 110 |    { | 
|---|
| 111 |       Tracer et1("Stage 10"); | 
|---|
| 112 |       // some tests on submatrices | 
|---|
| 113 |       UpperTriangularMatrix U(20); | 
|---|
| 114 |       for (i=1; i<=20; i++) for (j=i; j<=20; j++) U(i,j)=100 * i + j; | 
|---|
| 115 |       UpperTriangularMatrix V = U.SymSubMatrix(1,5); | 
|---|
| 116 |       UpperTriangularMatrix U1 = U; | 
|---|
| 117 |       U1.SubMatrix(4,8,5,9) /= 2; | 
|---|
| 118 |       U1.SubMatrix(4,8,5,9) += 388 * V; | 
|---|
| 119 |       U1.SubMatrix(4,8,5,9) *= 2; | 
|---|
| 120 |       U1.SubMatrix(4,8,5,9) += V; | 
|---|
| 121 |       U1 -= U; UpperTriangularMatrix U2 = U1; | 
|---|
| 122 |       U1 << U1.SubMatrix(4,8,5,9); | 
|---|
| 123 |       U2.SubMatrix(4,8,5,9) -= U1; Print(U2); | 
|---|
| 124 |       U1 -= (777*V); Print(U1); | 
|---|
| 125 |  | 
|---|
| 126 |       U1 = U; U1.SubMatrix(4,8,5,9) -= U.SymSubMatrix(1,5); | 
|---|
| 127 |       U1 -= U;  U2 = U1; U1 << U1.SubMatrix(4,8,5,9); | 
|---|
| 128 |       U2.SubMatrix(4,8,5,9) -= U1; Print(U2); | 
|---|
| 129 |       U1 += V; Print(U1); | 
|---|
| 130 |  | 
|---|
| 131 |       U1 = U; | 
|---|
| 132 |       U1.SubMatrix(3,10,15,19) += 29; | 
|---|
| 133 |       U1 -= U; | 
|---|
| 134 |       Matrix X = U1.SubMatrix(3,10,15,19); X -= 29; Print(X); | 
|---|
| 135 |       U1.SubMatrix(3,10,15,19) *= 0; Print(U1); | 
|---|
| 136 |  | 
|---|
| 137 |       LowerTriangularMatrix L = U.t(); | 
|---|
| 138 |       LowerTriangularMatrix M = L.SymSubMatrix(1,5); | 
|---|
| 139 |       LowerTriangularMatrix L1 = L; | 
|---|
| 140 |       L1.SubMatrix(5,9,4,8) /= 2; | 
|---|
| 141 |       L1.SubMatrix(5,9,4,8) += 388 * M; | 
|---|
| 142 |       L1.SubMatrix(5,9,4,8) *= 2; | 
|---|
| 143 |       L1.SubMatrix(5,9,4,8) += M; | 
|---|
| 144 |       L1 -= L; LowerTriangularMatrix L2 = L1; | 
|---|
| 145 |       L1 << L1.SubMatrix(5,9,4,8); | 
|---|
| 146 |       L2.SubMatrix(5,9,4,8) -= L1; Print(L2); | 
|---|
| 147 |       L1 -= (777*M); Print(L1); | 
|---|
| 148 |  | 
|---|
| 149 |       L1 = L; L1.SubMatrix(5,9,4,8) -= L.SymSubMatrix(1,5); | 
|---|
| 150 |       L1 -= L; L2 =L1; L1 << L1.SubMatrix(5,9,4,8); | 
|---|
| 151 |       L2.SubMatrix(5,9,4,8) -= L1; Print(L2); | 
|---|
| 152 |       L1 += M; Print(L1); | 
|---|
| 153 |  | 
|---|
| 154 |       L1 = L; | 
|---|
| 155 |       L1.SubMatrix(15,19,3,10) -= 29; | 
|---|
| 156 |       L1 -= L; | 
|---|
| 157 |       X = L1.SubMatrix(15,19,3,10); X += 29; Print(X); | 
|---|
| 158 |       L1.SubMatrix(15,19,3,10) *= 0; Print(L1); | 
|---|
| 159 |    } | 
|---|
| 160 |  | 
|---|
| 161 |    { | 
|---|
| 162 |       Tracer et1("Stage 11"); | 
|---|
| 163 |       // more tests on submatrices | 
|---|
| 164 |       Matrix M(20,30); | 
|---|
| 165 |       for (i=1; i<=20; i++) for (j=1; j<=30; j++) M(i,j)=100 * i + j; | 
|---|
| 166 |       Matrix M1 = M; | 
|---|
| 167 |  | 
|---|
| 168 |       for (j=1; j<=30; j++) | 
|---|
| 169 |          { ColumnVector CV = 3 * M1.Column(j); M.Column(j) += CV; } | 
|---|
| 170 |       for (i=1; i<=20; i++) | 
|---|
| 171 |          { RowVector RV = 5 * M1.Row(i); M.Row(i) -= RV; } | 
|---|
| 172 |  | 
|---|
| 173 |       M += M1; Print(M); | 
|---|
| 174 |   | 
|---|
| 175 |    } | 
|---|
| 176 |  | 
|---|
| 177 |    { | 
|---|
| 178 |       Tracer et1("Stage 12"); | 
|---|
| 179 |       // more tests on Release | 
|---|
| 180 |       Matrix M(20,30); | 
|---|
| 181 |       for (i=1; i<=20; i++) for (j=1; j<=30; j++) M(i,j)=100 * i + j; | 
|---|
| 182 |       Matrix M1 = M; | 
|---|
| 183 |       M.Release(); | 
|---|
| 184 |       Matrix M2 = M; | 
|---|
| 185 |       Matrix X = M;   Print(X); | 
|---|
| 186 |       X = M1 - M2;    Print(X); | 
|---|
| 187 |  | 
|---|
| 188 | #ifndef DONT_DO_NRIC | 
|---|
| 189 |       nricMatrix N = M1; | 
|---|
| 190 |       nricMatrix N1 = N; | 
|---|
| 191 |       N.Release(); | 
|---|
| 192 |       nricMatrix N2 = N; | 
|---|
| 193 |       nricMatrix Y = N;   Print(Y); | 
|---|
| 194 |       Y = N1 - N2;        Print(Y); | 
|---|
| 195 | #endif | 
|---|
| 196 |  | 
|---|
| 197 |    } | 
|---|
| 198 |  | 
|---|
| 199 | //   cout << "\nEnd of twelfth test\n"; | 
|---|
| 200 | } | 
|---|