Go to the documentation of this file.00001
00002 #define WANT_STREAM
00003
00004 #define WANT_MATH
00005
00006 #include "newmat.h"
00007
00008 #include "tmt.h"
00009
00010 #ifdef use_namespace
00011 using namespace NEWMAT;
00012 #endif
00013
00014
00015
00016
00017
00018 Real TestMax(const GenericMatrix& GM)
00019 {
00020 Matrix M = GM;
00021 ColumnVector CV = GM.AsColumn();
00022
00023 int c, i, j, k; int n = CV.Nrows(), nr = M.Nrows(), nc = M.Ncols();
00024 Real x1, x2, x3;
00025
00026 MatrixBandWidth mbw = GM.BandWidth();
00027 int u = mbw.Upper(); int l = mbw.Lower();
00028 bool IsBandMatrix = u > 0 && l > 0 && !(u == 0 && l == 0);
00029
00030 x1 = GM.MaximumAbsoluteValue();
00031 x2 = GM.MaximumAbsoluteValue1(i);
00032 if (fabs(CV(i)) != x2) return 1.1;
00033 x3 = GM.MaximumAbsoluteValue2(i,j);
00034 if (fabs(M(i,j)) != x3) return 1.2;
00035 if (x3 != x1) return 1.3;
00036 if (x2 != x1) return 1.4;
00037 c = 0;
00038 for (k = 1; k <= n; k++)
00039 {
00040 Real cvk = fabs(CV(k));
00041 if (x1 <= cvk) { if (x1 < cvk) return 1.5; else c++; }
00042 }
00043 if (c == 0) return 1.6;
00044
00045
00046 x1 = GM.MinimumAbsoluteValue();
00047 x2 = GM.MinimumAbsoluteValue1(i);
00048 if (fabs(CV(i)) != x2) return 2.1;
00049 x3 = GM.MinimumAbsoluteValue2(i,j);
00050 if (fabs(M(i,j)) != x3) return 2.2;
00051 if (x3 != x1) return 2.3;
00052 if (x2 != CV.MinimumAbsoluteValue()) return 2.4;
00053 c = 0;
00054 if (IsBandMatrix)
00055 {
00056 for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++)
00057 if (i - j <= l && j - i <= u)
00058 {
00059 Real mij = fabs(M(i,j));
00060 if (x1 >= mij) { if (x1 > mij) return 2.51; else c++; }
00061 }
00062 }
00063 else
00064 {
00065 for (k = 1; k <= n; k++)
00066 {
00067 Real cvk = fabs(CV(k));
00068 if (x1 >= cvk) { if (x1 > cvk) return 2.52; else c++; }
00069 }
00070 }
00071 if (c == 0) return 2.6;
00072
00073 x1 = GM.Maximum();
00074 x2 = GM.Maximum1(i);
00075 if (CV(i) != x2) return 3.1;
00076 x3 = GM.Maximum2(i,j);
00077 if (M(i,j) != x3) return 3.2;
00078 if (x3 != x1) return 3.3;
00079 if (x2 != CV.Maximum()) return 3.4;
00080 c = 0;
00081 if (IsBandMatrix)
00082 {
00083 for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++)
00084 if (i - j <= l && j - i <= u)
00085 {
00086 Real mij = M(i,j);
00087 if (x1 <= mij) { if (x1 < mij) return 3.51; else c++; }
00088 }
00089 }
00090 else
00091 {
00092 for (k = 1; k <= n; k++)
00093 {
00094 Real cvk = CV(k);
00095 if (x1 <= cvk) { if (x1 < cvk) return 3.52; else c++; }
00096 }
00097 }
00098 if (c == 0) return 3.6;
00099
00100 x1 = GM.Minimum();
00101 x2 = GM.Minimum1(i);
00102 if (CV(i) != x2) return 4.1;
00103 x3 = GM.Minimum2(i,j);
00104 if (M(i,j) != x3) return 4.2;
00105 if (x3 != x1) return 4.3;
00106 if (x2 != CV.Minimum()) return 4.4;
00107 c = 0;
00108 if (IsBandMatrix)
00109 {
00110 for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++)
00111 if (i - j <= l && j - i <= u)
00112 {
00113 Real mij = M(i,j);
00114 if (x1 >= mij) { if (x1 > mij) return 4.51; else c++; }
00115 }
00116 }
00117 else
00118 {
00119 for (k = 1; k <= n; k++)
00120 {
00121 Real cvk = CV(k);
00122 if (x1 >= cvk) { if (x1 > cvk) return 4.52; else c++; }
00123 }
00124 }
00125 if (c == 0) return 4.6;
00126
00127 return 0;
00128
00129 }
00130
00131
00132 void trymatl()
00133 {
00134 Tracer et("Twenty first test of Matrix package");
00135 Tracer::PrintTrace();
00136
00137 Matrix M(4, 4);
00138 M << 1.5 << 1.0 << -4.0 << 4.5
00139 << 3.5 << 7.0 << 2.0 << -1.0
00140 << -1.5 << 7.5 << -6.0 << 5.0
00141 << 0.5 << -8.0 << 5.5 << 4.0;
00142 UpperTriangularMatrix UM; UM << M;
00143 LowerTriangularMatrix LM; LM << -M;
00144 SymmetricMatrix SM; SM << (UM + UM.t());
00145 BandMatrix BM(4, 1, 2); BM.Inject(M);
00146 DiagonalMatrix DM; DM << M;
00147 SymmetricBandMatrix SBM(4,1); SBM.Inject(M);
00148 RowVector Test(28); int k = 0;
00149
00150
00151 Test(++k) = TestMax(GenericMatrix(M));
00152 Test(++k) = TestMax(GenericMatrix(UM));
00153 Test(++k) = TestMax(GenericMatrix(LM));
00154 Test(++k) = TestMax(GenericMatrix(SM));
00155 Test(++k) = TestMax(GenericMatrix(DM));
00156 Test(++k) = TestMax(GenericMatrix(BM));
00157 Test(++k) = TestMax(GenericMatrix(SBM));
00158
00159
00160 Matrix MX(4,4);
00161 MX = 1; Test(++k) = TestMax(GenericMatrix(MX));
00162 BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM));
00163 MX = 0; Test(++k) = TestMax(GenericMatrix(MX));
00164 BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM));
00165 MX = -1; Test(++k) = TestMax(GenericMatrix(MX));
00166 BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM));
00167
00168
00169 MX = M | (2 * M).t(); Test(++k) = TestMax(GenericMatrix(MX));
00170
00171
00172 RowVector RV(6);
00173 RV << 1 << 3 << -5 << 2 << -4 << 3;
00174 Test(++k) = TestMax(GenericMatrix(RV));
00175 Test(++k) = TestMax(GenericMatrix(RV.t()));
00176
00177
00178 Test(++k) = (MaximumAbsoluteValue(RV) - 5);
00179 Test(++k) = (MinimumAbsoluteValue(RV) - 1);
00180 Test(++k) = (Maximum(RV) - 3);
00181 Test(++k) = (Minimum(RV) + 5);
00182 Test(++k) = (MaximumAbsoluteValue(-RV) - 5);
00183 Test(++k) = (MinimumAbsoluteValue(-RV) - 1);
00184 Test(++k) = (Maximum(-RV) - 5);
00185 Test(++k) = (Minimum(-RV) + 3);
00186
00187
00188 RowVector RV2(6);
00189 RV2 << 2 << 8 << 1 << 9 << 3 << -1;
00190 Test(++k) = (MaximumAbsoluteValue(RV+RV2) - 11);
00191 Test(++k) = (MinimumAbsoluteValue(RV+RV2) - 1);
00192 Test(++k) = (Maximum(RV+RV2) - 11);
00193 Test(++k) = (Minimum(RV+RV2) + 4);
00194
00195
00196 Print(Test);
00197 }
00198