$search
00001 00002 00003 00006 00007 00008 //#define WANT_STREAM 00009 00010 #define WANT_MATH // for sqrt 00011 00012 #include "include.h" 00013 00014 #include "newmatap.h" 00015 00016 #include "tmt.h" 00017 00018 #ifdef use_namespace 00019 using namespace NEWMAT; 00020 #endif 00021 00022 00023 00024 void trymatg() 00025 { 00026 // cout << "\nSixteenth test of Matrix package\n"; 00027 // cout << "\n"; 00028 Tracer et("Sixteenth test of Matrix package"); 00029 Tracer::PrintTrace(); 00030 00031 int i,j; 00032 Matrix M(4,7); 00033 for (i=1; i<=4; i++) for (j=1; j<=7; j++) M(i,j) = 100 * i + j; 00034 ColumnVector CV = M.AsColumn(); 00035 { 00036 Tracer et1("Stage 1"); 00037 RowVector test(7); 00038 test(1) = SumSquare(M); 00039 test(2) = SumSquare(CV); 00040 test(3) = SumSquare(CV.t()); 00041 test(4) = SumSquare(CV.AsDiagonal()); 00042 test(5) = SumSquare(M.AsColumn()); 00043 test(6) = Matrix(CV.t()*CV)(1,1); 00044 test(7) = (CV.t()*CV).AsScalar(); 00045 test = test - 2156560.0; Print(test); 00046 } 00047 00048 UpperTriangularMatrix U(6); 00049 for (i=1; i<=6; i++) for (j=i; j<=6; j++) U(i,j) = i + (i-j) * (i-j); 00050 M = U; DiagonalMatrix D; D << U; 00051 LowerTriangularMatrix L = U.t(); SymmetricMatrix S; S << (L+U)/2.0; 00052 { 00053 Tracer et1("Stage 2"); 00054 RowVector test(6); 00055 test(1) = U.Trace(); 00056 test(2) = L.Trace(); 00057 test(3) = D.Trace(); 00058 test(4) = S.Trace(); 00059 test(5) = M.Trace(); 00060 test(6) = ((L+U)/2.0).Trace(); 00061 test = test - 21; Print(test); 00062 test(1) = LogDeterminant(U).Value(); 00063 test(2) = LogDeterminant(L).Value(); 00064 test(3) = LogDeterminant(D).Value(); 00065 test(4) = LogDeterminant(D).Value(); 00066 test(5) = LogDeterminant((L+D)/2.0).Value(); 00067 test(6) = Determinant((L+D)/2.0); 00068 test = test - 720; Clean(test,0.000000001); Print(test); 00069 } 00070 00071 { 00072 Tracer et1("Stage 3"); 00073 S << L*U; M = S; 00074 RowVector test(8); 00075 test(1) = LogDeterminant(S).Value(); 00076 test(2) = LogDeterminant(M).Value(); 00077 test(3) = LogDeterminant(L*U).Value(); 00078 test(4) = LogDeterminant(Matrix(L*L)).Value(); 00079 test(5) = Determinant(S); 00080 test(6) = Determinant(M); 00081 test(7) = Determinant(L*U); 00082 test(8) = Determinant(Matrix(L*L)); 00083 test = test - 720.0 * 720.0; Clean(test,0.00000001); Print(test); 00084 } 00085 00086 { 00087 Tracer et1("Stage 4"); 00088 M = S * S; 00089 Matrix SX = S; 00090 RowVector test(3); 00091 test(1) = SumSquare(S); 00092 test(2) = SumSquare(SX); 00093 test(3) = Trace(M); 00094 test = test - 3925961.0; Print(test); 00095 } 00096 { 00097 Tracer et1("Stage 5"); 00098 SymmetricMatrix SM(10), SN(10); 00099 Real S = 0.0; 00100 for (i=1; i<=10; i++) for (j=i; j<=10; j++) 00101 { 00102 SM(i,j) = 1.5 * i - j; SN(i,j) = SM(i,j) * SM(i,j); 00103 if (SM(i,j) < 0.0) SN(i,j) = - SN(i,j); 00104 S += SN(i,j) * ((i==j) ? 1.0 : 2.0); 00105 } 00106 Matrix M = SM, N = SN; RowVector test(4); 00107 test(1) = SumAbsoluteValue(SN); 00108 test(2) = SumAbsoluteValue(-SN); 00109 test(3) = SumAbsoluteValue(N); 00110 test(4) = SumAbsoluteValue(-N); 00111 test = test - 1168.75; Print(test); 00112 test(1) = Sum(SN); 00113 test(2) = -Sum(-SN); 00114 test(3) = Sum(N); 00115 test(4) = -Sum(-N); 00116 test = test - S; Print(test); 00117 test(1) = MaximumAbsoluteValue(SM); 00118 test(2) = MaximumAbsoluteValue(-SM); 00119 test(3) = MaximumAbsoluteValue(M); 00120 test(4) = MaximumAbsoluteValue(-M); 00121 test = test - 8.5; Print(test); 00122 } 00123 { 00124 Tracer et1("Stage 6"); 00125 Matrix M(15,20); Real value = 0.0; 00126 for (i=1; i<=15; i++) { for (j=1; j<=20; j++) M(i,j) = 1.5 * i - j; } 00127 for (i=1; i<=20; i++) 00128 { Real v = SumAbsoluteValue(M.Column(i)); if (value<v) value = v; } 00129 RowVector test(3); 00130 test(1) = value; 00131 test(2) = Norm1(M); 00132 test(3) = NormInfinity(M.t()); 00133 test = test - 165; Print(test); 00134 test.ReSize(5); 00135 ColumnVector CV = M.AsColumn(); M = CV.t(); 00136 test(1) = Norm1(CV.t()); 00137 test(2) = MaximumAbsoluteValue(M); 00138 test(3) = NormInfinity(CV); 00139 test(4) = Norm1(M); 00140 test(5) = NormInfinity(M.t()); 00141 test = test - 21.5; Print(test); 00142 } 00143 { 00144 Tracer et1("Stage 7"); 00145 Matrix M(15,20); 00146 for (i=1; i<=15; i++) { for (j=1; j<=20; j++) M(i,j) = 2.5 * i - j; } 00147 SymmetricMatrix SM; SM << M * M.t(); 00148 ColumnVector test(6); 00149 test(1) = sqrt(SumSquare(M)) - NormFrobenius(M); 00150 Real a = sqrt(SumSquare(SM)); 00151 test(2) = NormFrobenius(M * M.t()) - a; 00152 test(3) = SM.NormFrobenius() - a; 00153 test(4) = (M * M.t()).NormFrobenius() - a; 00154 test(5) = NormFrobenius(SM) - a; 00155 test(6) = Trace(SM) - M.SumSquare(); 00156 Clean(test, 0.00000001); Print(test); 00157 } 00158 00159 // cout << "\nEnd of Sixteenth test\n"; 00160 } 00161 00162