1 | |
---|
2 | #define WANT_STREAM |
---|
3 | |
---|
4 | #define WANT_MATH |
---|
5 | |
---|
6 | #include "newmat.h" |
---|
7 | |
---|
8 | #include "tmt.h" |
---|
9 | |
---|
10 | #ifdef use_namespace |
---|
11 | using namespace NEWMAT; |
---|
12 | #endif |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | // test maxima and minima |
---|
17 | |
---|
18 | Real TestMax(const GenericMatrix& GM) |
---|
19 | { |
---|
20 | Matrix M = GM; |
---|
21 | ColumnVector CV = GM.AsColumn(); |
---|
22 | |
---|
23 | int c, i, j, k; int n = CV.Nrows(), nr = M.Nrows(), nc = M.Ncols(); |
---|
24 | Real x1, x2, x3; |
---|
25 | |
---|
26 | MatrixBandWidth mbw = GM.BandWidth(); |
---|
27 | int u = mbw.Upper(); int l = mbw.Lower(); |
---|
28 | bool IsBandMatrix = u > 0 && l > 0 && !(u == 0 && l == 0); |
---|
29 | |
---|
30 | x1 = GM.MaximumAbsoluteValue(); |
---|
31 | x2 = GM.MaximumAbsoluteValue1(i); |
---|
32 | if (fabs(CV(i)) != x2) return 1.1; |
---|
33 | x3 = GM.MaximumAbsoluteValue2(i,j); |
---|
34 | if (fabs(M(i,j)) != x3) return 1.2; |
---|
35 | if (x3 != x1) return 1.3; |
---|
36 | if (x2 != x1) return 1.4; |
---|
37 | c = 0; |
---|
38 | for (k = 1; k <= n; k++) |
---|
39 | { |
---|
40 | Real cvk = fabs(CV(k)); |
---|
41 | if (x1 <= cvk) { if (x1 < cvk) return 1.5; else c++; } |
---|
42 | } |
---|
43 | if (c == 0) return 1.6; |
---|
44 | |
---|
45 | |
---|
46 | x1 = GM.MinimumAbsoluteValue(); |
---|
47 | x2 = GM.MinimumAbsoluteValue1(i); |
---|
48 | if (fabs(CV(i)) != x2) return 2.1; |
---|
49 | x3 = GM.MinimumAbsoluteValue2(i,j); |
---|
50 | if (fabs(M(i,j)) != x3) return 2.2; |
---|
51 | if (x3 != x1) return 2.3; |
---|
52 | if (x2 != CV.MinimumAbsoluteValue()) return 2.4; |
---|
53 | c = 0; |
---|
54 | if (IsBandMatrix) |
---|
55 | { |
---|
56 | for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++) |
---|
57 | if (i - j <= l && j - i <= u) |
---|
58 | { |
---|
59 | Real mij = fabs(M(i,j)); |
---|
60 | if (x1 >= mij) { if (x1 > mij) return 2.51; else c++; } |
---|
61 | } |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | for (k = 1; k <= n; k++) |
---|
66 | { |
---|
67 | Real cvk = fabs(CV(k)); |
---|
68 | if (x1 >= cvk) { if (x1 > cvk) return 2.52; else c++; } |
---|
69 | } |
---|
70 | } |
---|
71 | if (c == 0) return 2.6; |
---|
72 | |
---|
73 | x1 = GM.Maximum(); |
---|
74 | x2 = GM.Maximum1(i); |
---|
75 | if (CV(i) != x2) return 3.1; |
---|
76 | x3 = GM.Maximum2(i,j); |
---|
77 | if (M(i,j) != x3) return 3.2; |
---|
78 | if (x3 != x1) return 3.3; |
---|
79 | if (x2 != CV.Maximum()) return 3.4; |
---|
80 | c = 0; |
---|
81 | if (IsBandMatrix) |
---|
82 | { |
---|
83 | for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++) |
---|
84 | if (i - j <= l && j - i <= u) |
---|
85 | { |
---|
86 | Real mij = M(i,j); |
---|
87 | if (x1 <= mij) { if (x1 < mij) return 3.51; else c++; } |
---|
88 | } |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | for (k = 1; k <= n; k++) |
---|
93 | { |
---|
94 | Real cvk = CV(k); |
---|
95 | if (x1 <= cvk) { if (x1 < cvk) return 3.52; else c++; } |
---|
96 | } |
---|
97 | } |
---|
98 | if (c == 0) return 3.6; |
---|
99 | |
---|
100 | x1 = GM.Minimum(); |
---|
101 | x2 = GM.Minimum1(i); |
---|
102 | if (CV(i) != x2) return 4.1; |
---|
103 | x3 = GM.Minimum2(i,j); |
---|
104 | if (M(i,j) != x3) return 4.2; |
---|
105 | if (x3 != x1) return 4.3; |
---|
106 | if (x2 != CV.Minimum()) return 4.4; |
---|
107 | c = 0; |
---|
108 | if (IsBandMatrix) |
---|
109 | { |
---|
110 | for (i = 1; i <= nr; i++) for (j = 1; j <= nc; j++) |
---|
111 | if (i - j <= l && j - i <= u) |
---|
112 | { |
---|
113 | Real mij = M(i,j); |
---|
114 | if (x1 >= mij) { if (x1 > mij) return 4.51; else c++; } |
---|
115 | } |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | for (k = 1; k <= n; k++) |
---|
120 | { |
---|
121 | Real cvk = CV(k); |
---|
122 | if (x1 >= cvk) { if (x1 > cvk) return 4.52; else c++; } |
---|
123 | } |
---|
124 | } |
---|
125 | if (c == 0) return 4.6; |
---|
126 | |
---|
127 | return 0; |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | void trymatl() |
---|
133 | { |
---|
134 | Tracer et("Twenty first test of Matrix package"); |
---|
135 | Tracer::PrintTrace(); |
---|
136 | |
---|
137 | Matrix M(4, 4); |
---|
138 | M << 1.5 << 1.0 << -4.0 << 4.5 |
---|
139 | << 3.5 << 7.0 << 2.0 << -1.0 |
---|
140 | << -1.5 << 7.5 << -6.0 << 5.0 |
---|
141 | << 0.5 << -8.0 << 5.5 << 4.0; |
---|
142 | UpperTriangularMatrix UM; UM << M; |
---|
143 | LowerTriangularMatrix LM; LM << -M; |
---|
144 | SymmetricMatrix SM; SM << (UM + UM.t()); |
---|
145 | BandMatrix BM(4, 1, 2); BM.Inject(M); |
---|
146 | DiagonalMatrix DM; DM << M; |
---|
147 | SymmetricBandMatrix SBM(4,1); SBM.Inject(M); |
---|
148 | RowVector Test(28); int k = 0; |
---|
149 | |
---|
150 | // tests for different shapes |
---|
151 | Test(++k) = TestMax(GenericMatrix(M)); |
---|
152 | Test(++k) = TestMax(GenericMatrix(UM)); |
---|
153 | Test(++k) = TestMax(GenericMatrix(LM)); |
---|
154 | Test(++k) = TestMax(GenericMatrix(SM)); |
---|
155 | Test(++k) = TestMax(GenericMatrix(DM)); |
---|
156 | Test(++k) = TestMax(GenericMatrix(BM)); |
---|
157 | Test(++k) = TestMax(GenericMatrix(SBM)); |
---|
158 | |
---|
159 | // tests for constant matrices - don't put non-zeros in corners of BM |
---|
160 | Matrix MX(4,4); |
---|
161 | MX = 1; Test(++k) = TestMax(GenericMatrix(MX)); |
---|
162 | BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM)); |
---|
163 | MX = 0; Test(++k) = TestMax(GenericMatrix(MX)); |
---|
164 | BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM)); |
---|
165 | MX = -1; Test(++k) = TestMax(GenericMatrix(MX)); |
---|
166 | BM.Inject(MX); Test(++k) = TestMax(GenericMatrix(BM)); |
---|
167 | |
---|
168 | // test for non-square |
---|
169 | MX = M | (2 * M).t(); Test(++k) = TestMax(GenericMatrix(MX)); |
---|
170 | |
---|
171 | // test on row and column vector |
---|
172 | RowVector RV(6); |
---|
173 | RV << 1 << 3 << -5 << 2 << -4 << 3; |
---|
174 | Test(++k) = TestMax(GenericMatrix(RV)); |
---|
175 | Test(++k) = TestMax(GenericMatrix(RV.t())); |
---|
176 | |
---|
177 | // test for function form |
---|
178 | Test(++k) = (MaximumAbsoluteValue(RV) - 5); |
---|
179 | Test(++k) = (MinimumAbsoluteValue(RV) - 1); |
---|
180 | Test(++k) = (Maximum(RV) - 3); |
---|
181 | Test(++k) = (Minimum(RV) + 5); |
---|
182 | Test(++k) = (MaximumAbsoluteValue(-RV) - 5); |
---|
183 | Test(++k) = (MinimumAbsoluteValue(-RV) - 1); |
---|
184 | Test(++k) = (Maximum(-RV) - 5); |
---|
185 | Test(++k) = (Minimum(-RV) + 3); |
---|
186 | |
---|
187 | // test formulae |
---|
188 | RowVector RV2(6); |
---|
189 | RV2 << 2 << 8 << 1 << 9 << 3 << -1; |
---|
190 | Test(++k) = (MaximumAbsoluteValue(RV+RV2) - 11); |
---|
191 | Test(++k) = (MinimumAbsoluteValue(RV+RV2) - 1); |
---|
192 | Test(++k) = (Maximum(RV+RV2) - 11); |
---|
193 | Test(++k) = (Minimum(RV+RV2) + 4); |
---|
194 | |
---|
195 | |
---|
196 | Print(Test); |
---|
197 | } |
---|
198 | |
---|