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


kni
Author(s): Martin Günther
autogenerated on Fri Jun 7 2019 22:06:45