| 1 | |
|---|
| 2 | //#define WANT_STREAM |
|---|
| 3 | |
|---|
| 4 | #define WANT_MATH // for sqrt |
|---|
| 5 | |
|---|
| 6 | #include "include.h" |
|---|
| 7 | |
|---|
| 8 | #include "newmatap.h" |
|---|
| 9 | |
|---|
| 10 | #include "tmt.h" |
|---|
| 11 | |
|---|
| 12 | #ifdef use_namespace |
|---|
| 13 | using namespace NEWMAT; |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | void trymatg() |
|---|
| 19 | { |
|---|
| 20 | // cout << "\nSixteenth test of Matrix package\n"; |
|---|
| 21 | // cout << "\n"; |
|---|
| 22 | Tracer et("Sixteenth test of Matrix package"); |
|---|
| 23 | Tracer::PrintTrace(); |
|---|
| 24 | |
|---|
| 25 | int i,j; |
|---|
| 26 | Matrix M(4,7); |
|---|
| 27 | for (i=1; i<=4; i++) for (j=1; j<=7; j++) M(i,j) = 100 * i + j; |
|---|
| 28 | ColumnVector CV = M.AsColumn(); |
|---|
| 29 | { |
|---|
| 30 | Tracer et1("Stage 1"); |
|---|
| 31 | RowVector test(7); |
|---|
| 32 | test(1) = SumSquare(M); |
|---|
| 33 | test(2) = SumSquare(CV); |
|---|
| 34 | test(3) = SumSquare(CV.t()); |
|---|
| 35 | test(4) = SumSquare(CV.AsDiagonal()); |
|---|
| 36 | test(5) = SumSquare(M.AsColumn()); |
|---|
| 37 | test(6) = Matrix(CV.t()*CV)(1,1); |
|---|
| 38 | test(7) = (CV.t()*CV).AsScalar(); |
|---|
| 39 | test = test - 2156560.0; Print(test); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | UpperTriangularMatrix U(6); |
|---|
| 43 | for (i=1; i<=6; i++) for (j=i; j<=6; j++) U(i,j) = i + (i-j) * (i-j); |
|---|
| 44 | M = U; DiagonalMatrix D; D << U; |
|---|
| 45 | LowerTriangularMatrix L = U.t(); SymmetricMatrix S; S << (L+U)/2.0; |
|---|
| 46 | { |
|---|
| 47 | Tracer et1("Stage 2"); |
|---|
| 48 | RowVector test(6); |
|---|
| 49 | test(1) = U.Trace(); |
|---|
| 50 | test(2) = L.Trace(); |
|---|
| 51 | test(3) = D.Trace(); |
|---|
| 52 | test(4) = S.Trace(); |
|---|
| 53 | test(5) = M.Trace(); |
|---|
| 54 | test(6) = ((L+U)/2.0).Trace(); |
|---|
| 55 | test = test - 21; Print(test); |
|---|
| 56 | test(1) = LogDeterminant(U).Value(); |
|---|
| 57 | test(2) = LogDeterminant(L).Value(); |
|---|
| 58 | test(3) = LogDeterminant(D).Value(); |
|---|
| 59 | test(4) = LogDeterminant(D).Value(); |
|---|
| 60 | test(5) = LogDeterminant((L+D)/2.0).Value(); |
|---|
| 61 | test(6) = Determinant((L+D)/2.0); |
|---|
| 62 | test = test - 720; Clean(test,0.000000001); Print(test); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | { |
|---|
| 66 | Tracer et1("Stage 3"); |
|---|
| 67 | S << L*U; M = S; |
|---|
| 68 | RowVector test(8); |
|---|
| 69 | test(1) = LogDeterminant(S).Value(); |
|---|
| 70 | test(2) = LogDeterminant(M).Value(); |
|---|
| 71 | test(3) = LogDeterminant(L*U).Value(); |
|---|
| 72 | test(4) = LogDeterminant(Matrix(L*L)).Value(); |
|---|
| 73 | test(5) = Determinant(S); |
|---|
| 74 | test(6) = Determinant(M); |
|---|
| 75 | test(7) = Determinant(L*U); |
|---|
| 76 | test(8) = Determinant(Matrix(L*L)); |
|---|
| 77 | test = test - 720.0 * 720.0; Clean(test,0.00000001); Print(test); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | { |
|---|
| 81 | Tracer et1("Stage 4"); |
|---|
| 82 | M = S * S; |
|---|
| 83 | Matrix SX = S; |
|---|
| 84 | RowVector test(3); |
|---|
| 85 | test(1) = SumSquare(S); |
|---|
| 86 | test(2) = SumSquare(SX); |
|---|
| 87 | test(3) = Trace(M); |
|---|
| 88 | test = test - 3925961.0; Print(test); |
|---|
| 89 | } |
|---|
| 90 | { |
|---|
| 91 | Tracer et1("Stage 5"); |
|---|
| 92 | SymmetricMatrix SM(10), SN(10); |
|---|
| 93 | Real S = 0.0; |
|---|
| 94 | for (i=1; i<=10; i++) for (j=i; j<=10; j++) |
|---|
| 95 | { |
|---|
| 96 | SM(i,j) = 1.5 * i - j; SN(i,j) = SM(i,j) * SM(i,j); |
|---|
| 97 | if (SM(i,j) < 0.0) SN(i,j) = - SN(i,j); |
|---|
| 98 | S += SN(i,j) * ((i==j) ? 1.0 : 2.0); |
|---|
| 99 | } |
|---|
| 100 | Matrix M = SM, N = SN; RowVector test(4); |
|---|
| 101 | test(1) = SumAbsoluteValue(SN); |
|---|
| 102 | test(2) = SumAbsoluteValue(-SN); |
|---|
| 103 | test(3) = SumAbsoluteValue(N); |
|---|
| 104 | test(4) = SumAbsoluteValue(-N); |
|---|
| 105 | test = test - 1168.75; Print(test); |
|---|
| 106 | test(1) = Sum(SN); |
|---|
| 107 | test(2) = -Sum(-SN); |
|---|
| 108 | test(3) = Sum(N); |
|---|
| 109 | test(4) = -Sum(-N); |
|---|
| 110 | test = test - S; Print(test); |
|---|
| 111 | test(1) = MaximumAbsoluteValue(SM); |
|---|
| 112 | test(2) = MaximumAbsoluteValue(-SM); |
|---|
| 113 | test(3) = MaximumAbsoluteValue(M); |
|---|
| 114 | test(4) = MaximumAbsoluteValue(-M); |
|---|
| 115 | test = test - 8.5; Print(test); |
|---|
| 116 | } |
|---|
| 117 | { |
|---|
| 118 | Tracer et1("Stage 6"); |
|---|
| 119 | Matrix M(15,20); Real value = 0.0; |
|---|
| 120 | for (i=1; i<=15; i++) { for (j=1; j<=20; j++) M(i,j) = 1.5 * i - j; } |
|---|
| 121 | for (i=1; i<=20; i++) |
|---|
| 122 | { Real v = SumAbsoluteValue(M.Column(i)); if (value<v) value = v; } |
|---|
| 123 | RowVector test(3); |
|---|
| 124 | test(1) = value; |
|---|
| 125 | test(2) = Norm1(M); |
|---|
| 126 | test(3) = NormInfinity(M.t()); |
|---|
| 127 | test = test - 165; Print(test); |
|---|
| 128 | test.ReSize(5); |
|---|
| 129 | ColumnVector CV = M.AsColumn(); M = CV.t(); |
|---|
| 130 | test(1) = Norm1(CV.t()); |
|---|
| 131 | test(2) = MaximumAbsoluteValue(M); |
|---|
| 132 | test(3) = NormInfinity(CV); |
|---|
| 133 | test(4) = Norm1(M); |
|---|
| 134 | test(5) = NormInfinity(M.t()); |
|---|
| 135 | test = test - 21.5; Print(test); |
|---|
| 136 | } |
|---|
| 137 | { |
|---|
| 138 | Tracer et1("Stage 7"); |
|---|
| 139 | Matrix M(15,20); |
|---|
| 140 | for (i=1; i<=15; i++) { for (j=1; j<=20; j++) M(i,j) = 2.5 * i - j; } |
|---|
| 141 | SymmetricMatrix SM; SM << M * M.t(); |
|---|
| 142 | ColumnVector test(6); |
|---|
| 143 | test(1) = sqrt(SumSquare(M)) - NormFrobenius(M); |
|---|
| 144 | Real a = sqrt(SumSquare(SM)); |
|---|
| 145 | test(2) = NormFrobenius(M * M.t()) - a; |
|---|
| 146 | test(3) = SM.NormFrobenius() - a; |
|---|
| 147 | test(4) = (M * M.t()).NormFrobenius() - a; |
|---|
| 148 | test(5) = NormFrobenius(SM) - a; |
|---|
| 149 | test(6) = Trace(SM) - M.SumSquare(); |
|---|
| 150 | Clean(test, 0.00000001); Print(test); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | // cout << "\nEnd of Sixteenth test\n"; |
|---|
| 154 | } |
|---|